summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/tests/test_uninstall.py
blob: 2d0e896bb627871bce955ae9a62b392a7cbea5d0 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tests for the uninstall command."""
import os
import sys

from packaging.database import disable_cache, enable_cache
from packaging.run import main
from packaging.errors import PackagingError
from packaging.install import remove
from packaging.command.install_dist import install_dist

from packaging.tests import unittest, support

SETUP_CFG = """
[metadata]
name = %(name)s
version = %(version)s

[files]
packages =
    %(pkg)s
    %(pkg)s.sub
"""


class UninstallTestCase(support.TempdirManager,
                        support.LoggingCatcher,
                        support.EnvironRestorer,
                        unittest.TestCase):

    restore_environ = ['PLAT']

    def setUp(self):
        super(UninstallTestCase, self).setUp()
        self.addCleanup(setattr, sys, 'stdout', sys.stdout)
        self.addCleanup(setattr, sys, 'stderr', sys.stderr)
        self.addCleanup(os.chdir, os.getcwd())
        self.addCleanup(enable_cache)
        self.root_dir = self.mkdtemp()
        disable_cache()

    def run_setup(self, *args):
        # run setup with args
        args = ['run'] + list(args)
        dist = main(args)
        return dist

    def get_path(self, dist, name):
        cmd = install_dist(dist)
        cmd.prefix = self.root_dir
        cmd.finalize_options()
        return getattr(cmd, 'install_' + name)

    def make_dist(self, name='Foo', **kw):
        kw['name'] = name
        pkg = name.lower()
        if 'version' not in kw:
            kw['version'] = '0.1'
        project_dir, dist = self.create_dist(**kw)
        kw['pkg'] = pkg

        pkg_dir = os.path.join(project_dir, pkg)
        os.mkdir(pkg_dir)
        os.mkdir(os.path.join(pkg_dir, 'sub'))

        self.write_file((project_dir, 'setup.cfg'), SETUP_CFG % kw)
        self.write_file((pkg_dir, '__init__.py'), '#')
        self.write_file((pkg_dir, pkg + '_utils.py'), '#')
        self.write_file((pkg_dir, 'sub', '__init__.py'), '#')
        self.write_file((pkg_dir, 'sub', pkg + '_utils.py'), '#')

        return project_dir

    def install_dist(self, name='Foo', dirname=None, **kw):
        if not dirname:
            dirname = self.make_dist(name, **kw)
        os.chdir(dirname)
        dist = self.run_setup('install_dist', '--prefix=' + self.root_dir)
        install_lib = self.get_path(dist, 'purelib')
        return dist, install_lib

    def test_uninstall_unknow_distribution(self):
        self.assertRaises(PackagingError, remove, 'Foo',
                          paths=[self.root_dir])

    @unittest.skipIf(sys.platform == 'win32', 'deactivated for now')
    def test_uninstall(self):
        dist, install_lib = self.install_dist()
        self.assertIsFile(install_lib, 'foo', '__init__.py')
        self.assertIsFile(install_lib, 'foo', 'sub', '__init__.py')
        self.assertIsFile(install_lib, 'Foo-0.1.dist-info', 'RECORD')
        remove('Foo', paths=[install_lib])
        self.assertIsNotFile(install_lib, 'foo', 'sub', '__init__.py')
        self.assertIsNotFile(install_lib, 'Foo-0.1.dist-info', 'RECORD')


def test_suite():
    return unittest.makeSuite(UninstallTestCase)

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')