|
| 1 | +"""Tests for the prefer_single_line option.""" |
| 2 | +import ast |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | + |
| 7 | +from python_minifier import minify, unparse |
| 8 | +from python_minifier.ast_annotation import add_parent |
| 9 | +from python_minifier.rename import add_namespace |
| 10 | + |
| 11 | +from subprocess_compat import run_subprocess, safe_decode |
| 12 | + |
| 13 | + |
| 14 | +# minify() tests |
| 15 | + |
| 16 | +def test_minify_default_uses_newlines(): |
| 17 | + """Default behavior uses newlines between module-level statements.""" |
| 18 | + source = ''' |
| 19 | +a = 1 |
| 20 | +b = 2 |
| 21 | +c = 3 |
| 22 | +''' |
| 23 | + expected = 'a=1\nb=2\nc=3' |
| 24 | + assert minify(source) == expected |
| 25 | + |
| 26 | + |
| 27 | +def test_minify_prefer_single_line_false_uses_newlines(): |
| 28 | + """prefer_single_line=False uses newlines between module-level statements.""" |
| 29 | + source = ''' |
| 30 | +a = 1 |
| 31 | +b = 2 |
| 32 | +c = 3 |
| 33 | +''' |
| 34 | + expected = 'a=1\nb=2\nc=3' |
| 35 | + assert minify(source, prefer_single_line=False) == expected |
| 36 | + |
| 37 | + |
| 38 | +def test_minify_prefer_single_line_true_uses_semicolons(): |
| 39 | + """prefer_single_line=True uses semicolons between module-level statements.""" |
| 40 | + source = ''' |
| 41 | +a = 1 |
| 42 | +b = 2 |
| 43 | +c = 3 |
| 44 | +''' |
| 45 | + expected = 'a=1;b=2;c=3' |
| 46 | + assert minify(source, prefer_single_line=True) == expected |
| 47 | + |
| 48 | + |
| 49 | +def test_minify_single_statement_no_trailing_separator(): |
| 50 | + """Single statement has no trailing separator regardless of option.""" |
| 51 | + source = 'a = 1' |
| 52 | + expected = 'a=1' |
| 53 | + assert minify(source, prefer_single_line=False) == expected |
| 54 | + assert minify(source, prefer_single_line=True) == expected |
| 55 | + |
| 56 | + |
| 57 | +def test_minify_empty_module(): |
| 58 | + """Empty module produces empty output.""" |
| 59 | + source = '' |
| 60 | + expected = '' |
| 61 | + assert minify(source, prefer_single_line=False) == expected |
| 62 | + assert minify(source, prefer_single_line=True) == expected |
| 63 | + |
| 64 | + |
| 65 | +def test_minify_function_body_uses_semicolons(): |
| 66 | + """Function body statements use semicolons regardless of option.""" |
| 67 | + source = ''' |
| 68 | +def f(): |
| 69 | + a = 1 |
| 70 | + b = 2 |
| 71 | + return a + b |
| 72 | +''' |
| 73 | + # Both produce identical output since the option only affects module level |
| 74 | + expected = 'def f():A=1;B=2;return A+B' |
| 75 | + assert minify(source, prefer_single_line=False) == expected |
| 76 | + assert minify(source, prefer_single_line=True) == expected |
| 77 | + |
| 78 | + |
| 79 | +def test_minify_class_body_uses_semicolons(): |
| 80 | + """Class body statements use semicolons regardless of option.""" |
| 81 | + source = ''' |
| 82 | +class C: |
| 83 | + a = 1 |
| 84 | + b = 2 |
| 85 | +''' |
| 86 | + # Both produce identical output since the option only affects module level |
| 87 | + expected = 'class C:a=1;b=2' |
| 88 | + assert minify(source, prefer_single_line=False) == expected |
| 89 | + assert minify(source, prefer_single_line=True) == expected |
| 90 | + |
| 91 | + |
| 92 | +def test_minify_mixed_module_and_function(): |
| 93 | + """Compound statements like def require newlines regardless of option.""" |
| 94 | + source = ''' |
| 95 | +x = 1 |
| 96 | +def f(): |
| 97 | + a = 1 |
| 98 | + b = 2 |
| 99 | +y = 2 |
| 100 | +''' |
| 101 | + # Both outputs are identical because compound statements require newlines |
| 102 | + expected = 'x=1\ndef f():A=1;B=2\ny=2' |
| 103 | + assert minify(source, prefer_single_line=False) == expected |
| 104 | + assert minify(source, prefer_single_line=True) == expected |
| 105 | + |
| 106 | + |
| 107 | +def test_minify_imports(): |
| 108 | + """Import statements respect prefer_single_line option.""" |
| 109 | + source = ''' |
| 110 | +import os |
| 111 | +import sys |
| 112 | +a = 1 |
| 113 | +''' |
| 114 | + # Imports are combined, module-level separator differs |
| 115 | + expected_newlines = 'import os,sys\na=1' |
| 116 | + expected_semicolons = 'import os,sys;a=1' |
| 117 | + assert minify(source, prefer_single_line=False) == expected_newlines |
| 118 | + assert minify(source, prefer_single_line=True) == expected_semicolons |
| 119 | + |
| 120 | + |
| 121 | +# unparse() tests |
| 122 | + |
| 123 | +def _prepare_module(source): |
| 124 | + """Parse and annotate a module for unparsing.""" |
| 125 | + module = ast.parse(source) |
| 126 | + add_parent(module) |
| 127 | + add_namespace(module) |
| 128 | + return module |
| 129 | + |
| 130 | + |
| 131 | +def test_unparse_default_uses_newlines(): |
| 132 | + """unparse() default uses newlines.""" |
| 133 | + source = 'a=1\nb=2' |
| 134 | + expected = 'a=1\nb=2' |
| 135 | + module = _prepare_module(source) |
| 136 | + assert unparse(module) == expected |
| 137 | + |
| 138 | + |
| 139 | +def test_unparse_prefer_single_line_false(): |
| 140 | + """unparse() with prefer_single_line=False uses newlines.""" |
| 141 | + source = 'a=1\nb=2' |
| 142 | + expected = 'a=1\nb=2' |
| 143 | + module = _prepare_module(source) |
| 144 | + assert unparse(module, prefer_single_line=False) == expected |
| 145 | + |
| 146 | + |
| 147 | +def test_unparse_prefer_single_line_true(): |
| 148 | + """unparse() with prefer_single_line=True uses semicolons.""" |
| 149 | + source = 'a=1\nb=2' |
| 150 | + expected = 'a=1;b=2' |
| 151 | + module = _prepare_module(source) |
| 152 | + assert unparse(module, prefer_single_line=True) == expected |
| 153 | + |
| 154 | + |
| 155 | +# CLI tests |
| 156 | + |
| 157 | +def test_cli_default_uses_newlines(): |
| 158 | + """CLI without --prefer-single-line uses newlines.""" |
| 159 | + code = 'a = 1\nb = 2\nc = 3' |
| 160 | + expected = 'a=1\nb=2\nc=3' |
| 161 | + |
| 162 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: |
| 163 | + f.write(code) |
| 164 | + temp_file = f.name |
| 165 | + |
| 166 | + try: |
| 167 | + result = run_subprocess([ |
| 168 | + sys.executable, '-m', 'python_minifier', temp_file |
| 169 | + ], timeout=30) |
| 170 | + |
| 171 | + assert result.returncode == 0 |
| 172 | + stdout_text = safe_decode(result.stdout) |
| 173 | + assert stdout_text == expected |
| 174 | + finally: |
| 175 | + os.unlink(temp_file) |
| 176 | + |
| 177 | + |
| 178 | +def test_cli_prefer_single_line_flag(): |
| 179 | + """CLI with --prefer-single-line uses semicolons.""" |
| 180 | + code = 'a = 1\nb = 2\nc = 3' |
| 181 | + expected = 'a=1;b=2;c=3' |
| 182 | + |
| 183 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: |
| 184 | + f.write(code) |
| 185 | + temp_file = f.name |
| 186 | + |
| 187 | + try: |
| 188 | + result = run_subprocess([ |
| 189 | + sys.executable, '-m', 'python_minifier', |
| 190 | + '--prefer-single-line', temp_file |
| 191 | + ], timeout=30) |
| 192 | + |
| 193 | + assert result.returncode == 0 |
| 194 | + stdout_text = safe_decode(result.stdout) |
| 195 | + assert stdout_text == expected |
| 196 | + finally: |
| 197 | + os.unlink(temp_file) |
| 198 | + |
| 199 | + |
| 200 | +def test_cli_stdin_prefer_single_line(): |
| 201 | + """CLI --prefer-single-line works with stdin.""" |
| 202 | + code = 'a = 1\nb = 2\nc = 3' |
| 203 | + expected = 'a=1;b=2;c=3' |
| 204 | + |
| 205 | + result = run_subprocess([ |
| 206 | + sys.executable, '-m', 'python_minifier', |
| 207 | + '--prefer-single-line', '-' |
| 208 | + ], input_data=code, timeout=30) |
| 209 | + |
| 210 | + assert result.returncode == 0 |
| 211 | + stdout_text = safe_decode(result.stdout) |
| 212 | + assert stdout_text == expected |
0 commit comments