From 349e33139adf073c3b55065b1de658868622ccdd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 2 Jul 2016 12:27:59 -0400 Subject: Rename test for msvc module as well --- setuptools/tests/test_msvc.py | 179 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 setuptools/tests/test_msvc.py (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py new file mode 100644 index 00000000..86475834 --- /dev/null +++ b/setuptools/tests/test_msvc.py @@ -0,0 +1,179 @@ +""" +Tests for msvc9compiler. +""" + +import os +import contextlib +import distutils.errors + +import pytest +try: + from unittest import mock +except ImportError: + import mock + +from . import contexts + +# importing only setuptools should apply the patch +__import__('setuptools') + +pytest.importorskip("distutils.msvc9compiler") + + +def mock_reg(hkcu=None, hklm=None): + """ + Return a mock for distutils.msvc9compiler.Reg, patched + to mock out the functions that access the registry. + """ + + _winreg = getattr(distutils.msvc9compiler, '_winreg', None) + winreg = getattr(distutils.msvc9compiler, 'winreg', _winreg) + + hives = { + winreg.HKEY_CURRENT_USER: hkcu or {}, + winreg.HKEY_LOCAL_MACHINE: hklm or {}, + } + + @classmethod + def read_keys(cls, base, key): + """Return list of registry keys.""" + hive = hives.get(base, {}) + return [ + k.rpartition('\\')[2] + for k in hive if k.startswith(key.lower()) + ] + + @classmethod + def read_values(cls, base, key): + """Return dict of registry keys and values.""" + hive = hives.get(base, {}) + return dict( + (k.rpartition('\\')[2], hive[k]) + for k in hive if k.startswith(key.lower()) + ) + + return mock.patch.multiple(distutils.msvc9compiler.Reg, + read_keys=read_keys, read_values=read_values) + + +class TestModulePatch: + """ + Ensure that importing setuptools is sufficient to replace + the standard find_vcvarsall function with a version that + recognizes the "Visual C++ for Python" package. + """ + + key_32 = r'software\microsoft\devdiv\vcforpython\9.0\installdir' + key_64 = r'software\wow6432node\microsoft\devdiv\vcforpython\9.0\installdir' + + def test_patched(self): + "Test the module is actually patched" + mod_name = distutils.msvc9compiler.find_vcvarsall.__module__ + assert mod_name == "setuptools.msvc", "find_vcvarsall unpatched" + + def test_no_registry_entryies_means_nothing_found(self): + """ + No registry entries or environment variable should lead to an error + directing the user to download vcpython27. + """ + find_vcvarsall = distutils.msvc9compiler.find_vcvarsall + query_vcvarsall = distutils.msvc9compiler.query_vcvarsall + + with contexts.environment(VS90COMNTOOLS=None): + with mock_reg(): + assert find_vcvarsall(9.0) is None + + expected = distutils.errors.DistutilsPlatformError + with pytest.raises(expected) as exc: + query_vcvarsall(9.0) + assert 'aka.ms/vcpython27' in str(exc) + + @pytest.yield_fixture + def user_preferred_setting(self): + """ + Set up environment with different install dirs for user vs. system + and yield the user_install_dir for the expected result. + """ + with self.mock_install_dir() as user_install_dir: + with self.mock_install_dir() as system_install_dir: + reg = mock_reg( + hkcu={ + self.key_32: user_install_dir, + }, + hklm={ + self.key_32: system_install_dir, + self.key_64: system_install_dir, + }, + ) + with reg: + yield user_install_dir + + def test_prefer_current_user(self, user_preferred_setting): + """ + Ensure user's settings are preferred. + """ + result = distutils.msvc9compiler.find_vcvarsall(9.0) + expected = os.path.join(user_preferred_setting, 'vcvarsall.bat') + assert expected == result + + @pytest.yield_fixture + def local_machine_setting(self): + """ + Set up environment with only the system environment configured. + """ + with self.mock_install_dir() as system_install_dir: + reg = mock_reg( + hklm={ + self.key_32: system_install_dir, + }, + ) + with reg: + yield system_install_dir + + def test_local_machine_recognized(self, local_machine_setting): + """ + Ensure machine setting is honored if user settings are not present. + """ + result = distutils.msvc9compiler.find_vcvarsall(9.0) + expected = os.path.join(local_machine_setting, 'vcvarsall.bat') + assert expected == result + + @pytest.yield_fixture + def x64_preferred_setting(self): + """ + Set up environment with 64-bit and 32-bit system settings configured + and yield the canonical location. + """ + with self.mock_install_dir() as x32_dir: + with self.mock_install_dir() as x64_dir: + reg = mock_reg( + hklm={ + # This *should* only exist on 32-bit machines + self.key_32: x32_dir, + # This *should* only exist on 64-bit machines + self.key_64: x64_dir, + }, + ) + with reg: + yield x32_dir + + def test_ensure_64_bit_preferred(self, x64_preferred_setting): + """ + Ensure 64-bit system key is preferred. + """ + result = distutils.msvc9compiler.find_vcvarsall(9.0) + expected = os.path.join(x64_preferred_setting, 'vcvarsall.bat') + assert expected == result + + @staticmethod + @contextlib.contextmanager + def mock_install_dir(): + """ + Make a mock install dir in a unique location so that tests can + distinguish which dir was detected in a given scenario. + """ + with contexts.tempdir() as result: + vcvarsall = os.path.join(result, 'vcvarsall.bat') + with open(vcvarsall, 'w'): + pass + yield result -- cgit v1.2.1 From f2a0b309367cfdade3c0c18d8c69612010351120 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 2 Jul 2016 12:38:21 -0400 Subject: Extract template as variable to avoid line continuation. --- setuptools/tests/test_msvc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 86475834..2fbd56bf 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -1,5 +1,5 @@ """ -Tests for msvc9compiler. +Tests for msvc support module. """ import os -- cgit v1.2.1 From 451b84a0c56e23eea5396981747d570c80aef773 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 22 Jul 2016 15:16:19 -0400 Subject: Correct spelling --- setuptools/tests/test_msvc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 2fbd56bf..9b5ffba0 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -71,7 +71,7 @@ class TestModulePatch: mod_name = distutils.msvc9compiler.find_vcvarsall.__module__ assert mod_name == "setuptools.msvc", "find_vcvarsall unpatched" - def test_no_registry_entryies_means_nothing_found(self): + def test_no_registry_entries_means_nothing_found(self): """ No registry entries or environment variable should lead to an error directing the user to download vcpython27. -- cgit v1.2.1 From 2b8cf28f3be32903b79e5b5b579fab38105791dd Mon Sep 17 00:00:00 2001 From: "J. Goutin" Date: Sat, 23 Jul 2016 12:56:52 +0200 Subject: Exception will not raise because MSVC9 may be find by new behavior at setuptools/msvc.py line 171. --- setuptools/tests/test_msvc.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 9b5ffba0..ad96752d 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -83,10 +83,12 @@ class TestModulePatch: with mock_reg(): assert find_vcvarsall(9.0) is None - expected = distutils.errors.DistutilsPlatformError - with pytest.raises(expected) as exc: + try: query_vcvarsall(9.0) - assert 'aka.ms/vcpython27' in str(exc) + except Exception as exc: + expected = distutils.errors.DistutilsPlatformError + assert isinstance(expected, exc) + assert 'aka.ms/vcpython27' in str(exc) @pytest.yield_fixture def user_preferred_setting(self): -- cgit v1.2.1 From 1a0644e065bfe2f05a64d92b4abd31392d471751 Mon Sep 17 00:00:00 2001 From: "J. Goutin" Date: Sat, 23 Jul 2016 13:01:13 +0200 Subject: Update test_msvc.py --- setuptools/tests/test_msvc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index ad96752d..a0c76ea0 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -87,7 +87,7 @@ class TestModulePatch: query_vcvarsall(9.0) except Exception as exc: expected = distutils.errors.DistutilsPlatformError - assert isinstance(expected, exc) + assert isinstance(exc, expected) assert 'aka.ms/vcpython27' in str(exc) @pytest.yield_fixture -- cgit v1.2.1 From d38321d4bbb2f04de89706cd7274397cbfc879fb Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 30 Jul 2016 11:12:31 -0400 Subject: For now mark test as xfail until the issue can be diagnosed and resolved. Ref #707. --- setuptools/tests/test_msvc.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index a0c76ea0..0d0a90f5 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -6,6 +6,8 @@ import os import contextlib import distutils.errors +import six + import pytest try: from unittest import mock @@ -71,6 +73,8 @@ class TestModulePatch: mod_name = distutils.msvc9compiler.find_vcvarsall.__module__ assert mod_name == "setuptools.msvc", "find_vcvarsall unpatched" + @pytest.mark.xfail(six.PY2, + reason="https://github.com/pypa/setuptools/issues/707") def test_no_registry_entries_means_nothing_found(self): """ No registry entries or environment variable should lead to an error -- cgit v1.2.1 From 09234d3084739075e0aba59002419c341a59a47e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 30 Jul 2016 11:19:38 -0400 Subject: Correct import --- setuptools/tests/test_msvc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 0d0a90f5..8c7e17d3 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -6,7 +6,7 @@ import os import contextlib import distutils.errors -import six +from setuptools.extern import six import pytest try: -- cgit v1.2.1 From 3ebd9363b715b4dd1a0aa1d89a596d08e4b41d84 Mon Sep 17 00:00:00 2001 From: "J. Goutin" Date: Tue, 2 Aug 2016 20:11:25 +0200 Subject: Update test_msvc.py --- setuptools/tests/test_msvc.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 8c7e17d3..14e0f208 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -73,8 +73,6 @@ class TestModulePatch: mod_name = distutils.msvc9compiler.find_vcvarsall.__module__ assert mod_name == "setuptools.msvc", "find_vcvarsall unpatched" - @pytest.mark.xfail(six.PY2, - reason="https://github.com/pypa/setuptools/issues/707") def test_no_registry_entries_means_nothing_found(self): """ No registry entries or environment variable should lead to an error -- cgit v1.2.1 From a343901877cfe4fa922c40076f3c16aa59d4b265 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 9 Sep 2016 11:05:18 -0400 Subject: Remove unused import --- setuptools/tests/test_msvc.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 14e0f208..a0c76ea0 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -6,8 +6,6 @@ import os import contextlib import distutils.errors -from setuptools.extern import six - import pytest try: from unittest import mock -- cgit v1.2.1 From 90e95f2367a2325dbf29e5a7a92ef483806ae7b9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 30 Jan 2017 14:30:55 -0500 Subject: Rely on backports.unittest_mock plugin to make mock available on old Python versions. Ref #949. --- setuptools/tests/test_msvc.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index a0c76ea0..fbeed1d5 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -5,12 +5,9 @@ Tests for msvc support module. import os import contextlib import distutils.errors +from unittest import mock import pytest -try: - from unittest import mock -except ImportError: - import mock from . import contexts -- cgit v1.2.1 From bf20d881df662da30d94687efb2ff3d3ba32f55a Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Fri, 14 Jul 2017 08:49:23 +0200 Subject: tests: switch back to mock instead of backports.unittest_mock --- setuptools/tests/test_msvc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index fbeed1d5..32d7a907 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -5,7 +5,7 @@ Tests for msvc support module. import os import contextlib import distutils.errors -from unittest import mock +import mock import pytest -- cgit v1.2.1 From 5cd86987530892bfb01f68ad5f1a2b997a3d01e7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 26 Jan 2019 20:20:12 -0500 Subject: Feed the hobgoblins (delint). --- setuptools/tests/test_msvc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 32d7a907..24e38ea8 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -49,7 +49,8 @@ def mock_reg(hkcu=None, hklm=None): for k in hive if k.startswith(key.lower()) ) - return mock.patch.multiple(distutils.msvc9compiler.Reg, + return mock.patch.multiple( + distutils.msvc9compiler.Reg, read_keys=read_keys, read_values=read_values) @@ -61,7 +62,7 @@ class TestModulePatch: """ key_32 = r'software\microsoft\devdiv\vcforpython\9.0\installdir' - key_64 = r'software\wow6432node\microsoft\devdiv\vcforpython\9.0\installdir' + key_64 = key_32.replace(r'\microsoft', r'\wow6432node\microsoft') def test_patched(self): "Test the module is actually patched" -- cgit v1.2.1 From 701eee9e53dcbfe200bef46420da420052699e68 Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen Date: Sun, 13 Dec 2020 18:01:50 +0800 Subject: Fix tests with pytest 6.2 The latest pytest deprecates pytest.yield_fixture in favor of pytest.fixture [1]. The changelog [2] says that both are the same. [1] https://github.com/pytest-dev/pytest/pull/7988 [2] https://docs.pytest.org/en/stable/changelog.html#pytest-6-2-0-2020-12-12 --- setuptools/tests/test_msvc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'setuptools/tests/test_msvc.py') diff --git a/setuptools/tests/test_msvc.py b/setuptools/tests/test_msvc.py index 24e38ea8..d1527bfa 100644 --- a/setuptools/tests/test_msvc.py +++ b/setuptools/tests/test_msvc.py @@ -88,7 +88,7 @@ class TestModulePatch: assert isinstance(exc, expected) assert 'aka.ms/vcpython27' in str(exc) - @pytest.yield_fixture + @pytest.fixture def user_preferred_setting(self): """ Set up environment with different install dirs for user vs. system @@ -116,7 +116,7 @@ class TestModulePatch: expected = os.path.join(user_preferred_setting, 'vcvarsall.bat') assert expected == result - @pytest.yield_fixture + @pytest.fixture def local_machine_setting(self): """ Set up environment with only the system environment configured. @@ -138,7 +138,7 @@ class TestModulePatch: expected = os.path.join(local_machine_setting, 'vcvarsall.bat') assert expected == result - @pytest.yield_fixture + @pytest.fixture def x64_preferred_setting(self): """ Set up environment with 64-bit and 32-bit system settings configured -- cgit v1.2.1