1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from pathlib import Path
from tempfile import TemporaryDirectory
from libcst.helpers.paths import chdir
from libcst.testing.utils import UnitTest
class PathsTest(UnitTest):
def test_chdir(self) -> None:
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
inner = tdp / "foo" / "bar"
inner.mkdir(parents=True)
with self.subTest("string paths"):
cwd1 = Path.cwd()
with chdir(tdp.as_posix()) as path2:
cwd2 = Path.cwd()
self.assertEqual(tdp, cwd2)
self.assertEqual(tdp, path2)
with chdir(inner.as_posix()) as path3:
cwd3 = Path.cwd()
self.assertEqual(inner, cwd3)
self.assertEqual(inner, path3)
cwd4 = Path.cwd()
self.assertEqual(tdp, cwd4)
self.assertEqual(cwd2, cwd4)
cwd5 = Path.cwd()
self.assertEqual(cwd1, cwd5)
with self.subTest("pathlib objects"):
cwd1 = Path.cwd()
with chdir(tdp) as path2:
cwd2 = Path.cwd()
self.assertEqual(tdp, cwd2)
self.assertEqual(tdp, path2)
with chdir(inner) as path3:
cwd3 = Path.cwd()
self.assertEqual(inner, cwd3)
self.assertEqual(inner, path3)
cwd4 = Path.cwd()
self.assertEqual(tdp, cwd4)
self.assertEqual(cwd2, cwd4)
cwd5 = Path.cwd()
self.assertEqual(cwd1, cwd5)
|