#336 Getting rid of python3-mock
Merged 2 years ago by clime. Opened 2 years ago by clime.

@@ -30,7 +30,6 @@ 

  

  BuildRequires: python3-pytest

  BuildRequires: python3-pytest-cov

- BuildRequires: python3-mock

  BuildRequires: python3-munch

  BuildRequires: python3-lockfile

  BuildRequires: python3-daemon

@@ -13,8 +13,8 @@ 

  from backend.daemons.backend import CoprBackend, run_backend

  from backend.exceptions import CoprBackendError

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  STDOUT = "stdout"

  STDERR = "stderr"

@@ -16,8 +16,8 @@ 

  from backend.job import BuildJob

  from backend.vm_manage.models import VmDescriptor

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  from backend.daemons.dispatcher import Worker

  

@@ -16,8 +16,8 @@ 

  import shutil

  import os

  

- import mock

- from mock import patch, MagicMock, call

+ from unittest import mock

+ from unittest.mock import patch, MagicMock, call

  import pytest

  

  from backend.daemons.job_grab import CoprJobGrab

@@ -7,11 +7,11 @@ 

  import tempfile

  import shutil

  import os

- 

- import mock

- from mock import patch, MagicMock

  import pytest

  

+ from unittest import mock

+ from unittest.mock import patch, MagicMock

+ 

  import backend.daemons.log as log_module

  from backend.daemons.log import RedisLogHandler

  

@@ -22,8 +22,8 @@ 

  from backend.constants import JOB_GRAB_TASK_END_PUBSUB

  from backend.exceptions import VmError, VmSpawnLimitReached

  

- import mock

- from mock import patch, MagicMock

+ from unittest import mock

+ from unittest.mock import patch, MagicMock

  import pytest

  

  

@@ -13,8 +13,8 @@ 

  

  from backend.job import BuildJob

  

- import mock

- from mock import patch, MagicMock

+ from unittest import mock

+ from unittest.mock import patch, MagicMock

  import pytest

  from types import MethodType

  

@@ -9,8 +9,8 @@ 

  import shutil

  import os

  

- import mock

- from mock import patch, MagicMock

+ from unittest import mock

+ from unittest.mock import patch, MagicMock

  import pytest

  

  from backend.mockremote import MockRemote

@@ -7,8 +7,8 @@ 

  from munch import Munch

  

  import pytest

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  from copr_prune_results import Pruner

  from copr_prune_results import main as prune_main

file modified
+2 -2

@@ -8,8 +8,8 @@ 

  

  import pytest

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  from backend.actions import Action, ActionType, ActionResult

  from backend.exceptions import CreateRepoError, CoprKeygenRequestError

@@ -6,8 +6,8 @@ 

  import time

  import pytest

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  from backend.createrepo import createrepo, createrepo_unsafe, add_appdata, run_cmd_unsafe

  from backend.exceptions import CreateRepoError

@@ -7,8 +7,8 @@ 

  

  from backend.frontend import FrontendClient

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  @pytest.yield_fixture

file modified
+2 -2

@@ -8,8 +8,8 @@ 

  

  from backend.exceptions import CoprSignError, CoprSignNoKeyError, CoprKeygenRequestError

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  from backend.sign import get_pubkey, _sign_one, sign_rpms_in_dir, create_user_keys

  

@@ -15,8 +15,8 @@ 

  from backend.vm_manage import EventTopics, PUBSUB_MB

  from backend.vm_manage.check import HealthChecker, check_health

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  

@@ -15,8 +15,8 @@ 

  from backend.vm_manage.event_handle import EventHandler, Recycle

  from backend.vm_manage.models import VmDescriptor

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  

@@ -9,7 +9,7 @@ 

  from backend.helpers import get_redis_connection

  from backend.vm_manage.executor import Executor

  

- import mock

+ from unittest import mock

  import pytest

  

  

@@ -15,8 +15,8 @@ 

  from backend.daemons.vm_master import VmMaster

  from backend.helpers import get_redis_connection

  

- import mock

- from mock import MagicMock

+ from  unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  

@@ -12,8 +12,8 @@ 

  from backend.helpers import get_redis_connection

  from backend.vm_manage.spawn import Spawner, spawn_instance, do_spawn_and_publish

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  

