当前位置: 首页>>代码示例>>Python>>正文


Python mock.patch函数代码示例

本文整理汇总了Python中tests.support.mock.patch函数的典型用法代码示例。如果您正苦于以下问题:Python patch函数的具体用法?Python patch怎么用?Python patch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了patch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_frontend_none

    def test_frontend_none(self):
        c_path, c_content = self._create_conf()
        new_path, new_content = self._create_rpmnew()

        with self.rpmconf_plugin as rpmconf, mock.patch(
            "rpmconf.rpmconf.RpmConf.flush_input", return_value="M"
        ), mock.patch("sys.stdout", new_callable=StringIO), mock.patch.dict("os.environ"):
            if os.environ.get("MERGE"):
                del os.environ["MERGE"]
            try:
                rpmconf.frontent = "env"
                rpmconf.run()
            except SystemExit as e:
                if e.code in (errno.ENOENT, errno.EINTR):
                    self.fail("rpmconf has exited prematurely in the merge phase")
                else:
                    self.fail("rpmconf has exited prematurely" "with unknown exit code {0}".format(safe_repr(e.code)))

        self.assertTrue(os.access(c_path, os.F_OK))
        with open(c_path, "rb") as f:
            c_result = f.read()

        self.assertEqual(c_result, c_content)

        self.assertTrue(os.access(new_path, os.F_OK))
        with open(new_path, "rb") as f:
            new_result = f.read()

        self.assertEqual(new_result, new_content)
开发者ID:pghmcfc,项目名称:dnf-plugins-extras,代码行数:29,代码来源:test_rpmconf.py

示例2: test_multi

    def test_multi(self):
        now = 1379406823.9
        fo = MockStdout()
        with mock.patch('dnf.cli.progress._term_width', return_value=60), \
             mock.patch('dnf.cli.progress.time', lambda: now):

            p = dnf.cli.progress.MultiFileProgressMeter(fo)
            p.start(2, 30)
            pload1 = FakePayload('foo', 10.0)
            pload2 = FakePayload('bar', 20.0)
            for i in range(11):
                p.progress(pload1, float(i))
                if i == 10:
                    p.end(pload1, None, None)
                now += 0.5

                p.progress(pload2, float(i*2))
                self.assertEqual(self._REFERENCE_TAB[i], fo.visible_lines())
                if i == 10:
                    p.end(pload2, dnf.callback.STATUS_FAILED, 'some error')
                now += 0.5

        # check "end" events
        self.assertEqual(fo.visible_lines(), [
            '(1/2): foo                  1.0  B/s |  10  B     00:10    ',
            '[FAILED] bar: some error                                   '])
        self.assertTrue(2.0 < p.rate < 4.0)
开发者ID:haghabozorgi,项目名称:dnf,代码行数:27,代码来源:test_cli_progress.py

示例3: test_print_versions

 def test_print_versions(self):
     base = support.MockBase()
     output = support.MockOutput()
     with mock.patch("sys.stdout") as stdout, mock.patch("dnf.sack.rpmdb_sack", return_value=base.sack):
         dnf.cli.cli.print_versions(["pepper", "tour"], base, output)
     written = "".join([mc[1][0] for mc in stdout.method_calls if mc[0] == "write"])
     self.assertEqual(written, VERSIONS_OUTPUT)
开发者ID:hutarova,项目名称:dnf,代码行数:7,代码来源:test_cli.py

示例4: test_print_versions

 def test_print_versions(self):
     output = tests.support.MockOutput()
     with mock.patch('sys.stdout') as stdout,\
             mock.patch('dnf.sack._rpmdb_sack', return_value=self.base.sack):
         dnf.cli.cli.print_versions(['pepper', 'tour'], self.base, output)
     written = ''.join([mc[1][0] for mc in stdout.method_calls
                        if mc[0] == 'write'])
     self.assertEqual(written, VERSIONS_OUTPUT)
开发者ID:rpm-software-management,项目名称:dnf,代码行数:8,代码来源:test_cli.py

示例5: __enter__

    def __enter__(self):
        self._patches = [
            mock.patch("rpm.TransactionSet.dbMatch", return_value=self.packages),
            mock.patch("rpm.fi", return_value=((self._conf_file, None, None, None, 1),)),
        ]
        for patch in self._patches:
            patch.__enter__()

        return self
