|
| 1 | +"""Tests for CLI output context disambiguation (ray start / ray up). |
| 2 | +
|
| 3 | +These tests directly import and call the output helper functions from |
| 4 | +``cli_output_helpers``, capturing ``cli_logger`` calls via mocks. |
| 5 | +
|
| 6 | +No Ray C++ runtime (``_raylet``) is required. The helpers module is |
| 7 | +imported by path to avoid triggering ``import ray``. |
| 8 | +
|
| 9 | +Validates that: |
| 10 | +1. ``print_next_steps_context_note`` emits "head node" and "cluster network". |
| 11 | +2. ``print_head_node_context_separator`` emits "head node" and "cluster network". |
| 12 | +3. ``USEFUL_COMMANDS_HEADING`` contains "local machine". |
| 13 | +4. Neither helper references ``ray up`` (valid for direct ``ray start`` use). |
| 14 | +""" |
| 15 | + |
| 16 | +import importlib.util |
| 17 | +import os |
| 18 | +import unittest |
| 19 | +from unittest.mock import MagicMock |
| 20 | + |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +# Import cli_output_helpers by file path so we bypass ``import ray`` entirely. |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | +_HELPERS_PATH = os.path.join( |
| 25 | + os.path.dirname(__file__), |
| 26 | + os.pardir, |
| 27 | + "autoscaler", |
| 28 | + "_private", |
| 29 | + "cli_output_helpers.py", |
| 30 | +) |
| 31 | +_spec = importlib.util.spec_from_file_location( |
| 32 | + "cli_output_helpers", os.path.abspath(_HELPERS_PATH) |
| 33 | +) |
| 34 | +_helpers = importlib.util.module_from_spec(_spec) |
| 35 | +_spec.loader.exec_module(_helpers) |
| 36 | + |
| 37 | +print_next_steps_context_note = _helpers.print_next_steps_context_note |
| 38 | +print_head_node_context_separator = _helpers.print_head_node_context_separator |
| 39 | +USEFUL_COMMANDS_HEADING = _helpers.USEFUL_COMMANDS_HEADING |
| 40 | + |
| 41 | + |
| 42 | +def _capture_printed_text(helper_fn): |
| 43 | + """Call *helper_fn(cli_logger, cf)* with mocks and return all text |
| 44 | + that was passed to ``cli_logger.print`` as a single concatenated string. |
| 45 | +
|
| 46 | + ``cf.dimmed`` is mocked as an identity function so the raw text |
| 47 | + passes through unchanged. |
| 48 | + """ |
| 49 | + mock_logger = MagicMock() |
| 50 | + mock_cf = MagicMock() |
| 51 | + # cf.dimmed should return its argument so we can inspect the raw text |
| 52 | + mock_cf.dimmed = lambda x: x |
| 53 | + |
| 54 | + helper_fn(mock_logger, mock_cf) |
| 55 | + |
| 56 | + parts = [] |
| 57 | + for c in mock_logger.print.call_args_list: |
| 58 | + args, _ = c |
| 59 | + parts.extend(str(a) for a in args) |
| 60 | + return "\n".join(parts) |
| 61 | + |
| 62 | + |
| 63 | +class TestNextStepsContextNote(unittest.TestCase): |
| 64 | + """Tests for ``print_next_steps_context_note``.""" |
| 65 | + |
| 66 | + def test_mentions_head_node(self): |
| 67 | + text = _capture_printed_text(print_next_steps_context_note) |
| 68 | + self.assertIn("head node", text) |
| 69 | + |
| 70 | + def test_mentions_cluster_network(self): |
| 71 | + text = _capture_printed_text(print_next_steps_context_note) |
| 72 | + self.assertIn("cluster network", text) |
| 73 | + |
| 74 | + def test_calls_newline(self): |
| 75 | + mock_logger = MagicMock() |
| 76 | + mock_cf = MagicMock() |
| 77 | + mock_cf.dimmed = lambda x: x |
| 78 | + print_next_steps_context_note(mock_logger, mock_cf) |
| 79 | + mock_logger.newline.assert_called() |
| 80 | + |
| 81 | + def test_does_not_mention_ray_up(self): |
| 82 | + text = _capture_printed_text(print_next_steps_context_note) |
| 83 | + self.assertNotIn("ray up", text) |
| 84 | + |
| 85 | + |
| 86 | +class TestHeadNodeContextSeparator(unittest.TestCase): |
| 87 | + """Tests for ``print_head_node_context_separator``.""" |
| 88 | + |
| 89 | + def test_mentions_head_node(self): |
| 90 | + text = _capture_printed_text(print_head_node_context_separator) |
| 91 | + self.assertIn("head node", text) |
| 92 | + |
| 93 | + def test_mentions_cluster_network(self): |
| 94 | + text = _capture_printed_text(print_head_node_context_separator) |
| 95 | + self.assertIn("cluster network", text) |
| 96 | + |
| 97 | + def test_prints_ascii_separator(self): |
| 98 | + text = _capture_printed_text(print_head_node_context_separator) |
| 99 | + # Should contain a run of ASCII dashes as a visual separator |
| 100 | + self.assertIn("----", text) |
| 101 | + |
| 102 | + def test_calls_newline(self): |
| 103 | + mock_logger = MagicMock() |
| 104 | + mock_cf = MagicMock() |
| 105 | + mock_cf.dimmed = lambda x: x |
| 106 | + print_head_node_context_separator(mock_logger, mock_cf) |
| 107 | + mock_logger.newline.assert_called() |
| 108 | + |
| 109 | + |
| 110 | +class TestUsefulCommandsHeading(unittest.TestCase): |
| 111 | + """Tests for the ``USEFUL_COMMANDS_HEADING`` constant.""" |
| 112 | + |
| 113 | + def test_contains_local_machine(self): |
| 114 | + self.assertIn("local machine", USEFUL_COMMANDS_HEADING) |
| 115 | + |
| 116 | + def test_does_not_use_old_label(self): |
| 117 | + # The old heading was exactly "Useful commands:" with no qualifier |
| 118 | + self.assertNotEqual(USEFUL_COMMANDS_HEADING, "Useful commands:") |
| 119 | + |
| 120 | + |
| 121 | +class TestHeadNodeContextGating(unittest.TestCase): |
| 122 | + """Verify that the head node context separator is only printed when |
| 123 | + `ray start` commands are actually executed. |
| 124 | + """ |
| 125 | + |
| 126 | + def test_separator_gated_by_ray_start_commands(self): |
| 127 | + import ast |
| 128 | + |
| 129 | + # Parse commands.py to verify the gating logic without importing it |
| 130 | + filepath = os.path.join( |
| 131 | + os.path.dirname(__file__), |
| 132 | + os.pardir, |
| 133 | + "autoscaler", |
| 134 | + "_private", |
| 135 | + "commands.py", |
| 136 | + ) |
| 137 | + with open(filepath, "r", encoding="utf-8") as f: |
| 138 | + source = f.read() |
| 139 | + |
| 140 | + tree = ast.parse(source) |
| 141 | + |
| 142 | + call_found = False |
| 143 | + # Find the call to print_head_node_context_separator |
| 144 | + for node in ast.walk(tree): |
| 145 | + if isinstance(node, ast.If): |
| 146 | + # Check if this If block contains our call |
| 147 | + for child in ast.walk(node): |
| 148 | + if isinstance(child, ast.Call) and getattr( |
| 149 | + child.func, "id", None |
| 150 | + ) == "print_head_node_context_separator": |
| 151 | + # Verify the condition of the If statement is ray_start_commands |
| 152 | + test_node = node.test |
| 153 | + self.assertIsInstance(test_node, ast.Name) |
| 154 | + self.assertEqual(test_node.id, "ray_start_commands") |
| 155 | + call_found = True |
| 156 | + |
| 157 | + self.assertTrue( |
| 158 | + call_found, "Could not find conditional call to print_head_node_context_separator" |
| 159 | + ) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + unittest.main() |
0 commit comments