Blame tests/cli/commands/test_clean.py

Packit 6f3914
# -*- coding: utf-8 -*-
Packit 6f3914
Packit 6f3914
# Copyright (C) 2014-2018 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
Packit 6f3914
from __future__ import absolute_import
Packit 6f3914
from __future__ import unicode_literals
Packit 6f3914
Packit 6f3914
import os
Packit 6f3914
from io import StringIO
Packit 6f3914
Packit 6f3914
import dnf.cli.cli
Packit 6f3914
Packit 6f3914
import tests.support
Packit 6f3914
from tests.support import mock
Packit 6f3914
Packit 6f3914
'''
Packit 6f3914
def _run(cli, args):
Packit 6f3914
    with mock.patch('sys.stdout', new_callable=StringIO), \
Packit 6f3914
            mock.patch('dnf.rpm.detect_releasever', return_value=69):
Packit 6f3914
        cli.configure(['clean', '--config', '/dev/null'] + args)
Packit 6f3914
        cli.run()
Packit 6f3914
Packit 6f3914
Packit 6f3914
class CleanTest(tests.support.TestCase):
Packit 6f3914
    def setUp(self):
Packit 6f3914
        conf = dnf.conf.Conf()
Packit 6f3914
        base = tests.support.Base(conf)
Packit 6f3914
        base.repos.add(tests.support.MockRepo('main', conf))
Packit 6f3914
        base.conf.reposdir = '/dev/null'
Packit 6f3914
        base.conf.plugins = False
Packit 6f3914
        base.output = tests.support.MockOutput()
Packit 6f3914
Packit 6f3914
        repo = base.repos['main']
Packit 6f3914
        repo.baseurl = ['http:///dnf-test']
Packit 6f3914
        repo.basecachedir = base.conf.cachedir
Packit 6f3914
Packit 6f3914
        walk = [
Packit 6f3914
            (
Packit 6f3914
                repo.basecachedir,
Packit 6f3914
                [os.path.basename(repo._cachedir)],
Packit 6f3914
                [repo.id + '.solv'],
Packit 6f3914
            ),
Packit 6f3914
            (repo._cachedir, ['repodata', 'packages'], ['metalink.xml']),
Packit 6f3914
            (repo._cachedir + '/repodata', [], ['foo.xml', 'bar.xml.bz2']),
Packit 6f3914
            (repo._cachedir + '/packages', [], ['foo.rpm']),
Packit 6f3914
        ]
Packit 6f3914
        os.walk = self.walk = mock.Mock(return_value=walk)
Packit 6f3914
        self.base = base
Packit 6f3914
        self.cli = dnf.cli.cli.Cli(base)
Packit 6f3914
Packit 6f3914
    def tearDown(self):
Packit 6f3914
        self.base.close()
Packit 6f3914
Packit 6f3914
    def test_run(self):
Packit 6f3914
        with mock.patch('dnf.cli.commands.clean._clean') as _clean:
Packit 6f3914
            for args in [['all'],
Packit 6f3914
                         ['metadata'],
Packit 6f3914
                         ['metadata', 'packages'],
Packit 6f3914
                         ['metadata', 'packages', 'expire-cache'],
Packit 6f3914
                         ['dbcache'],
Packit 6f3914
                         ['expire-cache']]:
Packit 6f3914
                _run(self.cli, args)
Packit 6f3914
Packit 6f3914
        calls = [call[0] for call in _clean.call_args_list]
Packit 6f3914
        counts = (5, 4, 5, 5, 1, 0)
Packit 6f3914
        for call, count in zip(calls, counts):
Packit 6f3914
            files = list(call[1])
Packit 6f3914
            assert len(files) == count
Packit 6f3914
Packit 6f3914
    def test_walk_once(self):
Packit 6f3914
        _run(self.cli, ['all'])
Packit 6f3914
        assert len(self.walk.call_args_list) == 1
Packit 6f3914
Packit 6f3914
    def test_clean_local_repo(self):
Packit 6f3914
        cachedir = self.base.conf.cachedir
Packit 6f3914
        repo = self.base.repos['main']
Packit 6f3914
        repo.baseurl = ['file:///localrepo']
Packit 6f3914
Packit 6f3914
        _run(self.cli, ['all'])
Packit 6f3914
Packit 6f3914
        # Make sure we never looked outside the base cachedir
Packit 6f3914
        dirs = [call[0][0] for call in self.walk.call_args_list]
Packit 6f3914
        assert all(d == cachedir for d in dirs)
Packit 6f3914
'''