Skip to content

Commit cf016d3

Browse files
committed
Moved validate methods to an interface. Added import testing
1 parent c1ce61b commit cf016d3

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-test",
3-
"version": "0.0.13",
3+
"version": "0.0.15",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -15,7 +15,7 @@
1515
"dependencies": {
1616
"ajv": "^8.12.0",
1717
"ajv-formats": "^3.0.1",
18-
"codify-schemas": "1.0.45"
18+
"codify-schemas": "1.0.52"
1919
},
2020
"devDependencies": {
2121
"@oclif/prettier-config": "^0.2.1",

src/plugin-tester.ts

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Ajv from 'ajv';
22
import {
33
ApplyRequestData,
4+
ImportRequestData,
5+
ImportResponseData,
46
InitializeResponseData,
57
IpcMessageSchema,
68
MessageCmd,
@@ -15,6 +17,7 @@ import {
1517
ValidateResponseData
1618
} from 'codify-schemas';
1719
import { ChildProcess, SpawnOptions, fork, spawn } from 'node:child_process';
20+
import inspector from 'node:inspector'
1821
import path from 'node:path';
1922

2023
import { CodifyTestUtils } from './test-utils.js';
@@ -25,6 +28,7 @@ const ajv = new Ajv.default({
2528
const ipcMessageValidator = ajv.compile(IpcMessageSchema);
2629
const sudoRequestValidator = ajv.compile(SudoRequestDataSchema);
2730

31+
2832
export class PluginTester {
2933
childProcess: ChildProcess
3034

@@ -38,6 +42,9 @@ export class PluginTester {
3842
throw new Error('A fully qualified path must be supplied to PluginTester');
3943
}
4044

45+
console.log('Node Inspector:')
46+
console.log(inspector.url());
47+
4148
this.childProcess = fork(
4249
pluginPath,
4350
[],
@@ -52,7 +59,19 @@ export class PluginTester {
5259
this.handleSudoRequests(this.childProcess);
5360
}
5461

55-
async fullTest(configs: ResourceConfig[], skipUninstall = false, assertPlans?: (plans: PlanResponseData[]) => void): Promise<void> {
62+
async fullTest(
63+
configs: ResourceConfig[],
64+
{
65+
skipUninstall = false,
66+
...options
67+
}: {
68+
skipUninstall: boolean,
69+
validatePlan?: (plans: PlanResponseData[]) => Promise<void> | void
70+
validateApply?: (plans: PlanResponseData[]) => Promise<void> | void,
71+
validateDestroy?: (plans: PlanResponseData[]) => Promise<void> | void,
72+
validateImport?: (importResults: (ImportResponseData['result'][0])[]) => Promise<void> | void,
73+
}): Promise<void> {
74+
5675
const initializeResult = await this.initialize();
5776

5877
const unsupportedConfigs = configs.filter((c) =>
@@ -78,8 +97,8 @@ export class PluginTester {
7897
}));
7998
}
8099

81-
if (assertPlans) {
82-
assertPlans(plans);
100+
if (options.validatePlan) {
101+
await options.validatePlan(plans);
83102
}
84103

85104
for (const plan of plans) {
@@ -105,12 +124,40 @@ ${JSON.stringify(unsuccessfulPlans, null, 2)}`
105124
)
106125
}
107126

127+
if (options.validateApply) {
128+
await options.validateApply(plans);
129+
}
130+
131+
const importResults = [];
132+
const unsuccessfulImports = [];
133+
for (const config of configs) {
134+
const importResult = await this.import({ config })
135+
importResults.push(importResult);
136+
137+
if (importResult.result.length !== 1 ||
138+
Object.entries(config).some(([k, v]) => importResult.result[0][k] !== v)
139+
) {
140+
unsuccessfulImports.push(importResult);
141+
}
142+
}
143+
144+
if (unsuccessfulImports.length > 0) {
145+
throw new Error(`The following imports were not successful. The imports differed from the original.
146+
${JSON.stringify(unsuccessfulImports, null, 2)}`);
147+
}
148+
149+
if (options.validateImport) {
150+
await options.validateImport(importResults.map((r) => r.result[0]));
151+
}
152+
108153
if (!skipUninstall) {
109-
await this.uninstall(configs.toReversed());
154+
await this.uninstall(configs.toReversed(), options);
110155
}
111156
}
112157

113-
async uninstall(configs: ResourceConfig[]) {
158+
async uninstall(configs: ResourceConfig[], options: {
159+
validateDestroy?: (plans: PlanResponseData[]) => Promise<void> | void
160+
}) {
114161
const plans = [];
115162

116163
for (const config of configs) {
@@ -145,6 +192,10 @@ ${JSON.stringify(validationPlan, null, 2)}
145192
`);
146193
}
147194
}
195+
196+
if (options.validateDestroy) {
197+
await options.validateDestroy(plans);
198+
}
148199
}
149200

150201
async initialize(): Promise<InitializeResponseData> {
@@ -175,6 +226,13 @@ ${JSON.stringify(validationPlan, null, 2)}
175226
});
176227
}
177228

229+
async import(data: ImportRequestData): Promise<ImportResponseData> {
230+
return CodifyTestUtils.sendMessageAndAwaitResponse(this.childProcess, {
231+
cmd: 'import',
232+
data,
233+
});
234+
}
235+
178236
kill() {
179237
this.childProcess.kill();
180238
}

0 commit comments

Comments
 (0)