@@ -14,8 +14,8 @@ 

  from backend.vm_manage import EventTopics

  from backend.vm_manage.terminate import Terminator, terminate_vm

  

- import mock

- from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  import pytest

  

  

@@ -18,7 +18,6 @@ 

  BuildRequires: python3-munch

  BuildRequires: python3-requests

  BuildRequires: python3-rpkg

- BuildRequires: python3-mock

  BuildRequires: python3-pytest

  BuildRequires: python3-pytest-cov

  

@@ -5,9 +5,10 @@ 

  import shutil

  import time

  import os

- import mock

  import logging

  

+ from unittest import mock

+ 

  from munch import Munch

  from subprocess import check_output

  

@@ -6,9 +6,7 @@ 

  import copy

  import pytest

  

- from mock import call

  from munch import Munch

- 

  from base import Base

  

  from unittest import mock

@@ -120,10 +118,10 @@ 

          assert mc_import_package.call_args[0][3] == 'somepath.src.rpm'

  

          print(self.importer.post_back_safe.has_calls([

-             call({'build_id': 125, 'pkg_name': 'foo', 'branch': self.BRANCH,

-                   'pkg_version': '1.2', 'git_hash': '123', 'repo_name': 'foo'}),

-             call({'build_id': 125, 'pkg_name': 'foo', 'branch': self.BRANCH2,

-                   'pkg_version': '1.2', 'git_hash': '124', 'repo_name': 'foo'})

+             mock.call({'build_id': 125, 'pkg_name': 'foo', 'branch': self.BRANCH,

+                        'pkg_version': '1.2', 'git_hash': '123', 'repo_name': 'foo'}),

+             mock.call({'build_id': 125, 'pkg_name': 'foo', 'branch': self.BRANCH2,

+                        'pkg_version': '1.2', 'git_hash': '124', 'repo_name': 'foo'})

          ]))

  

      def test_run(self, mc_time, mc_worker):

@@ -4,7 +4,6 @@ 

  import pytest

  

  from pyrpkg import rpkgError

- from mock import call

  from munch import Munch

  

  from base import Base

@@ -123,13 +122,13 @@ 

          branches = ['f25', 'f26']

          setup_git_repo(reponame, branches)

          assert mc_subprocess_check_output.has_calls([

-             call(['/usr/share/dist-git/setup_git_package', 'foo']),

-             call(['/usr/share/dist-git/mkbranch', 'f25', 'foo']),

-             call(['/usr/share/dist-git/mkbranch', 'f26', 'foo']),

+             mock.call(['/usr/share/dist-git/setup_git_package', 'foo']),

+             mock.call(['/usr/share/dist-git/mkbranch', 'f25', 'foo']),

+             mock.call(['/usr/share/dist-git/mkbranch', 'f26', 'foo']),

          ])

  

      def refresh_cgit_listing(self, mc_subprocess_check_output):

          refresh_cgit_listing(self.opts)

          assert mc_subprocess_check_output.has_calls([

-             call(["/usr/share/copr/dist_git/bin/cgit_pkg_list", self.opts.cgit_pkg_list_location])

+             mock.call(["/usr/share/copr/dist_git/bin/cgit_pkg_list", self.opts.cgit_pkg_list_location])

          ])

@@ -80,7 +80,6 @@ 

  BuildRequires: python3-redis

  BuildRequires: python3-dateutil

  BuildRequires: python3-pytest

- BuildRequires: python3-mock

  BuildRequires: python3-decorator

  BuildRequires: python3-markdown

  BuildRequires: python3-pytz

@@ -1,6 +1,4 @@ 

  # coding: utf-8

- import six

- 

  

  class ApiError(Exception):

      def __init__(self, code, msg, data=None, **kwargs):

@@ -15,10 +13,6 @@ 

      def __str__(self):

          return str(self.data)

  

-     if six.PY2:

-         def __unicode__(self):

-             return unicode(self.data)

- 

  

  class AuthFailed(ApiError):

      def __init__(self, msg=None, data=None, **kwargs):

@@ -18,15 +18,10 @@ 

  from coprs import models

  from coprs.logic.coprs_logic import BranchesLogic

  

- import six

  from coprs.helpers import StatusEnum

  

- if six.PY3:

-     from unittest import mock

-     from unittest.mock import MagicMock

- else:

