summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2021-04-07 00:02:07 (GMT)
committerGitHub <noreply@github.com>2021-04-07 00:02:07 (GMT)
commit04732ca993fa077a8b9640cc77fb2f152339585a (patch)
treee5aec9caf5770fd1aff72efc13640712155b8cbe /Lib/test/test_importlib
parentb57e045320d1d2a70eab236b7d31a3ebb75037c3 (diff)
downloadcpython-04732ca993fa077a8b9640cc77fb2f152339585a.zip
cpython-04732ca993fa077a8b9640cc77fb2f152339585a.tar.gz
cpython-04732ca993fa077a8b9640cc77fb2f152339585a.tar.bz2
bpo-43105: Importlib now resolves relative paths when creating module spec objects from file locations (GH-25121)
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/test_spec.py19
-rw-r--r--Lib/test/test_importlib/test_windows.py45
2 files changed, 60 insertions, 4 deletions
diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py
index b57eb6c..dcb0527 100644
--- a/Lib/test/test_importlib/test_spec.py
+++ b/Lib/test/test_importlib/test_spec.py
@@ -506,7 +506,7 @@ class FactoryTests:
def setUp(self):
self.name = 'spam'
- self.path = 'spam.py'
+ self.path = os.path.abspath('spam.py')
self.cached = self.util.cache_from_source(self.path)
self.loader = TestLoader()
self.fileloader = TestLoader(self.path)
@@ -645,7 +645,7 @@ class FactoryTests:
self.assertEqual(spec.loader, self.fileloader)
self.assertEqual(spec.origin, self.path)
self.assertIs(spec.loader_state, None)
- self.assertEqual(spec.submodule_search_locations, [''])
+ self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
self.assertEqual(spec.cached, self.cached)
self.assertTrue(spec.has_location)
@@ -744,7 +744,7 @@ class FactoryTests:
self.assertEqual(spec.loader, self.fileloader)
self.assertEqual(spec.origin, self.path)
self.assertIs(spec.loader_state, None)
- self.assertEqual(spec.submodule_search_locations, [''])
+ self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
self.assertEqual(spec.cached, self.cached)
self.assertTrue(spec.has_location)
@@ -769,7 +769,7 @@ class FactoryTests:
self.assertEqual(spec.loader, self.pkgloader)
self.assertEqual(spec.origin, self.path)
self.assertIs(spec.loader_state, None)
- self.assertEqual(spec.submodule_search_locations, [''])
+ self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
self.assertEqual(spec.cached, self.cached)
self.assertTrue(spec.has_location)
@@ -817,6 +817,17 @@ class FactoryTests:
self.assertEqual(spec.cached, self.cached)
self.assertTrue(spec.has_location)
+ def test_spec_from_file_location_relative_path(self):
+ spec = self.util.spec_from_file_location(self.name,
+ os.path.basename(self.path), loader=self.fileloader)
+
+ self.assertEqual(spec.name, self.name)
+ self.assertEqual(spec.loader, self.fileloader)
+ self.assertEqual(spec.origin, self.path)
+ self.assertIs(spec.loader_state, None)
+ self.assertIs(spec.submodule_search_locations, None)
+ self.assertEqual(spec.cached, self.cached)
+ self.assertTrue(spec.has_location)
(Frozen_FactoryTests,
Source_FactoryTests
diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index 64ffe10..802fb34 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -126,3 +126,48 @@ class WindowsExtensionSuffixTests:
(Frozen_WindowsExtensionSuffixTests,
Source_WindowsExtensionSuffixTests
) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery)
+
+
+@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
+class WindowsBootstrapPathTests(unittest.TestCase):
+ def check_join(self, expected, *inputs):
+ from importlib._bootstrap_external import _path_join
+ actual = _path_join(*inputs)
+ if expected.casefold() == actual.casefold():
+ return
+ self.assertEqual(expected, actual)
+
+ def test_path_join(self):
+ self.check_join(r"C:\A\B", "C:\\", "A", "B")
+ self.check_join(r"C:\A\B", "D:\\", "D", "C:\\", "A", "B")
+ self.check_join(r"C:\A\B", "C:\\", "A", "C:B")
+ self.check_join(r"C:\A\B", "C:\\", "A\\B")
+ self.check_join(r"C:\A\B", r"C:\A\B")
+
+ self.check_join("D:A", r"D:", "A")
+ self.check_join("D:A", r"C:\B\C", "D:", "A")
+ self.check_join("D:A", r"C:\B\C", r"D:A")
+
+ self.check_join(r"A\B\C", "A", "B", "C")
+ self.check_join(r"A\B\C", "A", r"B\C")
+ self.check_join(r"A\B/C", "A", "B/C")
+ self.check_join(r"A\B\C", "A/", "B\\", "C")
+
+ # Dots are not normalised by this function
+ self.check_join(r"A\../C", "A", "../C")
+ self.check_join(r"A.\.\B", "A.", ".", "B")
+
+ self.check_join(r"\\Server\Share\A\B\C", r"\\Server\Share", "A", "B", "C")
+ self.check_join(r"\\Server\Share\A\B\C", r"\\Server\Share", "D", r"\A", "B", "C")
+ self.check_join(r"\\Server\Share\A\B\C", r"\\Server2\Share2", "D",
+ r"\\Server\Share", "A", "B", "C")
+ self.check_join(r"\\Server\Share\A\B\C", r"\\Server", r"\Share", "A", "B", "C")
+ self.check_join(r"\\Server\Share", r"\\Server\Share")
+ self.check_join(r"\\Server\Share\\", r"\\Server\Share\\")
+
+ # Handle edge cases with empty segments
+ self.check_join("C:\\A", "C:/A", "")
+ self.check_join("C:\\", "C:/", "")
+ self.check_join("C:", "C:", "")
+ self.check_join("//Server/Share\\", "//Server/Share/", "")
+ self.check_join("//Server/Share\\", "//Server/Share", "")