Skip to content

Commit 7418a49

Browse files
committed
fix: unit test destroy script and replace shebang
1 parent 9e20f77 commit 7418a49

File tree

4 files changed

+143
-37
lines changed

4 files changed

+143
-37
lines changed

bin/destroy.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1+
#!/usr/bin/env node
2+
3+
import * as path from 'path'
4+
import { fileURLToPath } from 'url'
15
import { spawnSync } from 'child_process'
6+
27
import parseArgs from 'minimist'
38

49
import { AWSAdapterProps } from '../adapter'
510

6-
let artifactPath = 'build'
7-
const argv = parseArgs(process.argv.slice(2))
8-
if (argv._.length) {
9-
artifactPath = argv._[0]
10-
}
11+
export async function main(args: string[]): Promise<void> {
12+
let artifactPath = 'build'
13+
const argv = parseArgs(args.slice(2))
14+
if (argv._.length) {
15+
artifactPath = argv._[0]
16+
}
17+
18+
const propsPath = path.resolve(
19+
process.cwd(),
20+
artifactPath,
21+
'.adapterprops.json'
22+
)
1123

12-
const adapterProps: AWSAdapterProps = await import(
13-
`${artifactPath}/.adapterprops.json`
14-
)
24+
const adapterProps: AWSAdapterProps = await import(propsPath)
1525

16-
spawnSync('pulumi', ['destroy', '-f', '-s', adapterProps.stackName!, '-y'], {
17-
cwd: adapterProps.pulumiPath,
18-
stdio: [process.stdin, process.stdout, process.stderr],
19-
env: process.env,
20-
})
26+
spawnSync('pulumi', ['destroy', '-f', '-s', adapterProps.stackName!, '-y'], {
27+
cwd: adapterProps.pulumiPath,
28+
stdio: [process.stdin, process.stdout, process.stderr],
29+
env: process.env,
30+
})
31+
}
32+
33+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
34+
main(process.argv)
35+
}

tests/bin.destroy.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import { spawnSync } from 'child_process'
4+
5+
import { getTempDir } from './utils'
6+
7+
vi.mock('child_process', async (importOriginal) => {
8+
const mod: any = await importOriginal()
9+
return {
10+
...mod,
11+
spawnSync: vi.fn(),
12+
}
13+
})
14+
15+
describe('bin/destroy.ts', () => {
16+
let destroy: typeof import('../bin/destroy')
17+
18+
beforeEach(async () => {
19+
vi.resetModules()
20+
destroy = await import('../bin/destroy')
21+
})
22+
23+
afterEach(() => {
24+
vi.clearAllMocks()
25+
vi.resetAllMocks()
26+
})
27+
28+
it('main (no args)', async () => {
29+
const tmpDir = getTempDir()
30+
const buildDir = path.join(tmpDir, 'build')
31+
fs.mkdirSync(buildDir)
32+
33+
const propsPath = path.join(buildDir, '.adapterprops.json')
34+
35+
const expectedStackName = 'mock'
36+
const expectedPulumiPath = 'mock/pulumi'
37+
const json = JSON.stringify({
38+
stackName: expectedStackName,
39+
pulumiPath: expectedPulumiPath,
40+
})
41+
fs.writeFileSync(propsPath, json)
42+
43+
const spy = vi.spyOn(process, 'cwd')
44+
spy.mockReturnValue(tmpDir)
45+
46+
const argv = ['node', 'destroy']
47+
await destroy.main(argv)
48+
49+
fs.rmSync(tmpDir, { recursive: true })
50+
51+
let spawnSyncMock = <any>spawnSync
52+
const args = spawnSyncMock.mock.calls[0]
53+
54+
expect(spawnSync).toHaveBeenCalledTimes(1)
55+
expect(args).toEqual(
56+
expect.arrayContaining([
57+
'pulumi',
58+
['destroy', '-f', '-s', expectedStackName, '-y'],
59+
expect.objectContaining({
60+
cwd: expectedPulumiPath,
61+
}),
62+
])
63+
)
64+
})
65+
66+
it('main (with build)', async () => {
67+
const tmpDir = getTempDir()
68+
const propsPath = path.join(tmpDir, '.adapterprops.json')
69+
70+
const expectedStackName = 'mock'
71+
const expectedPulumiPath = 'mock/pulumi'
72+
const json = JSON.stringify({
73+
stackName: expectedStackName,
74+
pulumiPath: expectedPulumiPath,
75+
})
76+
fs.writeFileSync(propsPath, json)
77+
78+
const argv = ['node', 'destroy', tmpDir]
79+
await destroy.main(argv)
80+
81+
fs.rmSync(tmpDir, { recursive: true })
82+
83+
let spawnSyncMock = <any>spawnSync
84+
const args = spawnSyncMock.mock.calls[0]
85+
86+
expect(spawnSync).toHaveBeenCalledTimes(1)
87+
expect(args).toEqual(
88+
expect.arrayContaining([
89+
'pulumi',
90+
['destroy', '-f', '-s', expectedStackName, '-y'],
91+
expect.objectContaining({
92+
cwd: expectedPulumiPath,
93+
}),
94+
])
95+
)
96+
})
97+
})

tests/tests.utils.test.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
2-
31
describe('tests/utils.ts', () => {
4-
let utils: typeof import('./utils')
5-
6-
beforeEach(async () => {
7-
vi.resetModules()
8-
utils = await import('./utils')
9-
})
10-
11-
it.each([
12-
['dsaGS-', false],
13-
['af2-431--fdwfef', false],
14-
['32546546*-fgdasg', false],
15-
['grae0-5344-gafgar', true]
16-
])('validName(%s)', async (test, expected) => {
17-
const result = utils.validName(test)
18-
expect(result).toBe(expected)
19-
})
2+
let utils: typeof import('./utils')
3+
4+
beforeEach(async () => {
5+
vi.resetModules()
6+
utils = await import('./utils')
7+
})
208

21-
})
9+
it.each([
10+
['dsaGS-', false],
11+
['af2-431--fdwfef', false],
12+
['32546546*-fgdasg', false],
13+
['grae0-5344-gafgar', true],
14+
])('validName(%s)', async (test, expected) => {
15+
const result = utils.validName(test)
16+
expect(result).toBe(expected)
17+
})
18+
})

tests/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export class MyMocks implements Mocks {
1010
public resources: { [key: string]: Record<string, any> } = {}
1111
private noNameTest: string[] = [
1212
'aws:route53/record:Record',
13-
'aws:s3/bucketObject:BucketObject'
13+
'aws:s3/bucketObject:BucketObject',
1414
]
1515
newResource(args: pulumi.runtime.MockResourceArgs): {
1616
id: string | undefined
1717
state: Record<string, any>
1818
} {
19-
console.log(args.type)
20-
2119
if (!validName(args.name) && !this.noNameTest.includes(args.type)) {
2220
throw Error(`'${args.name}' is not a valid value for the name field`)
2321
}
@@ -61,10 +59,9 @@ export class MyMocks implements Mocks {
6159
}
6260

6361
export function validName(name: string): boolean {
64-
console.log(name)
6562
const regex_valid = new RegExp('^[A-Za-z0-9-]*(?<!-)$')
6663
const regex_double = new RegExp('--')
67-
return regex_valid.test(name) && !(regex_double.test(name))
64+
return regex_valid.test(name) && !regex_double.test(name)
6865
}
6966

7067
// Convert a pulumi.Output to a promise of the same type.

0 commit comments

Comments
 (0)