Files
requirements/openstack_requirements/tests/test_project.py
Stephen Finucane f8b4fd224f Move parsing of setup.cfg, requirements.txt files
At least part of it anyway. Rather than waiting to split the lines and
strip the non-dependency information (comments etc) from the file, we do
it immediately on load.

We also add a whole lot of type hints to the modified code, to help us
reason about things a little easier.

Change-Id: Ide4574c53123ebe20d28211b80b9c079d01dd5e0
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
2025-10-28 17:22:08 +00:00

108 lines
3.3 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import textwrap
import fixtures
import testscenarios
import testtools
from openstack_requirements import project
from openstack_requirements.tests import common
load_tests = testscenarios.load_tests_apply_scenarios
class TestReadProject(testtools.TestCase):
def test_pyproject_toml(self):
root = self.useFixture(common.pep_518_fixture).root
proj = project.read(root)
self.assertEqual(proj['root'], root)
self.assertEqual(
list(sorted(proj['requirements'])),
['pyproject.toml'],
)
def test_setup_cfg(self):
root = self.useFixture(common.pbr_fixture).root
proj = project.read(root)
self.assertEqual(proj['root'], root)
self.assertEqual(
list(sorted(proj['requirements'])),
['requirements.txt', 'test-requirements.txt'],
)
def test_empty(self):
root = self.useFixture(fixtures.TempDir()).path
proj = project.read(root)
self.assertEqual(
proj,
{
'root': root,
'requirements': {},
'extras': {},
},
)
class TestProjectExtras(testtools.TestCase):
def test_pyproject_toml(self):
root = self.useFixture(fixtures.TempDir()).path
with open(os.path.join(root, 'pyproject.toml'), 'w') as fh:
fh.write(
textwrap.dedent("""
[project.optional-dependencies]
1 = [
"foo",
]
2 = [
"foo", # fred
"bar",
]
""")
)
expected = {'1': ['foo'], '2': ['foo', 'bar']}
self.assertEqual(expected, project._read_pyproject_toml_extras(root))
def test_setup_cfg(self):
root = self.useFixture(fixtures.TempDir()).path
with open(os.path.join(root, 'setup.cfg'), 'w') as fh:
fh.write(
textwrap.dedent("""
[extras]
1 =
foo
2 =
foo # fred
bar
""")
)
expected = {'1': ['foo'], '2': ['foo # fred', 'bar']}
self.assertEqual(expected, project._read_setup_cfg_extras(root))
def test_none(self):
root = self.useFixture(fixtures.TempDir()).path
with open(os.path.join(root, 'setup.cfg'), 'w') as fh:
fh.write(
textwrap.dedent("""
[metadata]
name = foo
""")
)
self.assertIsNone(project._read_setup_cfg_extras(root))
def test_no_setup_cfg(self):
root = self.useFixture(fixtures.TempDir()).path
self.assertIsNone(project._read_setup_cfg_extras(root))