-     import mock

-     from mock import MagicMock

+ from unittest import mock

+ from unittest.mock import MagicMock

  

  

  class CoprsTestCase(object):

@@ -1,11 +1,6 @@ 

  from copy import deepcopy

+ from unittest import mock

  from flask import Flask, current_app

- import six

- 

- if six.PY3:

-     from unittest import mock

- else:

-     import mock

  

  from coprs import app

  from coprs.helpers import parse_package_name, generate_repo_url, \

@@ -1,5 +1,5 @@ 

  import json

- import mock

+ from unittest import mock

  

  from coprs.helpers import ActionTypeEnum

  from coprs.logic.actions_logic import ActionsLogic

@@ -1,6 +1,7 @@ 

  import yaml

  from munch import Munch

- from mock import patch, ANY

+ from unittest import mock

+ 

  from tests.coprs_test_case import CoprsTestCase

  from coprs.logic.modules_logic import ModuleBuildFacade, ModulemdGenerator, MBSResponse, MBSProxy

  from modulemd.components.rpm import ModuleComponentRPM

@@ -84,7 +85,7 @@ 

          assert component.name == self.b1.package_name

          assert component.rationale == "A reason why package is in the module"

  

-         with patch("coprs.app.config", self.config):

+         with mock.patch("coprs.app.config", self.config):

              assert component.repository.startswith("http://distgiturl.org")

              assert component.repository.endswith(".git")

              assert chroot.dist_git_url.startswith(component.repository)

@@ -96,9 +97,9 @@ 

          builds = [self.b1.id, self.b3.id]

          generator = ModulemdGenerator(config=self.config)

  

-         with patch("coprs.logic.modules_logic.ModulemdGenerator.add_component") as add_component:

+         with mock.patch("coprs.logic.modules_logic.ModulemdGenerator.add_component") as add_component:

              generator.add_components(packages, filter_packages, builds)

-             add_component.assert_called_with(self.p2.name, self.b3, self.b3.build_chroots[-1], ANY, 1)

+             add_component.assert_called_with(self.p2.name, self.b3, self.b3.build_chroots[-1], mock.ANY, 1)

              assert add_component.call_count == 2

  

      def test_components_different_chroots(self, f_users, f_coprs, f_mock_chroots, f_builds, f_db):

@@ -146,7 +147,7 @@ 

  

  class TestMBSProxy(CoprsTestCase):

  

-     @patch("requests.post")

+     @mock.patch("requests.post")

      def test_post(self, post_mock):

          url = "http://some-module-build-service.org"

          proxy = MBSProxy(url)

@@ -1,10 +1,5 @@ 

  from copy import deepcopy

- import six

- 

- if six.PY3:

-     from unittest import mock

- else:

-     import mock

+ from unittest import mock

  

  from tests.coprs_test_case import CoprsTestCase

  

@@ -2,8 +2,7 @@ 

  import flask

  import pytest

  

- import mock

- 

+ from unittest import mock

  

  from coprs import models

  from coprs.helpers import ActionTypeEnum

file modified
-4

@@ -1,7 +1,6 @@ 

  %global with_test 1

  %if 0%{?rhel} < 7 && 0%{?rhel} > 0

  %global _pkgdocdir %{_docdir}/%{name}-%{version}

- %global __python2 %{__python}

  %endif

  

  Name:       {{{ git_dir_name }}}

@@ -30,7 +29,6 @@ 

  # for tests

  BuildRequires: python3-pytest

  BuildRequires: python3-pytest-cov

- BuildRequires: python3-mock

  

  Requires:   haveged

  Requires:   gnupg2

@@ -47,7 +45,6 @@ 

  # tests

  Requires:   python3-pytest

  Requires:   python3-pytest-cov

- Requires:   python3-mock

  

  # scriptlets

  Requires(post): initscripts

@@ -68,7 +65,6 @@ 

  

  BuildRequires: python3-devel

  BuildRequires: python3-setuptools

- BuildRequires: python3-mock

  BuildRequires: python3-requests

  BuildRequires: python3-six

  BuildRequires: python3-flask

@@ -1,10 +1,15 @@ 

  import os

  

- import mock

  from requests.models import Response

  

  from copr import CoprClient

  

+ try:

