Skip to content

Commit 833ae6e

Browse files
committed
Fixed import bugs by making import use refresh() instead of a custom eqs method
1 parent 67c02a6 commit 833ae6e

4 files changed

Lines changed: 133 additions & 49 deletions

File tree

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-test",
3-
"version": "0.0.18",
3+
"version": "0.0.22",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -15,20 +15,24 @@
1515
"dependencies": {
1616
"ajv": "^8.12.0",
1717
"ajv-formats": "^3.0.1",
18-
"codify-schemas": "1.0.52"
18+
"codify-schemas": "1.0.52",
19+
"lodash.matches": "^4.6.0",
20+
"lodash.differencewith": "4.5.0"
1921
},
2022
"devDependencies": {
2123
"@oclif/prettier-config": "^0.2.1",
2224
"@types/debug": "^4.1.12",
2325
"@types/node": "^18",
26+
"@types/lodash.matches": "^4.6.9",
27+
"@types/lodash.differencewith": "^4.5.9",
2428
"eslint": "^8.51.0",
2529
"eslint-config-oclif": "^5",
2630
"eslint-config-oclif-typescript": "^3",
2731
"eslint-config-prettier": "^9.0.0",
2832
"tsx": "^4.7.3",
2933
"typescript": "^5",
3034
"vitest": "^1.4.0",
31-
"codify-plugin-lib": "1.0.76"
35+
"codify-plugin-lib": "1.0.106"
3236
},
3337
"engines": {
3438
"node": ">=18.0.0"

src/plugin-tester.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ export class PluginTester {
4242
throw new Error('A fully qualified path must be supplied to PluginTester');
4343
}
4444

45-
console.log('Node Inspector:')
46-
console.log(inspector.url());
47-
4845
this.childProcess = fork(
4946
pluginPath,
5047
[],
@@ -134,9 +131,8 @@ ${JSON.stringify(unsuccessfulPlans, null, 2)}`
134131
const importResult = await this.import({ config })
135132
importResults.push(importResult);
136133

137-
if (importResult.result.length !== 1 ||
138-
Object.entries(config).some(([k, v]) => importResult.result[0][k] !== v)
139-
) {
134+
const validationPlan = await this.plan({ desired: importResult.result[0], isStateful: false, state: undefined });
135+
if (validationPlan.operation !== ResourceOperation.NOOP) {
140136
unsuccessfulImports.push(importResult);
141137
}
142138
}

test/plugin-tester.test.ts

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { describe, expect, it } from 'vitest';
22
import { PluginTester } from '../src/index.js';
33
import path from 'node:path';
44
import { ResourceOperation } from 'codify-schemas/src/types/index.js';
5+
import deepMatches from 'lodash.matches';
6+
import differenceWith from 'lodash.differencewith';
7+
58

69
describe('Plugin tester integration tests', () => {
710
it('Can instantiate a plugin', async () => {
@@ -122,7 +125,9 @@ describe('Plugin tester integration tests', () => {
122125
propA: 'a',
123126
propB: 10,
124127
propC: 'c',
125-
}]);
128+
}], {
129+
skipUninstall: true,
130+
});
126131
})
127132

128133
it('Full test supports plan assertions to ensure the generated plan is correct', async () => {
@@ -139,19 +144,57 @@ describe('Plugin tester integration tests', () => {
139144
propA: 'a',
140145
propB: 10,
141146
propC: 'c',
142-
}], false, (plans) => {
143-
expect(plans[0]).toMatchObject({
144-
planId: expect.any(String),
145-
operation: ResourceOperation.NOOP,
146-
resourceType: 'test',
147-
});
148-
149-
expect(plans[1]).toMatchObject({
150-
planId: expect.any(String),
151-
operation: ResourceOperation.NOOP,
152-
resourceType: 'test',
153-
});
154-
});
147+
}], {
148+
skipUninstall: true,
149+
validatePlan: (plans) => {
150+
expect(plans[0]).toMatchObject({
151+
planId: expect.any(String),
152+
operation: ResourceOperation.NOOP,
153+
resourceType: 'test',
154+
});
155+
156+
expect(plans[1]).toMatchObject({
157+
planId: expect.any(String),
158+
operation: ResourceOperation.NOOP,
159+
resourceType: 'test',
160+
});
161+
}
162+
})
163+
})
164+
165+
it('Full test supports plan assertions to ensure the generated plan is correct (2)', async () => {
166+
const plugin = new PluginTester(path.join(__dirname, './test-plugin.ts'));
167+
168+
// No expect needed here. This passes if it doesn't throw.
169+
await plugin.fullTest([{
170+
type: 'test',
171+
propA: 'a',
172+
propB: 10,
173+
}], {
174+
skipUninstall: true,
175+
validatePlan: (plans) => {
176+
expect(plans[0]).toMatchObject({
177+
planId: expect.any(String),
178+
operation: ResourceOperation.NOOP,
179+
resourceType: 'test',
180+
});
181+
}
182+
})
183+
})
184+
185+
it('Full test supports plan assertions to ensure the generated plan is correct (3)', async () => {
186+
const plugin = new PluginTester(path.join(__dirname, './test-plugin.ts'));
187+
188+
console.log(differenceWith(['b', 'a'], ['a', 'b', 'c'], (a, b) => deepMatches(a)(b)).length === 0);
189+
190+
// No expect needed here. This passes if it doesn't throw.
191+
await plugin.fullTest([{
192+
type: 'test2',
193+
propB: ['second', 'first'],
194+
propA: 'a',
195+
}], {
196+
skipUninstall: true,
197+
})
155198
})
156199

157200
it('Has helpers that can uninstall a resource', async () => {
@@ -177,4 +220,6 @@ describe('Plugin tester integration tests', () => {
177220
propC: 'c',
178221
}])).rejects.toThrowError();
179222
})
223+
224+
180225
})

test/test-plugin.ts

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, Resource, runPlugin } from 'codify-plugin-lib';
1+
import { CreatePlan, DestroyPlan, Plugin, Resource, ResourceSettings, runPlugin } from 'codify-plugin-lib';
22
import { StringIndexedObject } from 'codify-schemas';
33

44
export interface TestConfig extends StringIndexedObject {
@@ -7,16 +7,18 @@ export interface TestConfig extends StringIndexedObject {
77
propC: string;
88
}
99

10-
export class TestResource extends Resource<TestConfig> {
11-
constructor() {
12-
super({
13-
type: 'test'
14-
});
15-
}
10+
export interface TestConfig2 extends StringIndexedObject {
11+
propA: string;
12+
propB: string[];
13+
}
1614

17-
async applyCreate(): Promise<void> {}
1815

19-
async applyDestroy(): Promise<void> {}
16+
export class TestResource extends Resource<TestConfig> {
17+
getSettings(): ResourceSettings<TestConfig> {
18+
return {
19+
id: 'test'
20+
};
21+
}
2022

2123
async refresh(parameters: Partial<TestConfig>): Promise<Partial<TestConfig> | null> {
2224
if (parameters.propD) {
@@ -29,34 +31,71 @@ export class TestResource extends Resource<TestConfig> {
2931
propC: 'c',
3032
};
3133
}
34+
35+
async create(plan: CreatePlan<TestConfig>): Promise<void> {
36+
}
37+
38+
async destroy(plan: DestroyPlan<TestConfig>): Promise<void> {
39+
}
3240
}
3341

34-
export class TestUninstallResource extends Resource<TestConfig> {
35-
constructor() {
36-
super({
37-
type: 'test-uninstall'
38-
});
42+
export class TestResource2 extends Resource<TestConfig2> {
43+
getSettings(): ResourceSettings<TestConfig2> {
44+
return {
45+
id: 'test2',
46+
parameterSettings: {
47+
propB: { type: 'array' }
48+
}
49+
};
3950
}
4051

41-
async applyCreate(): Promise<void> {}
52+
async refresh(parameters: Partial<TestConfig2>): Promise<Partial<TestConfig2> | null> {
53+
if (parameters.propD) {
54+
throw new Error('Prop D is included');
55+
}
4256

43-
async applyDestroy(): Promise<void> {}
57+
return {
58+
propA: 'a',
59+
propB: ['first', 'second', 'third']
60+
};
61+
}
4462

45-
async refresh(): Promise<Partial<TestConfig> | null> {
46-
return null;
63+
async create(plan: CreatePlan<TestConfig2>): Promise<void> {
64+
}
65+
66+
async destroy(plan: DestroyPlan<TestConfig2>): Promise<void> {
4767
}
4868
}
4969

50-
function buildPlugin(): Plugin {
51-
const resourceMap = new Map();
70+
export class TestUninstallResource extends Resource<TestConfig> {
71+
first = true;
72+
getSettings(): ResourceSettings<TestConfig> {
73+
return {
74+
id: 'test-uninstall'
75+
}
76+
}
77+
78+
async create(plan: CreatePlan<TestConfig>): Promise<void> {
79+
}
5280

53-
const testResource = new TestResource();
54-
resourceMap.set(testResource.typeId, testResource);
81+
async destroy(plan: DestroyPlan<TestConfig>): Promise<void> {
82+
}
5583

56-
const testUninstallResource = new TestUninstallResource();
57-
resourceMap.set(testUninstallResource.typeId, testUninstallResource);
84+
async refresh(parameters: Partial<TestConfig>): Promise<Array<Partial<TestConfig>> | Partial<TestConfig> | null> {
85+
if (this.first) {
86+
this.first = false;
87+
return parameters;
88+
}
5889

59-
return new Plugin('test', resourceMap);
90+
return null;
91+
}
6092
}
6193

62-
runPlugin(buildPlugin());
94+
runPlugin(Plugin.create(
95+
'default',
96+
[
97+
new TestResource(),
98+
new TestResource2(),
99+
new TestUninstallResource()
100+
]
101+
));

0 commit comments

Comments
 (0)