From ae2c3b8edf294618793be820406c7d8c16f09383 Mon Sep 17 00:00:00 2001 From: Jeremy Bowman Date: Thu, 19 Sep 2019 13:53:37 -0400 Subject: [PATCH] Fix mocks of open() - BOM-597 --- .../xmodule/modulestore/tests/test_xml_importer.py | 8 +++++++- pavelib/paver_tests/test_paver_quality.py | 14 ++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py b/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py index 070e4163fc..0cdfb2bf52 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py @@ -9,6 +9,7 @@ import unittest from uuid import uuid4 import mock +import six from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from path import Path as path @@ -22,6 +23,11 @@ from xmodule.modulestore.xml_importer import StaticContentImporter, _update_and_ from xmodule.tests import DATA_DIR from xmodule.x_module import XModuleMixin +if six.PY2: + OPEN_BUILTIN = '__builtin__.open' +else: + OPEN_BUILTIN = 'builtins.open' + class ModuleStoreNoSettings(unittest.TestCase): """ @@ -379,7 +385,7 @@ class StaticContentImporterTest(unittest.TestCase): base_dir = path('/path/to/dir') full_file_path = os.path.join(base_dir, 'static/some_file.txt') self.mocked_content_store.generate_thumbnail.return_value = (None, None) - with mock.patch("__builtin__.open", mock.mock_open(read_data="data")) as mock_file: + with mock.patch(OPEN_BUILTIN, mock.mock_open(read_data=b"data")) as mock_file: self.static_content_importer.import_static_file( full_file_path=full_file_path, base_dir=base_dir diff --git a/pavelib/paver_tests/test_paver_quality.py b/pavelib/paver_tests/test_paver_quality.py index 44309b5d89..0d558dd0ff 100644 --- a/pavelib/paver_tests/test_paver_quality.py +++ b/pavelib/paver_tests/test_paver_quality.py @@ -9,6 +9,7 @@ import tempfile import textwrap import unittest +import six from ddt import data, ddt, file_data, unpack from mock import MagicMock, mock_open, patch from path import Path as path @@ -17,6 +18,11 @@ from paver.easy import BuildFailure import pavelib.quality from pavelib.paver_tests.utils import PaverTestCase, fail_on_eslint +if six.PY2: + OPEN_BUILTIN = '__builtin__.open' +else: + OPEN_BUILTIN = 'builtins.open' + @ddt class TestPaverQualityViolations(unittest.TestCase): @@ -287,7 +293,7 @@ class TestPaverRunQuality(PaverTestCase): self.addCleanup(shutil.rmtree, self.report_dir) self.addCleanup(report_dir_patcher.stop) - @patch('__builtin__.open', mock_open()) + @patch(OPEN_BUILTIN, mock_open()) def test_failure_on_diffquality_pylint(self): """ If diff-quality fails on pylint, the paver task should also fail, but @@ -308,7 +314,7 @@ class TestPaverRunQuality(PaverTestCase): # of a diff-quality pylint failure, eslint is still called. self.assertEqual(self._mock_paver_sh.call_count, 2) - @patch('__builtin__.open', mock_open()) + @patch(OPEN_BUILTIN, mock_open()) def test_failure_on_diffquality_eslint(self): """ If diff-quality fails on eslint, the paver task should also fail @@ -329,7 +335,7 @@ class TestPaverRunQuality(PaverTestCase): # and once for diff quality with eslint self.assertEqual(self._mock_paver_sh.call_count, 4) - @patch('__builtin__.open', mock_open()) + @patch(OPEN_BUILTIN, mock_open()) def test_other_exception(self): """ If diff-quality fails for an unknown reason on the first run, then @@ -342,7 +348,7 @@ class TestPaverRunQuality(PaverTestCase): # Test that pylint is NOT called by counting calls self.assertEqual(self._mock_paver_sh.call_count, 1) - @patch('__builtin__.open', mock_open()) + @patch(OPEN_BUILTIN, mock_open()) def test_no_diff_quality_failures(self): # Assert nothing is raised pavelib.quality.run_quality("")