+      from unittest import mock

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+ 

  path = os.path.abspath(__file__)

  dir_path = os.path.dirname(path)

  resource_location = os.path.join(dir_path, "resources")

file modified
-1

@@ -102,7 +102,6 @@ 

  BuildRequires: python3-devel

  BuildRequires: python3-setuptools

  BuildRequires: python3-pytest

- BuildRequires: python3-mock

  BuildRequires: python3-requests

  BuildRequires: python3-requests-toolbelt

  BuildRequires: python3-marshmallow

file modified
+15 -1

@@ -18,6 +18,7 @@ 

  Release: 1%{?dist}

  URL: https://pagure.io/copr/copr

  License: GPLv2+

+ BuildArch: noarch

  

  # Source is created by:

  # git clone https://pagure.io/copr/copr.git

@@ -26,16 +27,26 @@ 

  # rpkg spec --sources

  Source0: {{{ git_dir_pack }}}

  

- BuildArch: noarch

  BuildRequires: %python-devel

  BuildRequires: %rpm_python

  BuildRequires: asciidoc

  BuildRequires: %python-setuptools

+ BuildRequires: %python-pytest

+ BuildRequires: %python_pfx-munch

+ BuildRequires: %python-requests

+ BuildRequires: %python_pfx-jinja2

+ BuildRequires: %python-configparser

+ 

+ %if %{?python} == "python2"

+ BuildRequires: python2-mock

+ %endif

+ 

  Requires: %python

  Requires: %python_pfx-jinja2

  Requires: %python_pfx-munch

  Requires: %python-configparser

  Requires: %python-simplejson

+ Requires: %python-requests

  

  Requires: mock

  Requires: git

@@ -54,6 +65,9 @@ 

  %prep

  %setup -q

  

+ %check

+ PYTHON=%{python} ./run_tests.sh

+ 

  %build

  name="%{name}" version="%{version}" summary="%{summary}" %python_build

  a2x -d manpage -f manpage man/copr-rpmbuild.1.asciidoc

@@ -45,8 +45,8 @@ 

          self.use_bootstrap_container = task.get("use_bootstrap_container")

          self.pkg_manager_conf = "dnf" if "custom-1" in task.get("chroot") else "yum"

          self.timeout = task.get("timeout", 3600)

-         self.with_opts = task.get("with_opts")

-         self.without_opts = task.get("without_opts")

+         self.with_opts = task.get("with_opts", [])

+         self.without_opts = task.get("without_opts", [])

          self.sourcedir = sourcedir

          self.resultdir = resultdir

          self.config = config

file modified
+4 -1

@@ -1,2 +1,5 @@ 

  #!/bin/sh

- python3 -m pytest tests -s $@

+ 

+ path="${1:-tests}"

+ 

+ PYTHONPATH=".:$PYTHONPATH" "${PYTHON:-python3}" -m pytest -s "$path"

file modified
+2 -2

@@ -2,7 +2,7 @@ 

  import tempfile

  import unittest

  

- from ..copr_rpmbuild import helpers

