diff options
author | Joseph Brill <48932340+jcbrill@users.noreply.github.com> | 2022-07-16 08:16:38 -0400 |
---|---|---|
committer | Joseph Brill <48932340+jcbrill@users.noreply.github.com> | 2022-07-16 08:16:38 -0400 |
commit | 6d35399005cf32b92dac1c1b54436b01565021a0 (patch) | |
tree | 2759b6865d01014dd28bcb6477f6cbfe59c88791 | |
parent | 8ad53e893644bd84c0044ee4ee987140ac6eccee (diff) | |
download | scons-git-6d35399005cf32b92dac1c1b54436b01565021a0.tar.gz |
Rework unit tests: move global data to Data class and move monkey patching to Patch class.
-rw-r--r-- | SCons/Tool/MSCommon/MSVC/ConfigTests.py | 49 | ||||
-rw-r--r-- | SCons/Tool/MSCommon/MSVC/DispatcherTests.py | 52 | ||||
-rw-r--r-- | SCons/Tool/MSCommon/MSVC/ScriptArgumentsTests.py | 66 | ||||
-rw-r--r-- | SCons/Tool/MSCommon/MSVC/UtilTests.py | 8 | ||||
-rw-r--r-- | SCons/Tool/MSCommon/MSVC/WinSDKTests.py | 61 | ||||
-rw-r--r-- | SCons/Tool/MSCommon/vcTests.py | 61 |
6 files changed, 189 insertions, 108 deletions
diff --git a/SCons/Tool/MSCommon/MSVC/ConfigTests.py b/SCons/Tool/MSCommon/MSVC/ConfigTests.py index 570861c7d..052893093 100644 --- a/SCons/Tool/MSCommon/MSVC/ConfigTests.py +++ b/SCons/Tool/MSCommon/MSVC/ConfigTests.py @@ -31,28 +31,57 @@ from SCons.Tool.MSCommon import vc from SCons.Tool.MSCommon.MSVC import Config from SCons.Tool.MSCommon.MSVC.Exceptions import MSVCInternalError -_VCVER = vc._VCVER -_MSVC_VERSION_INTERNAL = Config.MSVC_VERSION_INTERNAL +class Patch: + + class vc: + + class _VCVER: + + _VCVER = vc._VCVER + + @classmethod + def enable_copy(cls): + hook = list(cls._VCVER) + vc._VCVER = hook + return hook + + @classmethod + def restore(cls): + vc._VCVER = cls._VCVER + + class Config: + + class MSVC_VERSION_INTERNAL: + + MSVC_VERSION_INTERNAL = Config.MSVC_VERSION_INTERNAL + + @classmethod + def enable_copy(cls): + hook = dict(cls.MSVC_VERSION_INTERNAL) + Config.MSVC_VERSION_INTERNAL = hook + return hook + + @classmethod + def restore(cls): + Config.MSVC_VERSION_INTERNAL = cls.MSVC_VERSION_INTERNAL class ConfigTests(unittest.TestCase): def test_vcver(self): # all vc._VCVER in Config.MSVC_VERSION_SUFFIX - _ADD_VCVER = list(_VCVER) - _ADD_VCVER.append('99.9') - vc._VCVER = _ADD_VCVER + _VCVER = Patch.vc._VCVER.enable_copy() + _VCVER.append('99.9') with self.assertRaises(MSVCInternalError): Config.verify() - vc._VCVER = _VCVER + Patch.vc._VCVER.restore() def test_msvc_version_internal(self): # all vc._VCVER numstr in Config.MSVC_VERSION_INTERNAL - _DEL_MSVC_VERSION_INTERNAL = dict(_MSVC_VERSION_INTERNAL) - del _DEL_MSVC_VERSION_INTERNAL['14.3'] - Config.MSVC_VERSION_INTERNAL = _DEL_MSVC_VERSION_INTERNAL + MSVC_VERSION_INTERNAL = Patch.Config.MSVC_VERSION_INTERNAL.enable_copy() + del MSVC_VERSION_INTERNAL['14.3'] with self.assertRaises(MSVCInternalError): Config.verify() - Config.MSVC_VERSION_INTERNAL = _MSVC_VERSION_INTERNAL + Patch.Config.MSVC_VERSION_INTERNAL.restore() def test_verify(self): Config.verify() diff --git a/SCons/Tool/MSCommon/MSVC/DispatcherTests.py b/SCons/Tool/MSCommon/MSVC/DispatcherTests.py index 01379fc4c..ce7880c07 100644 --- a/SCons/Tool/MSCommon/MSVC/DispatcherTests.py +++ b/SCons/Tool/MSCommon/MSVC/DispatcherTests.py @@ -30,58 +30,52 @@ import unittest from SCons.Tool.MSCommon import MSVC MSVC.Dispatcher.register_modulename(__name__) +class Data: + + reset_count = 0 + verify_count = 0 + # current module - not callable _reset = None reset = None _verify = None verify = None -reset_count = 0 -verify_count = 0 - class StaticMethods: @staticmethod def _reset(): - global reset_count - reset_count += 1 + Data.reset_count += 1 @staticmethod def reset(): - global reset_count - reset_count += 1 + Data.reset_count += 1 @staticmethod def _verify(): - global verify_count - verify_count += 1 + Data.verify_count += 1 @staticmethod def verify(): - global verify_count - verify_count += 1 + Data.verify_count += 1 class ClassMethods: @classmethod def _reset(cls): - global reset_count - reset_count += 1 + Data.reset_count += 1 @classmethod def reset(cls): - global reset_count - reset_count += 1 + Data.reset_count += 1 @classmethod def _verify(cls): - global verify_count - verify_count += 1 + Data.verify_count += 1 @classmethod def verify(cls): - global verify_count - verify_count += 1 + Data.verify_count += 1 class NotCallable: @@ -98,28 +92,24 @@ MSVC.Dispatcher.register_class(NotCallable) class DispatcherTests(unittest.TestCase): def test_dispatcher_reset(self): - global reset_count MSVC.Dispatcher.reset() - self.assertTrue(reset_count == 4, "MSVC.Dispatcher.reset() count failed") - reset_count = 0 + self.assertTrue(Data.reset_count == 4, "MSVC.Dispatcher.reset() count failed") + Data.reset_count = 0 def test_dispatcher_verify(self): - global verify_count MSVC.Dispatcher.verify() - self.assertTrue(verify_count == 4, "MSVC.Dispatcher.verify() count failed") - verify_count = 0 + self.assertTrue(Data.verify_count == 4, "MSVC.Dispatcher.verify() count failed") + Data.verify_count = 0 def test_msvc_reset(self): - global reset_count MSVC._reset() - self.assertTrue(reset_count == 4, "MSVC._reset() count failed") - reset_count = 0 + self.assertTrue(Data.reset_count == 4, "MSVC._reset() count failed") + Data.reset_count = 0 def test_msvc_verify(self): - global verify_count MSVC._verify() - self.assertTrue(verify_count == 4, "MSVC._verify() count failed") - verify_count = 0 + self.assertTrue(Data.verify_count == 4, "MSVC._verify() count failed") + Data.verify_count = 0 if __name__ == "__main__": unittest.main() diff --git a/SCons/Tool/MSCommon/MSVC/ScriptArgumentsTests.py b/SCons/Tool/MSCommon/MSVC/ScriptArgumentsTests.py index 90a76c2fe..f3884d255 100644 --- a/SCons/Tool/MSCommon/MSVC/ScriptArgumentsTests.py +++ b/SCons/Tool/MSCommon/MSVC/ScriptArgumentsTests.py @@ -45,43 +45,55 @@ def Environment(**kwargs): del kwargs[tools_key] return SCons.Environment.Base(tools=tools, **kwargs) -class ScriptArgumentsTests(unittest.TestCase): +class Data: # all versions - _all_versions_list = [] + ALL_VERSIONS_PAIRS = [] # installed versions - _vcdir_list = [] - - @classmethod - def setUpClass(cls): - for vcver in Config.MSVC_VERSION_SUFFIX.keys(): - version_def = Util.msvc_version_components(vcver) - vc_dir = vc.find_vc_pdir(None, vcver) - t = (version_def, vc_dir) - cls._all_versions_list.append(t) - if vc_dir: - cls._vcdir_list.append(t) - - def setUp(self): - self.all_versions_list = self.__class__._all_versions_list - self.vcdir_list = self.__class__._vcdir_list + INSTALLED_VERSIONS_PAIRS = [] + + for vcver in Config.MSVC_VERSION_SUFFIX.keys(): + version_def = Util.msvc_version_components(vcver) + vc_dir = vc.find_vc_pdir(None, vcver) + t = (version_def, vc_dir) + ALL_VERSIONS_PAIRS.append(t) + if vc_dir: + INSTALLED_VERSIONS_PAIRS.append(t) + +class Patch: + + class Config: + + class MSVC_SDK_VERSIONS: + + MSVC_SDK_VERSIONS = Config.MSVC_SDK_VERSIONS + + @classmethod + def enable_copy(cls): + hook = set(cls.MSVC_SDK_VERSIONS) + Config.MSVC_SDK_VERSIONS = hook + return hook + + @classmethod + def restore(cls): + Config.MSVC_SDK_VERSIONS = cls.MSVC_SDK_VERSIONS + +class ScriptArgumentsTests(unittest.TestCase): def test_verify(self): - _MSVC_SDK_VERSIONS = Config.MSVC_SDK_VERSIONS - msvc_sdk_versions = set(Config.MSVC_SDK_VERSIONS) - msvc_sdk_versions.add('99.0') - Config.MSVC_SDK_VERSIONS = msvc_sdk_versions + MSVC_SDK_VERSIONS = Patch.Config.MSVC_SDK_VERSIONS.enable_copy() + MSVC_SDK_VERSIONS.add('99.0') with self.assertRaises(MSVCInternalError): ScriptArguments.verify() - Config.MSVC_SDK_VERSIONS = _MSVC_SDK_VERSIONS + Patch.Config.MSVC_SDK_VERSIONS.restore() def test_msvc_script_arguments_defaults(self): - env = Environment() func = ScriptArguments.msvc_script_arguments + env = Environment() # disable forcing sdk and toolset versions as arguments force = ScriptArguments.msvc_force_default_arguments(force=False) - for version_def, vc_dir in self.vcdir_list: + for version_def, vc_dir in Data.INSTALLED_VERSIONS_PAIRS: for arg in ('', 'arch'): scriptargs = func(env, version_def.msvc_version, vc_dir, arg) self.assertTrue(scriptargs == arg, "{}({},{}) != {} [force=False]".format( @@ -89,7 +101,7 @@ class ScriptArgumentsTests(unittest.TestCase): )) # enable forcing sdk and toolset versions as arguments force = ScriptArguments.msvc_force_default_arguments(force=True) - for version_def, vc_dir in self.vcdir_list: + for version_def, vc_dir in Data.INSTALLED_VERSIONS_PAIRS: scriptargs = func(env, version_def.msvc_version, vc_dir, '') for arg in ('', 'arch'): scriptargs = func(env, version_def.msvc_version, vc_dir, arg) @@ -110,7 +122,7 @@ class ScriptArgumentsTests(unittest.TestCase): def test_msvc_toolset_versions_internal(self): func = ScriptArguments._msvc_toolset_versions_internal - for version_def, vc_dir in self.vcdir_list: + for version_def, vc_dir in Data.INSTALLED_VERSIONS_PAIRS: for full in (True, False): for sxs in (True, False): toolset_versions = func(version_def.msvc_version, vc_dir, full=full, sxs=sxs) @@ -133,7 +145,7 @@ class ScriptArgumentsTests(unittest.TestCase): def test_msvc_toolset_internal(self): func = ScriptArguments._msvc_toolset_internal - for version_def, vc_dir in self.vcdir_list: + for version_def, vc_dir in Data.INSTALLED_VERSIONS_PAIRS: toolset_versions = ScriptArguments._msvc_toolset_versions_internal(version_def.msvc_version, vc_dir, full=True, sxs=True) if not toolset_versions: continue diff --git a/SCons/Tool/MSCommon/MSVC/UtilTests.py b/SCons/Tool/MSCommon/MSVC/UtilTests.py index 404bf47c9..049787801 100644 --- a/SCons/Tool/MSCommon/MSVC/UtilTests.py +++ b/SCons/Tool/MSCommon/MSVC/UtilTests.py @@ -32,7 +32,9 @@ import re from SCons.Tool.MSCommon.MSVC import Config from SCons.Tool.MSCommon.MSVC import Util -util_parent_dir = os.path.join(os.path.dirname(Util.__file__), os.pardir) +class Data: + + UTIL_PARENT_DIR = os.path.join(os.path.dirname(Util.__file__), os.pardir) class UtilTests(unittest.TestCase): @@ -40,7 +42,7 @@ class UtilTests(unittest.TestCase): func = Util.listdir_dirs for dirname, expect in [ (None, False), ('', False), ('doesnotexist.xyz.abc', False), - (util_parent_dir, True), + (Data.UTIL_PARENT_DIR, True), ]: dirs = func(dirname) self.assertTrue((len(dirs) > 0) == expect, "{}({}): {}".format( @@ -51,7 +53,7 @@ class UtilTests(unittest.TestCase): func = Util.process_path for p, expect in [ (None, True), ('', True), - ('doesnotexist.xyz.abc', False), (util_parent_dir, False), + ('doesnotexist.xyz.abc', False), (Data.UTIL_PARENT_DIR, False), ]: rval = func(p) self.assertTrue((p == rval) == expect, "{}({}): {}".format( diff --git a/SCons/Tool/MSCommon/MSVC/WinSDKTests.py b/SCons/Tool/MSCommon/MSVC/WinSDKTests.py index 6b074e0ff..2a40e9a68 100644 --- a/SCons/Tool/MSCommon/MSVC/WinSDKTests.py +++ b/SCons/Tool/MSCommon/MSVC/WinSDKTests.py @@ -32,27 +32,62 @@ from SCons.Tool.MSCommon.MSVC import WinSDK from SCons.Tool.MSCommon.MSVC import Registry from SCons.Tool.MSCommon.MSVC.Exceptions import MSVCInternalError -_REGISTRY_SDK_QUERY_PATHS = Registry.sdk_query_paths +class Patch: -def registry_sdk_query_paths(version): - # return duplicate sdk version roots - sdk_roots = _REGISTRY_SDK_QUERY_PATHS(version) - if sdk_roots: - sdk_roots = sdk_roots + sdk_roots - return sdk_roots + class Config: -Registry.sdk_query_paths = registry_sdk_query_paths + class MSVC_SDK_VERSIONS: + + MSVC_SDK_VERSIONS = Config.MSVC_SDK_VERSIONS + + @classmethod + def enable_copy(cls): + hook = set(cls.MSVC_SDK_VERSIONS) + Config.MSVC_SDK_VERSIONS = hook + return hook + + @classmethod + def restore(cls): + Config.MSVC_SDK_VERSIONS = cls.MSVC_SDK_VERSIONS + + class Registry: + + class sdk_query_paths: + + sdk_query_paths = Registry.sdk_query_paths + + @classmethod + def sdk_query_paths_duplicate(cls, version): + sdk_roots = cls.sdk_query_paths(version) + sdk_roots = sdk_roots + sdk_roots if sdk_roots else sdk_roots + return sdk_roots + + @classmethod + def enable_duplicate(cls): + hook = cls.sdk_query_paths_duplicate + Registry.sdk_query_paths = hook + return hook + + @classmethod + def restore(cls): + Registry.sdk_query_paths = cls.sdk_query_paths class WinSDKTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + Patch.Registry.sdk_query_paths.enable_duplicate() + + @classmethod + def tearDownClass(cls): + Patch.Registry.sdk_query_paths.restore() + def test_verify(self): - _MSVC_SDK_VERSIONS = Config.MSVC_SDK_VERSIONS - msvc_sdk_versions = set(Config.MSVC_SDK_VERSIONS) - msvc_sdk_versions.add('99.0') - Config.MSVC_SDK_VERSIONS = msvc_sdk_versions + MSVC_SDK_VERSIONS = Patch.Config.MSVC_SDK_VERSIONS.enable_copy() + MSVC_SDK_VERSIONS.add('99.0') with self.assertRaises(MSVCInternalError): WinSDK.verify() - Config.MSVC_SDK_VERSIONS = _MSVC_SDK_VERSIONS + Patch.Config.MSVC_SDK_VERSIONS.restore() def _run_reset(self): WinSDK.reset() diff --git a/SCons/Tool/MSCommon/vcTests.py b/SCons/Tool/MSCommon/vcTests.py index b66e31965..e84494469 100644 --- a/SCons/Tool/MSCommon/vcTests.py +++ b/SCons/Tool/MSCommon/vcTests.py @@ -94,14 +94,14 @@ class MSVcTestCase(unittest.TestCase): create_path = path if create_path and not os.path.isdir(create_path): os.makedirs(create_path) - + create_this = os.path.join(create_path,'cl.exe') # print("Creating: %s"%create_this) with open(create_this,'w') as ct: ct.write('created') - + def runTest(self): """ Check that all proper HOST_PLATFORM and TARGET_PLATFORM are handled. @@ -115,7 +115,7 @@ class MSVcTestCase(unittest.TestCase): _, clpathcomps = SCons.Tool.MSCommon.vc._LE2015_HOST_TARGET_BATCHARG_CLPATHCOMPS[('x86','x86')] path = os.path.join('.', *clpathcomps) MSVcTestCase._createDummyCl(path, add_bin=False) - + # print("retval:%s"%check(env, '.', '8.0')) @@ -239,18 +239,33 @@ class MSVcTestCase(unittest.TestCase): self.fail('Did not fail when TARGET_ARCH specified as: %s' % env['TARGET_ARCH']) -_HAVE_MSVC = True if MSCommon.vc.msvc_default_version() else False +class Data: + + HAVE_MSVC = True if MSCommon.vc.msvc_default_version() else False + +class Patch: -_orig_msvc_default_version = MSCommon.vc.msvc_default_version + class MSCommon: -def _msvc_default_version_none(): - return None + class vc: -def _enable_msvc_default_version_none(): - MSCommon.vc.msvc_default_version = _msvc_default_version_none + class msvc_default_version: -def _restore_msvc_default_version(): - MSCommon.vc.msvc_default_version = _orig_msvc_default_version + msvc_default_version = MSCommon.vc.msvc_default_version + + @classmethod + def msvc_default_version_none(cls): + return None + + @classmethod + def enable_none(cls): + hook = cls.msvc_default_version_none + MSCommon.vc.msvc_default_version = hook + return hook + + @classmethod + def restore(cls): + MSCommon.vc.msvc_default_version = cls.msvc_default_version class MsvcSdkVersionsTests(unittest.TestCase): """Test msvc_sdk_versions""" @@ -266,10 +281,10 @@ class MsvcSdkVersionsTests(unittest.TestCase): self.assertFalse(sdk_list, "SDK list is not empty for msvc version {}".format(repr(None))) def test_valid_default_msvc(self): - if _HAVE_MSVC: - _enable_msvc_default_version_none() + if Data.HAVE_MSVC: + Patch.MSCommon.vc.msvc_default_version.enable_none() self.run_valid_default_msvc() - _restore_msvc_default_version() + Patch.MSCommon.vc.msvc_default_version.restore() self.run_valid_default_msvc() def test_valid_vcver(self): @@ -277,7 +292,7 @@ class MsvcSdkVersionsTests(unittest.TestCase): version_def = MSCommon.msvc_version_components(symbol) for msvc_uwp_app in (True, False): sdk_list = MSCommon.vc.msvc_sdk_versions(version=symbol, msvc_uwp_app=msvc_uwp_app) - if _HAVE_MSVC and version_def.msvc_vernum >= 14.0: + if Data.HAVE_MSVC and version_def.msvc_vernum >= 14.0: self.assertTrue(sdk_list, "SDK list is empty for msvc version {}".format(repr(symbol))) else: self.assertFalse(sdk_list, "SDK list is not empty for msvc version {}".format(repr(symbol))) @@ -308,7 +323,6 @@ class MsvcSdkVersionsTests(unittest.TestCase): with self.assertRaises(MSCommon.vc.MSVCArgumentError): _ = MSCommon.vc.msvc_sdk_versions(version=symbol, msvc_uwp_app=msvc_uwp_app) - class MsvcToolsetVersionsTests(unittest.TestCase): """Test msvc_toolset_versions""" @@ -339,10 +353,10 @@ class MsvcToolsetVersionsTests(unittest.TestCase): self.assertFalse(toolset_none_list, "Toolset none list is not empty for msvc version {}".format(repr(None))) def test_valid_default_msvc(self): - if _HAVE_MSVC: - _enable_msvc_default_version_none() + if Data.HAVE_MSVC: + Patch.MSCommon.vc.msvc_default_version.enable_none() self.run_valid_default_msvc() - _restore_msvc_default_version() + Patch.MSCommon.vc.msvc_default_version.restore() self.run_valid_default_msvc() def test_valid_vcver(self): @@ -367,7 +381,6 @@ class MsvcToolsetVersionsTests(unittest.TestCase): with self.assertRaises(MSCommon.vc.MSVCArgumentError): _ = MSCommon.vc.msvc_toolset_versions(msvc_version=symbol) - class MsvcQueryVersionToolsetTests(unittest.TestCase): """Test msvc_query_toolset_version""" @@ -388,11 +401,11 @@ class MsvcQueryVersionToolsetTests(unittest.TestCase): )) def test_valid_default_msvc(self): - if _HAVE_MSVC: - _enable_msvc_default_version_none() + if Data.HAVE_MSVC: + Patch.MSCommon.vc.msvc_default_version.enable_none() self.run_valid_default_msvc(have_msvc=False) - _restore_msvc_default_version() - self.run_valid_default_msvc(have_msvc=_HAVE_MSVC) + Patch.MSCommon.vc.msvc_default_version.restore() + self.run_valid_default_msvc(have_msvc=Data.HAVE_MSVC) def test_valid_vcver(self): for symbol in MSCommon.vc._VCVER: |