1
1
import { jest } from '@jest/globals'
2
2
import { Command } from 'commander'
3
+ import path from 'path'
3
4
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'
5
6
6
7
const action = jest . fn ( )
7
8
@@ -15,172 +16,209 @@ const process_exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
15
16
throw new Error ( `process.exit()` )
16
17
} )
17
18
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
-
27
19
const { makeProgram } = await import ( '../src/command.js' )
28
20
29
21
describe ( 'Commmand' , ( ) => {
30
22
beforeEach ( async ( ) => {
31
23
// Reset metadata
32
24
ResetEnvMetadata ( )
33
25
ResetCoreMetadata ( )
34
-
35
- // Create a new program before each test
36
- program = await makeProgram ( )
37
26
} )
38
27
39
28
afterEach ( ( ) => {
40
29
jest . resetAllMocks ( )
41
30
} )
42
31
43
32
describe ( 'makeProgram()' , ( ) => {
44
- it ( 'Returns a Program' , ( ) => {
33
+ it ( 'Returns a Program' , async ( ) => {
34
+ const program = await makeProgram ( )
35
+
45
36
expect ( program ) . not . toBe ( null )
46
37
expect ( program ) . toBeInstanceOf ( Command )
47
38
} )
48
39
49
- it ( 'Has a run command' , ( ) => {
40
+ it ( 'Has a run command' , async ( ) => {
41
+ const program = await makeProgram ( )
42
+
50
43
expect ( program . commands . find ( c => c . name ( ) === 'run' ) ) . toBeInstanceOf (
51
44
Command
52
45
)
53
46
} )
54
47
55
48
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 (
59
53
[
60
54
'./__fixtures__/typescript/success' ,
61
55
'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'
63
61
] ,
64
62
{
65
63
from : 'user'
66
64
}
67
65
)
68
66
69
- expect ( process_exitSpy ) . not . toHaveBeenCalled ( )
70
67
expect ( action ) . toHaveBeenCalled ( )
71
68
} )
72
69
73
70
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 (
77
77
[
78
78
'./__fixtures__/typescript/success-yaml' ,
79
79
'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'
81
85
] ,
82
86
{
83
87
from : 'user'
84
88
}
85
89
)
86
90
87
- expect ( process_exitSpy ) . not . toHaveBeenCalled ( )
88
91
expect ( action ) . toHaveBeenCalled ( )
89
92
} )
90
93
91
94
it ( 'Exits if no path argument is provided' , async ( ) => {
92
- await ( await makeProgram ( ) ) . parseAsync ( [ ] , { from : 'user' } )
95
+ const program = await makeProgram ( )
93
96
94
- expect ( process_exitSpy ) . toHaveBeenCalled ( )
97
+ program . parse ( [ ] , { from : 'user' } )
95
98
96
- process_stderrSpy . mockRestore ( )
99
+ expect ( process_exitSpy ) . toHaveBeenCalled ( )
97
100
} )
98
101
99
102
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' )
103
105
104
- expect ( process_exitSpy ) . toHaveBeenCalled ( )
106
+ program . parse ( [ './__fixtures__/typescript/success' , '' ] , { from : 'user' } )
105
107
106
- process_stderrSpy . mockRestore ( )
108
+ expect ( process_exitSpy ) . toHaveBeenCalled ( )
107
109
} )
108
110
109
111
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' ] , {
113
116
from : 'user'
114
117
} )
115
118
116
119
expect ( process_exitSpy ) . toHaveBeenCalled ( )
117
-
118
- process_stderrSpy . mockRestore ( )
119
120
} )
120
121
121
122
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' )
130
125
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' )
132
131
} )
133
132
134
133
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' ] ,
138
161
{
139
162
from : 'user'
140
163
}
141
164
)
142
- ) . rejects . toThrow ( 'Action path does not exist' )
143
-
144
- process_stderrSpy . mockRestore ( )
165
+ } ) . toThrow ( 'Entrypoint does not exist' )
145
166
} )
146
167
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
+ ] ,
151
181
{
152
182
from : 'user'
153
183
}
154
184
)
155
- ) . rejects . toThrow ( 'Path must contain an action.yml / action.yaml file' )
156
-
157
- process_stderrSpy . mockRestore ( )
185
+ } ) . toThrow ( 'PRE entrypoint does not exist' )
158
186
} )
159
187
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
+ ] ,
164
203
{
165
204
from : 'user'
166
205
}
167
206
)
168
- ) . rejects . toThrow ( 'Entrypoint does not exist' )
169
-
170
- process_stderrSpy . mockRestore ( )
207
+ } ) . toThrow ( 'POST entrypoint does not exist' )
171
208
} )
172
209
173
210
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 (
176
216
[ './__fixtures__/typescript/success' , 'src/main.ts' , '.notreal.env' ] ,
177
217
{
178
218
from : 'user'
179
219
}
180
220
)
181
- ) . rejects . toThrow ( 'Environment file does not exist' )
182
-
183
- process_stderrSpy . mockRestore ( )
221
+ } ) . toThrow ( 'Environment file does not exist' )
184
222
} )
185
223
} )
186
224
} )
0 commit comments