Skip to content

Commit 430eee7

Browse files
committed
Added planAssertion to full test so that plans can be asserted during tests. Added tests.
1 parent f31472a commit 430eee7

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

src/plugin-tester.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import {
1515
ValidateRequestData,
1616
ValidateResponseData
1717
} from 'codify-schemas';
18-
import { ChildProcess, fork, spawn, SpawnOptions } from 'node:child_process';
18+
import { ChildProcess, SpawnOptions, fork, spawn } from 'node:child_process';
19+
import path from 'node:path';
1920

2021
import { CodifyTestUtils } from './test-utils.js';
21-
import path from 'node:path';
2222

2323
const ajv = new Ajv2020.default({
2424
strict: true
@@ -53,7 +53,7 @@ export class PluginTester {
5353
this.handleSudoRequests(this.childProcess);
5454
}
5555

56-
async fullTest(configs: ResourceConfig[]): Promise<void> {
56+
async fullTest(configs: ResourceConfig[], assertPlans?: (plans: PlanResponseData[]) => void): Promise<void> {
5757
const initializeResult = await this.initialize();
5858

5959
const unsupportedConfigs = configs.filter((c) =>
@@ -75,6 +75,10 @@ export class PluginTester {
7575
plans.push(await this.plan(config));
7676
}
7777

78+
if (assertPlans) {
79+
assertPlans(plans);
80+
}
81+
7882
for (const plan of plans) {
7983
await this.apply({
8084
planId: plan.planId
@@ -97,25 +101,25 @@ ${JSON.stringify(unsuccessfulPlans, null, 2)}`
97101

98102
async uninstall(configs: ResourceConfig[]) {
99103
for (const config of configs) {
100-
const { type, dependsOn, name, ...parameters } = config
104+
const { dependsOn, name, type, ...parameters } = config
101105

102106
await this.apply({
103107
plan: {
104108
operation: ResourceOperation.DESTROY,
105-
resourceType: config.type,
106109
parameters: Object.entries(parameters).map(([key, value]) => ({
107110
name: key,
108-
previousValue: value,
109111
newValue: null,
110112
operation: ParameterOperation.REMOVE,
113+
previousValue: value,
111114
})),
115+
resourceType: type,
112116
}
113117
});
114118

115119
// Validate that the destroy was successful
116120
const validationPlan = await this.plan(config);
117121
if (validationPlan.operation !== ResourceOperation.CREATE) {
118-
throw new Error(`Resource ${config.type} was not successfully destroyed.
122+
throw new Error(`Resource ${type} was not successfully destroyed.
119123
Validation plan shows:
120124
${JSON.stringify(validationPlan, null, 2)}
121125
Previous config:
@@ -190,8 +194,6 @@ type CodifySpawnOptions = {
190194
*
191195
* @param cmd Command to run. Ex: `rm -rf`
192196
* @param opts Options for spawn
193-
* @param secureMode Secure mode for sudo
194-
* @param pluginName Optional plugin name so that stdout and stderr can be piped
195197
*
196198
* @see promiseSpawn
197199
* @see spawn

test/plugin-tester.test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { describe, expect, it } from 'vitest';
2-
import { PluginTester } from '../src/plugin-tester.js';
2+
import { PluginTester } from '../src/index.js';
33
import path from 'node:path';
44
import { ResourceOperation } from 'codify-schemas/src/types/index.js';
5-
import { ParameterOperation } from 'codify-schemas';
65

76
describe('Plugin tester integration tests', () => {
87
it('Can instantiate a plugin', async () => {
@@ -13,7 +12,7 @@ describe('Plugin tester integration tests', () => {
1312
expect(plugin.childProcess.stderr).to.not.be.undefined;
1413
expect(plugin.childProcess.channel).to.not.be.undefined;
1514

16-
await plugin.initialize({});
15+
await plugin.initialize();
1716
})
1817

1918
it('Can validate a config', async () => {
@@ -110,6 +109,35 @@ describe('Plugin tester integration tests', () => {
110109
}]);
111110
})
112111

112+
it('Full test supports plan assertions to ensure the generated plan is correct', async () => {
113+
const plugin = new PluginTester(path.join(__dirname, './test-plugin.ts'));
114+
115+
// No expect needed here. This passes if it doesn't throw.
116+
await plugin.fullTest([{
117+
type: 'test',
118+
propA: 'a',
119+
propB: 10,
120+
propC: 'c',
121+
}, {
122+
type: 'test',
123+
propA: 'a',
124+
propB: 10,
125+
propC: 'c',
126+
}], (plans) => {
127+
expect(plans[0]).toMatchObject({
128+
planId: expect.any(String),
129+
operation: ResourceOperation.NOOP,
130+
resourceType: 'test',
131+
});
132+
133+
expect(plans[1]).toMatchObject({
134+
planId: expect.any(String),
135+
operation: ResourceOperation.NOOP,
136+
resourceType: 'test',
137+
});
138+
});
139+
})
140+
113141
it('Has helpers that can uninstall a resource', async () => {
114142
const plugin = new PluginTester(path.join(__dirname, './test-plugin.ts'));
115143

0 commit comments

Comments
 (0)