Debian Codegraph @

cmd/shfmt/testdata/script/walk.txtar

raw
1 mkdir symlink/subdir
2 # Remember that the symlink target is relative to the symlink directory.
3 [symlink] symlink symlink/subdir/symlink-file -> ../../modify/ext-shebang.sh
4 [symlink] symlink symlink/symlink-shebang.sh -> ../modify/ext-shebang.sh
5 [symlink] cp modify/ext.bash symlink/target-ext-bash # a copy that won't be formatted on its own
6 [symlink] symlink symlink/symlink-ext.bash -> target-ext-bash
7 [symlink] symlink symlink/symlink-dir -> subdir
8 [symlink] symlink symlink/symlink-none -> nonexistent
9
10 # Other non-regular files like FIFOs could cause issues like blocking reads
11 # when we look for shebangs. We use the "mkfifo" tool here, available on Linux.
12 [exec:mkfifo] exec mkfifo named-pipe
13
14 exec shfmt -f .
15 ! stderr .
16 cmpenv stdout find.golden
17
18 exec shfmt --find .
19 ! stderr .
20 cmpenv stdout find.golden
21
22 # try to format missing paths
23 ! exec shfmt nonexistent
24 stderr -count=1 nonexistent
25
26 ! exec shfmt nonexistent-1 nonexistent-2 nonexistent-3
27 stderr -count=1 nonexistent-1
28 stderr -count=1 nonexistent-2
29 stderr -count=1 nonexistent-3
30
31 # format an entire directory without -l or -w
32 ! exec shfmt .
33 stdout 'foo'
34 stdout 'bin/env'
35 stderr -count=1 'parse-error\.sh'
36
37 # just -l, as a dry run
38 ! exec shfmt --list .
39 stderr -count=1 'parse-error\.sh'
40 ! stderr '^modify'
41 cmpenv stdout modify.golden
42
43 # format an entire directory with -l and -w
44 ! exec shfmt -l -w .
45 stderr -count=1 'parse-error\.sh'
46 ! stderr '^modify'
47 cmpenv stdout modify.golden
48
49 # parse-error again, but now as a lone file
50 ! exec shfmt error/parse-error.sh
51 stderr -count=1 'parse-error\.sh'
52
53 # format files directly which we would ignore when walking directories
54 exec shfmt none/ext-shebang.other
55 stdout 'foo'
56 exec shfmt none/noext-noshebang
57 stdout 'foo'
58 [symlink] exec shfmt symlink/symlink-shebang.sh
59 [symlink] stdout 'foo'
60 [symlink] exec shfmt symlink/symlink-dir
61 [symlink] ! stdout . # note that filepath.WalkDir does not follow symlinks
62
63 # writing to non-regular files is forbidden as they'd be replaced by a regular file.
64 [symlink] ! exec shfmt -l symlink/symlink-ext.bash
65 [symlink] stdout 'symlink-ext.bash'
66 [symlink] ! exec shfmt -w symlink/symlink-ext.bash
67 [symlink] stderr 'refusing to atomically replace'
68 [symlink] ! exec shfmt -l symlink/symlink-ext.bash
69 [symlink] stdout 'symlink-ext.bash'
70
71 # -f on files should still check extension and shebang
72 exec shfmt -f modify/ext.sh modify/shebang-sh none/ext-shebang.other none/noext-noshebang
73 stdout -count=2 '^modify'
74 ! stdout '^none'
75
76 # -f=0 should apply the same checks
77 exec shfmt -f=0 modify/ext.sh modify/shebang-sh none/ext-shebang.other none/noext-noshebang
78 stdout -count=2 'modify'
79 ! stdout 'none'
80
81 # A file holding just a shebang with no final newline is nine bytes long,
82 # the shortest possible file which is a shell script per its shebang.
83 # The source file is hidden so that the walks above don't pick it up,
84 # and copied and trimmed here as txtar files always end with a newline.
85 cp nonl/.shebang-src nonl/shebang
86 trim-newline nonl/shebang
87 exec shfmt -f nonl
88 stdout 'shebang'
89 exec shfmt nonl/shebang
90 stdout '#!/bin/sh'
91
92 # -ln shouldn't be overwritten by a filename
93 mkdir modify-ln
94 cp modify/ext.mksh modify-ln
95 ! exec shfmt -ln=bash modify-ln
96 stderr '|& must be followed by a statement'
97 rm modify-ln
98
99 # -ln shouldn't be overwritten by a shebang
100 mkdir modify-ln
101 cp modify/shebang-mksh modify-ln
102 ! exec shfmt -ln=bash modify-ln
103 stderr '|& must be followed by a statement'
104 rm modify-ln
105
106 -- find.golden --
107 error${/}parse-error.sh
108 modify${/}dir${/}ext.sh
109 modify${/}ext-shebang.sh
110 modify${/}ext.bash
111 modify${/}ext.bats
112 modify${/}ext.mksh
113 modify${/}ext.sh
114 modify${/}ext.zsh
115 modify${/}shebang-args
116 modify${/}shebang-bash
117 modify${/}shebang-bash.sh
118 modify${/}shebang-dash
119 modify${/}shebang-env-bash
120 modify${/}shebang-env-bats
121 modify${/}shebang-env-sh
122 modify${/}shebang-mksh
123 modify${/}shebang-sh
124 modify${/}shebang-space
125 modify${/}shebang-tabs
126 modify${/}shebang-usr-sh
127 modify${/}shebang-zsh
128 -- modify.golden --
129 modify${/}dir${/}ext.sh
130 modify${/}ext-shebang.sh
131 modify${/}ext.bash
132 modify${/}ext.bats
133 modify${/}ext.mksh
134 modify${/}ext.sh
135 modify${/}ext.zsh
136 modify${/}shebang-args
137 modify${/}shebang-bash
138 modify${/}shebang-bash.sh
139 modify${/}shebang-dash
140 modify${/}shebang-env-bash
141 modify${/}shebang-env-bats
142 modify${/}shebang-env-sh
143 modify${/}shebang-mksh
144 modify${/}shebang-sh
145 modify${/}shebang-space
146 modify${/}shebang-tabs
147 modify${/}shebang-usr-sh
148 modify${/}shebang-zsh
149 -- modify/shebang-sh --
150 #!/bin/sh
151 foo
152 -- modify/shebang-bash --
153 #!/bin/bash
154 foo=(bar)
155 -- modify/shebang-bash.sh --
156 #!/bin/bash
157 foo=(bar)
158 -- modify/shebang-dash --
159 #!/bin/dash
160 foo
161 -- modify/shebang-usr-sh --
162 #!/usr/bin/sh
163 foo
164 -- modify/shebang-env-bash --
165 #!/usr/bin/env bash
166 foo=(bar)
167 -- modify/shebang-env-sh --
168 #!/bin/env sh
169 foo
170 -- modify/shebang-mksh --
171 #!/bin/mksh
172 foo |&
173 -- modify/shebang-zsh --
174 #!/bin/zsh
175 ${+foo}
176 -- modify/shebang-env-bats --
177 #!/usr/bin/env bats
178 @test "foo" { bar; }
179 -- modify/shebang-space --
180 #! /bin/sh
181 foo
182 -- modify/shebang-tabs --
183 #! /bin/env sh
184 foo
185 -- modify/shebang-args --
186 #!/bin/bash -e -x
187 foo
188 -- modify/ext.sh --
189 foo
190 -- modify/ext.bash --
191 foo=(bar)
192 -- modify/ext.mksh --
193 foo |&
194 -- modify/ext.zsh --
195 ${+foo}
196 -- modify/ext.bats --
197 @test "foo" { bar; }
198 -- modify/ext-shebang.sh --
199 #!/bin/sh
200 foo
201 -- modify/dir/ext.sh --
202 foo
203
204 -- none/.hidden --
205 foo long enough
206 -- none/.hidden-shebang --
207 #!/bin/sh
208 foo
209 -- none/..hidden-shebang --
210 #!/bin/sh
211 foo
212 -- none/noext-empty --
213 foo
214 -- none/noext-noshebang --
215 foo long enough
216 -- none/shebang-nonewline --
217 #!/bin/shfoo
218 -- none/ext.other --
219 foo
220 -- none/empty --
221 -- none/ext-shebang.other --
222 #!/bin/sh
223 foo
224 -- none/shebang-nospace --
225 #!/bin/envsh
226 foo
227
228 -- nonl/.shebang-src --
229 #!/bin/sh
230 -- skip/.git/ext.sh --
231 foo
232 -- skip/.svn/ext.sh --
233 foo
234 -- skip/.hg/ext.sh --
235 foo
236
237 -- error/parse-error.sh --
238 foo(