|
| 1 | +import textwrap |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import nox |
| 7 | + |
| 8 | + |
| 9 | +def test_load_pyproject(tmp_path: Path) -> None: |
| 10 | + filepath = tmp_path / "example.toml" |
| 11 | + filepath.write_text( |
| 12 | + """ |
| 13 | + [project] |
| 14 | + name = "hi" |
| 15 | + version = "1.0" |
| 16 | + dependencies = ["numpy", "requests"] |
| 17 | + """ |
| 18 | + ) |
| 19 | + |
| 20 | + toml = nox.project.load_toml(filepath) |
| 21 | + assert toml["project"]["dependencies"] == ["numpy", "requests"] |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("example", ["example.py", "example"]) |
| 25 | +def test_load_script_block(tmp_path: Path, example: str) -> None: |
| 26 | + filepath = tmp_path / example |
| 27 | + filepath.write_text( |
| 28 | + textwrap.dedent( |
| 29 | + """\ |
| 30 | + #!/usr/bin/env pipx run |
| 31 | + # /// script |
| 32 | + # requires-python = ">=3.11" |
| 33 | + # dependencies = [ |
| 34 | + # "requests<3", |
| 35 | + # "rich", |
| 36 | + # ] |
| 37 | + # /// |
| 38 | +
|
| 39 | + import requests |
| 40 | + from rich.pretty import pprint |
| 41 | +
|
| 42 | + resp = requests.get("https://peps.python.org/api/peps.json") |
| 43 | + data = resp.json() |
| 44 | + pprint([(k, v["title"]) for k, v in data.items()][:10]) |
| 45 | + """ |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + toml = nox.project.load_toml(filepath) |
| 50 | + assert toml["dependencies"] == ["requests<3", "rich"] |
| 51 | + |
| 52 | + |
| 53 | +def test_load_no_script_block(tmp_path: Path) -> None: |
| 54 | + filepath = tmp_path / "example.py" |
| 55 | + filepath.write_text( |
| 56 | + textwrap.dedent( |
| 57 | + """\ |
| 58 | + #!/usr/bin/python |
| 59 | +
|
| 60 | + import requests |
| 61 | + from rich.pretty import pprint |
| 62 | +
|
| 63 | + resp = requests.get("https://peps.python.org/api/peps.json") |
| 64 | + data = resp.json() |
| 65 | + pprint([(k, v["title"]) for k, v in data.items()][:10]) |
| 66 | + """ |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + with pytest.raises(ValueError, match="No script block found"): |
| 71 | + nox.project.load_toml(filepath) |
| 72 | + |
| 73 | + |
| 74 | +def test_load_multiple_script_block(tmp_path: Path) -> None: |
| 75 | + filepath = tmp_path / "example.py" |
| 76 | + filepath.write_text( |
| 77 | + textwrap.dedent( |
| 78 | + """\ |
| 79 | + # /// script |
| 80 | + # dependencies = [ |
| 81 | + # "requests<3", |
| 82 | + # "rich", |
| 83 | + # ] |
| 84 | + # /// |
| 85 | +
|
| 86 | + # /// script |
| 87 | + # requires-python = ">=3.11" |
| 88 | + # /// |
| 89 | +
|
| 90 | + import requests |
| 91 | + from rich.pretty import pprint |
| 92 | +
|
| 93 | + resp = requests.get("https://peps.python.org/api/peps.json") |
| 94 | + data = resp.json() |
| 95 | + pprint([(k, v["title"]) for k, v in data.items()][:10]) |
| 96 | + """ |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + with pytest.raises(ValueError, match="Multiple script blocks found"): |
| 101 | + nox.project.load_toml(filepath) |
| 102 | + |
| 103 | + |
| 104 | +def test_load_non_recongnised_extension(): |
| 105 | + msg = "Extension must be .py or .toml, got .txt" |
| 106 | + with pytest.raises(ValueError, match=msg): |
| 107 | + nox.project.load_toml("some.txt") |
0 commit comments