Skip to content

Personal AI, On Personal Devices

Python

fix(cli): accept a filesystem path for `jarvis init --config` (#768) (#790)

ethanhawkes-gif29b8302c52a9
diff --git a/src/openjarvis/cli/init_cmd.py b/src/openjarvis/cli/init_cmd.py
index 2866b098..e9c53d47 100644
--- a/src/openjarvis/cli/init_cmd.py
+++ b/src/openjarvis/cli/init_cmd.py
@@ -230,7 +230,7 @@ def _do_download(engine: str, model: str, spec, console: Console) -> None:
)
@click.option(
"--config",
- type=click.Path(exists=True),
+ type=click.Path(exists=True, dir_okay=False, path_type=Path),
help="Path to config file to use.",
)
@click.option(
@@ -445,7 +445,7 @@ def init(
)
if config:
- toml_content = config.read_text()
+ toml_content = config.read_text(encoding="utf-8")
else:
if full_config:
toml_content = generate_default_toml(hw, engine=engine, host=host)
@@ -453,10 +453,10 @@ def init(
toml_content = generate_minimal_toml(hw, engine=engine, host=host)
DEFAULT_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
- if config:
- config.write_text(toml_content)
- else:
- DEFAULT_CONFIG_PATH.write_text(toml_content)
+ # ``--config`` selects an existing file to install; it is not an alternate
+ # output path. Always activate the selected/generated configuration at
+ # the canonical location that subsequent ``jarvis`` commands load.
+ DEFAULT_CONFIG_PATH.write_text(toml_content, encoding="utf-8")
console.print()
console.print(
@@ -493,7 +493,7 @@ sources = ["gcalendar"]
[digest.world]
sources = ["hackernews", "news_rss"]
"""
- target = config if config else DEFAULT_CONFIG_PATH
+ target = DEFAULT_CONFIG_PATH
existing = target.read_text()
target.write_text(existing + digest_section)
toml_content = target.read_text()
diff --git a/tests/cli/test_init_config.py b/tests/cli/test_init_config.py
new file mode 100644
index 00000000..58cc512a
--- /dev/null
+++ b/tests/cli/test_init_config.py
@@ -0,0 +1,88 @@
+"""Tests for ``jarvis init --config`` option (regression for #768).
+
+The ``--config`` option is declared with ``click.Path`` and the command body
+uses the value as a :class:`pathlib.Path` (``config.read_text()`` /
+``config.write_text()``). Without ``path_type=Path`` Click hands the callback a
+plain ``str``, so every invocation of ``--config`` crashed with
+``AttributeError: 'str' object has no attribute 'read_text'``.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest import mock
+
+from click.testing import CliRunner
+
+from openjarvis.cli import cli
+
+_NO_DL = "--no-download"
+
+
+class TestInitConfig:
+ def test_init_config_installs_provided_file(self, tmp_path: Path) -> None:
+ """jarvis init --config <file> activates it at the canonical path.
+
+ Regression for #768: the callback receives a Path and can call
+ ``read_text()`` / ``write_text()`` on it.
+ """
+ provided = tmp_path / "some-config.toml"
+ supplied_content = '[engine]\nname = "ollama"\n'
+ provided.write_text(supplied_content, encoding="utf-8")
+
+ config_dir = tmp_path / ".openjarvis"
+ config_path = config_dir / "config.toml" # absent -> passes the exists() guard
+ with (
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_DIR", config_dir),
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_PATH", config_path),
+ mock.patch("openjarvis.cli.init_cmd.PrivacyScanner"),
+ ):
+ result = CliRunner().invoke(
+ cli, ["init", "--config", str(provided), _NO_DL]
+ )
+
+ assert result.exception is None, result.output
+ assert result.exit_code == 0
+ assert config_path.read_text(encoding="utf-8") == supplied_content
+ # Treat the supplied file as input: init must not rewrite it.
+ assert provided.read_text(encoding="utf-8") == supplied_content
+
+ def test_init_config_digest_updates_installed_copy(self, tmp_path: Path) -> None:
+ """Post-processing applies to the active copy, not the input file."""
+ provided = tmp_path / "some-config.toml"
+ supplied_content = '[engine]\nname = "ollama"\n'
+ provided.write_text(supplied_content, encoding="utf-8")
+ config_dir = tmp_path / ".openjarvis"
+ config_path = config_dir / "config.toml"
+
+ with (
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_DIR", config_dir),
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_PATH", config_path),
+ mock.patch("openjarvis.cli.init_cmd.PrivacyScanner"),
+ ):
+ result = CliRunner().invoke(
+ cli,
+ [
+ "init",
+ "--config",
+ str(provided),
+ "--digest",
+ _NO_DL,
+ ],
+ )
+
+ assert result.exception is None, result.output
+ assert result.exit_code == 0
+ assert "[digest]" in config_path.read_text(encoding="utf-8")
+ assert provided.read_text(encoding="utf-8") == supplied_content
+
+ def test_init_config_rejects_directory(self, tmp_path: Path) -> None:
+ """A directory is rejected by Click (dir_okay=False), not by a later crash."""
+ a_dir = tmp_path / "not-a-file"
+ a_dir.mkdir()
+
+ result = CliRunner().invoke(cli, ["init", "--config", str(a_dir), _NO_DL])
+
+ # Click usage error (exit 2), and specifically NOT the AttributeError crash.
+ assert result.exit_code == 2
+ assert not isinstance(result.exception, AttributeError)