Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 410e88d

Browse files
committed
Change tests for imp.cache_from_source() to follow os.path.join/split
semantics.
1 parent 6240bd1 commit 410e88d

1 file changed

Lines changed: 32 additions & 43 deletions

File tree

Lib/test/test_imp.py

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -227,73 +227,62 @@ class PEP3147Tests(unittest.TestCase):
227227
def test_cache_from_source(self):
228228
# Given the path to a .py file, return the path to its PEP 3147
229229
# defined .pyc file (i.e. under __pycache__).
230-
self.assertEqual(
231-
imp.cache_from_source('/foo/bar/baz/qux.py', True),
232-
'/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag))
233-
# Directory with a dot, filename without dot
234-
self.assertEqual(
235-
imp.cache_from_source('/foo.bar/file', True),
236-
'/foo.bar/__pycache__/file{}.pyc'.format(self.tag))
230+
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
231+
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
232+
'qux.{}.pyc'.format(self.tag))
233+
self.assertEqual(imp.cache_from_source(path, True), expect)
234+
235+
def test_cache_from_source_no_dot(self):
236+
# Directory with a dot, filename without dot.
237+
path = os.path.join('foo.bar', 'file')
238+
expect = os.path.join('foo.bar', '__pycache__',
239+
'file{}.pyc'.format(self.tag))
240+
self.assertEqual(imp.cache_from_source(path, True), expect)
237241

238242
def test_cache_from_source_optimized(self):
239243
# Given the path to a .py file, return the path to its PEP 3147
240244
# defined .pyo file (i.e. under __pycache__).
241-
self.assertEqual(
242-
imp.cache_from_source('/foo/bar/baz/qux.py', False),
243-
'/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag))
245+
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
246+
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
247+
'qux.{}.pyo'.format(self.tag))
248+
self.assertEqual(imp.cache_from_source(path, False), expect)
244249

245250
def test_cache_from_source_cwd(self):
246-
self.assertEqual(imp.cache_from_source('foo.py', True),
247-
os.sep.join(('__pycache__',
248-
'foo.{}.pyc'.format(self.tag))))
251+
path = 'foo.py'
252+
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
253+
self.assertEqual(imp.cache_from_source(path, True), expect)
249254

250255
def test_cache_from_source_override(self):
251256
# When debug_override is not None, it can be any true-ish or false-ish
252257
# value.
253-
self.assertEqual(
254-
imp.cache_from_source('/foo/bar/baz.py', []),
255-
'/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag))
256-
self.assertEqual(
257-
imp.cache_from_source('/foo/bar/baz.py', [17]),
258-
'/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag))
258+
path = os.path.join('foo', 'bar', 'baz.py')
259+
partial_expect = os.path.join('foo', 'bar', '__pycache__',
260+
'baz.{}.py'.format(self.tag))
261+
self.assertEqual(imp.cache_from_source(path, []), partial_expect + 'o')
262+
self.assertEqual(imp.cache_from_source(path, [17]),
263+
partial_expect + 'c')
259264
# However if the bool-ishness can't be determined, the exception
260265
# propagates.
261266
class Bearish:
262267
def __bool__(self): raise RuntimeError
263-
self.assertRaises(
264-
RuntimeError,
265-
imp.cache_from_source, '/foo/bar/baz.py', Bearish())
266-
267-
@unittest.skipIf(os.altsep is None,
268-
'test meaningful only where os.altsep is defined')
269-
def test_altsep_cache_from_source(self):
270-
# Windows path and PEP 3147.
271-
self.assertEqual(
272-
imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True),
273-
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
268+
with self.assertRaises(RuntimeError):
269+
imp.cache_from_source('/foo/bar/baz.py', Bearish())
274270

275-
@unittest.skipIf(os.altsep is None,
276-
'test meaningful only where os.altsep is defined')
277-
def test_altsep_and_sep_cache_from_source(self):
278-
# Windows path and PEP 3147 where altsep is right of sep.
279-
self.assertEqual(
280-
imp.cache_from_source('\\foo\\bar/baz\\qux.py', True),
281-
'\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
282-
283-
@unittest.skipIf(os.altsep is None,
271+
@unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
284272
'test meaningful only where os.altsep is defined')
285273
def test_sep_altsep_and_sep_cache_from_source(self):
286274
# Windows path and PEP 3147 where sep is right of altsep.
287275
self.assertEqual(
288276
imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
289-
'\\foo\\bar\\baz/__pycache__/qux.{}.pyc'.format(self.tag))
277+
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
290278

291279
def test_source_from_cache(self):
292280
# Given the path to a PEP 3147 defined .pyc file, return the path to
293281
# its source. This tests the good path.
294-
self.assertEqual(imp.source_from_cache(
295-
'/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)),
296-
'/foo/bar/baz/qux.py')
282+
path = os.path.join('foo', 'bar', 'baz', '__pycache__',
283+
'qux.{}.pyc'.format(self.tag))
284+
expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
285+
self.assertEqual(imp.source_from_cache(path), expect)
297286

298287
def test_source_from_cache_bad_path(self):
299288
# When the path to a pyc file is not in PEP 3147 format, a ValueError

0 commit comments

Comments
 (0)