1# -*- coding: utf-8 -*-
2
3# Copyright (C) 2014-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
21
22import dnf.cli.commands.group as group
23import dnf.comps
24import dnf.exceptions
25from dnf.comps import CompsQuery
26from dnf.cli.option_parser import OptionParser
27
28import tests.support
29
30
31class GroupCommandStaticTest(tests.support.TestCase):
32
33    def test_canonical(self):
34        cmd = group.GroupCommand(tests.support.mock.MagicMock())
35
36        for args, out in [
37                (['grouplist', 'crack'], ['list', 'crack']),
38                (['groups'], ['summary']),
39                (['group', 'info', 'crack'], ['info', 'crack']),
40                (['group', 'update', 'crack'], ['upgrade', 'crack'])]:
41            parser = OptionParser()
42            parser.parse_main_args(args)
43            parser.parse_command_args(cmd, args)
44            cmd._canonical()
45            self.assertEqual(cmd.opts.subcmd, out[0])
46            self.assertEqual(cmd.opts.args, out[1:])
47
48    def test_split_extcmds(self):
49        cmd = group.GroupCommand(tests.support.mock.MagicMock())
50        cmd.base.conf = dnf.conf.Conf()
51        tests.support.command_run(cmd, ['install', 'crack'])
52        cmd.base.env_group_install.assert_called_with(
53            ['crack'], ('mandatory', 'default', 'conditional'),
54            cmd.base.conf.strict)
55
56
57class GroupCommandTest(tests.support.DnfBaseTestCase):
58
59    REPOS = ["main"]
60    COMPS = True
61    INIT_SACK = True
62
63    def setUp(self):
64        super(GroupCommandTest, self).setUp()
65        self.cmd = group.GroupCommand(self.base.mock_cli())
66        self.parser = OptionParser()
67
68    def test_environment_list(self):
69        env_inst, env_avail = self.cmd._environment_lists(['sugar*'])
70        self.assertLength(env_inst, 0)
71        self.assertLength(env_avail, 1)
72        self.assertEqual(env_avail[0].name, 'Sugar Desktop Environment')
73
74    def test_configure(self):
75        tests.support.command_configure(self.cmd, ['remove', 'crack'])
76        demands = self.cmd.cli.demands
77        self.assertTrue(demands.allow_erasing)
78        self.assertFalse(demands.freshest_metadata)
79
80
81class CompsQueryTest(tests.support.DnfBaseTestCase):
82
83    REPOS = []
84    COMPS = True
85
86    def test_all(self):
87        status_all = CompsQuery.AVAILABLE | CompsQuery.INSTALLED
88        kinds_all = CompsQuery.ENVIRONMENTS | CompsQuery.GROUPS
89        q = CompsQuery(self.comps, self.history, kinds_all, status_all)
90
91        res = q.get('sugar*', '*er*')
92        self.assertCountEqual(res.environments,
93                              ('sugar-desktop-environment',))
94        self.assertCountEqual(res.groups, ("Peppers", 'somerset'))
95
96    def test_err(self):
97        q = CompsQuery(self.comps, self.history, CompsQuery.ENVIRONMENTS,
98                       CompsQuery.AVAILABLE)
99        with self.assertRaises(dnf.exceptions.CompsError):
100            q.get('*er*')
101
102    def test_installed(self):
103        q = CompsQuery(self.comps, self.history, CompsQuery.GROUPS,
104                       CompsQuery.INSTALLED)
105        self.base.read_mock_comps(False)
106        grp = self.base.comps.group_by_pattern('somerset')
107        self.base.group_install(grp.id, ('mandatory',))
108
109        self._swdb_commit()
110
111        res = q.get('somerset')
112        self.assertEmpty(res.environments)
113        grp_ids = list(res.groups)
114        self.assertCountEqual(grp_ids, ('somerset',))
115