0

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
Flimm
  • 136,138
  • 45
  • 251
  • 267

1 Answers1

0

This problem can be reduced down to these two steps:

  1. Convert Pipfile.lock into a requirements.txt file (in the format generated by pip freeze).

    • This is easily done by running pipenv requirements (or pipenv requirements --dev). (Note that this command is supported in pipenv >= 2022.4.8)
  2. Check that the installed packages match the generated requirements.txt file.

Implementation:

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)
Flimm
  • 136,138
  • 45
  • 251
  • 267