Skip to content

Commit 0263bad

Browse files
committed
Add pre and post options to command processor
1 parent 0034f02 commit 0263bad

File tree

2 files changed

+184
-90
lines changed

2 files changed

+184
-90
lines changed

__tests__/command.test.ts

Lines changed: 110 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { jest } from '@jest/globals'
22
import { Command } from 'commander'
3+
import path from 'path'
34
import { ResetCoreMetadata } from '../src/stubs/core/core.js'
4-
import { ResetEnvMetadata } from '../src/stubs/env.js'
5+
import { EnvMeta, ResetEnvMetadata } from '../src/stubs/env.js'
56

67
const action = jest.fn()
78

@@ -15,172 +16,209 @@ const process_exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
1516
throw new Error(`process.exit()`)
1617
})
1718

18-
const process_stderrSpy = jest
19-
.spyOn(process.stderr, 'write')
20-
.mockImplementation(() => true)
21-
22-
let program: Command
23-
24-
// Prevent output during tests
25-
jest.spyOn(console, 'log').mockImplementation(() => {})
26-
2719
const { makeProgram } = await import('../src/command.js')
2820

2921
describe('Commmand', () => {
3022
beforeEach(async () => {
3123
// Reset metadata
3224
ResetEnvMetadata()
3325
ResetCoreMetadata()
34-
35-
// Create a new program before each test
36-
program = await makeProgram()
3726
})
3827

3928
afterEach(() => {
4029
jest.resetAllMocks()
4130
})
4231

4332
describe('makeProgram()', () => {
44-
it('Returns a Program', () => {
33+
it('Returns a Program', async () => {
34+
const program = await makeProgram()
35+
4536
expect(program).not.toBe(null)
4637
expect(program).toBeInstanceOf(Command)
4738
})
4839

49-
it('Has a run command', () => {
40+
it('Has a run command', async () => {
41+
const program = await makeProgram()
42+
5043
expect(program.commands.find(c => c.name() === 'run')).toBeInstanceOf(
5144
Command
5245
)
5346
})
5447

5548
it('Runs if all arguments are provided (action.yml)', async () => {
56-
await (
57-
await makeProgram()
58-
).parseAsync(
49+
const program = await makeProgram()
50+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
51+
52+
program.parse(
5953
[
6054
'./__fixtures__/typescript/success',
6155
'src/main.ts',
62-
'./__fixtures__/typescript/success/.env.fixture'
56+
'./__fixtures__/typescript/success/.env.fixture',
57+
'--pre',
58+
'pre/main.ts',
59+
'--post',
60+
'post/main.ts'
6361
],
6462
{
6563
from: 'user'
6664
}
6765
)
6866

69-
expect(process_exitSpy).not.toHaveBeenCalled()
7067
expect(action).toHaveBeenCalled()
7168
})
7269

7370
it('Runs if all arguments are provided (action.yaml)', async () => {
74-
await (
75-
await makeProgram()
76-
).parseAsync(
71+
const program = await makeProgram()
72+
EnvMeta.actionPath = path.resolve(
73+
'./__fixtures__/typescript/success-yaml'
74+
)
75+
76+
program.parse(
7777
[
7878
'./__fixtures__/typescript/success-yaml',
7979
'src/main.ts',
80-
'./__fixtures__/typescript/success-yaml/.env.fixture'
80+
'./__fixtures__/typescript/success-yaml/.env.fixture',
81+
'--pre',
82+
'pre/main.ts',
83+
'--post',
84+
'post/main.ts'
8185
],
8286
{
8387
from: 'user'
8488
}
8589
)
8690

87-
expect(process_exitSpy).not.toHaveBeenCalled()
8891
expect(action).toHaveBeenCalled()
8992
})
9093

9194
it('Exits if no path argument is provided', async () => {
92-
await (await makeProgram()).parseAsync([], { from: 'user' })
95+
const program = await makeProgram()
9396

94-
expect(process_exitSpy).toHaveBeenCalled()
97+
program.parse([], { from: 'user' })
9598

96-
process_stderrSpy.mockRestore()
99+
expect(process_exitSpy).toHaveBeenCalled()
97100
})
98101

99102
it('Exits if no entrypoint argument is provided', async () => {
100-
await (
101-
await makeProgram()
102-
).parseAsync(['./__fixtures__/typescript/success', ''], { from: 'user' })
103+
const program = await makeProgram()
104+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
103105

104-
expect(process_exitSpy).toHaveBeenCalled()
106+
program.parse(['./__fixtures__/typescript/success', ''], { from: 'user' })
105107

106-
process_stderrSpy.mockRestore()
108+
expect(process_exitSpy).toHaveBeenCalled()
107109
})
108110

109111
it('Exits if no env-file argument is provided', async () => {
110-
await (
111-
await makeProgram()
112-
).parseAsync(['./__fixtures__/typescript/success', 'src/main.ts'], {
112+
const program = await makeProgram()
113+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
114+
115+
program.parse(['./__fixtures__/typescript/success', 'src/main.ts'], {
113116
from: 'user'
114117
})
115118

116119
expect(process_exitSpy).toHaveBeenCalled()
117-
118-
process_stderrSpy.mockRestore()
119120
})
120121

121122
it('Exits if the action path is not a directory', async () => {
122-
await expect(
123-
(await makeProgram()).parseAsync(
124-
['./package.json', 'src/main.ts', '.env'],
125-
{
126-
from: 'user'
127-
}
128-
)
129-
).rejects.toThrow('Action path must be a directory')
123+
const program = await makeProgram()
124+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
130125

131-
process_stderrSpy.mockRestore()
126+
expect(() => {
127+
program.parse(['./package.json', 'src/main.ts', '.env'], {
128+
from: 'user'
129+
})
130+
}).toThrow('Action path must be a directory')
132131
})
133132

134133
it('Exits if the action path does not exist', async () => {
135-
await expect(
136-
(await makeProgram()).parseAsync(
137-
['/test/path/does/not/exist', 'src/main.ts', '.env'],
134+
const program = await makeProgram()
135+
EnvMeta.actionPath = path.resolve('/test/path/does/not/exist')
136+
137+
expect(() => {
138+
program.parse(['/test/path/does/not/exist', 'src/main.ts', '.env'], {
139+
from: 'user'
140+
})
141+
}).toThrow('Action path does not exist')
142+
})
143+
144+
it('Exits if the action path does not contain an action.yml or action.yaml', async () => {
145+
const program = await makeProgram()
146+
EnvMeta.actionPath = path.resolve('./__fixtures__')
147+
148+
expect(() => {
149+
program.parse(['./__fixtures__', 'src/main.ts', '.env'], {
150+
from: 'user'
151+
})
152+
}).toThrow('Path must contain an action.yml / action.yaml file')
153+
})
154+
155+
it('Exits if the entrypoint does not exist', async () => {
156+
const program = await makeProgram()
157+
158+
expect(() => {
159+
program.parse(
160+
['./__fixtures__/typescript/success', 'src/fake.ts', '.env'],
138161
{
139162
from: 'user'
140163
}
141164
)
142-
).rejects.toThrow('Action path does not exist')
143-
144-
process_stderrSpy.mockRestore()
165+
}).toThrow('Entrypoint does not exist')
145166
})
146167

147-
it('Exits if the action path does not contain an action.yml or action.yaml', async () => {
148-
await expect(
149-
(await makeProgram()).parseAsync(
150-
['./__fixtures__', 'src/main.ts', '.env'],
168+
it('Exits if the pre entrypoint does not exist', async () => {
169+
const program = await makeProgram()
170+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
171+
172+
expect(() => {
173+
program.parse(
174+
[
175+
'./__fixtures__/typescript/success',
176+
'src/main.ts',
177+
'./__fixtures__/typescript/success/.env.fixture',
178+
'--pre',
179+
'pre/fake.ts'
180+
],
151181
{
152182
from: 'user'
153183
}
154184
)
155-
).rejects.toThrow('Path must contain an action.yml / action.yaml file')
156-
157-
process_stderrSpy.mockRestore()
185+
}).toThrow('PRE entrypoint does not exist')
158186
})
159187

160-
it('Exits if the entrypoint does not exist', async () => {
161-
await expect(
162-
(await makeProgram()).parseAsync(
163-
['./__fixtures__/typescript/success', 'src/fake.ts', '.env'],
188+
it('Exits if the post entrypoint does not exist', async () => {
189+
const program = await makeProgram()
190+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
191+
192+
expect(() => {
193+
program.parse(
194+
[
195+
'./__fixtures__/typescript/success',
196+
'src/main.ts',
197+
'./__fixtures__/typescript/success/.env.fixture',
198+
'--pre',
199+
'pre/main.ts',
200+
'--post',
201+
'post/fake.ts'
202+
],
164203
{
165204
from: 'user'
166205
}
167206
)
168-
).rejects.toThrow('Entrypoint does not exist')
169-
170-
process_stderrSpy.mockRestore()
207+
}).toThrow('POST entrypoint does not exist')
171208
})
172209

173210
it('Throws if the dotenv file does not exist', async () => {
174-
await expect(
175-
(await makeProgram()).parseAsync(
211+
const program = await makeProgram()
212+
EnvMeta.actionPath = path.resolve('./__fixtures__/typescript/success')
213+
214+
expect(() => {
215+
program.parse(
176216
['./__fixtures__/typescript/success', 'src/main.ts', '.notreal.env'],
177217
{
178218
from: 'user'
179219
}
180220
)
181-
).rejects.toThrow('Environment file does not exist')
182-
183-
process_stderrSpy.mockRestore()
221+
}).toThrow('Environment file does not exist')
184222
})
185223
})
186224
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy