-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_init_javascript.py
More file actions
486 lines (335 loc) · 19.6 KB
/
test_init_javascript.py
File metadata and controls
486 lines (335 loc) · 19.6 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"""Tests for JavaScript/TypeScript project initialization and package manager detection."""
import json
import tempfile
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
from codeflash.cli_cmds.init_javascript import (
JsPackageManager,
ProjectLanguage,
collect_js_setup_info,
detect_project_language,
determine_js_package_manager,
get_package_install_command,
should_modify_package_json_config,
)
@pytest.fixture
def tmp_project() -> Path:
"""Create a temporary project directory with a deterministic parent chain."""
with tempfile.TemporaryDirectory(dir=Path.cwd()) as tmp_dir:
yield Path(tmp_dir)
def assert_install_command(actual: list[str], executable: str, expected_args: list[str]) -> None:
assert Path(actual[0]).stem.lower() == executable
assert actual[1:] == expected_args
class TestDetermineJsPackageManager:
"""Tests for determine_js_package_manager function."""
def test_detects_pnpm_from_lockfile(self, tmp_project: Path) -> None:
"""Should detect pnpm from pnpm-lock.yaml."""
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.PNPM
def test_detects_yarn_from_lockfile(self, tmp_project: Path) -> None:
"""Should detect yarn from yarn.lock."""
(tmp_project / "yarn.lock").write_text("")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.YARN
def test_detects_npm_from_lockfile(self, tmp_project: Path) -> None:
"""Should detect npm from package-lock.json."""
(tmp_project / "package-lock.json").write_text("{}")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.NPM
def test_detects_bun_from_lockfile(self, tmp_project: Path) -> None:
"""Should detect bun from bun.lockb."""
(tmp_project / "bun.lockb").write_text("")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.BUN
def test_detects_bun_from_bun_lock(self, tmp_project: Path) -> None:
"""Should detect bun from bun.lock."""
(tmp_project / "bun.lock").write_text("")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.BUN
def test_defaults_to_npm_with_package_json_only(self, tmp_project: Path) -> None:
"""Should default to npm when only package.json exists."""
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.NPM
def test_returns_unknown_without_package_json(self, tmp_project: Path) -> None:
"""Should return UNKNOWN when no package.json exists."""
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.UNKNOWN
def test_pnpm_takes_precedence_over_npm(self, tmp_project: Path) -> None:
"""Should prefer pnpm when both lockfiles exist (migration scenario)."""
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package-lock.json").write_text("{}")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.PNPM
def test_bun_takes_precedence_over_others(self, tmp_project: Path) -> None:
"""Should prefer bun when bun.lockb exists alongside others."""
(tmp_project / "bun.lockb").write_text("")
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package.json").write_text("{}")
result = determine_js_package_manager(tmp_project)
assert result == JsPackageManager.BUN
# Monorepo tests - lock file in parent directory
def test_detects_pnpm_from_parent_lockfile(self, tmp_project: Path) -> None:
"""Should detect pnpm from pnpm-lock.yaml in parent directory (monorepo)."""
# Create monorepo structure: root/packages/my-package
workspace_root = tmp_project
package_dir = workspace_root / "packages" / "my-package"
package_dir.mkdir(parents=True)
# Lock file at workspace root
(workspace_root / "pnpm-lock.yaml").write_text("")
(workspace_root / "package.json").write_text("{}")
# Package has its own package.json but no lock file
(package_dir / "package.json").write_text("{}")
result = determine_js_package_manager(package_dir)
assert result == JsPackageManager.PNPM
def test_detects_yarn_from_parent_lockfile(self, tmp_project: Path) -> None:
"""Should detect yarn from yarn.lock in parent directory (monorepo)."""
workspace_root = tmp_project
package_dir = workspace_root / "packages" / "my-package"
package_dir.mkdir(parents=True)
(workspace_root / "yarn.lock").write_text("")
(workspace_root / "package.json").write_text("{}")
(package_dir / "package.json").write_text("{}")
result = determine_js_package_manager(package_dir)
assert result == JsPackageManager.YARN
def test_detects_npm_from_parent_lockfile(self, tmp_project: Path) -> None:
"""Should detect npm from package-lock.json in parent directory (monorepo)."""
workspace_root = tmp_project
package_dir = workspace_root / "packages" / "my-package"
package_dir.mkdir(parents=True)
(workspace_root / "package-lock.json").write_text("{}")
(workspace_root / "package.json").write_text("{}")
(package_dir / "package.json").write_text("{}")
result = determine_js_package_manager(package_dir)
assert result == JsPackageManager.NPM
def test_detects_bun_from_parent_lockfile(self, tmp_project: Path) -> None:
"""Should detect bun from bun.lockb in parent directory (monorepo)."""
workspace_root = tmp_project
package_dir = workspace_root / "packages" / "my-package"
package_dir.mkdir(parents=True)
(workspace_root / "bun.lockb").write_text("")
(workspace_root / "package.json").write_text("{}")
(package_dir / "package.json").write_text("{}")
result = determine_js_package_manager(package_dir)
assert result == JsPackageManager.BUN
def test_local_lockfile_takes_precedence_over_parent(self, tmp_project: Path) -> None:
"""Should prefer local lock file over parent directory lock file."""
workspace_root = tmp_project
package_dir = workspace_root / "packages" / "my-package"
package_dir.mkdir(parents=True)
# Parent has pnpm, but local package has yarn
(workspace_root / "pnpm-lock.yaml").write_text("")
(workspace_root / "package.json").write_text("{}")
(package_dir / "yarn.lock").write_text("")
(package_dir / "package.json").write_text("{}")
result = determine_js_package_manager(package_dir)
# Should detect yarn from local directory first
assert result == JsPackageManager.YARN
def test_deeply_nested_package_finds_root_lockfile(self, tmp_project: Path) -> None:
"""Should find lock file in deeply nested monorepo structure."""
workspace_root = tmp_project
# Simulate: root/apps/web/src/features/auth
deep_dir = workspace_root / "apps" / "web" / "src" / "features" / "auth"
deep_dir.mkdir(parents=True)
(workspace_root / "pnpm-lock.yaml").write_text("")
(workspace_root / "package.json").write_text("{}")
result = determine_js_package_manager(deep_dir)
assert result == JsPackageManager.PNPM
class TestGetPackageInstallCommand:
"""Tests for get_package_install_command function."""
def test_npm_install_command(self, tmp_project: Path) -> None:
"""Should return npm install command for npm projects."""
(tmp_project / "package-lock.json").write_text("{}")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=True)
assert_install_command(result, "npm", ["install", "codeflash", "--save-dev"])
def test_npm_install_command_non_dev(self, tmp_project: Path) -> None:
"""Should return npm install command without --save-dev when dev=False."""
(tmp_project / "package-lock.json").write_text("{}")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=False)
assert_install_command(result, "npm", ["install", "codeflash"])
def test_pnpm_add_command(self, tmp_project: Path) -> None:
"""Should return pnpm add command for pnpm projects."""
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=True)
assert_install_command(result, "pnpm", ["add", "codeflash", "--save-dev"])
def test_pnpm_add_command_non_dev(self, tmp_project: Path) -> None:
"""Should return pnpm add command without --save-dev when dev=False."""
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=False)
assert_install_command(result, "pnpm", ["add", "codeflash"])
def test_yarn_add_command(self, tmp_project: Path) -> None:
"""Should return yarn add command for yarn projects."""
(tmp_project / "yarn.lock").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=True)
assert_install_command(result, "yarn", ["add", "codeflash", "--dev"])
def test_yarn_add_command_non_dev(self, tmp_project: Path) -> None:
"""Should return yarn add command without --dev when dev=False."""
(tmp_project / "yarn.lock").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=False)
assert_install_command(result, "yarn", ["add", "codeflash"])
def test_bun_add_command(self, tmp_project: Path) -> None:
"""Should return bun add command for bun projects."""
(tmp_project / "bun.lockb").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=True)
assert_install_command(result, "bun", ["add", "codeflash", "--dev"])
def test_bun_add_command_non_dev(self, tmp_project: Path) -> None:
"""Should return bun add command without --dev when dev=False."""
(tmp_project / "bun.lockb").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "codeflash", dev=False)
assert_install_command(result, "bun", ["add", "codeflash"])
def test_defaults_to_npm_for_unknown(self, tmp_project: Path) -> None:
"""Should default to npm for unknown package manager."""
# No lockfile, no package.json - unknown package manager
result = get_package_install_command(tmp_project, "codeflash", dev=True)
assert_install_command(result, "npm", ["install", "codeflash", "--save-dev"])
def test_different_package_name(self, tmp_project: Path) -> None:
"""Should work with different package names."""
(tmp_project / "pnpm-lock.yaml").write_text("")
(tmp_project / "package.json").write_text("{}")
result = get_package_install_command(tmp_project, "typescript", dev=True)
assert_install_command(result, "pnpm", ["add", "typescript", "--save-dev"])
class TestShouldModifySkipConfirm:
"""Tests for should_modify_package_json_config with skip_confirm."""
def test_should_modify_skip_confirm_no_config(self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""With skip_confirm and no codeflash config, should return (True, None)."""
monkeypatch.chdir(tmp_project)
(tmp_project / "package.json").write_text(json.dumps({"name": "test"}))
should_modify, config = should_modify_package_json_config(skip_confirm=True)
assert should_modify is True
assert config is None
def test_should_modify_skip_confirm_with_valid_config(
self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""With skip_confirm and valid config, should return (False, config) — no reconfigure."""
monkeypatch.chdir(tmp_project)
codeflash_config = {"moduleRoot": "."}
(tmp_project / "package.json").write_text(json.dumps({"name": "test", "codeflash": codeflash_config}))
should_modify, config = should_modify_package_json_config(skip_confirm=True)
assert should_modify is False
assert config == codeflash_config
def test_should_modify_skip_confirm_with_invalid_config(
self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""With skip_confirm and invalid config (bad moduleRoot), should return (True, None)."""
monkeypatch.chdir(tmp_project)
codeflash_config = {"moduleRoot": "/nonexistent/path/that/does/not/exist"}
(tmp_project / "package.json").write_text(json.dumps({"name": "test", "codeflash": codeflash_config}))
should_modify, config = should_modify_package_json_config(skip_confirm=True)
assert should_modify is True
assert config is None
def test_should_modify_valid_config_uses_default_on_eof(
self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""EOF on the reconfigure prompt should keep the existing config."""
monkeypatch.chdir(tmp_project)
codeflash_config = {"moduleRoot": "."}
(tmp_project / "package.json").write_text(json.dumps({"name": "test", "codeflash": codeflash_config}))
with patch("rich.prompt.Confirm.ask", side_effect=EOFError):
should_modify, config = should_modify_package_json_config()
assert should_modify is False
assert config == codeflash_config
class TestCollectJsSetupInfoSkipConfirm:
"""Tests for collect_js_setup_info with skip_confirm."""
def test_collect_js_setup_info_skip_confirm(self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""skip_confirm should return defaults without any interactive prompts."""
monkeypatch.chdir(tmp_project)
(tmp_project / "package.json").write_text(json.dumps({"name": "test"}))
from codeflash.cli_cmds.init_javascript import ProjectLanguage, collect_js_setup_info
# Should not call any prompt functions
with patch("codeflash.cli_cmds.init_javascript.inquirer") as mock_inquirer:
setup_info = collect_js_setup_info(ProjectLanguage.JAVASCRIPT, skip_confirm=True)
mock_inquirer.prompt.assert_not_called()
assert setup_info.module_root_override is None
assert setup_info.formatter_override is None
assert setup_info.git_remote == "origin"
def test_collect_js_setup_info_uses_defaults_on_eof(
self, tmp_project: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""EOF on the settings confirm should keep auto-detected defaults."""
monkeypatch.chdir(tmp_project)
(tmp_project / "package.json").write_text(json.dumps({"name": "test"}))
get_git_remote = Mock(return_value="origin")
monkeypatch.setattr("codeflash.cli_cmds.init_javascript._get_git_remote_for_setup", get_git_remote)
monkeypatch.setattr("codeflash.cli_cmds.init_config.ask_for_telemetry", Mock(return_value=True))
with (
patch("rich.prompt.Confirm.ask", side_effect=EOFError),
patch("codeflash.cli_cmds.init_javascript.inquirer") as mock_inquirer,
):
setup_info = collect_js_setup_info(ProjectLanguage.JAVASCRIPT)
mock_inquirer.prompt.assert_not_called()
get_git_remote.assert_called_once_with()
assert setup_info.module_root_override is None
assert setup_info.formatter_override is None
assert setup_info.git_remote == "origin"
assert setup_info.disable_telemetry is False
class TestDetectProjectLanguage:
"""Tests for detect_project_language function."""
def test_detects_java_from_pom_xml(self, tmp_project: Path) -> None:
(tmp_project / "pom.xml").write_text("<project/>")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_detects_java_from_build_gradle(self, tmp_project: Path) -> None:
(tmp_project / "build.gradle").write_text("")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_detects_java_from_build_gradle_kts(self, tmp_project: Path) -> None:
(tmp_project / "build.gradle.kts").write_text("")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_detects_typescript_from_tsconfig(self, tmp_project: Path) -> None:
(tmp_project / "tsconfig.json").write_text("{}")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.TYPESCRIPT
def test_detects_javascript_from_package_json(self, tmp_project: Path) -> None:
(tmp_project / "package.json").write_text("{}")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVASCRIPT
def test_detects_python_from_pyproject_toml(self, tmp_project: Path) -> None:
(tmp_project / "pyproject.toml").write_text("")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.PYTHON
def test_defaults_to_python_for_empty_directory(self, tmp_project: Path) -> None:
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.PYTHON
def test_java_takes_priority_over_python(self, tmp_project: Path) -> None:
(tmp_project / "pom.xml").write_text("<project/>")
(tmp_project / "pyproject.toml").write_text("")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_java_takes_priority_over_javascript(self, tmp_project: Path) -> None:
(tmp_project / "build.gradle").write_text("")
(tmp_project / "package.json").write_text("{}")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_java_takes_priority_over_typescript(self, tmp_project: Path) -> None:
(tmp_project / "pom.xml").write_text("<project/>")
(tmp_project / "tsconfig.json").write_text("{}")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVA
def test_javascript_with_js_indicators_over_python(self, tmp_project: Path) -> None:
(tmp_project / "package.json").write_text("{}")
(tmp_project / "pyproject.toml").write_text("")
(tmp_project / "node_modules").mkdir()
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.JAVASCRIPT
def test_python_over_package_json_without_js_indicators(self, tmp_project: Path) -> None:
(tmp_project / "package.json").write_text("{}")
(tmp_project / "pyproject.toml").write_text("")
result = detect_project_language(tmp_project)
assert result == ProjectLanguage.PYTHON