+ from copr_rpmbuild import helpers

  

  CONFIG = """

  [main]

@@ -35,7 +35,7 @@ 

  

      def read_config_data(self, config_data):

          fd, config_path = tempfile.mkstemp()

-         f = open(fd, 'w')

+         f = open(config_path, 'w')

          f.write(config_data)

          f.close()

          return config_path, helpers.read_config(config_path)

file modified
+12 -10

@@ -1,10 +1,13 @@ 

- import mock

- from mock import MagicMock

- from mock import call

- 

- from ..copr_rpmbuild.providers.base import Provider

+ from copr_rpmbuild.providers.base import Provider

  from . import TestCase

  

+ try:

+      from unittest import mock

+      builtins = 'builtins'

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+      builtins = '__builtin__'

  

  class TestProvider(TestCase):

      def setUp(self):

@@ -12,16 +15,15 @@ 

          self.source_json = {}

          self.resultdir = "/path/to/resultdir"

  

-     @mock.patch('builtins.open', new_callable=mock.mock_open())

+     @mock.patch('{}.open'.format(builtins), new_callable=mock.mock_open())

      def test_create_rpmmacros(self, mock_open):

          provider = Provider(self.source_json, self.resultdir, self.config)

-         rpmmacros = MagicMock()

+         rpmmacros = mock.MagicMock()

          mock_open.return_value = rpmmacros

          provider.create_rpmmacros()

          mock_open.assert_called_with("{}/.rpmmacros".format(provider.workdir), "w")

          calls = [

-             call.__enter__().write('%_disable_source_fetch 0\n'),

-             call.__enter__().write('%__urlhelper_localopts --proto -all,+https,+ftps\n'),

+             mock.call.__enter__().write('%_disable_source_fetch 0\n'),

+             mock.call.__enter__().write('%__urlhelper_localopts --proto -all,+https,+ftps\n'),

          ]

          rpmmacros.assert_has_calls(calls, any_order=True)

- 

@@ -1,8 +1,9 @@ 

  import unittest

  import tempfile

+ import shutil

  import os

  

- from ..copr_rpmbuild.helpers import string2list, locate_srpm

+ from copr_rpmbuild.helpers import string2list, locate_srpm

  

  class TestHelpers(unittest.TestCase):

      def test_string2list(self):

@@ -13,7 +14,8 @@ 

          self.assertEqual(string2list(',,foo\tbar\tbaz'), ['foo', 'bar', 'baz'])

  

      def test_locate_srpm(self):

-         with tempfile.TemporaryDirectory() as tmpdir:

-             srpm_path = os.path.join(tmpdir, "dummy.src.rpm")

-             open(srpm_path, "w").close()

-             self.assertEqual(srpm_path, locate_srpm(tmpdir))

+         tmpdir = tempfile.mkdtemp()

+         srpm_path = os.path.join(tmpdir, "dummy.src.rpm")

+         open(srpm_path, "w").close()

+         self.assertEqual(srpm_path, locate_srpm(tmpdir))

+         shutil.rmtree(tmpdir)

file modified
+19 -9

@@ -1,13 +1,19 @@ 

  import re

  import unittest

- import mock

  import configparser

- from os.path import realpath, dirname

- from ..copr_rpmbuild.builders.mock import MockBuilder

  import subprocess

  import datetime

+ from os.path import realpath, dirname

+ 

+ from copr_rpmbuild.builders.mock import MockBuilder

  

- from unittest.mock import MagicMock

+ try:

+      from unittest import mock

+      builtins = 'builtins'

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+      builtins = '__builtin__'

  

  class TestMockBuilder(unittest.TestCase):

      def setUp(self):

@@ -35,6 +41,8 @@ 

              "task_id": "10-fedora-24-x86_64",

              "timeout": 21600,

              "use_bootstrap_container": False,

+             "with_opts": [],

+             "without_opts": [],

          }

          self.sourcedir = "/path/to/sourcedir"

          self.resultdir = "/path/to/resultdir"

@@ -71,12 +79,12 @@ 

          self.assertEqual(config_opts["use_bootstrap_container"], False)

          self.assertEqual(config_opts["yum.conf"], [])

  

-     @mock.patch("rpmbuild.copr_rpmbuild.builders.mock.get_mock_uniqueext")

-     @mock.patch("rpmbuild.copr_rpmbuild.builders.mock.subprocess.Popen")

+     @mock.patch("copr_rpmbuild.builders.mock.get_mock_uniqueext")

+     @mock.patch("copr_rpmbuild.builders.mock.GentlyTimeoutedPopen")

      def test_produce_rpm(self, popen_mock, get_mock_uniqueext_mock):

          builder = MockBuilder(self.task, self.sourcedir, self.resultdir, self.config)

          get_mock_uniqueext_mock.return_value = '2'

-         process = MagicMock(returncode=0)

+         process = mock.MagicMock(returncode=0)

          popen_mock.return_value = process

          builder.produce_rpm("/path/to/pkg.src.rpm", "/path/to/configs", "/path/to/results")

          assert_cmd = ['unbuffer', '/usr/bin/mock',

@@ -85,9 +93,11 @@ 

                        '--resultdir', '/path/to/results',

                        '--uniqueext', '2',

                        '-r', 'child']

-         popen_mock.assert_called_with(assert_cmd, stdin=subprocess.PIPE, preexec_fn=builder.preexec_fn_build_stream)

+         popen_mock.assert_called_with(assert_cmd, stdin=subprocess.PIPE,

+                                       preexec_fn=builder.preexec_fn_build_stream,

+                                       timeout=21600)

  

-     @mock.patch('builtins.open', new_callable=mock.mock_open())

+     @mock.patch('{}.open'.format(builtins), new_callable=mock.mock_open())

      def test_touch_success_file(self, mock_open):

          builder = MockBuilder(self.task, self.sourcedir, self.resultdir, self.config)

          builder.touch_success_file()

@@ -1,8 +1,8 @@ 

  import unittest

- from ..copr_rpmbuild.providers import (factory, RubyGemsProvider, PyPIProvider,

-                                        SpecUrlProvider)

+ from copr_rpmbuild.providers import (factory, RubyGemsProvider, PyPIProvider,

+                                      SpecUrlProvider)

  

- from ..copr_rpmbuild.helpers import SourceType

+ from copr_rpmbuild.helpers import SourceType

  

  

  class TestProvidersFactory(unittest.TestCase):

file modified
+11 -5

@@ -1,8 +1,14 @@ 

- import mock

- import unittest

- from ..copr_rpmbuild.providers.pypi import PyPIProvider

+ from copr_rpmbuild.providers.pypi import PyPIProvider

  from . import TestCase

  

+ try:

+      from unittest import mock

+      builtins = 'builtins'

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+      builtins = '__builtin__'

+ 

  

  class TestPyPIProvider(TestCase):

      def setUp(self):

@@ -18,8 +24,8 @@ 

         self.assertEqual(provider.pypi_package_name, "motionpaint")

         self.assertEqual(provider.python_versions, [2, 3])

  

-     @mock.patch("rpmbuild.copr_rpmbuild.providers.pypi.run_cmd")

-     @mock.patch("builtins.open")

+     @mock.patch("copr_rpmbuild.providers.pypi.run_cmd")

+     @mock.patch("{}.open".format(builtins))

      def test_produce_srpm(self, mock_open, run_cmd):

          provider = PyPIProvider(self.source_json, "/some/tmp/directory", self.config)

          provider.produce_srpm()

@@ -1,9 +1,15 @@ 

- import mock

- import unittest

  from munch import Munch

- from ..copr_rpmbuild.providers.rubygems import RubyGemsProvider

+ from copr_rpmbuild.providers.rubygems import RubyGemsProvider

  from . import TestCase

  

+ try:

+      from unittest import mock

+      builtins = 'builtins'

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+      builtins = '__builtin__'

+ 

  

  class TestRubyGemsProvider(TestCase):

      def setUp(self):

@@ -15,16 +21,16 @@ 

          provider = RubyGemsProvider(self.source_json, self.resultdir, self.config)

          self.assertEqual(provider.gem_name, "A_123")

  

-     @mock.patch("rpmbuild.copr_rpmbuild.providers.rubygems.run_cmd")

-     @mock.patch("builtins.open")

+     @mock.patch("copr_rpmbuild.providers.rubygems.run_cmd")

+     @mock.patch("{}.open".format(builtins))

      def test_produce_srpm(self, mock_open, run_cmd):

          provider = RubyGemsProvider(self.source_json, self.resultdir, self.config)

          provider.produce_srpm()

          assert_cmd = ["gem2rpm", "A_123", "--srpm", "-C", "/path/to/resultdir", "--fetch"]

          run_cmd.assert_called_with(assert_cmd)

  

-     @mock.patch("rpmbuild.copr_rpmbuild.providers.rubygems.run_cmd")

-     @mock.patch("builtins.open")

+     @mock.patch("copr_rpmbuild.providers.rubygems.run_cmd")

+     @mock.patch("{}.open".format(builtins))

      def test_empty_license(self, mock_open, run_cmd):

          stderr = ("error: line 8: Empty tag: License:"

                    "Command failed: rpmbuild -bs --nodeps --define '_sourcedir /tmp/gem2rpm-foo-20170905-3367-c2flks'"

file modified
+12 -9

@@ -1,14 +1,17 @@ 

- import mock

- import unittest

  import tempfile

  import os

  import configparser

  import shutil

- from ..copr_rpmbuild.providers.scm import ScmProvider

- from ..copr_rpmbuild.helpers import read_config

+ 

+ from copr_rpmbuild.providers.scm import ScmProvider

+ from copr_rpmbuild.helpers import read_config

  from . import TestCase

  

- from mock import patch, MagicMock

+ try:

+      from unittest import mock

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

  

  RPKG_CONF_JINJA = """

  [rpkg]

