Skip to content

Commit a9b612d

Browse files
committed
feat: Added testSpawn and filter tests by OS
1 parent d6e90b2 commit a9b612d

7 files changed

Lines changed: 58 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-test",
3-
"version": "0.0.53-beta11",
3+
"version": "0.0.53-beta13",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './plugin-tester.js'
2+
export * from './spawn.js'
23
export * from './test-utils.js'

src/plugin-tester.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import chalk from 'chalk';
22
import {
3-
ImportResponseData,
3+
ImportResponseData, OS,
44
PlanResponseData,
55
ResourceConfig,
66
ResourceOperation,
77
} from 'codify-schemas';
88
import unionBy from 'lodash.unionby';
99

1010
import { PluginProcess } from './plugin-process.js';
11-
import { getResourceOs, splitUserConfig } from './utils.js';
11+
import { getPlatformOs, splitUserConfig } from './utils.js';
12+
import os from 'node:os';
1213

1314
export class PluginTester {
1415
static async fullTest(
@@ -26,7 +27,7 @@ export class PluginTester {
2627
validateModify?: (plans: PlanResponseData[]) => Promise<void> | void,
2728
}
2829
}): Promise<void> {
29-
configs = configs.filter((c) => !c.os || c.os.includes(getResourceOs()));
30+
configs = configs.filter((c) => !c.os || c.os.includes(getPlatformOs()));
3031
const ids = configs
3132
.map((c) => `${c.type}${c.name ? `.${c.name}` : ''}`)
3233
.join(', ')
@@ -50,6 +51,8 @@ export class PluginTester {
5051
throw new Error(`The plugin does not support the following configs supplied:\n ${JSON.stringify(unsupportedConfigs, null, 2)}\n Initialize result: ${JSON.stringify(initializeResult)}`)
5152
}
5253

54+
configs = configs.filter((c) => initializeResult.resourceDefinitions.find((rd) => rd.type === c.type)?.operatingSystems?.includes(os.platform() as OS));
55+
5356
console.info(chalk.cyan('Testing validate...'))
5457
const validate = await plugin.validate({ configs: configs.map((c) => {
5558
const { coreParameters, parameters } = splitUserConfig(c)

src/spawn.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface SpawnOptions {
1818
stdin?: boolean,
1919
}
2020

21+
export function testSpawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
22+
return spawnSafe(cmd, { interactive: true, ...options, });
23+
}
24+
2125
export function spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
2226
if (cmd.toLowerCase().includes('sudo')) {
2327
throw new Error('Command must not include sudo')

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function splitUserConfig<T extends StringIndexedObject>(
2020
};
2121
}
2222

23-
export function getResourceOs(): ResourceOs{
23+
export function getPlatformOs(): ResourceOs{
2424
const currOs = os.platform();
2525
switch (currOs) {
2626
case 'darwin': {

test/plugin-tester.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ describe('Plugin tester integration tests', () => {
313313
}
314314
})
315315

316+
it('Can filter out unsupported configs based on OS', { timeout: 300000 }, async () => {
317+
await PluginTester.fullTest(pluginPath, [{
318+
type: 'windows-only',
319+
}], {
320+
validatePlan(plan) {
321+
expect(plan.length).to.eq(0);
322+
}
323+
});
324+
});
325+
326+
316327

317328
// it('Can uninstall two resources with the same type', async () => {
318329
// await plugin.fullTest([{

test/test-plugin.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {
22
CreatePlan,
3-
DestroyPlan, ModifyPlan,
3+
DestroyPlan,
4+
ModifyPlan,
45
ParameterChange,
56
Plugin,
67
Resource,
78
ResourceSettings,
89
runPlugin
910
} from 'codify-plugin-lib';
10-
import { StringIndexedObject } from 'codify-schemas';
11-
import { b } from 'vitest/dist/reporters-yx5ZTtEV';
11+
import { OS, StringIndexedObject } from 'codify-schemas';
1212
import * as fs from 'node:fs';
1313

1414
export interface TestConfig extends StringIndexedObject {
@@ -27,6 +27,7 @@ export class TestResource extends Resource<TestConfig> {
2727
getSettings(): ResourceSettings<TestConfig> {
2828
return {
2929
id: 'test',
30+
operatingSystems: [OS.Linux, OS.Darwin],
3031
allowMultiple: true,
3132
};
3233
}
@@ -54,6 +55,7 @@ export class TestResource2 extends Resource<TestConfig2> {
5455
getSettings(): ResourceSettings<TestConfig2> {
5556
return {
5657
id: 'test2',
58+
operatingSystems: [OS.Linux, OS.Darwin],
5759
parameterSettings: {
5860
propB: { type: 'array' }
5961
}
@@ -82,7 +84,8 @@ export class TestUninstallResource extends Resource<TestConfig> {
8284
first = true;
8385
getSettings(): ResourceSettings<TestConfig> {
8486
return {
85-
id: 'test-uninstall'
87+
id: 'test-uninstall',
88+
operatingSystems: [OS.Linux, OS.Darwin],
8689
}
8790
}
8891

@@ -106,6 +109,7 @@ export class TestModifyResource extends Resource<TestConfig> {
106109
getSettings(): ResourceSettings<TestConfig> {
107110
return {
108111
id: 'test-modify',
112+
operatingSystems: [OS.Linux, OS.Darwin],
109113
parameterSettings: {
110114
propA: { type: 'string', canModify: true },
111115
propB: { type: 'string', canModify: true },
@@ -141,6 +145,7 @@ export class TestDestroyResource extends Resource<TestConfig> {
141145
getSettings(): ResourceSettings<TestConfig> {
142146
return {
143147
id: 'test-destroy',
148+
operatingSystems: [OS.Linux, OS.Darwin],
144149
}
145150
}
146151

@@ -165,6 +170,29 @@ export class TestDestroyResource extends Resource<TestConfig> {
165170
}
166171
}
167172

173+
export class WindowsOnlyResource extends Resource<TestConfig> {
174+
private name: string;
175+
176+
getSettings(): ResourceSettings<TestConfig> {
177+
return {
178+
id: 'windows-only',
179+
operatingSystems: [OS.Windows],
180+
}
181+
}
182+
183+
async refresh(parameters: Partial<TestConfig>): Promise<Array<Partial<TestConfig>> | Partial<TestConfig> | null> {
184+
return {};
185+
}
186+
187+
async modify(pc: ParameterChange<TestConfig>, plan: ModifyPlan<TestConfig>): Promise<void> {
188+
return super.modify(pc, plan);
189+
}
190+
191+
async create(plan: CreatePlan<TestConfig>): Promise<void> {}
192+
193+
async destroy(plan: DestroyPlan<TestConfig>): Promise<void> {}
194+
}
195+
168196
export class TestDestroyResource2 extends TestDestroyResource {
169197
getSettings(): ResourceSettings<TestConfig> {
170198
return {
@@ -181,6 +209,7 @@ runPlugin(Plugin.create(
181209
new TestUninstallResource(),
182210
new TestModifyResource(),
183211
new TestDestroyResource(),
184-
new TestDestroyResource2()
212+
new TestDestroyResource2(),
213+
new WindowsOnlyResource(),
185214
]
186215
));

0 commit comments

Comments
 (0)