开发者ID:pghmcfc,项目名称:dnf-plugins-extras,代码行数:9,代码来源:test_rpmconf.py

示例6: test_fit_lock_dir

 def test_fit_lock_dir(self):
     orig = '/some'
     with mock.patch('dnf.util.am_i_root', return_value=True):
         self.assertEqual(dnf.lock._fit_lock_dir(orig), '/some')
     with mock.patch('dnf.util.am_i_root', return_value=False):
         dir_ = dnf.lock._fit_lock_dir(orig)
         match = re.match(
             r'/run/user/\d+/dnf/8e58d9adbd213a8b602f30604a8875f2', dir_)
         self.assertTrue(match)
开发者ID:IMFTC,项目名称:dnf,代码行数:9,代码来源:test_lock.py

示例7: test_motd

 def test_motd(self):
     m = mock_open()
     with mock.patch('dnf.automatic.emitter.open', m, create=True):
         emitter = dnf.automatic.emitter.MotdEmitter('myhost')
         emitter.notify_available('packages...')
         emitter.notify_downloaded()
         with mock.patch('dnf.automatic.emitter.DOWNLOADED', 'downloaded on %s:'):
             emitter.commit()
         handle = m()
         handle.write.assert_called_once_with(MSG)
开发者ID:mavit,项目名称:dnf,代码行数:10,代码来源:test_emitter.py

示例8: test_clean_files_local

 def test_clean_files_local(self):
     """Do not delete files from a local repo."""
     base = tests.support.MockBase("main")
     repo = base.repos['main']
     repo.baseurl = ['file:///dnf-bad-test']
     repo.basecachedir = '/tmp/dnf-bad-test'
     with mock.patch('dnf.cli.commands.clean._clean_filelist'),\
          mock.patch('os.path.exists', return_value=True) as exists_mock:
         dnf.cli.commands.clean._clean_files(base.repos, ['rpm'], 'pkgdir',
                                             'package')
     # local repo is not even checked for directory existence:
     self.assertIsNone(exists_mock.call_args)
开发者ID:IMFTC,项目名称:dnf,代码行数:12,代码来源:test_clean.py

示例9: test_non_interactive

    def test_non_interactive(self):
        c_path, c_content = self._create_conf()
        new_path, new_content = self._create_rpmnew()

        with self.rpmconf_plugin as rpmconf, mock.patch(
            "rpmconf.rpmconf.RpmConf.flush_input", return_value="S"
        ), mock.patch("sys.stdout", new_callable=StringIO) as stdout:
            rpmconf._interactive = False
            rpmconf.run()

            lines = stdout.getvalue().splitlines()

        self.assertEqual(len(lines), 0)
开发者ID:pghmcfc,项目名称:dnf-plugins-extras,代码行数:13,代码来源:test_rpmconf.py

示例10: test_clean_binary_cache

 def test_clean_binary_cache(self):
     base = tests.support.MockBase('main')
     with mock.patch('os.access', return_value=True) as access,\
          mock.patch('dnf.cli.commands.clean._clean_filelist'):
         clean._clean_binary_cache(base.repos, base.conf.cachedir)
     self.assertEqual(len(access.call_args_list), 3)
     fname = access.call_args_list[0][0][0]
     assert fname.startswith(dnf.const.TMPDIR)
     assert fname.endswith(hawkey.SYSTEM_REPO_NAME + '.solv')
     fname = access.call_args_list[1][0][0]
     assert fname.endswith('main.solv')
     fname = access.call_args_list[2][0][0]
     assert fname.endswith('main-filenames.solvx')
开发者ID:IMFTC,项目名称:dnf,代码行数:13,代码来源:test_clean.py

示例11: test_pkg_option

 def test_pkg_option(self):
     args = ["--pkg", "foo"]
     self.cmd.base.add_remote_rpms([os.path.join(self.path, "noarch/foo-4-6.noarch.rpm")])
     with mock.patch("sys.stdout", new_callable=dnf.pycomp.StringIO) as stdout:
         support.command_run(self.cmd, args)
         expected_out = ["package: foo-4-6.noarch from @commandline",
                         "  unresolved deps:",
                         "    bar = 4-6"]
         self.assertEqual(stdout.getvalue()[:-1], "\n".join(expected_out))
     args = ["--pkg", "bar"]
     with mock.patch("sys.stdout", new_callable=dnf.pycomp.StringIO) as stdout:
         support.command_run(self.cmd, args)
         self.assertEmpty(stdout.getvalue())
