@@ -176,3 +176,155 @@ describe('getEnvironmentNames', () => {
176176 } )
177177 } )
178178} )
179+
180+ describe ( 'expandEnvironmentPatterns' , ( ) => {
181+ test ( 'returns dedeuplicated environment names' , async ( ) => {
182+ await inTemporaryDirectory ( async ( tmpDir ) => {
183+ // Given
184+ const filePath = joinPath ( tmpDir , fileName )
185+ await writeFile (
186+ filePath ,
187+ tomlEncode ( {
188+ environments : {
189+ 'us-production' : { store : 'us-store' } ,
190+ 'ca-production' : { store : 'ca-store' } ,
191+ } ,
192+ } ) ,
193+ )
194+
195+ // When
196+ const expanded = await environments . expandEnvironmentPatterns ( [ 'us-production' , 'us-production' ] , fileName , {
197+ from : tmpDir ,
198+ } )
199+
200+ // Then
201+ expect ( expanded ) . toEqual ( [ 'us-production' ] )
202+ } )
203+ } )
204+
205+ describe ( 'when the pattern contains glob characters' , ( ) => {
206+ test ( 'expands glob pattern to return multiple environments' , async ( ) => {
207+ await inTemporaryDirectory ( async ( tmpDir ) => {
208+ // Given
209+ const filePath = joinPath ( tmpDir , fileName )
210+ await writeFile (
211+ filePath ,
212+ tomlEncode ( {
213+ environments : {
214+ 'us-production' : { store : 'us-store' } ,
215+ 'ca-production' : { store : 'ca-store' } ,
216+ 'de-production' : { store : 'de-store' } ,
217+ 'us-staging' : { store : 'us-staging-store' } ,
218+ } ,
219+ } ) ,
220+ )
221+
222+ // When
223+ const expanded = await environments . expandEnvironmentPatterns ( [ '*-production' ] , fileName , { from : tmpDir } )
224+
225+ // Then
226+ expect ( expanded . sort ( ) ) . toEqual ( [ 'ca-production' , 'de-production' , 'us-production' ] )
227+ } )
228+ } )
229+
230+ test ( 'supports question mark glob patterns' , async ( ) => {
231+ await inTemporaryDirectory ( async ( tmpDir ) => {
232+ // Given
233+ const filePath = joinPath ( tmpDir , fileName )
234+ await writeFile (
235+ filePath ,
236+ tomlEncode ( {
237+ environments : {
238+ 'us-production' : { store : 'us-store' } ,
239+ 'uk-production' : { store : 'uk-store' } ,
240+ 'usa-production' : { store : 'usa-store' } ,
241+ } ,
242+ } ) ,
243+ )
244+
245+ // When
246+ const expanded = await environments . expandEnvironmentPatterns ( [ 'u?-production' ] , fileName , { from : tmpDir } )
247+
248+ // Then
249+ expect ( expanded . sort ( ) ) . toEqual ( [ 'uk-production' , 'us-production' ] )
250+ } )
251+ } )
252+
253+ test ( 'supports square bracket glob patterns' , async ( ) => {
254+ await inTemporaryDirectory ( async ( tmpDir ) => {
255+ // Given
256+ const filePath = joinPath ( tmpDir , fileName )
257+ await writeFile (
258+ filePath ,
259+ tomlEncode ( {
260+ environments : {
261+ 'us-production' : { store : 'us-store' } ,
262+ 'uk-production' : { store : 'uk-store' } ,
263+ 'usa-production' : { store : 'usa-store' } ,
264+ } ,
265+ } ) ,
266+ )
267+
268+ // When
269+ const expanded = await environments . expandEnvironmentPatterns ( [ 'u[a-z]-production' ] , fileName , { from : tmpDir } )
270+
271+ // Then
272+ expect ( expanded . sort ( ) ) . toEqual ( [ 'uk-production' , 'us-production' ] )
273+ } )
274+ } )
275+
276+ test ( 'supports curly bracket glob patterns' , async ( ) => {
277+ await inTemporaryDirectory ( async ( tmpDir ) => {
278+ // Given
279+ const filePath = joinPath ( tmpDir , fileName )
280+ await writeFile (
281+ filePath ,
282+ tomlEncode ( {
283+ environments : {
284+ 'us-production' : { store : 'us-store' } ,
285+ 'uk-production' : { store : 'uk-store' } ,
286+ 'usa-production' : { store : 'usa-store' } ,
287+ } ,
288+ } ) ,
289+ )
290+
291+ // When
292+ const expanded = await environments . expandEnvironmentPatterns ( [ '{uk,us}-production' ] , fileName , { from : tmpDir } )
293+
294+ // Then
295+ expect ( expanded . sort ( ) ) . toEqual ( [ 'uk-production' , 'us-production' ] )
296+ } )
297+ } )
298+ } )
299+
300+ test ( 'renders a warning if no match is found' , async ( ) => {
301+ await inTemporaryDirectory ( async ( tmpDir ) => {
302+ // Given
303+ const outputMock = mockAndCaptureOutput ( )
304+ outputMock . clear ( )
305+ const filePath = joinPath ( tmpDir , fileName )
306+ await writeFile (
307+ filePath ,
308+ tomlEncode ( {
309+ environments : {
310+ 'us-production' : { store : 'us-store' } ,
311+ } ,
312+ } ) ,
313+ )
314+
315+ // When
316+ const expanded = await environments . expandEnvironmentPatterns ( [ 'nonexistent' ] , fileName , { from : tmpDir } )
317+
318+ // Then
319+ expect ( expanded ) . toEqual ( [ ] )
320+ expect ( outputMock . warn ( ) ) . toMatchInlineSnapshot ( `
321+ "╭─ warning ────────────────────────────────────────────────────────────────────╮
322+ │ │
323+ │ Could not find any environments matching \`nonexistent\` │
324+ │ │
325+ ╰──────────────────────────────────────────────────────────────────────────────╯
326+ "
327+ ` )
328+ } )
329+ } )
330+ } )
0 commit comments