forked from vijithassar/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.bats
262 lines (227 loc) · 7.67 KB
/
test.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env bats
setup() {
mkdir test
}
teardown() {
rm -rf test
}
@test "live compilation script matches Markdown source" {
git checkout lit.sh
pre="$(less lit.sh)"
./lit.sh --input "lit.sh.md"
post="$(less lit.sh)"
[ "${pre}" == "${post}" ]
}
@test "compiled example matches Markdown source" {
git checkout hello-world.rb
pre="$(less hello-world.rb)"
./lit.sh --input "hello-world.rb.md"
post="$(less hello-world.rb)"
[ "${pre}" == "${post}" ]
}
@test "skips files that end in a single .md extension" {
touch test/README.md
./lit.sh --input "./test/*"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 1 ]
}
@test "skips files that end in a single file extension" {
touch test/script.py
touch test/script.js
./lit.sh --input "./test/*"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "skips non-Markdown files that end in a double file extension" {
touch test/script.py.js
./lit.sh --input "./test/*"
count="$(find test -type f | wc -l)"
[ "${count}" -eq 1 ]
}
@test "compiles Markdown files that end in a double file extension" {
touch test/script.py.md
./lit.sh --input "./test/*"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "preserves original file extensions in output filenames" {
touch test/script.py.md
./lit.sh --input "./test/*"
new_file="$([ -e test/script.py ])"
[ new_file ]
}
@test "compiles multiple files" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 4 ]
}
@test "compiles files in the current working directory" {
touch test/script.py.md
cd test
../lit.sh
cd ..
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "writes to hidden files" {
touch test/script.py.md
./lit.sh --input "./test" --hidden
count="$(find test/ -type f -name .*.py | wc -l)"
[ "${count}" -eq 1 ]
}
@test "prints processed script names by default" {
touch test/first.js.md
touch test/second.js.md
expected=$'test/output/first.js\ntest/output/second.js'
mkdir test/output
logs=$(./lit.sh --input "./test/*.js.md" --output test/output)
[ "${logs}" == "${expected}" ]
}
@test "verbose logging" {
touch test/first.py.md
touch test/second.py.md
expected_items=2
logs=$(./lit.sh --input "test" --verbose --output test)
item_count=$(echo "${logs}" | grep '.py.md' | wc -l)
[ "${item_count}" -eq "${expected_items}" ]
}
@test "uses stdio" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
result="$(echo "${markdown}" | ./lit.sh --stdio)"
[ "${result}" = 'some code' ]
}
@test "works with process substitution" {
markdown=$'# a heading\nsome text\n```\nprint("test")\n```'
result="$(python <( echo "${markdown}" | ./lit.sh --stdio))"
[ "${result}" = 'test' ]
}
@test "selects files to compile based on a file glob provided via the --input long argument" {
touch test/first.py.md
touch test/second.py.md
touch test/third.js.md
./lit.sh --input "./test/*.py.md"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 5 ]
}
@test "selects files to compile based on a file glob provided via the -i short argument" {
touch test/first.py.md
touch test/second.py.md
touch test/third.js.md
./lit.sh -i "./test/*.py.md"
count="$(find test/ -type f | wc -l)"
[ "${count}" -eq 5 ]
}
@test "writes to an output directory based on a path provided via the --output long argument" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*.py.md" --output test/subdirectory/
count="$(find test/subdirectory/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "writes to an output directory based on a path provided via the -o short argument" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*.py.md" -o test/subdirectory/
count="$(find test/subdirectory/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "writes to nested subdirectories" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*.py.md" --output test/subdirectory/a/b
count="$(find test/subdirectory/a/b/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "appends trailing slashes to input directories" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test" --output test/result
count="$(find test/result/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "normalizes trailing slashes in input directories" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/" --output test/result
count="$(find test/result/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "appends trailing slashes to output directories" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*.py.md" --output test/subdirectory
count="$(find test/subdirectory/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "normalizes trailing slashes in output directories" {
touch test/first.py.md
touch test/second.py.md
./lit.sh --input "./test/*.py.md" --output test/subdirectory/
count="$(find test/subdirectory/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "uses existing paths if no output directory is specified" {
mkdir test/subdirectory
touch test/subdirectory/first.py.md
./lit.sh --input "./test/subdirectory/*.py.md"
count="$(find test/subdirectory/ -type f | wc -l)"
[ "${count}" -eq 2 ]
}
@test "recursively processes subdirectories" {
mkdir test/subdirectory
touch test/first.py.md
touch test/subdirectory/second.py.md
touch test/subdirectory/third.py.md
result="$(./lit.sh --input test | wc -l)"
[ "${result}" -eq 3 ]
}
@test "removes Markdown and preserves code" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
printf "${markdown}" >> test/script.py.md
./lit.sh --input "./test/script.py.md"
code="$(less ./test/script.py)"
[ "${code}" == "some code" ]
}
@test "allows language annotation after backticks" {
markdown=$'# a heading\nsome text\n```javascript\nsome code\n```'
code="$(echo "${markdown}" | ./lit.sh --stdio)"
[ "${code}" == "some code" ]
}
@test "compiles multiple fenced code blocks" {
markdown=$'# a heading\nsome text\n```\nsome code\n```\n# a heading\nsome text\n```\nsome more code\n```'
expected=$'some code\nsome more code'
code="$(echo "${markdown}" | ./lit.sh --stdio)"
[ "${code}" == "${expected}" ]
}
@test "preserves line positions using line comments with the --before long argument" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
expected=$'// # a heading\n// some text\n// ```\nsome code\n// ```'
code="$(echo "${markdown}" | ./lit.sh --stdio --before '//')"
[ "${code}" == "${expected}" ]
}
@test "preserves line positions using line comments with the -b short argument" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
expected=$'// # a heading\n// some text\n// ```\nsome code\n// ```'
code="$(echo "${markdown}" | ./lit.sh --stdio -b '//')"
[ "${code}" == "${expected}" ]
}
@test "preserves line positions using block comments with the --after long argument" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
expected=$'/* # a heading */\n/* some text */\n/* ``` */\nsome code\n/* ``` */'
code="$(echo "${markdown}" | ./lit.sh --stdio --before '/*' --after '*/')"
[ "${code}" == "${expected}" ]
}
@test "preserves line positions using block comments with the -a short argument" {
markdown=$'# a heading\nsome text\n```\nsome code\n```'
expected=$'/* # a heading */\n/* some text */\n/* ``` */\nsome code\n/* ``` */'
code="$(echo "${markdown}" | ./lit.sh --stdio --before '/*' -a '*/')"
[ "${code}" == "${expected}" ]
}
@test "executes without creating unexpected artifacts" {
pre=$(ls -R . | wc -l)
./lit.sh
post=$(ls -R . | wc -l)
[ "${pre}" -eq "${post}" ]
}