File: test_utils.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 (226 lines) | stat: -rw-r--r-- 6,491 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""test_utils
------------------------

Tests for utils functions.
"""

from __future__ import annotations

import os

import pytest

from skbuild.utils import (
    PythonModuleFinder,
    mkdir_p,
    push_dir,
    to_platform_path,
    to_unix_path,
)

from . import SAMPLES_DIR, list_ancestors, push_env

saved_cwd = os.getcwd()


def setup_module():
    """setup any state specific to the execution of the given module."""
    os.chdir(os.path.dirname(__file__))


def teardown_module():
    """teardown any state that was previously setup with a setup_module
    method.
    """
    os.chdir(saved_cwd)


def test_push_dir(tmpdir):
    old_cwd = os.getcwd()
    try:
        level1 = tmpdir.mkdir("level1")
        level2 = level1.mkdir("level2")

        os.chdir(str(level2))
        assert os.path.split(os.getcwd())[-1] == "level2"

        # No directory
        with push_dir():
            os.chdir(os.path.join(os.getcwd(), ".."))
            assert os.path.split(os.getcwd())[-1] == "level1"
        assert os.path.split(os.getcwd())[-1] == "level2"

        # With existing directory
        with push_dir(directory=os.path.join(os.getcwd(), "..")):
            assert os.path.split(os.getcwd())[-1] == "level1"
        assert os.path.split(os.getcwd())[-1] == "level2"

        foo_directory = os.path.join(str(tmpdir), "foo")

        # With non existing directory
        failed = False
        try:
            with push_dir(directory=foo_directory):
                pass
        except OSError:
            failed = True
        assert failed
        assert not os.path.isdir(foo_directory)

        # With make_directory option
        with push_dir(directory=foo_directory, make_directory=True):
            assert os.getcwd() == foo_directory
        assert os.path.split(os.getcwd())[-1] == "level2"
        assert os.path.isdir(foo_directory)
    finally:
        os.chdir(old_cwd)


def test_push_dir_decorator(tmpdir):
    old_cwd = os.getcwd()
    try:
        level1 = tmpdir.mkdir("level1")
        level2 = level1.mkdir("level2")
        os.chdir(str(level2))
        assert os.path.split(os.getcwd())[-1] == "level2"

        # No directory
        @push_dir()
        def test_default():
            os.chdir(os.path.join(os.getcwd(), ".."))
            assert os.path.split(os.getcwd())[-1] == "level1"

        test_default()
        assert os.path.split(os.getcwd())[-1] == "level2"

        # With existing directory
        @push_dir(directory=os.path.join(os.getcwd(), ".."))
        def test():
            assert os.path.split(os.getcwd())[-1] == "level1"

        test()
        assert os.path.split(os.getcwd())[-1] == "level2"

        foo_directory = os.path.join(str(tmpdir), "foo")

        # With non existing directory
        failed = False
        try:

            @push_dir(directory=foo_directory)
            def test():
                pass

            test()
        except OSError:
            failed = True
        assert failed
        assert not os.path.isdir(foo_directory)

        # With make_directory option
        @push_dir(directory=foo_directory, make_directory=True)
        def test():
            assert os.getcwd() == foo_directory

        test()
        assert os.path.split(os.getcwd())[-1] == "level2"
        assert os.path.isdir(foo_directory)
    finally:
        os.chdir(old_cwd)


def test_mkdir_p(tmpdir):
    tmp_dir = str(tmpdir)
    assert os.path.isdir(tmp_dir)

    foo_bar_dir = os.path.join(tmp_dir, "foo", "bar")

    mkdir_p(foo_bar_dir)
    assert os.path.isdir(foo_bar_dir)

    # Make sure calling function twice does not raise an exception
    mkdir_p(foo_bar_dir)
    assert os.path.isdir(foo_bar_dir)


def test_push_env():
    assert "SKBUILD_NEW_VAR" not in os.environ

    os.environ["SKBUILD_ANOTHER_VAR"] = "abcd"
    assert "SKBUILD_ANOTHER_VAR" in os.environ

    saved_env = dict(os.environ)

    # Setting and un-setting variables can be done simultaneously
    with push_env(SKBUILD_NEW_VAR="1234", SKBUILD_ANOTHER_VAR=None):
        assert "SKBUILD_NEW_VAR" in os.environ
        assert "SKBUILD_ANOTHER_VAR" not in os.environ
        assert os.getenv("SKBUILD_NEW_VAR") == "1234"

    assert "SKBUILD_NEW_VAR" not in os.environ
    assert "SKBUILD_ANOTHER_VAR" in os.environ
    assert saved_env == dict(os.environ)

    # Trying to unset an unknown variable should be a no-op
    with push_env(SKBUILD_NOT_SET=None):
        assert saved_env == dict(os.environ)
    assert saved_env == dict(os.environ)

    # Calling without argument should be a no-op
    with push_env():
        assert saved_env == dict(os.environ)
    assert saved_env == dict(os.environ)


def test_python_module_finder():
    modules = PythonModuleFinder(["bonjour", "hello"], {}, []).find_all_modules(os.path.join(SAMPLES_DIR, "hello-cpp"))
    assert sorted(modules) == sorted(
        [
            ("bonjour", "__init__", to_platform_path("bonjour/__init__.py")),
            ("hello", "__init__", to_platform_path("hello/__init__.py")),
            ("hello", "__main__", to_platform_path("hello/__main__.py")),
        ]
    )


@pytest.mark.parametrize(
    ("input_path", "expected_path"),
    [
        (None, None),
        ("", ""),
        ("/bar/foo/baz", f"{os.sep}bar{os.sep}foo{os.sep}baz"),
        ("C:\\bar\\foo\\baz", f"C:{os.sep}bar{os.sep}foo{os.sep}baz"),
        ("C:\\bar/foo\\baz/", f"C:{os.sep}bar{os.sep}foo{os.sep}baz{os.sep}"),
    ],
)
def test_to_platform_path(input_path, expected_path):
    assert to_platform_path(input_path) == expected_path


@pytest.mark.parametrize(
    ("input_path", "expected_path"),
    [
        (None, None),
        ("", ""),
        ("/bar/foo/baz", "/bar/foo/baz"),
        ("C:\\bar\\foo\\baz", "C:/bar/foo/baz"),
        ("C:\\bar/foo\\baz/", "C:/bar/foo/baz/"),
    ],
)
def test_to_unix_path(input_path, expected_path):
    assert to_unix_path(input_path) == expected_path


@pytest.mark.parametrize(
    ("input_path", "expected_ancestors"),
    [
        ("", []),
        (".", []),
        ("part1/part2/part3/part4", ["part1/part2/part3", "part1/part2", "part1"]),
        ("part1\\part2\\part3\\part4", []),
        ("/part1/part2/part3/part4", ["/part1/part2/part3", "/part1/part2", "/part1", "/"]),
        ("C:/part1/part2/part3/part4", ["C:/part1/part2/part3", "C:/part1/part2", "C:/part1", "C:"]),
    ],
)
def test_list_ancestors(input_path, expected_ancestors):
    assert list_ancestors(input_path) == expected_ancestors