Skip to content

Commit

Permalink
feat(lib): exposed testing matchers for other languages properly (#1935)
Browse files Browse the repository at this point in the history
* fix(lib): exposed testing matchers for other languages properly

* added documentation for increased support of unit testing

* updated templates to add testing and various small changes to integration tests

* fixed docs formating

* Update website/docs/cdktf/test/unit-tests.mdx

Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>

* Update website/docs/cdktf/test/unit-tests.mdx

Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>

* Update website/docs/cdktf/test/unit-tests.mdx

Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>

* Update website/docs/cdktf/test/unit-tests.mdx

Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>

* Revert "Update website/docs/cdktf/test/unit-tests.mdx"

This reverts commit 1258245.

* Revert "Update website/docs/cdktf/test/unit-tests.mdx"

This reverts commit a9cc009.

* fixed templates that caused build errors in tests

* update to testing docs

* fixed async tests for java dotnet

* chore: fix wording

Co-authored-by: Jon Steinich <jsteinich@gmail.com>

* chore: improve wording

Co-authored-by: Jon Steinich <jsteinich@gmail.com>

* chore: fix wording

Co-authored-by: Jon Steinich <jsteinich@gmail.com>

* chore: use default test path in java

* chore: only expose boolean instead of AssertionReturn for now

Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>
Co-authored-by: Daniel Schmidt <danielmschmidt92@gmail.com>
Co-authored-by: Jon Steinich <jsteinich@gmail.com>
  • Loading branch information
4 people authored Jul 26, 2022
1 parent 129b1a9 commit abc1596
Show file tree
Hide file tree
Showing 31 changed files with 1,319 additions and 104 deletions.
4 changes: 3 additions & 1 deletion packages/cdktf-cli/templates/csharp/.hooks.sscaff.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ exports.post = options => {
</packageSources>
</configuration>`, 'utf-8');
}

execSync(`dotnet add package Microsoft.NET.Test.Sdk --version 17.2.0`, { stdio: 'inherit' });
execSync(`dotnet add package xunit --version 2.4.1`, { stdio: 'inherit' });
execSync(`dotnet add package xunit.runner.visualstudio --version 2.4.5`, { stdio: 'inherit' });
execSync(`dotnet restore`, { stdio: 'inherit' });
console.log(readFileSync('./help', 'utf-8'));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
Expand Down
38 changes: 38 additions & 0 deletions packages/cdktf-cli/templates/csharp/TestProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Xunit;
using HashiCorp.Cdktf;
using System;
using System.Collections.Generic;

namespace MyCompany.MyApp{
// The tests below are example tests, you can find more information at
// https://cdk.tf/testing
public class TestProgram{

[Fact]
public void myAppTest(){
Assert.True(true);
}

//private static TerraformStack stack = new TerraformStack(Testing.app(), "stack");
//private static MyApplicationsAbstraction appAbstraction = new MyApplicationsAbstraction(stack, "construct");
//private static string synthesized = Testing.synth(stack);

//[Fact]
//public void CheckValidity(){
// Assert.True(Testing.ToBeValidTerraform(Testing.FullSynth(stack)) );
//}

//[Fact]
//public void shouldContainContainer(){
// Assert.True(Testing.ToHaveResource(synthesized, Container.TfResourceType) );
//}

//[Fact]
//public void shouldUseUbuntuImage(){
// Assert.True(Testing.ToHaveResourceWithProperties(synthesized, Image.TfResourceType, new Dictionary<String, Object>() {
// {"name", "ubuntu:latest"}
// }) );
//}
}

}
42 changes: 42 additions & 0 deletions packages/cdktf-cli/templates/go/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"testing"
"github.com/hashicorp/terraform-cdk-go/cdktf"
"github.com/aws/jsii-runtime-go"
)

// The tests below are example tests, you can find more information at
// https://cdk.tf/testing

/*
var stack = NewMyApplicationsAbstraction(cdktf.Assertions_App(nil), "stack")
var synth = cdktf.Assertions_Synth(stack)
func TestShouldContainContainer(t *testing.T){
assertion := cdktf.Assertions_ToHaveResource(synth, docker.Container_TfResourceType())
if !*assertion {
t.Error(assertion.Message())
}
}
func TestShouldUseUbuntuImage(t *testing.T){
properties := map[string]interface{}{
"name": "ubuntu:latest",
}
assertion := cdktf.Assertions_ToHaveResourceWithProperties(synth, docker.Image_TfResourceType(), &properties)
if !*assertion {
t.Error(assertion.Message())
}
}
func TestCheckValidity(t *testing.T){
assertion := cdktf.Testing_ToBeValidTerraform(cdktf.Testing_FullSynth(stack))
if !*assertion {
t.Error(assertion.Message())
}
}
*/
16 changes: 16 additions & 0 deletions packages/cdktf-cli/templates/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
<artifactId>constructs</artifactId>
<version>{{ constructs_version }}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -49,6 +59,12 @@
<mainClass>com.mycompany.app.Main</mainClass>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import com.hashicorp.cdktf.Testing;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

// The tests below are example tests, you can find more information at
// https://cdk.tf/testing
public class MainTest {

@Test
void myAppTest() {
assertTrue(true);
}
//private final TerraformStack stack = new TerraformStack(Testing.app(), "stack");

//private final MyApplicationsAbstraction appAbstraction = new MyApplicationsAbstraction(stack, "resource");
//private final String synthesized = Testing.synth(stack);

//@Test
//void shouldContainContainer() {
// assertTrue(Testing.toHaveResource(synthesized, Container.TF_RESOURCE_TYPE) );
//}

//@Test
//void shouldUseUbuntuImage() {
// assertTrue(Testing
// .toHaveResourceWithProperties(synthesized, Image.TF_RESOURCE_TYPE, new HashMap<String, Object>() {
// {
// put("name", "ubuntu:latest");
// }
// }) );
//}

//@Test
//void checkValidity() {
// assertTrue(Testing.toBeValidTerraform(Testing.fullSynth(stack)) );
//}

}
1 change: 1 addition & 0 deletions packages/cdktf-cli/templates/python-pip/.hooks.sscaff.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports.post = options => {
}

writeFileSync('requirements.txt', pypi_cdktf, 'utf-8');
writeFileSync('requirements.txt', 'pytest', 'utf-8');
let installArgs = '';
if (!process.env.VIRTUAL_ENV) {
installArgs += '--user'
Expand Down
21 changes: 21 additions & 0 deletions packages/cdktf-cli/templates/python-pip/main-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from cdktf import Testing, TerraformStack

# The tests below are example tests, you can find more information at
# https://cdk.tf/testing
class TestMain:

stack = TerraformStack(Testing.app(), "stack")
#app_abstraction = MyApplicationsAbstraction(stack, "app-abstraction")
#synthesized = Testing.synth(stack)

#def test_should_contain_container(self):
# assert Testing.to_have_resource(self.synthesized, Container.TF_RESOURCE_TYPE)

#def test_should_use_an_ubuntu_image(self):
# assert Testing.to_have_resource_with_properties(self.synthesized, Image.TF_RESOURCE_TYPE, {
# "name": "ubuntu:latest",
# })

#def test_check_validity(self):
# assert Testing.to_be_valid_terraform(Testing.full_synth(stack))
1 change: 1 addition & 0 deletions packages/cdktf-cli/templates/python/.hooks.sscaff.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.post = options => {

execSync('pipenv install', { stdio: 'inherit' });
execSync(`pipenv install ${pypi_cdktf}`, { stdio: 'inherit' });
execSync(`pipenv install pytest`, { stdio: 'inherit' });
chmodSync('main.py', '700');

console.log(readFileSync('./help', 'utf-8'));
Expand Down
25 changes: 25 additions & 0 deletions packages/cdktf-cli/templates/python/main-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from cdktf import Testing

# The tests below are example tests, you can find more information at
# https://cdk.tf/testing

class TestMain:

def test_my_app(self):
assert True

#stack = TerraformStack(Testing.app(), "stack")
#app_abstraction = MyApplicationsAbstraction(stack, "app-abstraction")
#synthesized = Testing.synth(stack)

#def test_should_contain_container(self):
# assert Testing.to_have_resource(self.synthesized, Container.TF_RESOURCE_TYPE)

#def test_should_use_an_ubuntu_image(self):
# assert Testing.to_have_resource_with_properties(self.synthesized, Image.TF_RESOURCE_TYPE, {
# "name": "ubuntu:latest",
# })

#def test_check_validity(self):
# assert Testing.to_be_valid_terraform(Testing.full_synth(stack))
16 changes: 8 additions & 8 deletions packages/cdktf/lib/testing/__tests__/matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("matchers", () => {
const res = toHaveResourceWithProperties(synthesizedStack, TestResource);

expect(res.pass).toBeTruthy();
expect(res.message()).toMatchInlineSnapshot(`
expect(res.message).toMatchInlineSnapshot(`
"Expected no test_resource with properties {} to be present in synthesised stack.
Found 1 test_resource resources instead:
[
Expand All @@ -147,7 +147,7 @@ describe("matchers", () => {
);

expect(res.pass).toBeFalsy();
expect(res.message()).toMatchInlineSnapshot(`
expect(res.message).toMatchInlineSnapshot(`
"Expected test_data_source with properties {} to be present in synthesised stack.
Found no test_data_source resources instead"
`);
Expand Down Expand Up @@ -202,7 +202,7 @@ describe("matchers", () => {
it("fails if anything but a path is passed", () => {
const res = toBeValidTerraform("not a path");
expect(res.pass).toBeFalsy();
expect(res.message()).toMatchInlineSnapshot(
expect(res.message).toMatchInlineSnapshot(
`"Expected subject to be a terraform directory: Error: ENOENT: no such file or directory, stat 'not a path'"`
);
});
Expand All @@ -216,7 +216,7 @@ describe("matchers", () => {

const res = toBeValidTerraform(Testing.fullSynth(stack));
expect(res.pass).toBeTruthy();
expect(res.message()).toMatchInlineSnapshot(
expect(res.message).toMatchInlineSnapshot(
`"Expected subject not to be a valid terraform stack"`
);
});
Expand All @@ -232,7 +232,7 @@ describe("matchers", () => {

const res = toBeValidTerraform(result);
expect(res.pass).toBeFalsy();
expect(res.message()).toEqual(
expect(res.message).toEqual(
expect.stringContaining(
"Expected subject to be a valid terraform stack"
)
Expand All @@ -244,7 +244,7 @@ describe("matchers", () => {
it("fails if anything but a path is passed", () => {
const res = toPlanSuccessfully("not a path");
expect(res.pass).toBeFalsy();
expect(res.message()).toMatchInlineSnapshot(
expect(res.message).toMatchInlineSnapshot(
`"Expected subject to be a terraform directory: Error: ENOENT: no such file or directory, stat 'not a path'"`
);
});
Expand All @@ -258,7 +258,7 @@ describe("matchers", () => {

const res = toPlanSuccessfully(Testing.fullSynth(stack));
expect(res.pass).toBeTruthy();
expect(res.message()).toMatchInlineSnapshot(
expect(res.message).toMatchInlineSnapshot(
`"Expected subject not to plan successfully"`
);
});
Expand All @@ -275,7 +275,7 @@ describe("matchers", () => {

const res = toPlanSuccessfully(result);
expect(res.pass).toBeFalsy();
expect(res.message()).toEqual(
expect(res.message).toEqual(
expect.stringContaining("Expected subject to plan successfully")
);
});
Expand Down
Loading

0 comments on commit abc1596

Please sign in to comment.