Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public static (App app, Stack stack) CreateTestStack(
return (app, stack);
}

/// <summary>
/// Creates a test CDK app and stack with custom stack props.
/// Note: You must create the Template.FromStack(stack) after adding constructs to the stack.
/// </summary>
/// <param name="stackName">The name of the test stack</param>
/// <param name="stackProps">Custom stack props implementing IStackProps</param>
/// <returns>Tuple containing the app and stack for testing</returns>
public static (App app, Stack stack) CreateTestStack(string stackName, IStackProps stackProps)
{
var app = new App();
var stack = new Stack(app, stackName, stackProps);

return (app, stack);
}

/// <summary>
/// Creates a test CDK stack (app is created internally).
/// Note: You must create the Template.FromStack(stack) after adding constructs to the stack.
Expand All @@ -52,6 +67,19 @@ public static Stack CreateTestStackMinimal(
return stack;
}

/// <summary>
/// Creates a test CDK stack with custom stack props (app is created internally).
/// Note: You must create the Template.FromStack(stack) after adding constructs to the stack.
/// </summary>
/// <param name="stackName">The name of the test stack</param>
/// <param name="stackProps">Custom stack props implementing IStackProps</param>
/// <returns>The stack for testing</returns>
public static Stack CreateTestStackMinimal(string stackName, IStackProps stackProps)
{
var (_, stack) = CreateTestStack(stackName, stackProps);
return stack;
}

/// <summary>
/// Gets the path to a test asset relative to the executing assembly location.
/// This ensures test assets can be found regardless of the current working directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,93 @@ public void CdkTestHelper_ShouldReturnStandardLambdaZipPath()
zipPath.Should().EndWith("TestAssets/test-lambda.zip");
Path.IsPathRooted(zipPath).Should().BeTrue("Should return absolute path");
}

[Fact]
public void CdkTestHelper_ShouldCreateStackWithCustomStackProps()
{
var customProps = new Amazon.CDK.StackProps
{
Env = new Amazon.CDK.Environment
{
Account = "987654321098",
Region = "eu-west-1"
},
Tags = new Dictionary<string, string>
{
{ "Environment", "test" },
{ "Project", "custom-test" }
}
};

var (app, stack) = CdkTestHelper.CreateTestStack("custom-test-stack", customProps);

app.Should().NotBeNull();
stack.Should().NotBeNull();
stack.StackName.Should().Be("custom-test-stack");
stack.Account.Should().Be("987654321098");
stack.Region.Should().Be("eu-west-1");
}

[Fact]
public void CdkTestHelper_ShouldCreateMinimalStackWithCustomStackProps()
{
var customProps = new Amazon.CDK.StackProps
{
Env = new Amazon.CDK.Environment
{
Account = "555666777888",
Region = "ap-southeast-2"
},
Description = "Test stack with custom props"
};

var stack = CdkTestHelper.CreateTestStackMinimal("minimal-custom-stack", customProps);

stack.Should().NotBeNull();
stack.StackName.Should().Be("minimal-custom-stack");
stack.Account.Should().Be("555666777888");
stack.Region.Should().Be("ap-southeast-2");
}

[Fact]
public void CdkTestHelper_ShouldWorkWithRealWorldCustomStackProps()
{
// Simulate a real-world scenario similar to LightsaberStackProps
var customEnv = new Amazon.CDK.Environment
{
Account = "123456789012",
Region = "us-west-2"
};

var customProps = new Amazon.CDK.StackProps
{
Env = customEnv,
Tags = new Dictionary<string, string>
{
{ "Application", "MyApp" },
{ "Environment", "test" },
{ "Owner", "DevTeam" }
},
Description = "Test infrastructure stack"
};

var stack = CdkTestHelper.CreateTestStackMinimal("real-world-test", customProps);

stack.Should().NotBeNull();
stack.StackName.Should().Be("real-world-test");
stack.Account.Should().Be("123456789012");
stack.Region.Should().Be("us-west-2");

// Verify we can create constructs on this stack
var props = CdkTestHelper.CreatePropsBuilder(AssetPathExtensions.GetTestLambdaZipPath())
.WithFunctionName("real-world-function")
.Build();

_ = new LambdaFunctionConstruct(stack, "RealWorldConstruct", props);

// Create template AFTER adding constructs to the stack
var template = Template.FromStack(stack);

template.ShouldHaveLambdaFunction("real-world-function-test");
}
}