File: test_tomli_install.py

package info (click to toggle)
python-taskipy 1.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 436 kB
  • sloc: python: 1,262; makefile: 16; sh: 1
file content (49 lines) | stat: -rw-r--r-- 1,722 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
import subprocess
import sys
import unittest
from typing import Dict, List, Tuple


class TomliInstallTestCase(unittest.TestCase):
    def test_correct_tomli_version_installed(self):
        python_major, python_minor = self.__get_current_python_version()
        packages = self.__get_installed_pip_packages()
        tomli_version = packages.get('tomli')
        v1_regex = r'1.[0-9]+.[0-9]+'
        v2_regex = r'2.[0-9]+.[0-9]+'

        if python_major == 3 and python_minor == 6:
            self.assertRegex(tomli_version, v1_regex)
        elif python_major == 3 and python_minor >= 7:
            self.assertRegex(tomli_version, v2_regex)
        else:
            self.fail("Executed with invalid Python version")

    def __get_current_python_version(self) -> Tuple[int, int]:
        python_version = sys.version_info

        return python_version.major, python_version.minor

    def __get_installed_pip_packages(self) -> Dict[str, str]:
        cmd = "pip list"
        process = subprocess.Popen(
            cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        stdout, _ = process.communicate()
        exit_code = process.returncode
        if exit_code != 0:
            raise RuntimeError(
                'listing pip packages got a non-zero exit code'
            )

        result = {}
        packages = self.__remove_pip_list_table_header(stdout.splitlines())
        for package in packages:
            decoded_package = package.decode('utf-8')
            name, version, *_ = decoded_package.split()
            result[name] = version

        return result

    def __remove_pip_list_table_header(self, lines: List[bytes]) -> List[bytes]:
        return lines[2:]