Repository navigation
Expand file tree
/
Copy pathtest_readme_examples.py
More file actions
436 lines (357 loc) · 13.9 KB
/
Copy pathtest_readme_examples.py
File metadata and controls
436 lines (357 loc) · 13.9 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
"""Test CodeCanopy according to README examples."""
import os
import tempfile
import shutil
import subprocess
import sys
from pathlib import Path
def create_test_project():
"""Create a comprehensive test project structure matching README examples."""
test_dir = Path(tempfile.mkdtemp(prefix="codecanopy_readme_test_"))
# Create React-like project structure
(test_dir / "src").mkdir()
(test_dir / "src" / "components").mkdir()
(test_dir / "src" / "hooks").mkdir()
(test_dir / "src" / "utils").mkdir()
(test_dir / "src" / "auth").mkdir()
(test_dir / "src" / "middleware").mkdir()
(test_dir / "tests").mkdir()
(test_dir / "tests" / "unit").mkdir()
(test_dir / "tests" / "integration").mkdir()
(test_dir / "docs").mkdir()
(test_dir / "node_modules").mkdir()
(test_dir / "dist").mkdir()
(test_dir / "build").mkdir()
(test_dir / "coverage").mkdir()
# Create files
files = {
# React components
"src/components/Header.js": """import React from 'react';
const Header = ({ title }) => {
return <h1>{title}</h1>;
};
export default Header;""",
"src/components/Footer.js": """import React from 'react';
const Footer = () => {
return <footer>© 2024 My App</footer>;
};
export default Footer;""",
"src/components/Navigation.js": """import React from 'react';
const Navigation = () => {
return <nav>Navigation</nav>;
};
export default Navigation;""",
# Hooks
"src/hooks/useAuth.js": """import { useState, useEffect } from 'react';
export const useAuth = () => {
const [user, setUser] = useState(null);
useEffect(() => {
// Auth logic here
}, []);
return { user, setUser };
};""",
# Utils
"src/utils/api.js": """export const fetchUser = async (id) => {
const response = await fetch(`/api/users/${id}`);
return response.json();
};
export const fetchPosts = async () => {
const response = await fetch('/api/posts');
return response.json();
};""",
"src/utils/helpers.js": """export const formatDate = (date) => {
return new Date(date).toLocaleDateString();
};
export const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};""",
# Auth
"src/auth/login.js": """export const login = async (credentials) => {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials)
});
return response.json();
};""",
# Middleware
"src/middleware/auth.js": """export const authMiddleware = (req, res, next) => {
const token = req.headers.authorization;
if (!token) {
return res.status(401).json({ error: 'No token provided' });
}
next();
};""",
# Main files
"src/index.js": """import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));""",
"src/App.js": """import React from 'react';
import Header from './components/Header';
import Footer from './components/Footer';
const App = () => {
return (
<div>
<Header title="My App" />
<main>Content here</main>
<Footer />
</div>
);
};
export default App;""",
# Test files
"tests/unit/Header.test.js": """import { render } from '@testing-library/react';
import Header from '../../src/components/Header';
test('Header renders title', () => {
const { getByText } = render(<Header title="Test" />);
expect(getByText('Test')).toBeInTheDocument();
});""",
"tests/integration/api.test.js": """import { fetchUser } from '../../src/utils/api';
test('fetchUser returns user data', async () => {
const user = await fetchUser(1);
expect(user).toBeDefined();
});""",
# Config files
"package.json": '{"name": "test-project", "version": "1.0.0"}',
"README.md": "# Test Project\n\nThis is a test project for CodeCanopy.",
# Files to be ignored
"node_modules/some-package/index.js": "module.exports = {};",
"dist/bundle.js": "console.log('bundled');",
"build/index.html": "<html><body>Built</body></html>",
"coverage/lcov.info": "SF:src/index.js",
# Large file to test size limits
"src/large-file.js": "// " + "x" * 50000, # 50KB file
}
for file_path, content in files.items():
full_path = test_dir / file_path
full_path.parent.mkdir(parents=True, exist_ok=True)
full_path.write_text(content)
return test_dir
def run_codecanopy_command(cmd, cwd=None):
"""Run a codecanopy command and return the output."""
try:
result = subprocess.run(
["python", "-m", "codecanopy"] + cmd.split(),
cwd=cwd,
capture_output=True,
text=True,
timeout=30,
)
return result.stdout, result.stderr, result.returncode
except subprocess.TimeoutExpired:
return "", "Command timed out", 1
except Exception as e:
return "", str(e), 1
def test_tree_examples():
"""Test tree command examples from README."""
print("Testing tree command examples...")
test_dir = create_test_project()
try:
# Test 1: Basic tree with focus
print("\n1. Testing: codecanopy tree --focus src --ignore node_modules,dist")
stdout, stderr, code = run_codecanopy_command(
"tree --focus src --ignore node_modules,dist", cwd=test_dir
)
if code == 0:
assert "src/" in stdout, "src/ should be in output"
assert "components/" in stdout, "components/ should be in output"
assert "node_modules" not in stdout, "node_modules should be ignored"
assert "dist" not in stdout, "dist should be ignored"
print("✓ Basic tree with focus works")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 2: Multiple focused directories
print(
"\n2. Testing: codecanopy tree --focus src/components,src/hooks --depth 2"
)
stdout, stderr, code = run_codecanopy_command(
"tree --focus src/components,src/hooks --depth 2", cwd=test_dir
)
if code == 0:
assert "src/" in stdout, "src/ should be in output"
assert "components/" in stdout, "components/ should be in output"
assert "hooks/" in stdout, "hooks/ should be in output"
print("✓ Multiple focused directories work")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 3: Show files
print("\n3. Testing: codecanopy tree --files --focus src")
stdout, stderr, code = run_codecanopy_command(
"tree --files --focus src", cwd=test_dir
)
if code == 0:
assert "Header.js" in stdout, "Header.js should be shown"
assert "Footer.js" in stdout, "Footer.js should be shown"
print("✓ Show files works")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 4: Custom depth
print("\n4. Testing: codecanopy tree --depth 1")
stdout, stderr, code = run_codecanopy_command("tree --depth 1", cwd=test_dir)
if code == 0:
assert "src/" in stdout, "src/ should be in output"
# Should not show deep files
assert "Header.js" not in stdout, "Header.js should not be shown at depth 1"
print("✓ Custom depth works")
else:
print(f"❌ Command failed: {stderr}")
return False
return True
finally:
shutil.rmtree(test_dir)
def test_cat_examples():
"""Test cat command examples from README."""
print("\nTesting cat command examples...")
test_dir = create_test_project()
try:
# Test 1: All JavaScript files
print("\n1. Testing: codecanopy cat 'src/**/*.js'")
stdout, stderr, code = run_codecanopy_command("cat 'src/**/*.js'", cwd=test_dir)
if code == 0:
assert "Header.js" in stdout, "Header.js should be in output"
assert "Footer.js" in stdout, "Footer.js should be in output"
assert "App.js" in stdout, "App.js should be in output"
assert "const Header" in stdout, "Header component should be in output"
print("✓ All JavaScript files work")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 2: Exclude test files
print("\n2. Testing: codecanopy cat 'src/**/*.js' --exclude '**/*.test.js'")
stdout, stderr, code = run_codecanopy_command(
"cat 'src/**/*.js' --exclude '**/*.test.js'", cwd=test_dir
)
if code == 0:
assert "Header.js" in stdout, "Header.js should be in output"
assert "Header.test.js" not in stdout, "Header.test.js should be excluded"
print("✓ Exclude patterns work")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 3: Custom headers
print("\n3. Testing: codecanopy cat 'src/index.js' --header '// File: {path}'")
stdout, stderr, code = run_codecanopy_command(
"cat 'src/index.js' --header '// File: {path}'", cwd=test_dir
)
if code == 0:
assert "// File: src/index.js" in stdout, "Custom header should be present"
assert "ReactDOM.render" in stdout, "File content should be present"
print("✓ Custom headers work")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test 4: Size limits
print("\n4. Testing: codecanopy cat 'src/**/*.js' --max-size 1KB")
stdout, stderr, code = run_codecanopy_command(
"cat 'src/**/*.js' --max-size 1KB", cwd=test_dir
)
if code == 0:
assert "large-file.js" not in stdout, "Large file should be skipped"
assert "Header.js" in stdout, "Small files should still be included"
print("✓ Size limits work")
else:
print(f"❌ Command failed: {stderr}")
return False
return True
finally:
shutil.rmtree(test_dir)
def test_config_examples():
"""Test configuration examples from README."""
print("\nTesting configuration examples...")
test_dir = create_test_project()
try:
# Create config file
config_content = {
"ignore": ["node_modules", "dist", "build"],
"default_depth": 2,
"header_format": "--- {path} ---",
"max_file_size": "10KB",
"focus_dirs": ["src"],
}
config_file = test_dir / ".codecanopy.json"
import json
config_file.write_text(json.dumps(config_content))
# Test config loading
print("\n1. Testing config file loading")
stdout, stderr, code = run_codecanopy_command("tree", cwd=test_dir)
if code == 0:
assert "src/" in stdout, "src/ should be in output"
assert "node_modules" not in stdout, "node_modules should be ignored"
assert "dist" not in stdout, "dist should be ignored"
print("✓ Config file loading works")
else:
print(f"❌ Command failed: {stderr}")
return False
# Test custom header format
print("\n2. Testing custom header format")
stdout, stderr, code = run_codecanopy_command(
"cat 'src/index.js'", cwd=test_dir
)
if code == 0:
assert (
"--- src/index.js ---" in stdout
), "Custom header format should be used"
print("✓ Custom header format works")
else:
print(f"❌ Command failed: {stderr}")
return False
return True
finally:
shutil.rmtree(test_dir)
def test_real_world_examples():
"""Test real-world examples from README."""
print("\nTesting real-world examples...")
test_dir = create_test_project()
try:
# Frontend React Project example
print("\n1. Testing Frontend React Project example")
stdout, stderr, code = run_codecanopy_command(
"tree --focus src/components,src/hooks --depth 2", cwd=test_dir
)
if code == 0:
assert "components/" in stdout, "components/ should be focused"
assert "hooks/" in stdout, "hooks/ should be focused"
print("✓ Frontend React Project example works")
else:
print(f"❌ Command failed: {stderr}")
return False
# Component code for LLM review
print("\n2. Testing component code for LLM review")
stdout, stderr, code = run_codecanopy_command(
"cat 'src/components/**/*.js' --exclude '**/*.test.*'", cwd=test_dir
)
if code == 0:
assert "Header.js" in stdout, "Header.js should be included"
assert "Footer.js" in stdout, "Footer.js should be included"
assert "Header.test.js" not in stdout, "Test files should be excluded"
print("✓ Component code for LLM review works")
else:
print(f"❌ Command failed: {stderr}")
return False
return True
finally:
shutil.rmtree(test_dir)
if __name__ == "__main__":
print("Running CodeCanopy README Examples Tests...\n")
all_passed = True
try:
all_passed &= test_tree_examples()
all_passed &= test_cat_examples()
all_passed &= test_config_examples()
all_passed &= test_real_world_examples()
if all_passed:
print("\n✅ All README examples work correctly!")
print("🎉 CodeCanopy is working as advertised in the README!")
else:
print("\n❌ Some README examples failed!")
sys.exit(1)
except Exception as e:
print(f"\n❌ Test failed with exception: {e}")
import traceback
traceback.print_exc()
sys.exit(1)