def test_cache_from_source_respects_pycache_prefix(self): # If pycache_prefix is set, cache_from_source will return a bytecode # path inside that directory (in a subdirectory mirroring the .py file's # path) rather than in a __pycache__ dir next to the py file. pycache_prefixes = [ os.path.join(os.path.sep, 'tmp', 'bytecode'), os.path.join(os.path.sep, 'tmp', '\u2603'), # non-ASCII in path! os.path.join(os.path.sep, 'tmp', 'trailing-slash') + os.path.sep, ] drive = '' if os.name == 'nt': drive = 'C:' pycache_prefixes = [ f'{drive}{prefix}' for prefix in pycache_prefixes] pycache_prefixes += [r'\\?\C:\foo', r'\\localhost\c$\bar'] for pycache_prefix in pycache_prefixes: with self.subTest(path=pycache_prefix): path = drive + os.path.join( os.path.sep, 'foo', 'bar', 'baz', 'qux.py') expect = os.path.join( pycache_prefix, 'foo', 'bar', 'baz', 'qux.{}.pyc'.format(self.tag)) with util.temporary_pycache_prefix(pycache_prefix): self.assertEqual( self.util.cache_from_source(path, optimization=''), expect)
def test_source_from_cache_outside_pycache_prefix(self): # If pycache_prefix is set but the cache path we get is not inside # it, just ignore it and handle the cache path according to the default # behavior. pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode') path = os.path.join('foo', 'bar', 'baz', '__pycache__', f'qux.{self.tag}.pyc') expect = os.path.join('foo', 'bar', 'baz', 'qux.py') with util.temporary_pycache_prefix(pycache_prefix): self.assertEqual(self.util.source_from_cache(path), expect)
def test_source_from_cache_inside_pycache_prefix(self): # If pycache_prefix is set and the cache path we get is inside it, # we return an absolute path to the py file based on the remainder of # the path within pycache_prefix. pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode') path = os.path.join(pycache_prefix, 'foo', 'bar', 'baz', f'qux.{self.tag}.pyc') expect = os.path.join(os.path.sep, 'foo', 'bar', 'baz', 'qux.py') with util.temporary_pycache_prefix(pycache_prefix): self.assertEqual(self.util.source_from_cache(path), expect)
def test_cache_from_source_respects_pycache_prefix_relative(self): # If the .py path we are given is relative, we will resolve to an # absolute path before prefixing with pycache_prefix, to avoid any # possible ambiguity. pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode') path = os.path.join('foo', 'bar', 'baz', 'qux.py') root = os.path.splitdrive(os.getcwd())[0] + os.path.sep expect = os.path.join(pycache_prefix, os.path.relpath(os.getcwd(), root), 'foo', 'bar', 'baz', f'qux.{self.tag}.pyc') with util.temporary_pycache_prefix(pycache_prefix): self.assertEqual( self.util.cache_from_source(path, optimization=''), expect)