|
6 | 6 |
|
7 | 7 | from pathlib import PurePath |
8 | 8 |
|
| 9 | +import manage.scriptutils as SU |
9 | 10 | from manage.scriptutils import ( |
10 | 11 | find_install_from_script, |
11 | 12 | _find_shebang_command, |
| 13 | + _parse_shebang, |
12 | 14 | _read_script, |
| 15 | + _replace_templates, |
13 | 16 | NewEncoding, |
14 | 17 | _maybe_quote, |
15 | 18 | quote_args, |
@@ -274,3 +277,61 @@ def test_quote_args(args, expect): |
274 | 277 | assert expect == quote_args(args) |
275 | 278 | # Test that our split function produces the same result |
276 | 279 | assert args == split_args(expect), expect |
| 280 | + |
| 281 | + |
| 282 | +@pytest.mark.parametrize("line, expect_id, expect_line", [pytest.param(*a, id=a[0]) for a in [ |
| 283 | + ("#!/usr/bin/python", "Test1", None), |
| 284 | + ("#!/usr/bin/python -Es", "Test1", None), |
| 285 | + ("#! /usr/bin/pythonw", "Test1", None), |
| 286 | + ("#! /usr/bin/python2", "Test2", None), |
| 287 | + ("#! /usr/bin/pythonw2", "Test2", None), |
| 288 | + ("#! /usr/bin/python3", "PythonCore3", None), |
| 289 | + ("#! /usr/bin/pythonw3", "PythonCore3", None), |
| 290 | + ("#! custom", None, "#!CUSTOM"), |
| 291 | + ("#! full line custom", None, "#!CUSTOM2"), |
| 292 | + ("#!full line custom with extra", None, None), |
| 293 | + ("custom", None, None), |
| 294 | + ("full line custom", None, None), |
| 295 | +]]) |
| 296 | +def test_shebang_templates(fake_config, line, expect_id, expect_line): |
| 297 | + fake_config.installs = [ |
| 298 | + dict(id="Test1", company="Test", tag="1", default=True), |
| 299 | + dict(id="Test2", company="Test", tag="2"), |
| 300 | + dict(id="Test3", company="Test", tag="3.2"), |
| 301 | + dict(id="PythonCore3", company="PythonCore", tag="3.2"), |
| 302 | + ] |
| 303 | + fake_config.shebang_templates = { |
| 304 | + "/usr/bin/python": "py", |
| 305 | + "/usr/bin/pythonw": "pyw", |
| 306 | + "/usr/bin/python2": "py -V:Test/2", |
| 307 | + "/usr/bin/pythonw2": "pyw -V:Test/2", |
| 308 | + "/usr/bin/python3": "py -3.2", |
| 309 | + "/usr/bin/pythonw3": "pyw -3.2", |
| 310 | + "custom": "CUSTOM", |
| 311 | + "full line custom": "CUSTOM2", |
| 312 | + } |
| 313 | + actual, actual_line = _replace_templates(fake_config, line, False) |
| 314 | + if expect_id: |
| 315 | + assert actual |
| 316 | + assert expect_id == actual["id"] |
| 317 | + elif expect_line: |
| 318 | + assert expect_line == actual_line |
| 319 | + else: |
| 320 | + assert not actual |
| 321 | + assert not actual_line |
| 322 | + |
| 323 | + |
| 324 | +def test_parse_shebang_templates(monkeypatch): |
| 325 | + class Cmd: |
| 326 | + shebang_templates = True |
| 327 | + |
| 328 | + expect = {"an": "install"} |
| 329 | + monkeypatch.setattr(SU, "_replace_templates", lambda *a: (expect, None)) |
| 330 | + actual = _parse_shebang(Cmd, "Anything at all") |
| 331 | + assert expect == actual |
| 332 | + |
| 333 | + expect = {"id": "COMMAND"} |
| 334 | + monkeypatch.setattr(SU, "_replace_templates", lambda *a: (None, "#!COMMAND")) |
| 335 | + monkeypatch.setattr(SU, "_find_shebang_command", lambda cmd, full_cmd, **kw: {"id": full_cmd}) |
| 336 | + actual = _parse_shebang(Cmd, "Anything at all", windowed=False) |
| 337 | + assert expect == actual |
0 commit comments