| 1 | """Performance and import optimization regression tests.""" |
| 2 | |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import textwrap |
| 6 | |
| 7 | |
| 8 | def test_rich_not_imported_on_happy_path(): |
| 9 | """Ensure Rich library is not imported during happy path execution. |
| 10 | |
| 11 | Rich is only needed for error formatting and help display. |
| 12 | For successful CLI execution, it should not be imported to minimize startup time. |
| 13 | This test prevents regressions where Rich might accidentally get imported early. |
| 14 | """ |
| 15 | script = textwrap.dedent(""" |
| 16 | import sys |
| 17 | import cyclopts |
| 18 | |
| 19 | app = cyclopts.App(result_action="return_value") |
| 20 | |
| 21 | @app.default |
| 22 | def main(name: str = "world"): |
| 23 | return f"Hello, {name}!" |
| 24 | |
| 25 | # Execute happy path |
| 26 | sys.argv = ["test", "--name", "Alice"] |
| 27 | result = app() |
| 28 | |
| 29 | # Check Rich was not imported |
| 30 | assert "rich" not in sys.modules, f"Rich was imported! Modules: {[m for m in sys.modules if 'rich' in m]}" |
| 31 | print("SUCCESS: Rich not imported") |
| 32 | """) |
| 33 | |
| 34 | result = subprocess.run( |
| 35 | [sys.executable, "-c", script], |
| 36 | capture_output=True, |
| 37 | text=True, |
| 38 | ) |
| 39 | |
| 40 | assert result.returncode == 0, f"Script failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 41 | assert "SUCCESS: Rich not imported" in result.stdout |
| 42 | |
| 43 | |
| 44 | def test_docstring_parser_not_imported_on_happy_path(): |
| 45 | """Ensure docstring_parser is not imported during happy path execution. |
| 46 | |
| 47 | docstring_parser is only needed when parsing docstrings for help display. |
| 48 | For successful CLI execution, it should not be imported to minimize startup time. |
| 49 | This test prevents regressions where docstring_parser might accidentally get imported early. |
| 50 | """ |
| 51 | script = textwrap.dedent(""" |
| 52 | import sys |
| 53 | import cyclopts |
| 54 | |
| 55 | app = cyclopts.App(result_action="return_value") |
| 56 | |
| 57 | @app.default |
| 58 | def main(name: str = "world"): |
| 59 | '''A simple command. |
| 60 | |
| 61 | Parameters |
| 62 | ---------- |
| 63 | name |
| 64 | The name to greet. |
| 65 | ''' |
| 66 | return f"Hello, {name}!" |
| 67 | |
| 68 | # Execute happy path |
| 69 | sys.argv = ["test", "--name", "Alice"] |
| 70 | result = app() |
| 71 | assert result == "Hello, Alice!" |
| 72 | |
| 73 | # Check docstring_parser was not imported |
| 74 | assert "docstring_parser" not in sys.modules, f"docstring_parser was imported! Modules: {[m for m in sys.modules if 'docstring' in m]}" |
| 75 | print("SUCCESS: docstring_parser not imported") |
| 76 | """) |
| 77 | |
| 78 | result = subprocess.run( |
| 79 | [sys.executable, "-c", script], |
| 80 | capture_output=True, |
| 81 | text=True, |
| 82 | ) |
| 83 | |
| 84 | assert result.returncode == 0, f"Script failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 85 | assert "SUCCESS: docstring_parser not imported" in result.stdout |
| 86 | |
| 87 | |
| 88 | def test_lazy_modules_not_imported_on_happy_path(): |
| 89 | """Ensure lazy-loaded modules are not imported during happy path execution. |
| 90 | |
| 91 | Modules like types, validators, and _edit should only be imported when explicitly used. |
| 92 | This test prevents regressions where these might accidentally get eagerly imported. |
| 93 | """ |
| 94 | script = textwrap.dedent(""" |
| 95 | import sys |
| 96 | import cyclopts |
| 97 | |
| 98 | app = cyclopts.App(result_action="return_value") |
| 99 | |
| 100 | @app.default |
| 101 | def main(value: int = 42): |
| 102 | return value * 2 |
| 103 | |
| 104 | # Execute happy path |
| 105 | sys.argv = ["test", "--value", "21"] |
| 106 | result = app() |
| 107 | assert result == 42 |
| 108 | |
| 109 | # Check lazy modules were not imported |
| 110 | lazy_modules = ["cyclopts.types", "cyclopts.validators", "cyclopts._edit"] |
| 111 | imported_lazy = [m for m in lazy_modules if m in sys.modules] |
| 112 | assert not imported_lazy, f"Lazy modules were imported: {imported_lazy}" |
| 113 | print("SUCCESS: Lazy modules not imported") |
| 114 | """) |
| 115 | |
| 116 | result = subprocess.run( |
| 117 | [sys.executable, "-c", script], |
| 118 | capture_output=True, |
| 119 | text=True, |
| 120 | ) |
| 121 | |
| 122 | assert result.returncode == 0, f"Script failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 123 | assert "SUCCESS: Lazy modules not imported" in result.stdout |
| 124 | |
| 125 | |
| 126 | def test_rich_is_imported_on_error(): |
| 127 | """Verify Rich IS imported when displaying errors. |
| 128 | |
| 129 | This ensures the optimization didn't break error formatting. |
| 130 | """ |
| 131 | script = textwrap.dedent(""" |
| 132 | import sys |
| 133 | import cyclopts |
| 134 | |
| 135 | app = cyclopts.App() |
| 136 | |
| 137 | @app.default |
| 138 | def main(value: int): |
| 139 | return value |
| 140 | |
| 141 | # Execute with missing required argument (should error) |
| 142 | sys.argv = ["test"] |
| 143 | try: |
| 144 | app() |
| 145 | except SystemExit: |
| 146 | pass # Expected |
| 147 | |
| 148 | # Rich should now be imported for error display |
| 149 | assert "rich" in sys.modules, "Rich should be imported for error display" |
| 150 | print("SUCCESS: Rich imported for error") |
| 151 | """) |
| 152 | |
| 153 | result = subprocess.run( |
| 154 | [sys.executable, "-c", script], |
| 155 | capture_output=True, |
| 156 | text=True, |
| 157 | ) |
| 158 | |
| 159 | assert result.returncode == 0, f"Script failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 160 | assert "SUCCESS: Rich imported for error" in result.stdout |
| 161 | |
| 162 | |
| 163 | def test_lazy_modules_imported_when_accessed(): |
| 164 | """Verify lazy modules ARE imported when explicitly accessed. |
| 165 | |
| 166 | This ensures the lazy loading mechanism works correctly. |
| 167 | """ |
| 168 | script = textwrap.dedent(""" |
| 169 | import sys |
| 170 | import cyclopts |
| 171 | |
| 172 | # Initially not imported |
| 173 | assert "cyclopts.types" not in sys.modules |
| 174 | assert "cyclopts.validators" not in sys.modules |
| 175 | |
| 176 | # Access them |
| 177 | _ = cyclopts.types |
| 178 | _ = cyclopts.validators |
| 179 | |
| 180 | # Now they should be imported |
| 181 | assert "cyclopts.types" in sys.modules |
| 182 | assert "cyclopts.validators" in sys.modules |
| 183 | print("SUCCESS: Lazy modules imported on access") |
| 184 | """) |
| 185 | |
| 186 | result = subprocess.run( |
| 187 | [sys.executable, "-c", script], |
| 188 | capture_output=True, |
| 189 | text=True, |
| 190 | ) |
| 191 | |
| 192 | assert result.returncode == 0, f"Script failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 193 | assert "SUCCESS: Lazy modules imported on access" in result.stdout |