@@ -62,7 +65,7 @@ 

          source_json = self.source_json.copy()

          source_json["clone_url"] = "http://copr-dist-git.fedorainfracloud.org/git/clime/project/pkg.git"

  

-         with patch("rpmbuild.copr_rpmbuild.providers.scm.CONF_DIRS", new=[rpkg_tmpdir]):

+         with mock.patch("copr_rpmbuild.providers.scm.CONF_DIRS", new=[rpkg_tmpdir]):

              provider = ScmProvider(source_json, self.resultdir, self.config)

              rpkg_config_path = provider.generate_rpkg_config()

  

@@ -74,7 +77,7 @@ 

  

          source_json["clone_url"] = "http://unknownurl/git/clime/project/pkg.git"

  

-         with patch("rpmbuild.copr_rpmbuild.providers.scm.CONF_DIRS", new=[rpkg_tmpdir]):

+         with mock.patch("copr_rpmbuild.providers.scm.CONF_DIRS", new=[rpkg_tmpdir]):

              provider = ScmProvider(source_json, self.resultdir, self.config)

              rpkg_config_path = provider.generate_rpkg_config()

              self.assertEqual(rpkg_config_path, "/etc/rpkg.conf")

@@ -83,7 +86,7 @@ 

  

      def test_get_rpkg_command(self):

          provider = ScmProvider(self.source_json, self.resultdir, self.config)

