|
| 1 | +import sys |
| 2 | +import textwrap |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from robotpy import main |
| 7 | + |
| 8 | + |
| 9 | +class DummyWpilib: |
| 10 | + class RobotBase: |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +def mock_wpilib(monkeypatch): |
| 16 | + monkeypatch.setitem(sys.modules, "wpilib", DummyWpilib) |
| 17 | + |
| 18 | + |
| 19 | +def test_load_robot_class_exact_case(tmp_path, monkeypatch, capsys): |
| 20 | + # create correct-case file |
| 21 | + robot_py = tmp_path / "robot.py" |
| 22 | + robot_py.write_text( |
| 23 | + textwrap.dedent( |
| 24 | + """ |
| 25 | + import wpilib |
| 26 | + class MyRobot(wpilib.RobotBase): |
| 27 | + pass |
| 28 | + """ |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + monkeypatch.setattr(main, "robot_py_path", robot_py) |
| 33 | + |
| 34 | + # Should pass (exact case) |
| 35 | + main._load_robot_class() |
| 36 | + |
| 37 | + |
| 38 | +def test_load_robot_class_wrong_case(tmp_path, monkeypatch, capsys): |
| 39 | + robot_py = tmp_path / "robot.py" |
| 40 | + Robot_py = tmp_path / "Robot.py" |
| 41 | + Robot_py.write_text( |
| 42 | + textwrap.dedent( |
| 43 | + """ |
| 44 | + import wpilib |
| 45 | + class MyRobot(wpilib.RobotBase): |
| 46 | + pass |
| 47 | + """ |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + case_insensitive_fs = robot_py.exists() |
| 52 | + |
| 53 | + monkeypatch.setattr(main, "robot_py_path", robot_py) |
| 54 | + |
| 55 | + with pytest.raises(SystemExit): |
| 56 | + main._load_robot_class() |
| 57 | + err = capsys.readouterr().err |
| 58 | + |
| 59 | + if case_insensitive_fs: |
| 60 | + assert "must be robot.py" in err |
| 61 | + else: |
| 62 | + assert "does not exist" in err |
0 commit comments