@@ -2,7 +2,11 @@ import {
22 ChangedFilesMatchConfig ,
33 checkAllChangedFiles ,
44 checkAnyChangedFiles ,
5- toChangedFilesMatchConfig
5+ toChangedFilesMatchConfig ,
6+ checkIfAnyGlobMatchesAnyFile ,
7+ checkIfAllGlobsMatchAnyFile ,
8+ checkIfAnyGlobMatchesAllFiles ,
9+ checkIfAllGlobsMatchAllFiles
610} from '../src/changedFiles' ;
711
812jest . mock ( '@actions/core' ) ;
@@ -11,20 +15,28 @@ jest.mock('@actions/github');
1115describe ( 'checkAllChangedFiles' , ( ) => {
1216 const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
1317
14- describe ( 'when the globs match every file that has been changed' , ( ) => {
15- const globs = [ '*.txt' ] ;
18+ describe ( 'when all given glob pattern configs matched' , ( ) => {
19+ const globPatternsConfigs = [
20+ { AnyGlobToAnyFile : [ 'foo.txt' ] } ,
21+ { AnyGlobToAllFiles : [ '*.txt' ] } ,
22+ { AllGlobsToAllFiles : [ '**' ] }
23+ ] ;
1624
1725 it ( 'returns true' , ( ) => {
18- const result = checkAllChangedFiles ( changedFiles , globs ) ;
26+ const result = checkAllChangedFiles ( changedFiles , globPatternsConfigs ) ;
1927 expect ( result ) . toBe ( true ) ;
2028 } ) ;
2129 } ) ;
2230
23- describe ( `when the globs don't match every file that has changed` , ( ) => {
24- const globs = [ 'foo.txt' ] ;
31+ describe ( `when some given glob pattern config did not match` , ( ) => {
32+ const globPatternsConfigs = [
33+ { AnyGlobToAnyFile : [ '*.md' ] } ,
34+ { AnyGlobToAllFiles : [ '*.txt' ] } ,
35+ { AllGlobsToAllFiles : [ '**' ] }
36+ ] ;
2537
2638 it ( 'returns false' , ( ) => {
27- const result = checkAllChangedFiles ( changedFiles , globs ) ;
39+ const result = checkAllChangedFiles ( changedFiles , globPatternsConfigs ) ;
2840 expect ( result ) . toBe ( false ) ;
2941 } ) ;
3042 } ) ;
@@ -33,20 +45,26 @@ describe('checkAllChangedFiles', () => {
3345describe ( 'checkAnyChangedFiles' , ( ) => {
3446 const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
3547
36- describe ( 'when any glob matches any of the files that have changed' , ( ) => {
37- const globs = [ '*.txt' , '*.md' ] ;
48+ describe ( 'when any given glob pattern config matched' , ( ) => {
49+ const globPatternsConfigs = [
50+ { AnyGlobToAnyFile : [ '*.md' ] } ,
51+ { AnyGlobToAllFiles : [ '*.txt' ] }
52+ ] ;
3853
3954 it ( 'returns true' , ( ) => {
40- const result = checkAnyChangedFiles ( changedFiles , globs ) ;
55+ const result = checkAnyChangedFiles ( changedFiles , globPatternsConfigs ) ;
4156 expect ( result ) . toBe ( true ) ;
4257 } ) ;
4358 } ) ;
4459
45- describe ( 'when none of the globs match any files that have changed' , ( ) => {
46- const globs = [ '*.md' ] ;
60+ describe ( 'when none of the given glob pattern configs matched' , ( ) => {
61+ const globPatternsConfigs = [
62+ { AnyGlobToAnyFile : [ '*.md' ] } ,
63+ { AnyGlobToAllFiles : [ '!*.txt' ] }
64+ ] ;
4765
4866 it ( 'returns false' , ( ) => {
49- const result = checkAnyChangedFiles ( changedFiles , globs ) ;
67+ const result = checkAnyChangedFiles ( changedFiles , globPatternsConfigs ) ;
5068 expect ( result ) . toBe ( false ) ;
5169 } ) ;
5270 } ) ;
@@ -63,44 +81,140 @@ describe('toChangedFilesMatchConfig', () => {
6381 } ) ;
6482
6583 describe ( `when there is a 'changed-files' key in the config` , ( ) => {
66- describe ( 'and the value is an array of strings' , ( ) => {
67- const config = { 'changed-files' : [ 'testing' ] } ;
84+ describe ( 'but the glob pattern config key is not provided' , ( ) => {
85+ const config = { 'changed-files' : [ 'bar' ] } ;
86+
87+ it ( 'throws the error' , ( ) => {
88+ expect ( ( ) => {
89+ toChangedFilesMatchConfig ( config ) ;
90+ } ) . toThrow (
91+ `The "changed-files" section must have a valid config structure. Please read the action documentation for more information`
92+ ) ;
93+ } ) ;
94+ } ) ;
6895
69- it ( 'sets the value in the config object' , ( ) => {
70- const result = toChangedFilesMatchConfig ( config ) ;
71- expect ( result ) . toEqual < ChangedFilesMatchConfig > ( {
72- changedFiles : [ 'testing' ]
73- } ) ;
96+ describe ( 'but the glob pattern config key is not valid' , ( ) => {
97+ const config = { 'changed-files' : [ { NotValidConfigKey : [ 'bar' ] } ] } ;
98+
99+ it ( 'throws the error' , ( ) => {
100+ expect ( ( ) => {
101+ toChangedFilesMatchConfig ( config ) ;
102+ } ) . toThrow (
103+ `Unknown config options were under "changed-files": NotValidConfigKey`
104+ ) ;
74105 } ) ;
75106 } ) ;
76107
77- describe ( 'and the value is a string' , ( ) => {
78- const config = { 'changed-files' : 'testing' } ;
108+ describe ( 'and the glob pattern config key is provided' , ( ) => {
109+ describe ( 'and the value is an array of strings' , ( ) => {
110+ const config = { 'changed-files' : [ { AnyGlobToAnyFile : [ 'testing' ] } ] } ;
79111
80- it ( `sets the string as an array in the config object` , ( ) => {
81- const result = toChangedFilesMatchConfig ( config ) ;
82- expect ( result ) . toEqual < ChangedFilesMatchConfig > ( {
83- changedFiles : [ 'testing' ]
112+ it ( 'sets the value in the config object' , ( ) => {
113+ const result = toChangedFilesMatchConfig ( config ) ;
114+ expect ( result ) . toEqual < ChangedFilesMatchConfig > ( {
115+ changedFiles : [ { AnyGlobToAnyFile : [ 'testing' ] } ]
116+ } ) ;
84117 } ) ;
85118 } ) ;
86- } ) ;
87119
88- describe ( 'but the value is an empty string' , ( ) => {
89- const config = { 'changed-files' : '' } ;
120+ describe ( 'and the value is a string' , ( ) => {
121+ const config = { 'changed-files' : [ { AnyGlobToAnyFile : 'testing' } ] } ;
90122
91- it ( `returns an empty object` , ( ) => {
92- const result = toChangedFilesMatchConfig ( config ) ;
93- expect ( result ) . toEqual < ChangedFilesMatchConfig > ( { } ) ;
123+ it ( `sets the string as an array in the config object` , ( ) => {
124+ const result = toChangedFilesMatchConfig ( config ) ;
125+ expect ( result ) . toEqual < ChangedFilesMatchConfig > ( {
126+ changedFiles : [ { AnyGlobToAnyFile : [ 'testing' ] } ]
127+ } ) ;
128+ } ) ;
94129 } ) ;
95130 } ) ;
131+ } ) ;
132+ } ) ;
96133
97- describe ( 'but the value is an empty array ' , ( ) => {
98- const config = { 'changed-files' : [ ] } ;
134+ describe ( 'checkIfAnyGlobMatchesAnyFile ' , ( ) => {
135+ const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
99136
100- it ( `returns an empty object` , ( ) => {
101- const result = toChangedFilesMatchConfig ( config ) ;
102- expect ( result ) . toEqual < ChangedFilesMatchConfig > ( { } ) ;
103- } ) ;
137+ describe ( 'when any given glob pattern matched any file' , ( ) => {
138+ const globPatterns = [ '*.md' , 'foo.txt' ] ;
139+
140+ it ( 'returns true' , ( ) => {
141+ const result = checkIfAnyGlobMatchesAnyFile ( changedFiles , globPatterns ) ;
142+ expect ( result ) . toBe ( true ) ;
143+ } ) ;
144+ } ) ;
145+
146+ describe ( 'when none of the given glob pattern matched any file' , ( ) => {
147+ const globPatterns = [ '*.md' , '!*.txt' ] ;
148+
149+ it ( 'returns false' , ( ) => {
150+ const result = checkIfAnyGlobMatchesAnyFile ( changedFiles , globPatterns ) ;
151+ expect ( result ) . toBe ( false ) ;
152+ } ) ;
153+ } ) ;
154+ } ) ;
155+
156+ describe ( 'checkIfAllGlobsMatchAnyFile' , ( ) => {
157+ const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
158+
159+ describe ( 'when all given glob patterns matched any file' , ( ) => {
160+ const globPatterns = [ '**/bar.txt' , 'bar.txt' ] ;
161+
162+ it ( 'returns true' , ( ) => {
163+ const result = checkIfAllGlobsMatchAnyFile ( changedFiles , globPatterns ) ;
164+ expect ( result ) . toBe ( true ) ;
165+ } ) ;
166+ } ) ;
167+
168+ describe ( 'when some of the given glob patterns did not match any file' , ( ) => {
169+ const globPatterns = [ '*.txt' , '*.md' ] ;
170+
171+ it ( 'returns false' , ( ) => {
172+ const result = checkIfAllGlobsMatchAnyFile ( changedFiles , globPatterns ) ;
173+ expect ( result ) . toBe ( false ) ;
174+ } ) ;
175+ } ) ;
176+ } ) ;
177+
178+ describe ( 'checkIfAnyGlobMatchesAllFiles' , ( ) => {
179+ const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
180+
181+ describe ( 'when any given glob pattern matched all files' , ( ) => {
182+ const globPatterns = [ '*.md' , '*.txt' ] ;
183+
184+ it ( 'returns true' , ( ) => {
185+ const result = checkIfAnyGlobMatchesAllFiles ( changedFiles , globPatterns ) ;
186+ expect ( result ) . toBe ( true ) ;
187+ } ) ;
188+ } ) ;
189+
190+ describe ( 'when none of the given glob patterns matched all files' , ( ) => {
191+ const globPatterns = [ '*.md' , 'bar.txt' , 'foo.txt' ] ;
192+
193+ it ( 'returns false' , ( ) => {
194+ const result = checkIfAnyGlobMatchesAllFiles ( changedFiles , globPatterns ) ;
195+ expect ( result ) . toBe ( false ) ;
196+ } ) ;
197+ } ) ;
198+ } ) ;
199+
200+ describe ( 'checkIfAllGlobsMatchAllFiles' , ( ) => {
201+ const changedFiles = [ 'foo.txt' , 'bar.txt' ] ;
202+
203+ describe ( 'when all given glob patterns matched all files' , ( ) => {
204+ const globPatterns = [ '*.txt' , '**' ] ;
205+
206+ it ( 'returns true' , ( ) => {
207+ const result = checkIfAllGlobsMatchAllFiles ( changedFiles , globPatterns ) ;
208+ expect ( result ) . toBe ( true ) ;
209+ } ) ;
210+ } ) ;
211+
212+ describe ( 'when some of the given glob patterns did not match all files' , ( ) => {
213+ const globPatterns = [ '**' , 'foo.txt' ] ;
214+
215+ it ( 'returns false' , ( ) => {
216+ const result = checkIfAllGlobsMatchAllFiles ( changedFiles , globPatterns ) ;
217+ expect ( result ) . toBe ( false ) ;
104218 } ) ;
105219 } ) ;
106220} ) ;
0 commit comments