Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib): exposed testing matchers for other languages properly #1935

Merged
merged 22 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bb50cd9
fix(lib): exposed testing matchers for other languages properly
Maed223 Jul 19, 2022
263f81b
added documentation for increased support of unit testing
Maed223 Jul 22, 2022
7c07b64
Merge branch 'main' into fix-testing-matchers
Maed223 Jul 22, 2022
26f0e35
updated templates to add testing and various small changes to integra…
Maed223 Jul 25, 2022
6caeda8
Merge branch 'fix-testing-matchers' of github.com:hashicorp/terraform…
Maed223 Jul 25, 2022
293eacd
fixed docs formating
Maed223 Jul 25, 2022
2def527
Merge branch 'main' into fix-testing-matchers
Maed223 Jul 25, 2022
f8f1836
Update website/docs/cdktf/test/unit-tests.mdx
Maed223 Jul 25, 2022
d8e12a2
Update website/docs/cdktf/test/unit-tests.mdx
Maed223 Jul 25, 2022
a9cc009
Update website/docs/cdktf/test/unit-tests.mdx
Maed223 Jul 25, 2022
1258245
Update website/docs/cdktf/test/unit-tests.mdx
Maed223 Jul 25, 2022
d41c933
Revert "Update website/docs/cdktf/test/unit-tests.mdx"
Maed223 Jul 25, 2022
126f7b0
Revert "Update website/docs/cdktf/test/unit-tests.mdx"
Maed223 Jul 25, 2022
a6610b8
fixed templates that caused build errors in tests
Maed223 Jul 25, 2022
a9025f0
update to testing docs
Maed223 Jul 25, 2022
cb59a53
fixed async tests for java dotnet
Maed223 Jul 26, 2022
33c1b95
chore: fix wording
DanielMSchmidt Jul 26, 2022
fb2e2f3
chore: improve wording
DanielMSchmidt Jul 26, 2022
066c50f
chore: fix wording
DanielMSchmidt Jul 26, 2022
6f42fe1
Merge branch 'main' into fix-testing-matchers
DanielMSchmidt Jul 26, 2022
f6dcc70
chore: use default test path in java
DanielMSchmidt Jul 26, 2022
dacb81c
chore: only expose boolean instead of AssertionReturn for now
DanielMSchmidt Jul 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved
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
32 changes: 32 additions & 0 deletions packages/cdktf-cli/templates/csharp/TestProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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{
private static TerraformStack stack = new TerraformStack(Testing.app(), "stack");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably opt for doing this in a test setup rather than a static variable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do a follow up for this one 👍

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

//[Fact]
//public void checkValidity(){
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved
// Assert.True(Testing.ToBeValidTerraform(Testing.FullSynth(stack)).Pass);
//}

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

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

}
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.Pass() {
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.Pass() {
t.Error(assertion.Message())
}
}

func TestCheckValidity(t *testing.T){
assertion := cdktf.Testing_ToBeValidTerraform(cdktf.Testing_FullSynth(stack))

if !*assertion.Pass() {
t.Error(assertion.Message())
}
}
*/
18 changes: 17 additions & 1 deletion packages/cdktf-cli/templates/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@
<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>
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>junit-jupiter</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testSourceDirectory>src/main/java/com/mycompany/app</testSourceDirectory>
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved

<plugins>
<plugin>
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,34 @@
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 JunitTesting {

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).getPass());
//}

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

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

}
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).pass_

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

#def test_check_validity(self):
# assert Testing.to_be_valid_terraform(Testing.full_synth(stack)).pass_
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
21 changes: 21 additions & 0 deletions packages/cdktf-cli/templates/python/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).pass_

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

#def test_check_validity(self):
# assert Testing.to_be_valid_terraform(Testing.full_synth(stack)).pass_
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