File: test_toml.py

package info (click to toggle)
python-nox 2024.04.15-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,724 kB
  • sloc: python: 7,579; makefile: 194; sh: 6
file content (107 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import textwrap
from pathlib import Path

import pytest

import nox


def test_load_pyproject(tmp_path: Path) -> None:
    filepath = tmp_path / "example.toml"
    filepath.write_text(
        """
        [project]
        name = "hi"
        version = "1.0"
        dependencies = ["numpy", "requests"]
        """
    )

    toml = nox.project.load_toml(filepath)
    assert toml["project"]["dependencies"] == ["numpy", "requests"]


@pytest.mark.parametrize("example", ["example.py", "example"])
def test_load_script_block(tmp_path: Path, example: str) -> None:
    filepath = tmp_path / example
    filepath.write_text(
        textwrap.dedent(
            """\
            #!/usr/bin/env pipx run
            # /// script
            # requires-python = ">=3.11"
            # dependencies = [
            #   "requests<3",
            #   "rich",
            # ]
            # ///

            import requests
            from rich.pretty import pprint

            resp = requests.get("https://peps.python.org/api/peps.json")
            data = resp.json()
            pprint([(k, v["title"]) for k, v in data.items()][:10])
            """
        )
    )

    toml = nox.project.load_toml(filepath)
    assert toml["dependencies"] == ["requests<3", "rich"]


def test_load_no_script_block(tmp_path: Path) -> None:
    filepath = tmp_path / "example.py"
    filepath.write_text(
        textwrap.dedent(
            """\
            #!/usr/bin/python

            import requests
            from rich.pretty import pprint

            resp = requests.get("https://peps.python.org/api/peps.json")
            data = resp.json()
            pprint([(k, v["title"]) for k, v in data.items()][:10])
            """
        )
    )

    with pytest.raises(ValueError, match="No script block found"):
        nox.project.load_toml(filepath)


def test_load_multiple_script_block(tmp_path: Path) -> None:
    filepath = tmp_path / "example.py"
    filepath.write_text(
        textwrap.dedent(
            """\
            # /// script
            # dependencies = [
            #   "requests<3",
            #   "rich",
            # ]
            # ///

            # /// script
            # requires-python = ">=3.11"
            # ///

            import requests
            from rich.pretty import pprint

            resp = requests.get("https://peps.python.org/api/peps.json")
            data = resp.json()
            pprint([(k, v["title"]) for k, v in data.items()][:10])
            """
        )
    )

    with pytest.raises(ValueError, match="Multiple script blocks found"):
        nox.project.load_toml(filepath)


def test_load_non_recongnised_extension():
    msg = "Extension must be .py or .toml, got .txt"
    with pytest.raises(ValueError, match=msg):
        nox.project.load_toml("some.txt")