开发者ID:jsilhan,项目名称:dnf-plugins-core,代码行数:13,代码来源:test_repoclosure.py

示例12: test_check_option

 def test_check_option(self):
     args = ["--check", "@commandline"]
     self.cmd.base.repos.add(support.RepoStub("main"))
     self.cmd.base.add_remote_rpm(os.path.join(self.path, "noarch/foo-4-6.noarch.rpm"))
     self.cmd.configure(args)
     with mock.patch("sys.stdout", new_callable=dnf.pycomp.StringIO) as stdout:
         self.cmd.run(args)
         expected_out = ["package: foo-4-6.noarch from @commandline", "  unresolved deps:", "    bar = 4-6"]
         self.assertEqual(stdout.getvalue()[:-1], "\n".join(expected_out))
     args = ["--check", "main"]
     self.cmd.configure(args)
     with mock.patch("sys.stdout", new_callable=dnf.pycomp.StringIO) as stdout:
         self.cmd.run(args)
         self.assertEmpty(stdout.getvalue())
开发者ID:qtonthat,项目名称:dnf-plugins-extras,代码行数:14,代码来源:test_repoclosure.py

示例13: test_mirror

    def test_mirror(self):
        fo = MockStdout()
        p = dnf.cli.progress.MultiFileProgressMeter(fo, update_period=-1)
        p.start(1, 5)
        pload = FakePayload('foo', 5.0)
        now = 1379406823.9

        with mock.patch('dnf.cli.progress._term_width', return_value=60), \
             mock.patch('dnf.cli.progress.time', lambda: now):
            p.progress(pload, 3)
            p.end(pload, dnf.callback.STATUS_MIRROR, 'Timeout.')
            p.progress(pload, 4)
        self.assertEqual(fo.visible_lines(), [
            '[MIRROR] foo: Timeout.                                     ',
            'foo        80% [========  ] ---  B/s |   4  B     --:-- ETA'])
开发者ID:haghabozorgi,项目名称:dnf,代码行数:15,代码来源:test_cli_progress.py

示例14: setUp

 def setUp(self):
     """Prepare the test fixture."""
     super(UpdateInfoCommandTest, self).setUp()
     cachedir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, cachedir)
     self.cli.base.conf.cachedir = cachedir
     self.cli.base.add_test_dir_repo('rpm', self.cli.base.conf)
     self._stdout = dnf.pycomp.StringIO()
     self.addCleanup(mock.patch.stopall)
     mock.patch(
         'dnf.cli.commands.updateinfo._',
         dnf.pycomp.NullTranslations().ugettext).start()
     mock.patch(
         'dnf.cli.commands.updateinfo.print',
         self._stub_print, create=True).start()
开发者ID:mavit,项目名称:dnf,代码行数:15,代码来源:test_updateinfo.py

示例15: test_diff_output

    def test_diff_output(self):
        self._create_conf()
        self._create_rpmnew()
        self._create_rpmsave()

        with self.rpmconf_plugin as rpmconf, mock.patch("sys.stdout", new_callable=StringIO) as stdout:
            rpmconf.diff = True
            rpmconf.run()

            lines = stdout.getvalue().splitlines()

        expected_lines = [
            "--- {0}".format(*self.conf_file),
            "+++ {0}".format(*self.conf_file_rpmnew),
            "@@ -1,3 +1,2 @@",
            " package = good",
            " true = false",
            '-what = "tahw"',
            "--- {0}".format(*self.conf_file_rpmsave),
            "+++ {0}".format(*self.conf_file),
            "@@ -1,2 +1,3 @@",
            "-package = bad",
            "-true = true",
            "+package = good",
            "+true = false",
            '+what = "tahw"',
        ]

        msg_tmpl = "{0} does not start with {1}"
        for line, expected_line in zip_longest(lines, expected_lines, fillvalue=""):
            if not line.startswith(expected_line):
                self.fail(msg_tmpl.format(safe_repr(line), safe_repr(expected_line)))
开发者ID:pghmcfc,项目名称:dnf-plugins-extras,代码行数:32,代码来源:test_rpmconf.py


注:本文中的tests.support.mock.patch函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。