File: test_broken_project.py

package info (click to toggle)
scikit-build 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,792 kB
  • sloc: python: 5,258; cpp: 284; makefile: 171; f90: 12; sh: 7
file content (152 lines) | stat: -rw-r--r-- 4,612 bytes parent folder | download | duplicates (2)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""test_broken_cmakelists
----------------------------------

Tries to build the `fail-with-*-cmakelists` sample projects.  Ensures that the
attempt fails with a SystemExit exception that has an SKBuildError exception as
its value.
"""

from __future__ import annotations

from subprocess import CalledProcessError, run

import pytest

from skbuild.constants import CMAKE_DEFAULT_EXECUTABLE
from skbuild.exceptions import SKBuildError
from skbuild.platform_specifics import CMakeGenerator, get_platform
from skbuild.utils import push_dir

from . import project_setup_py_test, push_env


def test_cmakelists_with_fatalerror_fails(capfd):
    with push_dir():

        @project_setup_py_test("fail-with-fatal-error-cmakelists", ["build"], disable_languages_test=True)
        def should_fail():
            pass

        with pytest.raises(SystemExit) as excinfo:
            should_fail()

        e = excinfo.value
        assert isinstance(e.code, SKBuildError)

    _, err = capfd.readouterr()
    assert "Invalid CMakeLists.txt" in err
    assert "An error occurred while configuring with CMake." in str(e)


def test_cmakelists_with_syntaxerror_fails(capfd):
    with push_dir():

        @project_setup_py_test("fail-with-syntax-error-cmakelists", ["build"], disable_languages_test=True)
        def should_fail():
            pass

        with pytest.raises(SystemExit) as excinfo:
            should_fail()

        e = excinfo.value
        assert isinstance(e.code, SKBuildError)

    _, err = capfd.readouterr()
    assert 'Parse error.  Function missing ending ")"' in err
    assert "An error occurred while configuring with CMake." in str(e)


def test_hello_with_compileerror_fails(capfd):
    with push_dir():

        @project_setup_py_test("fail-hello-with-compile-error", ["build"])
        def should_fail():
            pass

        with pytest.raises(SystemExit) as excinfo:
            should_fail()

        e = excinfo.value
        assert isinstance(e.code, SKBuildError)

    out, err = capfd.readouterr()
    assert "_hello.cxx" in out or "_hello.cxx" in err
    assert "An error occurred while building with CMake." in str(e)


@pytest.mark.parametrize("exception", [CalledProcessError, OSError])
def test_invalid_cmake(exception, mocker):
    exceptions = {
        OSError: OSError("Unknown error"),
        CalledProcessError: CalledProcessError(1, [CMAKE_DEFAULT_EXECUTABLE, "--version"]),
    }

    run_original = run

    def run_mock(*args, **kwargs):
        if args[0] == [CMAKE_DEFAULT_EXECUTABLE, "--version"]:
            raise exceptions[exception]
        return run_original(*args, **kwargs)

    mocker.patch("skbuild.cmaker.subprocess.run", new=run_mock)

    with push_dir():

        @project_setup_py_test("hello-no-language", ["build"], disable_languages_test=True)
        def should_fail():
            pass

        with pytest.raises(SystemExit) as excinfo:
            should_fail()

        e = excinfo.value
        assert isinstance(e.code, SKBuildError)

    assert "Problem with the CMake installation, aborting build." in str(e)


def test_first_invalid_generator(mocker, capfd):
    platform = get_platform()
    default_generators = [CMakeGenerator("Invalid")]
    default_generators.extend(platform.default_generators)
    mocker.patch.object(
        type(platform), "default_generators", new_callable=mocker.PropertyMock, return_value=default_generators
    )

    mocker.patch("skbuild.cmaker.get_platform", return_value=platform)

    with push_dir(), push_env(CMAKE_GENERATOR=None):

        @project_setup_py_test("hello-no-language", ["build"])
        def run_build():
            pass

        run_build()

    _, err = capfd.readouterr()
    assert "CMake Error: Could not create named generator Invalid" in err


def test_invalid_generator(mocker, capfd):
    platform = get_platform()
    mocker.patch.object(
        type(platform), "default_generators", new_callable=mocker.PropertyMock, return_value=[CMakeGenerator("Invalid")]
    )
    mocker.patch("skbuild.cmaker.get_platform", return_value=platform)

    with push_dir(), push_env(CMAKE_GENERATOR=None):

        @project_setup_py_test("hello-no-language", ["build"])
        def should_fail():
            pass

        with pytest.raises(SystemExit) as excinfo:
            should_fail()

        e = excinfo.value
        assert isinstance(e.code, SKBuildError)

    _, err = capfd.readouterr()

    assert "CMake Error: Could not create named generator Invalid" in err
    assert "scikit-build could not get a working generator for your system. Aborting build." in str(e)