Skip to content

Commit 01ec6ec

Browse files
authored
Merge pull request #9 from nilenso/fix/filepath-bug
Fix: Add early target directory validation
2 parents 114ff3b + e090717 commit 01ec6ec

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/storymachine/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ def main():
127127
print(f"Error: Tech spec file not found: {tech_spec_path}", file=sys.stderr)
128128
sys.exit(1)
129129

130+
target_path = Path(args.target_dir)
131+
if not target_path.exists():
132+
print(f"Error: Target directory not found: {target_path}", file=sys.stderr)
133+
sys.exit(1)
134+
135+
if not target_path.is_dir():
136+
print(f"Error: Target path is not a directory: {target_path}", file=sys.stderr)
137+
sys.exit(1)
138+
130139
with spinner("Machining Stories"):
131140
created_stories = get_context_enriched_stories(
132141
client,

tests/test_cli.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,83 @@ def test_main_missing_tech_spec_file_exits(
296296
assert excinfo.value.code == 1
297297
err = capsys.readouterr().err
298298
assert f"Error: Tech spec file not found: {missing_tech}" in err
299+
300+
301+
def test_main_nonexistent_target_dir_exits(
302+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
303+
) -> None:
304+
"""Test that main() exits when target directory doesn't exist."""
305+
# Create both input files
306+
prd_file = tmp_path / "prd.md"
307+
tech_spec_file = tmp_path / "tech_spec.md"
308+
prd_file.write_text("PRD content")
309+
tech_spec_file.write_text("Tech spec content")
310+
311+
# Use non-existent target directory
312+
nonexistent_dir = tmp_path / "nonexistent"
313+
314+
with monkeypatch.context():
315+
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
316+
monkeypatch.setattr(
317+
"storymachine.cli.OpenAI", MagicMock(return_value=MagicMock())
318+
)
319+
monkeypatch.setattr(
320+
sys,
321+
"argv",
322+
[
323+
"storymachine",
324+
"--prd",
325+
str(prd_file),
326+
"--tech-spec",
327+
str(tech_spec_file),
328+
"--target-dir",
329+
str(nonexistent_dir),
330+
],
331+
)
332+
333+
with pytest.raises(SystemExit) as excinfo:
334+
main()
335+
336+
assert excinfo.value.code == 1
337+
err = capsys.readouterr().err
338+
assert f"Error: Target directory not found: {nonexistent_dir}" in err
339+
340+
341+
def test_main_target_is_file_exits(
342+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
343+
) -> None:
344+
"""Test that main() exits when target path is a file, not a directory."""
345+
# Create both input files
346+
prd_file = tmp_path / "prd.md"
347+
tech_spec_file = tmp_path / "tech_spec.md"
348+
target_file = tmp_path / "target.txt"
349+
350+
prd_file.write_text("PRD content")
351+
tech_spec_file.write_text("Tech spec content")
352+
target_file.write_text("This is a file, not a directory")
353+
354+
with monkeypatch.context():
355+
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
356+
monkeypatch.setattr(
357+
"storymachine.cli.OpenAI", MagicMock(return_value=MagicMock())
358+
)
359+
monkeypatch.setattr(
360+
sys,
361+
"argv",
362+
[
363+
"storymachine",
364+
"--prd",
365+
str(prd_file),
366+
"--tech-spec",
367+
str(tech_spec_file),
368+
"--target-dir",
369+
str(target_file),
370+
],
371+
)
372+
373+
with pytest.raises(SystemExit) as excinfo:
374+
main()
375+
376+
assert excinfo.value.code == 1
377+
err = capsys.readouterr().err
378+
assert f"Error: Target path is not a directory: {target_file}" in err

0 commit comments

Comments
 (0)