@@ -12,11 +12,46 @@ import {
1212 getPathValues ,
1313} from './utilities.js' ;
1414
15+ const runIsIgnoredByIgnoreFiles = async ( t , patterns , options , fn ) => {
16+ const promisePredicate = await isIgnoredByIgnoreFiles ( patterns , options ) ;
17+ const syncPredicate = isIgnoredByIgnoreFilesSync ( patterns , options ) ;
18+
19+ const promiseResult = fn ( promisePredicate ) ;
20+ const syncResult = fn ( syncPredicate ) ;
21+
22+ t [ Array . isArray ( promiseResult ) ? 'deepEqual' : 'is' ] (
23+ promiseResult ,
24+ syncResult ,
25+ 'isIgnoredByIgnoreFilesSync() result is different than isIgnoredByIgnoreFiles()' ,
26+ ) ;
27+
28+ return promiseResult ;
29+ } ;
30+
31+ const runIsGitIgnored = async ( t , options , fn ) => {
32+ const promisePredicate = await isGitIgnored ( options ) ;
33+ const syncPredicate = isGitIgnoredSync ( options ) ;
34+
35+ const promiseResult = fn ( promisePredicate ) ;
36+ const syncResult = fn ( syncPredicate ) ;
37+
38+ t [ Array . isArray ( promiseResult ) ? 'deepEqual' : 'is' ] (
39+ promiseResult ,
40+ syncResult ,
41+ 'isGitIgnoredSync() result is different than isGitIgnored()' ,
42+ ) ;
43+
44+ return promiseResult ;
45+ } ;
46+
1547test ( 'ignore' , async t => {
1648 for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/gitignore' ) ) ) {
1749 // eslint-disable-next-line no-await-in-loop
18- const isIgnored = await isGitIgnored ( { cwd} ) ;
19- const actual = [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ;
50+ const actual = await runIsGitIgnored (
51+ t ,
52+ { cwd} ,
53+ isIgnored => [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ,
54+ ) ;
2055 const expected = [ 'bar.js' ] ;
2156 t . deepEqual ( actual , expected ) ;
2257 }
@@ -25,44 +60,39 @@ test('ignore', async t => {
2560test ( 'ignore - mixed path styles' , async t => {
2661 const directory = path . join ( PROJECT_ROOT , 'fixtures/gitignore' ) ;
2762 for ( const cwd of getPathValues ( directory ) ) {
28- // eslint-disable-next-line no-await-in-loop
29- const isIgnored = await isGitIgnored ( { cwd} ) ;
30- t . true ( isIgnored ( slash ( path . resolve ( directory , 'foo.js' ) ) ) ) ;
63+ t . true (
64+ // eslint-disable-next-line no-await-in-loop
65+ await runIsGitIgnored (
66+ t ,
67+ { cwd} ,
68+ isIgnored => isIgnored ( slash ( path . resolve ( directory , 'foo.js' ) ) ) ,
69+ ) ,
70+ ) ;
3171 }
3272} ) ;
3373
3474test ( 'ignore - os paths' , async t => {
3575 const directory = path . join ( PROJECT_ROOT , 'fixtures/gitignore' ) ;
3676 for ( const cwd of getPathValues ( directory ) ) {
37- // eslint-disable-next-line no-await-in-loop
38- const isIgnored = await isGitIgnored ( { cwd} ) ;
39- t . true ( isIgnored ( path . resolve ( directory , 'foo.js' ) ) ) ;
40- }
41- } ) ;
42-
43- test ( 'ignore - sync' , t => {
44- for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/gitignore' ) ) ) {
45- const isIgnored = isGitIgnoredSync ( { cwd} ) ;
46- const actual = [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ;
47- const expected = [ 'bar.js' ] ;
48- t . deepEqual ( actual , expected ) ;
77+ t . true (
78+ // eslint-disable-next-line no-await-in-loop
79+ await runIsGitIgnored (
80+ t ,
81+ { cwd} ,
82+ isIgnored => isIgnored ( path . resolve ( directory , 'foo.js' ) ) ,
83+ ) ,
84+ ) ;
4985 }
5086} ) ;
5187
5288test ( 'negative ignore' , async t => {
5389 for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/negative' ) ) ) {
5490 // eslint-disable-next-line no-await-in-loop
55- const isIgnored = await isGitIgnored ( { cwd} ) ;
56- const actual = [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ;
57- const expected = [ 'foo.js' ] ;
58- t . deepEqual ( actual , expected ) ;
59- }
60- } ) ;
61-
62- test ( 'negative ignore - sync' , t => {
63- for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/negative' ) ) ) {
64- const isIgnored = isGitIgnoredSync ( { cwd} ) ;
65- const actual = [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ;
91+ const actual = await runIsGitIgnored (
92+ t ,
93+ { cwd} ,
94+ isIgnored => [ 'foo.js' , 'bar.js' ] . filter ( file => ! isIgnored ( file ) ) ,
95+ ) ;
6696 const expected = [ 'foo.js' ] ;
6797 t . deepEqual ( actual , expected ) ;
6898 }
@@ -71,30 +101,16 @@ test('negative ignore - sync', t => {
71101test ( 'multiple negation' , async t => {
72102 for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/multiple-negation' ) ) ) {
73103 // eslint-disable-next-line no-await-in-loop
74- const isIgnored = await isGitIgnored ( { cwd} ) ;
75-
76- const actual = [
77- '!!!unicorn.js' ,
78- '!!unicorn.js' ,
79- '!unicorn.js' ,
80- 'unicorn.js' ,
81- ] . filter ( file => ! isIgnored ( file ) ) ;
82-
83- const expected = [ '!!unicorn.js' , '!unicorn.js' ] ;
84- t . deepEqual ( actual , expected ) ;
85- }
86- } ) ;
87-
88- test ( 'multiple negation - sync' , t => {
89- for ( const cwd of getPathValues ( path . join ( PROJECT_ROOT , 'fixtures/multiple-negation' ) ) ) {
90- const isIgnored = isGitIgnoredSync ( { cwd} ) ;
91-
92- const actual = [
93- '!!!unicorn.js' ,
94- '!!unicorn.js' ,
95- '!unicorn.js' ,
96- 'unicorn.js' ,
97- ] . filter ( file => ! isIgnored ( file ) ) ;
104+ const actual = await runIsGitIgnored (
105+ t ,
106+ { cwd} ,
107+ isIgnored => [
108+ '!!!unicorn.js' ,
109+ '!!unicorn.js' ,
110+ '!unicorn.js' ,
111+ 'unicorn.js' ,
112+ ] . filter ( file => ! isIgnored ( file ) ) ,
113+ ) ;
98114
99115 const expected = [ '!!unicorn.js' , '!unicorn.js' ] ;
100116 t . deepEqual ( actual , expected ) ;
@@ -103,77 +119,67 @@ test('multiple negation - sync', t => {
103119
104120test ( 'check file' , async t => {
105121 const directory = path . join ( PROJECT_ROOT , 'fixtures/gitignore' ) ;
106- const ignoredFile = path . join ( directory , 'foo.js' ) ;
107- const isIgnored = await isGitIgnored ( { cwd : directory } ) ;
108- const isIgnoredSync = isGitIgnoredSync ( { cwd : directory } ) ;
109122
110- for ( const file of getPathValues ( ignoredFile ) ) {
111- t . true ( isIgnored ( file ) ) ;
112- t . true ( isIgnoredSync ( file ) ) ;
123+ for ( const ignoredFile of getPathValues ( path . join ( directory , 'foo.js' ) ) ) {
124+ t . true (
125+ // eslint-disable-next-line no-await-in-loop
126+ await runIsGitIgnored (
127+ t ,
128+ { cwd : directory } ,
129+ isIgnored => isIgnored ( ignoredFile ) ,
130+ ) ,
131+ ) ;
113132 }
114133
115- for ( const file of getPathValues ( path . join ( directory , 'bar.js' ) ) ) {
116- t . false ( isIgnored ( file ) ) ;
134+ for ( const notIgnoredFile of getPathValues ( path . join ( directory , 'bar.js' ) ) ) {
135+ t . false (
136+ // eslint-disable-next-line no-await-in-loop
137+ await runIsGitIgnored (
138+ t ,
139+ { cwd : directory } ,
140+ isIgnored => isIgnored ( notIgnoredFile ) ,
141+ ) ,
142+ ) ;
117143 }
118144} ) ;
119145
120- test ( 'custom ignore files - sync' , t => {
121- const cwd = path . join ( PROJECT_ROOT , 'fixtures/ignore-files' ) ;
122- const files = [
123- 'ignored-by-eslint.js' ,
124- 'ignored-by-prettier.js' ,
125- 'not-ignored.js' ,
126- ] ;
127-
128- const isEslintIgnored = isIgnoredByIgnoreFilesSync ( '.eslintignore' , { cwd} ) ;
129- const isPrettierIgnored = isIgnoredByIgnoreFilesSync ( '.prettierignore' , { cwd} ) ;
130- const isEslintOrPrettierIgnored = isIgnoredByIgnoreFilesSync ( '.{prettier,eslint}ignore' , { cwd} ) ;
131- t . deepEqual (
132- files . filter ( file => isEslintIgnored ( file ) ) ,
133- [
134- 'ignored-by-eslint.js' ,
135- ] ,
136- ) ;
137- t . deepEqual (
138- files . filter ( file => isPrettierIgnored ( file ) ) ,
139- [
140- 'ignored-by-prettier.js' ,
141- ] ,
142- ) ;
143- t . deepEqual (
144- files . filter ( file => isEslintOrPrettierIgnored ( file ) ) ,
145- [
146- 'ignored-by-eslint.js' ,
147- 'ignored-by-prettier.js' ,
148- ] ,
149- ) ;
150- } ) ;
151-
152- test ( 'custom ignore files - async' , async t => {
146+ test ( 'custom ignore files' , async t => {
153147 const cwd = path . join ( PROJECT_ROOT , 'fixtures/ignore-files' ) ;
154148 const files = [
155149 'ignored-by-eslint.js' ,
156150 'ignored-by-prettier.js' ,
157151 'not-ignored.js' ,
158152 ] ;
159153
160- const isEslintIgnored = await isIgnoredByIgnoreFiles ( '.eslintignore' , { cwd} ) ;
161- const isPrettierIgnored = await isIgnoredByIgnoreFiles ( '.prettierignore' , { cwd} ) ;
162- const isEslintOrPrettierIgnored = await isIgnoredByIgnoreFiles ( '.{prettier,eslint}ignore' , { cwd} ) ;
163154 t . deepEqual (
164- files . filter ( file => isEslintIgnored ( file ) ) ,
155+ await runIsIgnoredByIgnoreFiles (
156+ t ,
157+ '.eslintignore' ,
158+ { cwd} ,
159+ isEslintIgnored => files . filter ( file => isEslintIgnored ( file ) ) ,
160+ ) ,
165161 [
166162 'ignored-by-eslint.js' ,
167163 ] ,
168164 ) ;
169165 t . deepEqual (
170- files . filter ( file => isPrettierIgnored ( file ) ) ,
166+ await runIsIgnoredByIgnoreFiles (
167+ t ,
168+ '.prettierignore' ,
169+ { cwd} ,
170+ isPrettierIgnored => files . filter ( file => isPrettierIgnored ( file ) ) ,
171+ ) ,
171172 [
172173 'ignored-by-prettier.js' ,
173174 ] ,
174175 ) ;
175176 t . deepEqual (
176- files . filter ( file => isEslintOrPrettierIgnored ( file ) ) ,
177+ await runIsIgnoredByIgnoreFiles (
178+ t ,
179+ '.{prettier,eslint}ignore' ,
180+ { cwd} ,
181+ isEslintOrPrettierIgnored => files . filter ( file => isEslintOrPrettierIgnored ( file ) ) ,
182+ ) ,
177183 [
178184 'ignored-by-eslint.js' ,
179185 'ignored-by-prettier.js' ,
0 commit comments