-         provider.generate_rpkg_config = MagicMock(return_value="/etc/rpkg.conf")

+         provider.generate_rpkg_config = mock.MagicMock(return_value="/etc/rpkg.conf")

          assert_cmd = ["rpkg", "-C", "/etc/rpkg.conf", "srpm",

                        "--outdir", self.resultdir, "--spec", provider.spec_path]

          self.assertEqual(provider.get_rpkg_command(), assert_cmd)

@@ -98,7 +101,7 @@ 

          assert_cmd = ["tito", "build", "--test", "--srpm", "--output", self.resultdir]

          self.assertEqual(provider.get_tito_test_command(), assert_cmd)

  

-     @mock.patch("rpmbuild.copr_rpmbuild.providers.scm.get_mock_uniqueext")

+     @mock.patch("copr_rpmbuild.providers.scm.get_mock_uniqueext")

      def test_get_make_srpm_command(self, get_mock_uniqueext_mock):

          get_mock_uniqueext_mock.return_value = '2'

          provider = ScmProvider(self.source_json, self.resultdir, self.config)

file modified
+13 -6

@@ -1,9 +1,16 @@ 

- import mock

- import unittest

  import configparser

- from ..copr_rpmbuild.providers.spec import SpecUrlProvider

+ 

+ from copr_rpmbuild.providers.spec import SpecUrlProvider

  from . import TestCase

  

+ try:

+      from unittest import mock

+      builtins = 'builtins'

+ except ImportError:

+      # Python 2 version depends on mock

+      import mock

+      builtins = '__builtin__'

+ 

  

  class TestSpecUrlProvider(TestCase):

      def setUp(self):

@@ -16,8 +23,8 @@ 

          self.assertEqual(provider.url, "http://foo.ex/somepackage.spec")

  

      @mock.patch('requests.get')

-     @mock.patch("rpmbuild.copr_rpmbuild.providers.spec.run_cmd")

-     @mock.patch('builtins.open', new_callable=mock.mock_open())

+     @mock.patch("copr_rpmbuild.providers.spec.run_cmd")

+     @mock.patch('{}.open'.format(builtins), new_callable=mock.mock_open())

      def test_produce_srpm(self, mock_open, run_cmd, mock_get):

          provider = SpecUrlProvider(self.source_json, self.resultdir, self.config)

          provider.produce_srpm()

@@ -26,7 +33,7 @@ 

                                     cwd=provider.workdir)

  

      @mock.patch('requests.get')

-     @mock.patch('builtins.open', new_callable=mock.mock_open())

+     @mock.patch('{}.open'.format(builtins), new_callable=mock.mock_open())

      def test_save_spec(self, mock_open, mock_get):

          provider = SpecUrlProvider(self.source_json, self.resultdir, self.config)

          provider.save_spec()

