diff --git a/fle/run.py b/fle/run.py index 1419cac6c..35b0beb74 100644 --- a/fle/run.py +++ b/fle/run.py @@ -23,7 +23,56 @@ def fle_init(): sys.exit(1) +CLUSTER_SCENARIOS = ("open_world", "default_lab_scenario") + + +def _fle_cluster_python(args) -> None: + """Run `fle cluster` through the Python ClusterManager instead of run-envs.sh. + + Windows cannot execute the bash script directly (subprocess raises + `OSError: [WinError 193] %1 is not a valid Win32 application`), so on + Windows the CLI uses the same ClusterManager that `fle inspect-eval` + already uses to auto-start servers. Defaults and validation mirror + run-envs.sh. + """ + from fle.cluster.run_envs import ClusterManager + + command = (args.cluster_command if args else None) or "start" + if command == "help": + print( + "Usage: fle cluster [start|stop|restart|help] [-n NUMBER] [-s SCENARIO]\n" + " -n NUMBER Number of Factorio instances (1-33, default: 1)\n" + f" -s SCENARIO One of {', '.join(CLUSTER_SCENARIOS)} " + "(default: default_lab_scenario)" + ) + return + + manager = ClusterManager() + if command == "stop": + manager.stop() + elif command == "restart": + manager.restart() + else: + num_instances = args.n if args and args.n is not None else 1 + scenario = args.s if args and args.s else "default_lab_scenario" + if not 1 <= num_instances <= 33: + print( + "Error: Number of instances must be between 1 and 33.", file=sys.stderr + ) + sys.exit(1) + if scenario not in CLUSTER_SCENARIOS: + print( + f"Error: Scenario must be one of {', '.join(CLUSTER_SCENARIOS)}.", + file=sys.stderr, + ) + sys.exit(1) + manager.start(num_instances=num_instances, scenario=scenario) + + def fle_cluster(args): + if os.name == "nt": + _fle_cluster_python(args) + return cluster_path = Path(__file__).parent / "cluster" script = cluster_path / "run-envs.sh" if not script.exists(): diff --git a/tests/unit/test_cluster_cli.py b/tests/unit/test_cluster_cli.py new file mode 100644 index 000000000..7dde381b3 --- /dev/null +++ b/tests/unit/test_cluster_cli.py @@ -0,0 +1,73 @@ +"""`fle cluster` on Windows goes through ClusterManager instead of run-envs.sh. + +Windows cannot execute the bash script (WinError 193). These tests stub out +ClusterManager, so they need neither Docker nor a Factorio server. +""" + +import argparse +import types + +import pytest + +import fle.cluster.run_envs as run_envs +import fle.run as cli + + +class FakeManager: + calls: list = [] + + def start(self, num_instances, scenario): + FakeManager.calls.append(("start", num_instances, scenario)) + + def stop(self): + FakeManager.calls.append(("stop",)) + + def restart(self): + FakeManager.calls.append(("restart",)) + + +@pytest.fixture +def windows(monkeypatch): + """Pretend to run on Windows and record what the CLI asks the manager to do.""" + FakeManager.calls = [] + monkeypatch.setattr(run_envs, "ClusterManager", FakeManager) + monkeypatch.setattr(cli, "os", types.SimpleNamespace(name="nt")) + + def fail_subprocess(*a, **k): + raise AssertionError("run-envs.sh must not be executed on Windows") + + monkeypatch.setattr(cli.subprocess, "run", fail_subprocess) + return FakeManager.calls + + +def _args(command=None, n=None, s=None): + return argparse.Namespace(cluster_command=command, n=n, s=s) + + +def test_start_defaults_match_run_envs_sh(windows): + cli.fle_cluster(_args()) + assert windows == [("start", 1, "default_lab_scenario")] + + +def test_start_with_options(windows): + cli.fle_cluster(_args("start", n=3, s="open_world")) + assert windows == [("start", 3, "open_world")] + + +@pytest.mark.parametrize("command", ["stop", "restart"]) +def test_stop_and_restart(windows, command): + cli.fle_cluster(_args(command)) + assert windows == [(command,)] + + +def test_help_prints_usage(windows, capsys): + cli.fle_cluster(_args("help")) + assert "Usage: fle cluster" in capsys.readouterr().out + assert windows == [] + + +@pytest.mark.parametrize("n,s", [(0, None), (34, None), (1, "no_such_scenario")]) +def test_invalid_input_exits(windows, n, s): + with pytest.raises(SystemExit): + cli.fle_cluster(_args("start", n=n, s=s)) + assert windows == []