-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_all_code.py
More file actions
392 lines (322 loc) · 16.4 KB
/
Copy pathtest_all_code.py
File metadata and controls
392 lines (322 loc) · 16.4 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
Test script for all_code.py command-line arguments.
This script creates temporary directories and files, then uses the built-in
subprocess module to run all_code.py with different options:
- Default behavior (creates 'full_code.txt')
- Overriding the output file name with -o/--output-file
- Including only specific files with -i/--include-files
- Overriding programming extensions with -x/--extensions
- Excluding directories with -e/--exclude-dirs
No external libraries are required.
"""
import os
import shutil
import subprocess
import tempfile
import sys
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "all_code.py")
def run_script(args, cwd):
"""
Run all_code.py with the provided command-line arguments in directory cwd.
"""
cmd = [sys.executable, SCRIPT_PATH] + args
result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True)
return result
def test_default_arguments():
# Create a temporary directory to serve as the source for aggregation.
with tempfile.TemporaryDirectory() as tmp_dir:
# Copy all_code.py to tmp directory and run it without any args
TMP_SCRIPT_PATH = shutil.copy(SCRIPT_PATH, tmp_dir)
cmd = [sys.executable, TMP_SCRIPT_PATH]
subprocess.run(cmd, cwd=tmp_dir, capture_output=True, text=True)
assert os.path.exists(os.path.join(tmp_dir, "full_code.txt")), "full_code.txt not created by default"
def test_directory_argument():
with tempfile.TemporaryDirectory() as source_dir:
# Create a dummy Python file in the source directory.
dummy_file = os.path.join(source_dir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Aggregating from source directory')")
# Use a separate temporary directory as the working directory for the script.
with tempfile.TemporaryDirectory() as tmp_working:
custom_output = "test_output.txt"
# Run the script with -d pointing to the source directory.
run_script(["-d", source_dir, "-o", custom_output], cwd=tmp_working)
output_file = os.path.join(tmp_working, custom_output)
assert os.path.exists(output_file), "Output file not created when using --directory"
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "Aggregated content does not reflect the --directory argument"
print("test_directory_argument passed.")
def test_default_output():
with tempfile.TemporaryDirectory() as tmpdir:
# Create a dummy Python file so that there is something to include.
dummy_file = os.path.join(tmpdir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Hello World')")
# Run the script with default output (should create full_code.txt)
run_script(["-d", tmpdir], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
assert os.path.exists(output_file), "Default output file not created"
print("test_default_output passed.")
def test_override_output_file():
with tempfile.TemporaryDirectory() as tmpdir:
dummy_file = os.path.join(tmpdir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Hello World')")
custom_output = "custom_output.txt"
run_script(["-d", tmpdir, "-o", custom_output], cwd=tmpdir)
output_file = os.path.join(tmpdir, custom_output)
assert os.path.exists(output_file), "Overridden output file not created"
print("test_override_output_file passed.")
def test_include_files():
with tempfile.TemporaryDirectory() as tmpdir:
# Create two Python files.
file1 = os.path.join(tmpdir, "dummy.py")
with open(file1, "w") as f:
f.write("print('Hello from dummy')")
file2 = os.path.join(tmpdir, "extra.py")
with open(file2, "w") as f:
f.write("print('Hello from extra')")
# Specify only dummy.py to be included.
run_script(["-d", tmpdir, "-i", "dummy.py"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "dummy.py should be included"
assert "print('Hello from extra')" not in content, "extra.py should not be included"
print("test_include_files passed.")
def test_extensions():
with tempfile.TemporaryDirectory() as tmpdir:
# Create one .py file and one .txt file.
py_file = os.path.join(tmpdir, "dummy.py")
with open(py_file, "w") as f:
f.write("print('Python file')")
txt_file = os.path.join(tmpdir, "dummy.txt")
with open(txt_file, "w") as f:
f.write("This is a text file.")
# Override extensions to only include .py files.
run_script(["-d", tmpdir, "-x", ".py"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "dummy.py should be included with .py extension"
assert "This is a text file." not in content, "dummy.txt should be excluded when only .py is allowed"
print("test_extensions passed.")
def test_exclude_dirs():
with tempfile.TemporaryDirectory() as tmpdir:
# Create a subdirectory that will be excluded.
exclude_dir = os.path.join(tmpdir, "exclude_me")
os.mkdir(exclude_dir)
file_in_excluded = os.path.join(exclude_dir, "dummy.py")
with open(file_in_excluded, "w") as f:
f.write("print('This file is in an excluded directory')")
# Run the script with exclude_dirs set to 'exclude_me'
run_script(["-d", tmpdir, "-e", "exclude_me"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
# The directory tree should mark exclude_me as excluded.
assert "exclude_me/ [EXCLUDED]" in content, "exclude_me directory should be marked as excluded"
# The file inside should not be aggregated.
assert "dummy.py" not in content, "File inside excluded directory should not be included"
print("test_exclude_dirs passed.")
def test_extensions_allowlist():
with tempfile.TemporaryDirectory() as tmpdir:
py_file = os.path.join(tmpdir, "dummy.py")
with open(py_file, "w", encoding="utf-8") as f:
f.write("print('Python file')")
txt_file = os.path.join(tmpdir, "dummy.txt")
with open(txt_file, "w", encoding="utf-8") as f:
f.write("This is a text file.")
run_script(["-d", tmpdir, "-x", ".py"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "dummy.py" in content, "dummy.py should be included with .py extension"
assert "This is a text file." not in content, "dummy.txt should be excluded when only .py is allowed"
print("test_extensions_allowlist() passed.")
def test_exclude_dirs_additive():
with tempfile.TemporaryDirectory() as tmpdir:
exclude_dir = os.path.join(tmpdir, "exclude_me")
os.mkdir(exclude_dir)
file_in_excluded = os.path.join(exclude_dir, "dummy.py")
with open(file_in_excluded, "w", encoding="utf-8") as f:
f.write("print('This file is in an excluded directory')")
run_script(["-d", tmpdir, "-e", "exclude_me"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "exclude_me/ [EXCLUDED]" in content, "exclude_me directory should be marked as excluded"
assert "This file is in an excluded directory" not in content, "File inside excluded directory should not be included"
print("test_exclude_dirs_additive() passed.")
def test_exclude_files_glob():
with tempfile.TemporaryDirectory() as tmpdir:
os.makedirs(os.path.join(tmpdir, "foo"), exist_ok=True)
# a JSON file to exclude via glob
cfg = os.path.join(tmpdir, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"ok": true}')
# a file that must remain included
keep = os.path.join(tmpdir, "foo", "keep.py")
with open(keep, "w", encoding="utf-8") as f:
f.write("print('KEEP_ME')")
# exclude via glob
run_script(["-d", tmpdir, "--exclude-files", "foo/*.json"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
# tree should show the excluded file as [EXCLUDED]
assert "config.json [EXCLUDED]" in content, "Excluded file should be marked in the tree"
# aggregated content should NOT contain the file's contents
assert '{"ok": true}' not in content, "Excluded file contents must not be aggregated"
# keep.py stays in
assert "KEEP_ME" in content, "Non-excluded file should be included"
print("test_exclude_files_glob() passed.")
def test_exclude_files_absolute_path():
with tempfile.TemporaryDirectory() as tmpdir:
os.makedirs(os.path.join(tmpdir, "foo"), exist_ok=True)
cfg = os.path.join(tmpdir, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"abs": true}')
run_script(
["-d", tmpdir, "--exclude-files", cfg],
cwd=tmpdir,
)
with open(os.path.join(tmpdir, "full_code.txt"), "r", encoding="utf-8") as f:
content = f.read()
assert '{"abs": true}' not in content
print("test_exclude_files_absolute_path() passed.")
def test_exclude_files_dot_slash():
with tempfile.TemporaryDirectory() as tmpdir:
os.makedirs(os.path.join(tmpdir, "foo"), exist_ok=True)
cfg = os.path.join(tmpdir, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"dot": true}')
run_script(
["-d", tmpdir, "--exclude-files", "./foo/config.json"],
cwd=tmpdir,
)
with open(os.path.join(tmpdir, "full_code.txt"), "r", encoding="utf-8") as f:
content = f.read()
assert "config.json [EXCLUDED]" in content
assert '{"dot": true}' not in content
print("test_exclude_files_dot_slash() passed.")
def test_exclude_files_cwd_differs_from_startpath():
with tempfile.TemporaryDirectory() as tmpdir:
project = os.path.join(tmpdir, "project")
os.makedirs(os.path.join(project, "foo"), exist_ok=True)
cfg = os.path.join(project, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"cwd": true}')
run_script(
["-d", "project", "--exclude-files", "./foo/config.json"],
cwd=tmpdir,
)
with open(os.path.join(tmpdir, "full_code.txt"), "r", encoding="utf-8") as f:
content = f.read()
assert '{"cwd": true}' not in content
print("test_exclude_files_cwd_differs_from_startpath() passed.")
def test_exclude_files_dot_slash_glob():
with tempfile.TemporaryDirectory() as tmpdir:
os.makedirs(os.path.join(tmpdir, "foo"), exist_ok=True)
cfg = os.path.join(tmpdir, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"glob": true}')
run_script(
["-d", tmpdir, "--exclude-files", "./foo/*.json"],
cwd=tmpdir,
)
with open(os.path.join(tmpdir, "full_code.txt"), "r", encoding="utf-8") as f:
content = f.read()
assert '{"glob": true}' not in content
print("test_exclude_files_dot_slash_glob() passed.")
def test_exclude_files_backslash_path():
if os.name != "nt":
print("Backslash paths are Windows-only")
return
with tempfile.TemporaryDirectory() as tmpdir:
os.makedirs(os.path.join(tmpdir, "foo"), exist_ok=True)
cfg = os.path.join(tmpdir, "foo", "config.json")
with open(cfg, "w", encoding="utf-8") as f:
f.write('{"win": true}')
win_style = os.path.join("foo", "config.json").replace("/", "\\")
run_script(
["-d", tmpdir, "--exclude-files", win_style],
cwd=tmpdir,
)
with open(os.path.join(tmpdir, "full_code.txt"), "r", encoding="utf-8") as f:
content = f.read()
assert '{"win": true}' not in content
print("test_exclude_files_backslash_path() passed.")
def test_replace_exclude_dirs_behavior():
with tempfile.TemporaryDirectory() as tmpdir:
node_modules_dir = os.path.join(tmpdir, "node_modules")
os.makedirs(node_modules_dir, exist_ok=True)
nm_file = os.path.join(node_modules_dir, "lib.js")
with open(nm_file, "w", encoding="utf-8") as f:
f.write("console.log('IN_NODE_MODULES');")
custom_dir = os.path.join(tmpdir, "custom_dir")
os.makedirs(custom_dir, exist_ok=True)
custom_file = os.path.join(custom_dir, "x.py")
with open(custom_file, "w", encoding="utf-8") as f:
f.write("print('IN_CUSTOM_DIR')")
root_py = os.path.join(tmpdir, "main.py")
with open(root_py, "w", encoding="utf-8") as f:
f.write("print('ROOT_OK')")
run_script(["-d", tmpdir, "--replace-exclude-dirs", "-e", "custom_dir"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "custom_dir/ [EXCLUDED]" in content, "custom_dir should be marked [EXCLUDED] when replaced"
assert "IN_CUSTOM_DIR" not in content, "Files in custom_dir must be excluded in replace mode"
assert "IN_NODE_MODULES" in content, "node_modules should not be excluded in replace mode"
assert "ROOT_OK" in content, "root file should be included"
print("test_replace_exclude_dirs_behavior() passed.")
def test_extension_precedence_exclude_overrides_allowlist():
with tempfile.TemporaryDirectory() as tmpdir:
js_file = os.path.join(tmpdir, "a.js")
with open(js_file, "w", encoding="utf-8") as f:
f.write("console.log('JS_INCLUDED?');")
py_file = os.path.join(tmpdir, "b.py")
with open(py_file, "w", encoding="utf-8") as f:
f.write("print('PY_INCLUDED')")
run_script(["-d", tmpdir, "-x", ".py,.js", "-X", ".js"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "JS_INCLUDED?" not in content, ".js should be excluded by -X even if allowed by -x"
assert "PY_INCLUDED" in content, ".py should remain included"
print("test_extension_precedence_exclude_overrides_allowlist() passed.")
def test_tree_not_traversing_excluded():
with tempfile.TemporaryDirectory() as tmpdir:
deep = os.path.join(tmpdir, "secret")
os.makedirs(os.path.join(deep, "nested"), exist_ok=True)
secret_file = os.path.join(deep, "nested", "hidden.py")
with open(secret_file, "w", encoding="utf-8") as f:
f.write("print('SHOULD_NOT_APPEAR')")
run_script(["-d", tmpdir, "-e", "secret"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "secret/ [EXCLUDED]" in content, "Top-level excluded dir should be marked once"
assert "hidden.py" not in content, "Files inside excluded dir must not appear (no traversal)"
print("test_tree_not_traversing_excluded() passed.")
if __name__ == "__main__":
# Run everything if executed directly
test_default_arguments()
test_directory_argument()
test_default_output()
test_override_output_file()
test_include_files()
test_extensions_allowlist()
test_exclude_dirs_additive()
test_exclude_files_glob()
test_exclude_files_absolute_path()
test_exclude_files_backslash_path()
test_exclude_files_cwd_differs_from_startpath()
test_exclude_files_dot_slash()
test_exclude_files_dot_slash_glob()
test_replace_exclude_dirs_behavior()
test_extension_precedence_exclude_overrides_allowlist()
test_tree_not_traversing_excluded()
print("All tests passed!")