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

173 lines, 102 significant
1## oils_failures_allowed: 1
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
166# even if they don't match the patterns
167shopt -u globskipdots
168touch .hidden
169GLOBIGNORE=*.txt
170echo .*
171## STDOUT:
172.hidden
173## END