Debian Codegraph @

tests/sources/pip.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 #
14
15 # Pylint doesn't play well with fixtures and dependency injection from pytest
16 # pylint: disable=redefined-outer-name
17
18 import os
19 import pytest
20
21 from buildstream import _yaml
22 from buildstream.exceptions import ErrorDomain
23 from buildstream._testing import cli # pylint: disable=unused-import
24 from buildstream_plugins.sources.pip import _match_package_name
25
26 DATA_DIR = os.path.join(
27 os.path.dirname(os.path.realpath(__file__)),
28 "pip",
29 )
30
31
32 def generate_project(project_dir):
33 project_file = os.path.join(project_dir, "project.conf")
34 _yaml.roundtrip_dump(
35 {
36 "name": "foo",
37 "min-version": "2.0",
38 "plugins": [
39 {
40 "origin": "pip",
41 "package-name": "buildstream-plugins",
42 "sources": ["pip"],
43 }
44 ],
45 },
46 project_file,
47 )
48
49
50 # Test that without ref, consistency is set appropriately.
51 @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-ref"))
52 def test_no_ref(cli, datafiles):
53 project = str(datafiles)
54 generate_project(project)
55 assert cli.get_element_state(project, "target.bst") == "no reference"
56
57
58 # Test that pip is not allowed to be the first source
59 @pytest.mark.datafiles(os.path.join(DATA_DIR, "first-source-pip"))
60 def test_first_source(cli, datafiles):
61 project = str(datafiles)
62 generate_project(project)
63 result = cli.run(project=project, args=["show", "target.bst"])
64 result.assert_main_error(ErrorDomain.ELEMENT, None)
65
66
67 # Test that error is raised when neither packges nor requirements files
68 # have been specified
69 @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-packages"))
70 def test_no_packages(cli, datafiles):
71 project = str(datafiles)
72 generate_project(project)
73 result = cli.run(project=project, args=["show", "target.bst"])
74 result.assert_main_error(ErrorDomain.SOURCE, None)
75
76
77 # Test that pip source parses tar ball names correctly for the ref
78 @pytest.mark.parametrize(
79 "tarball, expected_name, expected_version",
80 [
81 ("dotted.package-0.9.8.tar.gz", "dotted.package", "0.9.8"),
82 ("hyphenated-package-2.6.0.tar.gz", "hyphenated-package", "2.6.0"),
83 ("underscore_pkg-3.1.0.tar.gz", "underscore_pkg", "3.1.0"),
84 ("numbers2and5-1.0.1.tar.gz", "numbers2and5", "1.0.1"),
85 (
86 "multiple.dots.package-5.6.7.tar.gz",
87 "multiple.dots.package",
88 "5.6.7",
89 ),
90 (
91 "multiple-hyphens-package-1.2.3.tar.gz",
92 "multiple-hyphens-package",
93 "1.2.3",
94 ),
95 (
96 "multiple_underscore_pkg-3.4.5.tar.gz",
97 "multiple_underscore_pkg",
98 "3.4.5",
99 ),
100 ("shortversion-1.0.tar.gz", "shortversion", "1.0"),
101 ("longversion-1.2.3.4.tar.gz", "longversion", "1.2.3.4"),
102 ],
103 )
104 def test_match_package_name(tarball, expected_name, expected_version):
105 name, version = _match_package_name(tarball)
106 assert (expected_name, expected_version) == (name, version)