1 | n/a | from . import util as test_util |
---|
2 | n/a | machinery = test_util.import_importlib('importlib.machinery') |
---|
3 | n/a | |
---|
4 | n/a | import os |
---|
5 | n/a | import re |
---|
6 | n/a | import sys |
---|
7 | n/a | import unittest |
---|
8 | n/a | from test import support |
---|
9 | n/a | from distutils.util import get_platform |
---|
10 | n/a | from contextlib import contextmanager |
---|
11 | n/a | from .util import temp_module |
---|
12 | n/a | |
---|
13 | n/a | support.import_module('winreg', required_on=['win']) |
---|
14 | n/a | from winreg import ( |
---|
15 | n/a | CreateKey, HKEY_CURRENT_USER, |
---|
16 | n/a | SetValue, REG_SZ, KEY_ALL_ACCESS, |
---|
17 | n/a | EnumKey, CloseKey, DeleteKey, OpenKey |
---|
18 | n/a | ) |
---|
19 | n/a | |
---|
20 | n/a | def delete_registry_tree(root, subkey): |
---|
21 | n/a | try: |
---|
22 | n/a | hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS) |
---|
23 | n/a | except OSError: |
---|
24 | n/a | # subkey does not exist |
---|
25 | n/a | return |
---|
26 | n/a | while True: |
---|
27 | n/a | try: |
---|
28 | n/a | subsubkey = EnumKey(hkey, 0) |
---|
29 | n/a | except OSError: |
---|
30 | n/a | # no more subkeys |
---|
31 | n/a | break |
---|
32 | n/a | delete_registry_tree(hkey, subsubkey) |
---|
33 | n/a | CloseKey(hkey) |
---|
34 | n/a | DeleteKey(root, subkey) |
---|
35 | n/a | |
---|
36 | n/a | @contextmanager |
---|
37 | n/a | def setup_module(machinery, name, path=None): |
---|
38 | n/a | if machinery.WindowsRegistryFinder.DEBUG_BUILD: |
---|
39 | n/a | root = machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG |
---|
40 | n/a | else: |
---|
41 | n/a | root = machinery.WindowsRegistryFinder.REGISTRY_KEY |
---|
42 | n/a | key = root.format(fullname=name, |
---|
43 | n/a | sys_version='%d.%d' % sys.version_info[:2]) |
---|
44 | n/a | try: |
---|
45 | n/a | with temp_module(name, "a = 1") as location: |
---|
46 | n/a | subkey = CreateKey(HKEY_CURRENT_USER, key) |
---|
47 | n/a | if path is None: |
---|
48 | n/a | path = location + ".py" |
---|
49 | n/a | SetValue(subkey, "", REG_SZ, path) |
---|
50 | n/a | yield |
---|
51 | n/a | finally: |
---|
52 | n/a | if machinery.WindowsRegistryFinder.DEBUG_BUILD: |
---|
53 | n/a | key = os.path.dirname(key) |
---|
54 | n/a | delete_registry_tree(HKEY_CURRENT_USER, key) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows') |
---|
58 | n/a | class WindowsRegistryFinderTests: |
---|
59 | n/a | # The module name is process-specific, allowing for |
---|
60 | n/a | # simultaneous runs of the same test on a single machine. |
---|
61 | n/a | test_module = "spamham{}".format(os.getpid()) |
---|
62 | n/a | |
---|
63 | n/a | def test_find_spec_missing(self): |
---|
64 | n/a | spec = self.machinery.WindowsRegistryFinder.find_spec('spam') |
---|
65 | n/a | self.assertIs(spec, None) |
---|
66 | n/a | |
---|
67 | n/a | def test_find_module_missing(self): |
---|
68 | n/a | loader = self.machinery.WindowsRegistryFinder.find_module('spam') |
---|
69 | n/a | self.assertIs(loader, None) |
---|
70 | n/a | |
---|
71 | n/a | def test_module_found(self): |
---|
72 | n/a | with setup_module(self.machinery, self.test_module): |
---|
73 | n/a | loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module) |
---|
74 | n/a | spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) |
---|
75 | n/a | self.assertIsNot(loader, None) |
---|
76 | n/a | self.assertIsNot(spec, None) |
---|
77 | n/a | |
---|
78 | n/a | def test_module_not_found(self): |
---|
79 | n/a | with setup_module(self.machinery, self.test_module, path="."): |
---|
80 | n/a | loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module) |
---|
81 | n/a | spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) |
---|
82 | n/a | self.assertIsNone(loader) |
---|
83 | n/a | self.assertIsNone(spec) |
---|
84 | n/a | |
---|
85 | n/a | (Frozen_WindowsRegistryFinderTests, |
---|
86 | n/a | Source_WindowsRegistryFinderTests |
---|
87 | n/a | ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery) |
---|
88 | n/a | |
---|
89 | n/a | @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows') |
---|
90 | n/a | class WindowsExtensionSuffixTests: |
---|
91 | n/a | def test_tagged_suffix(self): |
---|
92 | n/a | suffixes = self.machinery.EXTENSION_SUFFIXES |
---|
93 | n/a | expected_tag = ".cp{0.major}{0.minor}-{1}.pyd".format(sys.version_info, |
---|
94 | n/a | re.sub('[^a-zA-Z0-9]', '_', get_platform())) |
---|
95 | n/a | try: |
---|
96 | n/a | untagged_i = suffixes.index(".pyd") |
---|
97 | n/a | except ValueError: |
---|
98 | n/a | untagged_i = suffixes.index("_d.pyd") |
---|
99 | n/a | expected_tag = "_d" + expected_tag |
---|
100 | n/a | |
---|
101 | n/a | self.assertIn(expected_tag, suffixes) |
---|
102 | n/a | |
---|
103 | n/a | # Ensure the tags are in the correct order |
---|
104 | n/a | tagged_i = suffixes.index(expected_tag) |
---|
105 | n/a | self.assertLess(tagged_i, untagged_i) |
---|
106 | n/a | |
---|
107 | n/a | (Frozen_WindowsExtensionSuffixTests, |
---|
108 | n/a | Source_WindowsExtensionSuffixTests |
---|
109 | n/a | ) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery) |
---|