|
1 | 1 | import pytest |
2 | | -from inputimeout import TimeoutOccurred |
| 2 | +import select |
| 3 | +import sys |
3 | 4 |
|
4 | 5 | import pybamm |
5 | 6 | import uuid |
@@ -33,24 +34,73 @@ def test_write_read_uuid(self, tmp_path, write_opt_in): |
33 | 34 | assert config_dict["enable_telemetry"] is False |
34 | 35 |
|
35 | 36 | @pytest.mark.parametrize("user_opted_in, user_input", [(True, "y"), (False, "n")]) |
36 | | - def test_ask_user_opt_in(self, monkeypatch, user_opted_in, user_input): |
37 | | - # Mock the inputimeout function to return invalid input first, then valid input |
38 | | - inputs = iter(["invalid", user_input]) |
39 | | - monkeypatch.setattr( |
40 | | - "pybamm.config.inputimeout", lambda prompt, timeout: next(inputs) |
41 | | - ) |
| 37 | + def test_ask_user_opt_in(self, monkeypatch, capsys, user_opted_in, user_input): |
| 38 | + # Mock select.select to simulate user input |
| 39 | + def mock_select(*args, **kwargs): |
| 40 | + return [sys.stdin], [], [] |
| 41 | + |
| 42 | + monkeypatch.setattr(select, "select", mock_select) |
| 43 | + |
| 44 | + # Mock sys.stdin.readline to return the desired input |
| 45 | + monkeypatch.setattr(sys.stdin, "readline", lambda: user_input + "\n") |
42 | 46 |
|
43 | 47 | # Call the function to ask the user if they want to opt in |
44 | 48 | opt_in = pybamm.config.ask_user_opt_in() |
| 49 | + |
| 50 | + # Check the result |
45 | 51 | assert opt_in is user_opted_in |
46 | 52 |
|
47 | | - def test_ask_user_opt_in_timeout(self, monkeypatch): |
48 | | - # Mock the inputimeout function to raise a TimeoutOccurred exception |
49 | | - def mock_inputimeout(*args, **kwargs): |
50 | | - raise TimeoutOccurred |
| 53 | + # Check that the prompt was printed |
| 54 | + captured = capsys.readouterr() |
| 55 | + assert "Do you want to enable telemetry? (Y/n):" in captured.out |
| 56 | + |
| 57 | + def test_ask_user_opt_in_invalid_input(self, monkeypatch, capsys): |
| 58 | + # Mock select.select to simulate user input and then timeout |
| 59 | + def mock_select(*args, **kwargs): |
| 60 | + nonlocal call_count |
| 61 | + if call_count == 0: |
| 62 | + call_count += 1 |
| 63 | + return [sys.stdin], [], [] |
| 64 | + else: |
| 65 | + return [], [], [] |
51 | 66 |
|
52 | | - monkeypatch.setattr("pybamm.config.inputimeout", mock_inputimeout) |
| 67 | + monkeypatch.setattr(select, "select", mock_select) |
| 68 | + |
| 69 | + # Mock sys.stdin.readline to return invalid input |
| 70 | + monkeypatch.setattr(sys.stdin, "readline", lambda: "invalid\n") |
| 71 | + |
| 72 | + # Initialize call count |
| 73 | + call_count = 0 |
53 | 74 |
|
54 | 75 | # Call the function to ask the user if they want to opt in |
55 | | - opt_in = pybamm.config.ask_user_opt_in() |
| 76 | + opt_in = pybamm.config.ask_user_opt_in(timeout=1) |
| 77 | + |
| 78 | + # Check the result (should be False for timeout after invalid input) |
56 | 79 | assert opt_in is False |
| 80 | + |
| 81 | + # Check that the prompt, invalid input message, and timeout message were printed |
| 82 | + captured = capsys.readouterr() |
| 83 | + assert "Do you want to enable telemetry? (Y/n):" in captured.out |
| 84 | + assert ( |
| 85 | + "Invalid input. Please enter 'yes/y' for yes or 'no/n' for no." |
| 86 | + in captured.out |
| 87 | + ) |
| 88 | + assert "Timeout reached. Defaulting to not enabling telemetry." in captured.out |
| 89 | + |
| 90 | + def test_ask_user_opt_in_timeout(self, monkeypatch, capsys): |
| 91 | + # Mock select.select to simulate a timeout |
| 92 | + def mock_select(*args, **kwargs): |
| 93 | + return [], [], [] |
| 94 | + |
| 95 | + monkeypatch.setattr(select, "select", mock_select) |
| 96 | + |
| 97 | + # Call the function to ask the user if they want to opt in |
| 98 | + opt_in = pybamm.config.ask_user_opt_in(timeout=1) |
| 99 | + |
| 100 | + # Check the result (should be False for timeout) |
| 101 | + assert opt_in is False |
| 102 | + |
| 103 | + # Check that the prompt and timeout message were printed |
| 104 | + captured = capsys.readouterr() |
| 105 | + assert "Do you want to enable telemetry? (Y/n):" in captured.out |
| 106 | + assert "Timeout reached. Defaulting to not enabling telemetry." in captured.out |
0 commit comments