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

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