diff --git a/src/cloudai/cli/cli.py b/src/cloudai/cli/cli.py index 2fe161144..1d30d01dd 100644 --- a/src/cloudai/cli/cli.py +++ b/src/cloudai/cli/cli.py @@ -88,8 +88,9 @@ def common_options(f): "--system-config", "system_cfg", required=True, + envvar="CLOUDAI_SYSTEM_CONFIG", type=click.Path(exists=True, resolve_path=True, path_type=Path), - help="System config path.", + help="System config path. Can also be set via the CLOUDAI_SYSTEM_CONFIG environment variable.", )(f) f = click.option( "--tests-dir", diff --git a/tests/test_cli.py b/tests/test_cli.py index 7eda4dc2d..7cfb4b59c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -46,6 +46,47 @@ def test_tests_dir_is_optional(tmp_path: Path): assert "Missing option '--tests-dir'" not in result.output +def test_system_config_can_be_set_via_env_var(tmp_path: Path): + system_cfg, scenario_cfg = tmp_path / "system.toml", tmp_path / "scenario.toml" + system_cfg.touch() + scenario_cfg.touch() + runner = CliRunner() + result = runner.invoke( + main, + ["run", "--test-scenario", str(scenario_cfg)], + env={"CLOUDAI_SYSTEM_CONFIG": str(system_cfg)}, + ) + assert "Missing option '--system-config'" not in result.output + + +def test_system_config_flag_takes_precedence_over_env_var(tmp_path: Path): + system_cfg, other_cfg, scenario_cfg = ( + tmp_path / "system.toml", + tmp_path / "other.toml", + tmp_path / "scenario.toml", + ) + system_cfg.touch() + other_cfg.touch() + scenario_cfg.touch() + runner = CliRunner() + result = runner.invoke( + main, + ["run", "--system-config", str(system_cfg), "--test-scenario", str(scenario_cfg)], + env={"CLOUDAI_SYSTEM_CONFIG": str(other_cfg)}, + ) + assert "Missing 'scheduler' key" in result.output + assert str(system_cfg) in result.output + assert str(other_cfg) not in result.output + + +@pytest.mark.parametrize("subcommand", ["install", "uninstall", "dry-run", "run", "generate-report"]) +def test_system_config_env_var_documented_in_help(subcommand: str): + runner = CliRunner() + result = runner.invoke(main, [subcommand, "--help"]) + assert result.exit_code == 0 + assert "CLOUDAI_SYSTEM_CONFIG" in result.output + + @pytest.mark.parametrize("subcommand", ["install", "uninstall", "dry-run", "run", "generate-report"]) def test_hook_dir_is_available_on_run_commands(subcommand: str): runner = CliRunner()