As suggested in https://lists.fedoraproject.org/archives/list/copr-devel@lists.fedorahosted.org/thread/LNQZYFZJAWWRYN2PH7BSD6LXX3XUMGPG/, we should get rid of python3-mock and use unittest native python3 module. Only for python2 (copr-cli, python-copr, copr-rpmbuild), we should employ python2-mock, which is supposed to backport functionality from python3's unittest.mock into python2. Basically, we should be doing something like:

 try: 
     from unittest import mock 
 except ImportError: 
     # Python 2 version depends on mock 
     import mock

in our tests that should be run under python2 as well as well as python3. This is what this PR does.

I also needed to fix copr-rpmbuild tests under EPEL7 so that I could verify this change work. I enabled running the tests during build in %check phase.

1 new commit added

  • fix missing double-quotes in run_test.sh
2 years ago

Pull-Request has been merged by clime

2 years ago
Metadata
Changes Summary 46
+0 -1
file changed
backend/copr-backend.spec
+2 -2
file changed
backend/tests/daemons/test_backend.py
+2 -2
file changed
backend/tests/daemons/test_dispatcher.py
+2 -2
file changed
backend/tests/daemons/test_job_grab.py
+3 -3
file changed
backend/tests/daemons/test_log.py
+2 -2
file changed
backend/tests/daemons/test_vm_master.py
+2 -2
file changed
backend/tests/mockremote/test_builder.py
+2 -2
file changed
backend/tests/mockremote/test_mockremote.py
+2 -2
file changed
backend/tests/run/test_copr_prune_results.py
+2 -2
file changed
backend/tests/test_action.py
+2 -2
file changed
backend/tests/test_createrepo.py
+2 -2
file changed
backend/tests/test_frontend.py
+2 -2
file changed
backend/tests/test_sign.py
+2 -2
file changed
backend/tests/vm_manager/test_check.py
+2 -2
file changed
backend/tests/vm_manager/test_event_handle.py
+1 -1
file changed
backend/tests/vm_manager/test_executor.py
+2 -2
file changed
backend/tests/vm_manager/test_manager.py
+2 -2
file changed
backend/tests/vm_manager/test_spawn.py
+2 -2
file changed
backend/tests/vm_manager/test_terminate.py
+0 -1
file changed
dist-git/copr-dist-git.spec
+2 -1
file changed
dist-git/tests/test_crazy_merging.py
+4 -6
file changed
dist-git/tests/test_importer.py
+4 -5
file changed
dist-git/tests/test_package_import.py
+0 -1
file changed
frontend/copr-frontend.spec
+0 -6
file changed
frontend/coprs_frontend/coprs/rest_api/exceptions.py
+2 -7
file changed
frontend/coprs_frontend/tests/coprs_test_case.py
+1 -6
file changed
frontend/coprs_frontend/tests/test_helpers.py
+1 -1
file changed
frontend/coprs_frontend/tests/test_logic/test_complex_logic.py
+6 -5
file changed
frontend/coprs_frontend/tests/test_logic/test_modules_logic.py
+1 -6
file changed
frontend/coprs_frontend/tests/test_models.py
+1 -2
file changed
frontend/coprs_frontend/tests/test_views/test_coprs_ns/test_coprs_general.py
+0 -4
file changed
keygen/copr-keygen.spec
+6 -1
file changed
python/copr/test/test_client.py
+0 -1
file changed
python/python-copr.spec
+15 -1
file changed
rpmbuild/copr-rpmbuild.spec
+2 -2
file changed
rpmbuild/copr_rpmbuild/builders/mock.py
+4 -1
file changed
rpmbuild/run_tests.sh
+2 -2
file changed
rpmbuild/tests/__init__.py
+12 -10
file changed
rpmbuild/tests/test_base.py
+7 -5
file changed
rpmbuild/tests/test_helpers.py
+19 -9
file changed
rpmbuild/tests/test_mock.py
+3 -3
file changed
rpmbuild/tests/test_providers.py
+11 -5
file changed
rpmbuild/tests/test_pypi.py
+13 -7
file changed
rpmbuild/tests/test_rubygems.py
+12 -9
file changed
rpmbuild/tests/test_scm.py
+13 -6
file changed
rpmbuild/tests/test_spec.py