Skip to content

Commit 70a40f5

Browse files
Copilotchefgs
andcommitted
Add --dir option to devopsos init command
Lets users specify where the .devcontainer folder is scaffolded instead of always writing to the current working directory. python -m cli.devopsos init --dir my-project Renames the parameter from `dir` to `directory` to avoid shadowing the Python built-in. Adds _strip_ansi() test helper and two new tests: one for --help visibility, one functional test verifying the directory is used. Co-authored-by: chefgs <7605658+chefgs@users.noreply.github.com>
1 parent 3bdd071 commit 70a40f5

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

cli/devopsos.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class ProcessFirstSection(str, enum.Enum):
2727
app = typer.Typer(help="Unified DevOps-OS CLI tool")
2828

2929
@app.command()
30-
def init():
30+
def init(
31+
directory: str = typer.Option(".", "--dir", help="Target directory in which the .devcontainer folder will be created (defaults to the current directory)"),
32+
):
3133
"""Interactive project initializer."""
3234
typer.echo("Welcome to DevOps-OS Init Wizard!")
3335

@@ -73,8 +75,8 @@ def init():
7375
raise typer.Exit(1)
7476

7577
# Write to .devcontainer/devcontainer.env.json
76-
devcontainer_dir = Path(".devcontainer")
77-
devcontainer_dir.mkdir(exist_ok=True)
78+
devcontainer_dir = Path(directory) / ".devcontainer"
79+
devcontainer_dir.mkdir(parents=True, exist_ok=True)
7880
env_json_path = devcontainer_dir / "devcontainer.env.json"
7981
with open(env_json_path, "w") as f:
8082
json.dump(config, f, indent=2)

cli/test_cli.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import subprocess
22
import sys
3+
import re
34
import tempfile
45
import os
56
import yaml
@@ -15,12 +16,59 @@ def _run_module(module, extra_args=None):
1516
args = ["-m", module] + (extra_args or [])
1617
return _run(args)
1718

19+
_ANSI_ESCAPE = re.compile(r'\x1b\[[0-9;]*[mK]')
20+
21+
def _strip_ansi(s):
22+
"""Remove ANSI escape codes so plain-text assertions work regardless of terminal colour."""
23+
return _ANSI_ESCAPE.sub('', s)
24+
1825
# -- devopsos CLI ----------------------------------------------------------
1926

2027
def test_help():
2128
result = _run(["-m", "cli.devopsos", "--help"])
2229
assert "Unified DevOps-OS CLI tool" in result.stdout
2330

31+
def test_init_help_shows_dir_option():
32+
"""--dir option must appear in `devopsos init --help`."""
33+
result = subprocess.run(
34+
[sys.executable, "-m", "cli.devopsos", "init", "--help"],
35+
capture_output=True, text=True,
36+
cwd=os.path.dirname(os.path.dirname(__file__)),
37+
env={**os.environ, "NO_COLOR": "1"},
38+
)
39+
assert result.returncode == 0
40+
assert "--dir" in _strip_ansi(result.stdout)
41+
42+
def test_init_dir_option_creates_devcontainer_in_specified_dir():
43+
"""--dir places .devcontainer inside the given directory, not the cwd."""
44+
from unittest.mock import MagicMock, patch
45+
from typer.testing import CliRunner
46+
from cli.devopsos import app
47+
48+
checkbox_mock = MagicMock()
49+
checkbox_mock.execute.return_value = [] # nothing selected
50+
51+
# First confirm → Proceed = True
52+
# Second confirm → Generate Dockerfile = False (skip, we only need env.json)
53+
confirm_proceed = MagicMock()
54+
confirm_proceed.execute.return_value = True
55+
confirm_skip = MagicMock()
56+
confirm_skip.execute.return_value = False
57+
58+
with tempfile.TemporaryDirectory() as tmp:
59+
with patch("cli.devopsos.inquirer.checkbox", return_value=checkbox_mock), \
60+
patch("cli.devopsos.inquirer.confirm",
61+
side_effect=[confirm_proceed, confirm_skip]):
62+
runner = CliRunner()
63+
result = runner.invoke(app, ["init", "--dir", tmp])
64+
65+
assert result.exit_code == 0, result.output
66+
dc_dir = Path(tmp) / ".devcontainer"
67+
assert dc_dir.is_dir(), "Expected .devcontainer dir inside --dir target"
68+
assert (dc_dir / "devcontainer.env.json").exists(), (
69+
"Expected devcontainer.env.json inside .devcontainer"
70+
)
71+
2472
def test_scaffold_unknown():
2573
result = _run(["-m", "cli.devopsos", "scaffold", "unknown"])
2674
assert "Unknown scaffold target" in result.stdout

0 commit comments

Comments
 (0)