3
3
import * as awilix from "awilix" ;
4
4
import { expect } from "chai" ;
5
5
import sinon from "sinon" ;
6
- import tryToCatch from "try-to-catch" ;
7
6
8
7
import { run } from "../../src/cli.mjs" ;
9
8
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs" ;
@@ -39,7 +38,7 @@ describe("schema pull", function () {
39
38
gatherFSL = container . resolve ( "gatherFSL" ) ;
40
39
} ) ;
41
40
42
- it ( "can pull schema, adding new files and overwriting existing files" , async function ( ) {
41
+ it ( "can pull schema, adding new files and overwriting existing files" , async function ( ) {
43
42
gatherFSL . resolves ( [
44
43
{
45
44
name : "coll.fsl" ,
@@ -59,17 +58,18 @@ describe("schema pull", function () {
59
58
fetch . onCall ( 0 ) . resolves (
60
59
f ( {
61
60
version : "194838274939473" ,
62
- files : [
63
- { filename : "main.fsl" } ,
64
- { filename : "second.fsl" } ,
65
- { filename : "third.fsl" } ,
66
- ] ,
61
+ // assume no staged schema for this test
62
+ status : "none" ,
67
63
} ) ,
68
64
) ;
69
65
fetch . onCall ( 1 ) . resolves (
70
66
f ( {
71
67
version : "194838274939473" ,
72
- status : "ready" ,
68
+ files : [
69
+ { filename : "main.fsl" } ,
70
+ { filename : "second.fsl" } ,
71
+ { filename : "third.fsl" } ,
72
+ ] ,
73
73
} ) ,
74
74
) ;
75
75
fetch . onCall ( 2 ) . resolves (
@@ -98,35 +98,44 @@ describe("schema pull", function () {
98
98
expect ( gatherFSL ) . to . have . been . calledWith ( "." ) ;
99
99
100
100
expect ( fetch ) . to . have . been . calledWith (
101
- buildUrl ( "/schema/1/files " ) ,
101
+ buildUrl ( "/schema/1/staged/status " ) ,
102
102
commonFetchParams ,
103
103
) ;
104
104
// the version param in the URL is important - we use it for optimistic locking
105
105
expect ( fetch ) . to . have . been . calledWith (
106
- buildUrl ( "/schema/1/staged/status " , {
106
+ buildUrl ( "/schema/1/files " , {
107
107
version : "194838274939473" ,
108
- color : "ansi " ,
108
+ staged : "false " ,
109
109
} ) ,
110
110
commonFetchParams ,
111
111
) ;
112
112
expect ( fetch ) . to . have . been . calledWith (
113
- buildUrl ( "/schema/1/files/main.fsl" ) ,
113
+ buildUrl ( "/schema/1/files/main.fsl" , {
114
+ version : "194838274939473" ,
115
+ staged : "false" ,
116
+ } ) ,
114
117
commonFetchParams ,
115
118
) ;
116
119
expect ( fetch ) . to . have . been . calledWith (
117
- buildUrl ( "/schema/1/files/second.fsl" ) ,
120
+ buildUrl ( "/schema/1/files/second.fsl" , {
121
+ version : "194838274939473" ,
122
+ staged : "false" ,
123
+ } ) ,
118
124
commonFetchParams ,
119
125
) ;
120
126
expect ( fetch ) . to . have . been . calledWith (
121
- buildUrl ( "/schema/1/files/third.fsl" ) ,
127
+ buildUrl ( "/schema/1/files/third.fsl" , {
128
+ version : "194838274939473" ,
129
+ staged : "false" ,
130
+ } ) ,
122
131
commonFetchParams ,
123
132
) ;
124
133
125
134
expect ( logger . stdout ) . to . have . been . calledWith ( "overwrite: main.fsl" ) ;
126
135
expect ( logger . stdout ) . to . have . been . calledWith ( "add: second.fsl" ) ;
127
136
expect ( logger . stdout ) . to . have . been . calledWith ( "add: third.fsl" ) ;
128
137
expect ( logger . stdout ) . to . have . been . calledWith (
129
- "Pull will make the following changes:" ,
138
+ "Pulling active schema will make the following changes:" ,
130
139
) ;
131
140
132
141
expect ( fs . mkdirSync ) . to . have . been . calledWith ( "." , { recursive : true } ) ;
@@ -168,17 +177,18 @@ describe("schema pull", function () {
168
177
fetch . onCall ( 0 ) . resolves (
169
178
f ( {
170
179
version : "194838274939473" ,
171
- files : [
172
- { filename : "main.fsl" } ,
173
- { filename : "second.fsl" } ,
174
- { filename : "third.fsl" } ,
175
- ] ,
180
+ // assume no staged schema for this test
181
+ status : "none" ,
176
182
} ) ,
177
183
) ;
178
184
fetch . onCall ( 1 ) . resolves (
179
185
f ( {
180
186
version : "194838274939473" ,
181
- status : "ready" ,
187
+ files : [
188
+ { filename : "main.fsl" } ,
189
+ { filename : "second.fsl" } ,
190
+ { filename : "third.fsl" } ,
191
+ ] ,
182
192
} ) ,
183
193
) ;
184
194
@@ -189,7 +199,7 @@ describe("schema pull", function () {
189
199
expect ( logger . stdout ) . to . have . been . calledWith ( "add: third.fsl" ) ;
190
200
expect ( logger . stdout ) . to . have . been . calledWith ( "delete: coll.fsl" ) ;
191
201
expect ( logger . stdout ) . to . have . been . calledWith (
192
- "Pull will make the following changes:" ,
202
+ "Pulling active schema will make the following changes:" ,
193
203
) ;
194
204
expect ( logger . stdout ) . to . have . been . calledWith ( "Change cancelled." ) ;
195
205
expect ( fs . writeFile ) . to . have . not . been . called ;
@@ -215,13 +225,14 @@ describe("schema pull", function () {
215
225
fetch . onCall ( 0 ) . resolves (
216
226
f ( {
217
227
version : "194838274939473" ,
218
- files : [ { filename : "main.fsl" } ] ,
228
+ // assume no staged schema for this test
229
+ status : "none" ,
219
230
} ) ,
220
231
) ;
221
232
fetch . onCall ( 1 ) . resolves (
222
233
f ( {
223
234
version : "194838274939473" ,
224
- status : "ready" ,
235
+ files : [ { filename : "main.fsl" } ] ,
225
236
} ) ,
226
237
) ;
227
238
fetch . onCall ( 2 ) . resolves (
@@ -241,7 +252,7 @@ describe("schema pull", function () {
241
252
expect ( logger . stdout ) . to . have . been . calledWith ( "overwrite: main.fsl" ) ;
242
253
expect ( logger . stdout ) . to . have . been . calledWith ( "delete: coll.fsl" ) ;
243
254
expect ( logger . stdout ) . to . have . been . calledWith (
244
- "Pull will make the following changes:" ,
255
+ "Pulling active schema will make the following changes:" ,
245
256
) ;
246
257
247
258
expect ( fs . mkdirSync ) . to . have . been . calledWith ( "." , { recursive : true } ) ;
@@ -255,30 +266,60 @@ describe("schema pull", function () {
255
266
256
267
it . skip ( "does not modify the filesystem if it fails to read file contents" , async function ( ) { } ) ;
257
268
258
- it ( "errors if called with the --active flag while a schema change is staged" , async function ( ) {
259
- fetch . onCall ( 0 ) . resolves (
260
- f ( {
261
- version : "194838274939473" ,
262
- files : [
263
- { filename : "main.fsl" } ,
264
- { filename : "second.fsl" } ,
265
- { filename : "third.fsl" } ,
266
- ] ,
267
- } ) ,
268
- ) ;
269
- fetch . onCall ( 1 ) . resolves (
270
- f ( {
271
- version : "194838274939473" ,
272
- status : "ready" ,
273
- } ) ,
274
- ) ;
269
+ [
270
+ { argvActive : true , remoteStaged : true , expectedStagedParam : "false" } ,
271
+ { argvActive : false , remoteStaged : false , expectedStagedParam : "false" } ,
272
+ { argvActive : false , remoteStaged : true , expectedStagedParam : "true" } ,
273
+ ] . forEach ( ( { argvActive, remoteStaged, expectedStagedParam } ) => {
274
+ it ( `can pull ${ expectedStagedParam === "true" ? "staged" : "active" } schema while schema is ${ remoteStaged ? "staged" : "not staged" } , and --active is ${ argvActive ? "specified" : "not specified" } ` , async function ( ) {
275
+ fetch . onCall ( 0 ) . resolves (
276
+ f ( {
277
+ version : "194838274939473" ,
278
+ status : remoteStaged ? "ready" : "none" ,
279
+ } ) ,
280
+ ) ;
281
+ fetch . onCall ( 1 ) . resolves (
282
+ f ( {
283
+ version : "194838274939473" ,
284
+ files : [ { filename : "main.fsl" } ] ,
285
+ } ) ,
286
+ ) ;
287
+ fetch . onCall ( 2 ) . resolves (
288
+ f ( {
289
+ content :
290
+ "collection Main {\\n name: String\\n index byName {\\n terms [.name]\\n }\\n}\\n" ,
291
+ } ) ,
292
+ ) ;
275
293
276
- const [ error ] = await tryToCatch ( ( ) =>
277
- run ( `schema pull --secret "secret" --active` , container ) ,
278
- ) ;
279
- expect ( error ) . to . have . property ( "code" , 1 ) ;
280
- expect ( container . resolve ( "gatherFSL" ) ) . to . not . have . been . called ;
281
- } ) ;
294
+ // user accepts the changes in the interactive prompt
295
+ confirm . resolves ( true ) ;
296
+
297
+ await run (
298
+ `schema pull --secret "secret" ${ argvActive ? "--active" : "" } ` ,
299
+ container ,
300
+ ) ;
282
301
283
- it . skip ( "errors if there are no staged schema changes to pull" ) ;
302
+ expect ( fetch ) . to . have . been . calledWith (
303
+ buildUrl ( "/schema/1/staged/status" ) ,
304
+ commonFetchParams ,
305
+ ) ;
306
+ // the version param in the URL is important - we use it for optimistic locking
307
+ expect ( fetch ) . to . have . been . calledWith (
308
+ buildUrl ( "/schema/1/files" , {
309
+ version : "194838274939473" ,
310
+ staged : expectedStagedParam ,
311
+ } ) ,
312
+ commonFetchParams ,
313
+ ) ;
314
+ expect ( fetch ) . to . have . been . calledWith (
315
+ buildUrl ( "/schema/1/files/main.fsl" , {
316
+ version : "194838274939473" ,
317
+ staged : expectedStagedParam ,
318
+ } ) ,
319
+ commonFetchParams ,
320
+ ) ;
321
+
322
+ expect ( logger . stderr ) . to . not . have . been . called ;
323
+ } ) ;
324
+ } ) ;
284
325
} ) ;
0 commit comments