1# -*- coding: utf-8 -*-
2
3# Copyright (C) 2012-2018 Red Hat, Inc.
4#
5# This copyrighted material is made available to anyone wishing to use,
6# modify, copy, or redistribute it subject to the terms and conditions of
7# the GNU General Public License v.2, or (at your option) any later version.
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY expressed or implied, including the implied warranties of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11# Public License for more details.  You should have received a copy of the
12# GNU General Public License along with this program; if not, write to the
13# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
15# source code or documentation are not subject to the GNU General Public
16# License and may only be used or replicated with the express permission of
17# Red Hat, Inc.
18#
19
20from __future__ import absolute_import
21from __future__ import unicode_literals
22
23import itertools
24
25import libdnf.transaction
26
27import dnf.cli.commands
28
29import tests.support
30
31
32class Remove(tests.support.ResultTestCase):
33
34    REPOS = []
35
36    def setUp(self):
37        super(Remove, self).setUp()
38        self.allow_erasing = True
39
40    def test_not_installed(self):
41        """ Removing a not-installed package is a void operation. """
42        with self.assertRaises(dnf.exceptions.PackagesNotInstalledError) as context:
43            self.base.remove('mrkite')
44        self.assertEqual(context.exception.pkg_spec, 'mrkite')
45        installed_pkgs = self.base.sack.query().installed().run()
46        self.assertResult(self.base, installed_pkgs)
47
48    def test_remove(self):
49        """ Simple remove. """
50        self.base.remove("pepper")
51        self.assertResult(self.base,
52                          tests.support.installed_but(self.base.sack, "pepper"))
53
54    def test_remove_dependent(self):
55        """ Remove a lib that some other package depends on. """
56        self.base.remove("librita")
57        # we should end up with nothing in this case:
58        new_set = tests.support.installed_but(self.base.sack, "librita", "pepper")
59        self.assertResult(self.base, new_set)
60
61    def test_remove_nevra(self):
62        self.base.remove("pepper-20-0.x86_64")
63        pepper = self.base.sack.query().installed().filter(name="pepper")
64        (installed, removed) = self.installed_removed(self.base)
65        self.assertLength(installed, 0)
66        self.assertCountEqual(removed, pepper.run())
67
68    def test_remove_glob(self):
69        """ Test that weird input combinations with globs work. """
70        ret = self.base.remove("*.i686")
71        self.assertEqual(ret, 1)
72
73    def test_remove_provides(self):
74        """Remove uses provides too."""
75        self.assertEqual(1, self.base.remove('parking'))
76
77    def test_reponame(self):
78        """Test whether only packages from the repository are uninstalled."""
79        pkg_subj = dnf.subject.Subject('librita.x86_64')
80
81        tsis = []
82        for pkg in pkg_subj.get_best_query(self.base.sack).installed():
83            pkg._force_swdb_repoid = "main"
84            self.history.rpm.add_install(pkg)
85#            tsi = dnf.transaction.TransactionItem(
86#                dnf.transaction.INSTALL,
87#                installed=pkg,
88#                reason=libdnf.transaction.TransactionItemReason_USER
89#            )
90#            tsis.append(tsi)
91        self._swdb_commit(tsis)
92
93        self.base.remove('librita', 'main')
94        self.assertResult(self.base, itertools.chain(
95            self.base.sack.query().installed().filter(name__neq='librita'),
96            dnf.subject.Subject('librita.i686').get_best_query(self.base.sack).installed())
97        )
98