File: test_sp.py

package info (click to toggle)
software-properties 0.111-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,944 kB
  • sloc: python: 8,238; makefile: 19; sh: 18; xml: 10
file content (61 lines) | stat: -rw-r--r-- 1,766 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
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import apt_pkg
import aptsources

import logging
import os
import shutil
import sys
import tempfile
import unittest

from unittest.mock import patch

sys.path.insert(0, "../")
from softwareproperties.SoftwareProperties import (
    SoftwareProperties)

class SoftwarePropertiesTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        for k in apt_pkg.config.keys():
            apt_pkg.config.clear(k)
        apt_pkg.init()
        cls.get_distro = aptsources.distro.get_distro
        aptsources.distro.get_distro = lambda *a, **b: aptsources.distro.UbuntuDistribution(id="Ubuntu", codename="bionic", description="18.04", release="bionic")

    @classmethod
    def tearDownClass(cls):
        aptsources.distro.get_distro = cls.get_distro

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.sp = SoftwareProperties()
        self.mock_key = os.path.join(self.temp_dir, u"määäp.asc")
        with open(self.mock_key, "wb") as fp:
            fp.write(u"bäää".encode("utf-8"))

    def tearDown(self):
        shutil.rmtree(self.temp_dir)

    def test_add_key_str(self):
        with patch.object(self.sp, "apt_key") as mock_apt_key:
            self.sp.add_key(self.mock_key)
        self.assertTrue(mock_apt_key.add.called)

    def test_add_key_bytes(self):
        with patch.object(self.sp, "apt_key") as mock_apt_key:
            mock_name_as_bytes = self.mock_key.encode("utf-8")
            self.sp.add_key(mock_name_as_bytes)
        self.assertTrue(mock_apt_key.add.called)


if __name__ == "__main__":
    if "-d" in sys.argv:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    unittest.main()