Skip to content

Commit

Permalink
test: environment-type
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Nov 21, 2024
1 parent 6210cf9 commit bcb4adb
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/__test__/TestableProjectStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export class TestableProjectStack extends SmartStack {
});

project.node.setContext("account-type", accountType);
project.node.setContext("account", accountType);

let wrapper: AccountWrapper | EnvironmentWrapper;

if (environmentType) {
project.node.setContext("environment-type", environmentType);
project.node.setContext("environment", environmentType);
project.node.setContext("env", environmentType);
wrapper = new EnvironmentWrapper(project);
} else {
wrapper = new AccountWrapper(project);
Expand Down
167 changes: 167 additions & 0 deletions src/project/environment-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { EnvironmentType } from "./environment-type";
import { TestableProjectStack } from "../__test__/TestableProjectStack";

import { expectErrorMetadata } from "../__test__/expectErrorMetadata";
import { AccountStrategy, AccountType } from "../configurations/accounts";

describe("set", () => {
test("sets all environment context values", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});

EnvironmentType.set(stack, "development");

expect(stack.node.tryGetContext("environment-type")).toBe("development");
expect(stack.node.tryGetContext("environment")).toBe("development");
expect(stack.node.tryGetContext("env")).toBe("development");
});
});

describe("tryGet", () => {
test("prefers environment-type over environment and env", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("environment-type", "development");
stack.node.setContext("environment", "staging");
stack.node.setContext("env", "production");

expect(EnvironmentType.tryGet(stack)).toBe("development");
});

test("prefers environment over env when environment-type not set", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("environment", "staging");
stack.node.setContext("env", "production");

expect(EnvironmentType.tryGet(stack)).toBe("staging");
});

test("uses env when environment-type and environment not set", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("env", "production");

expect(EnvironmentType.tryGet(stack)).toBe("production");
});

test("returns undefined if no environment context set", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});

expect(EnvironmentType.tryGet(stack)).toBeUndefined();
});
});

describe("get", () => {
test("returns environment type if valid", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
environmentType: "development",
});

const result = EnvironmentType.get(stack, ["development", "production"]);
expect(result).toBe("development");
expectErrorMetadata(stack, undefined);
});

test("respects preference order when multiple contexts set", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("environment-type", "development");
stack.node.setContext("environment", "staging");
stack.node.setContext("env", "production");

const result = EnvironmentType.get(stack, [
"development",
"staging",
"production",
]);
expect(result).toBe("development");
expectErrorMetadata(stack, undefined);
});

test("returns empty string and sets error if environment type not specified", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});

const result = EnvironmentType.get(stack, ["development", "production"]);
expect(result).toBe("");
expectErrorMetadata(
stack,
expect.stringContaining("Environment Type not specified"),
);
});

test("returns empty string and sets error if environment type not allowed", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("environment-type", "test");

const result = EnvironmentType.get(stack, ["development", "production"]);
expect(result).toBe("");
expectErrorMetadata(
stack,
expect.stringContaining("Environment Type test not allowed"),
);
});

test("matches environment type using regex pattern", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.two({
dev: { id: "111111111111" },
prod: { id: "222222222222" },
}),
accountType: AccountType.DEV,
});
stack.node.setContext("environment-type", "feature/ABC-123");

const result = EnvironmentType.get(stack, ["feature/.*", "production"]);
expect(result).toBe("feature/ABC-123");
expectErrorMetadata(stack, undefined);
});
});

0 comments on commit bcb4adb

Please sign in to comment.