fork/mons

git clone https://git.julia.blue/fork/mons

coloursofnoiseEnsure os.stat returns consistent result in mocked unit tests4540ca1

fixes
5.7 KiB200 linesraw
1import logging
2
3import pytest
4from click import ClickException
5
6from mons.commands import main
7from mons.version import Version
8
9
10@pytest.mark.parametrize(
11    "mock_filesystem",
12    [
13        pytest.param({}, id="empty_fs"),
14        pytest.param(
15            {"proj_1": ["unrelated.py", "different.csproj", "bin/"]},
16            id="no_matching_csproj",
17        ),
18    ],
19    indirect=True,
20)
21def test_determine_configuration_no_projects(mock_filesystem):
22    with pytest.raises(ClickException, match="No projects found"):
23        main.determine_configuration(mock_filesystem)
24
25
26# These both result in the same error, but from different places
27@pytest.mark.parametrize(
28    "mock_filesystem",
29    [
30        pytest.param(
31            {
32                "proj_1": [
33                    "proj_1.csproj",
34                ]
35            },
36            id="no bin/",
37        ),
38        pytest.param({"proj_1": ["proj_1.csproj", "bin/"]}, id="empty bin/"),
39    ],
40    indirect=True,
41)
42def test_determine_configuration_no_artifacts(mock_filesystem):
43    with pytest.raises(ClickException, match="No build artifacts"):
44        main.determine_configuration(mock_filesystem)
45
46
47@pytest.mark.mock_filesystem(
48    {
49        "proj_1": ["proj_1.csproj", {"bin": {"DEBUG": {"net452": "assembly.dll"}}}],
50        "proj_2": ["proj_2.csproj", {"bin": {"DEBUG": {"core": "assembly.dll"}}}],
51    }
52)
53def test_determine_configuration_no_common(caplog, mock_filesystem):
54    assert main.determine_configuration(mock_filesystem) is None
55    assert "No common output" in caplog.text
56
57
58@pytest.mark.mock_filesystem(
59    {
60        "proj_1": [
61            "proj_1.csproj",
62            {
63                "bin": {
64                    "DEBUG": {
65                        "common": "assembly.dll",
66                        "other": "assembly.dll",
67                    },
68                    "RELEASE": {"net452": "assembly.dll"},
69                }
70            },
71        ],
72        "proj_2": [
73            "proj_2.csproj",
74            {
75                "bin": {
76                    "DEBUG": {"common": "assembly.dll"},
77                    "RELEASE": {
78                        "net452": "assembly.dll",
79                        "other": "assembly.dll",
80                    },
81                }
82            },
83        ],
84    }
85)
86def test_determine_configuration_most_recent(caplog, mock_filesystem):
87    with caplog.at_level(logging.DEBUG, main.__name__):
88        assert main.determine_configuration(mock_filesystem) == "RELEASE/net452"
89        assert "Most recent" in caplog.text
90
91
92@pytest.mark.mock_filesystem(
93    {
94        "proj_1": [
95            "proj_1.csproj",
96            {
97                "bin": {
98                    "DEBUG": {
99                        "common": "assembly.dll",
100                        "other": "assembly.dll",
101                    },
102                    "RELEASE": {"net452": "assembly.dll"},
103                }
104            },
105        ],
106        "proj_2": [
107            "proj_2.csproj",
108            {
109                "bin": {
110                    "DEBUG": {"common": "assembly.dll"},
111                    "RELEASE": {
112                        "other": "assembly.dll",
113                    },
114                }
115            },
116        ],
117    }
118)
119def test_determine_configuration_one_shared(caplog, mock_filesystem):
120    with caplog.at_level(logging.DEBUG, main.__name__):
121        assert main.determine_configuration(mock_filesystem) == "DEBUG/common"
122        assert "Only one" in caplog.text
123
124
125def fetch_build_list(*args):
126    builds = [
127        {"branch": "dev", "version": 36},
128        {"branch": "beta", "version": 35},
129        {"branch": "dev", "version": 27},
130        {"branch": "dev", "version": 26},
131        {"branch": "beta", "version": 25},
132        {"branch": "stable", "version": 20},
133        {"branch": "dev", "version": 11},
134        {"branch": "stable", "version": 10},
135    ]
136    for build in builds:
137        build["mainDownload"] = ""
138        build["mainFileSize"] = 0
139    return builds
140
141
142def fetch_latest_build_azure(source):
143    if source == "refs/unknown":
144        return None
145    return 5
146
147
148def fetch_build_artifact_azure(build):
149    return ""
150
151
152def build(build: int):
153    return Version(1, build, 0)
154
155
156@pytest.mark.parametrize(
157    ("source", "result"),
158    [
159        pytest.param(build(20), build(20), id="stable version"),
160        pytest.param(build(25), build(35), id="beta version"),
161        # This test should pass, but currently only the minor version number is
162        # checked anyways.
163        pytest.param(Version(1, 20, 0, Tag="-beta+azure"), build(20)),
164        pytest.param("http://test.domain/file.zip", None, id="url"),
165        pytest.param(None, build(36), id="latest"),
166        pytest.param("refs/heads/stable", build(5), id="azure"),
167        pytest.param("stable", build(20)),
168        pytest.param("beta", build(35)),
169        pytest.param("27", build(27)),
170        pytest.param("1.20.0", build(20)),
171    ],
172)
173def test_fetch_artifact_source(ctx, source, result):
174    version, _ = main.fetch_artifact_source(ctx, source)
175    assert version == result
176
177
178@pytest.mark.parametrize(
179    "source",
180    [
181        # Currently only the minor version number is checked.
182        pytest.param(Version(2, 10, 7, 2), marks=pytest.mark.xfail(strict=True)),
183        ("refs/unknown"),
184        ("unknown_string"),
185        ("99"),
186        ("NOVERSION"),
187        # See above
188        pytest.param("4.25.8+github", marks=pytest.mark.xfail(strict=True)),
189    ],
190)
191def test_fetch_artifact_source_fail(ctx, source):
192    with pytest.raises(NotImplementedError):
193        main.fetch_artifact_source(ctx, source)
194
195
196# This occurrs when no VERSIONSPEC is supplied and the current
197# Everest branch cannot be determined
198def test_fetch_artifact_source_unknown_branch(ctx):
199    with pytest.raises(ClickException, match="Could not determine"):
200        main.fetch_artifact_source(ctx, build(99))