OILS / spec / globignore.test.sh View on Github | oils.pub

229 lines, 132 significant
1## oils_failures_allowed: 0
2## compare_shells: bash
3## legacy_tmp_dir: true
4
5#### Don't glob flags on file system with GLOBIGNORE
6touch _tmp/-n _tmp/zzzzz
7cd _tmp
8GLOBIGNORE=-*:zzzzz # colon-separated pattern list
9echo -* hello zzzz?
10## STDOUT:
11-* hello zzzz?
12## END
13## N-I dash/mksh/ash stdout-json: "hello zzzzz"
14## status: 0
15
16#### Ignore *.txt
17touch one.md one.txt
18mkdir -p foo
19touch foo/{two.md,two.txt}
20GLOBIGNORE=*.txt
21echo *.* foo/*.*
22## STDOUT:
23one.md foo/two.md foo/two.txt
24## END
25
26#### Ignore ?.txt
27touch {1,10}.txt
28mkdir -p foo
29touch foo/{2,20}.txt
30GLOBIGNORE=?.txt
31echo *.* foo/*.*
32## STDOUT:
3310.txt foo/2.txt foo/20.txt
34## END
35
36#### Ignore *.o:*.h
37touch {hello.c,hello.h,hello.o,hello}
38GLOBIGNORE=*.o:*.h
39echo hello*
40## STDOUT:
41hello hello.c
42## END
43
44#### Ignore single file src/__main__.py
45mkdir src
46touch src/{__init__.py,__main__.py}
47GLOBIGNORE='src/__init__.py'
48echo src/*
49## STDOUT:
50src/__main__.py
51## END
52
53#### Ignore dirs dist/*:node_modules/*
54mkdir {src,compose,dist,node_modules}
55touch src/{a.js,b.js}
56touch compose/{base.compose.yaml,dev.compose.yaml}
57touch dist/index.js
58touch node_modules/package.js
59GLOBIGNORE=dist/*:node_modules/*
60echo */*
61## STDOUT:
62compose/base.compose.yaml compose/dev.compose.yaml src/a.js src/b.js
63## END
64
65#### find files in subdirectory but not the ignored pattern
66mkdir {dir1,dir2}
67touch dir1/{a.txt,ignore.txt}
68touch dir2/{a.txt,ignore.txt}
69GLOBIGNORE=*/ignore*
70echo */*
71## STDOUT:
72dir1/a.txt dir2/a.txt
73## END
74
75#### Ignore globs with char patterns like [!ab]
76rm -rf _tmp
77touch {a,b,c,d,A,B,C,D}
78GLOBIGNORE=*[ab]*
79echo *
80GLOBIGNORE=*[ABC]*
81echo *
82GLOBIGNORE=*[!ab]*
83echo *
84## STDOUT:
85A B C D c d
86D a b c d
87a b
88## END
89
90#### Ignore globs with char classes like [[:alnum:]]
91touch {_testing.py,pyproject.toml,20231114.log,.env}
92touch 'has space.docx'
93GLOBIGNORE=[[:alnum:]]*
94echo *.*
95GLOBIGNORE=[![:alnum:]]*
96echo *.*
97GLOBIGNORE=*[[:space:]]*
98echo *.*
99GLOBIGNORE=[[:digit:]_.]*
100echo *.*
101## STDOUT:
102.env _testing.py
10320231114.log has space.docx pyproject.toml
104.env 20231114.log _testing.py pyproject.toml
105has space.docx pyproject.toml
106## END
107
108#### Ignore *
109# This pattern appears in public repositories
110touch {1.txt,2.log,3.md}
111GLOBIGNORE=*
112echo *
113## STDOUT:
114*
115## END
116
117#### treat escaped patterns literally
118touch {escape-10.txt,escape*.txt}
119GLOBIGNORE="escape\*.txt"
120echo *.*
121## STDOUT:
122escape-10.txt
123## END
124
125#### resetting globignore reverts to default behaviour
126touch reset.txt
127GLOBIGNORE=*.txt
128echo *.*
129GLOBIGNORE=
130echo *.*
131## STDOUT:
132*.*
133reset.txt
134## END
135
136#### Ignore .:..
137# globskipdots is enabled by default in bash >=5.2
138# for bash <5.2 this pattern is a common way to match dotfiles but not . or ..
139shopt -u globskipdots
140touch .env
141GLOBIGNORE=.:..
142echo .*
143GLOBIGNORE=
144echo .* | sort
145## STDOUT:
146.env
147. .. .env
148## END
149
150#### Quoting GLOBIGNORE
151# each style of "ignore everything" spotted in a public repo
152touch image.jpeg
153GLOBIGNORE=*
154echo *
155GLOBIGNORE='*'
156echo *
157GLOBIGNORE="*"
158echo *
159GLOBIGNORE=\*
160echo *
161## STDOUT:
162*
163*
164*
165*
166## END
167
168#### . and .. always filtered when GLOBIGNORE is set
169# When GLOBIGNORE is set to any non-null value, . and .. are always filtered
170touch .hidden
171GLOBIGNORE=*.txt
172
173echo .*
174shopt -u globskipdots
175echo .*
176
177## STDOUT:
178.hidden
179.hidden
180## END
181
182#### When GLOBIGNORE is set, glob may become empty (nullglob too)
183touch -- foo.txt -foo.txt
184
185echo *t
186
187GLOBIGNORE=*.txt
188echo *t
189
190shopt -s nullglob
191echo nullglob *t
192
193## STDOUT:
194-foo.txt foo.txt
195*t
196nullglob
197## END
198
199#### When GLOBIGNORE is set, no_dash_glob isn't respected
200case $SH in bash) exit ;; esac
201
202touch -- foo.txt -foo.txt
203
204shopt -s no_dash_glob # YSH option
205
206echo * # expansion does NOT include -foo.txt
207
208GLOBIGNORE=f*.txt
209echo * # expansion includes -foo.txt, because it doesn't match GLOBIGNORE
210
211## STDOUT:
212_tmp foo.txt
213-foo.txt _tmp
214## END
215## N-I bash STDOUT:
216## END
217
218#### Extended glob expansion combined with GLOBIGNORE
219shopt -s extglob
220
221touch foo.cc foo.h bar.cc bar.h
222echo @(*.cc|*.h)
223GLOBIGNORE=foo.*
224echo @(*.cc|*.h)
225
226## STDOUT:
227bar.cc bar.h foo.cc foo.h
228bar.cc bar.h
229## END