In my tests, I would like to run a command to make sure that the installed packages in my virtual environment match the packages found in Pipfile.lock
.
Is there a command like this?
pipenv checkifinstalled || exit 1
This problem can be reduced down to these two steps:
Convert Pipfile.lock
into a requirements.txt
file (in the format generated by pip freeze
).
pipenv requirements
(or pipenv requirements --dev
). (Note that this command is supported in pipenv >= 2022.4.8)Check that the installed packages match the generated requirements.txt
file.
Here is how I put it all together in a test:
import pkg_resources
import subprocess
import unittest
class DependencyChecks(unittest.TestCase):
def test_requirements_installed(self):
requirements_lines = subprocess.check_output(["pipenv", "requirements", "--dev"], text=True).splitlines()
req_lines = [line for line in requirements_lines if not line.startswith("-i ")]
requirements = pkg_resources.parse_requirements(req_lines)
for requirement in requirements:
req = str(requirement)
with self.subTest(requirement=req):
pkg_resources.require(req)