def test_working_dir_nested(self): with tempfile.TemporaryDirectory() as tempdir: cwd = os.getcwd() # Change into the tempdir and use getcwd() so that our asserts # work properly when the temp directory has a symlink. os.chdir(tempdir) real_tempdir = os.getcwd() dir1 = os.path.join(os.getcwd(), '1') dir2 = os.path.join(os.getcwd(), '2') os.mkdir(dir1) os.mkdir(dir2) with working_dir(dir1): self.assertEqual(dir1, os.getcwd()) with working_dir(dir2): self.assertEqual(dir2, os.getcwd()) self.assertEqual(dir1, os.getcwd()) self.assertEqual(real_tempdir, os.getcwd()) os.chdir(cwd)
def test_working_dir(self): """ Verify the workingdir context manager switches to the directory and switches back correctly. :return: """ cwd = os.getcwd() tempdir = tempfile.mkdtemp() # Change into the temp directory and get the cwd value, we can't use # the path for tempdir because the values could be different if # there are symlinks in the path. os.chdir(tempdir) expected_result = os.getcwd() os.chdir(cwd) with working_dir(expected_result): self.assertEqual(os.getcwd(), expected_result) self.assertEqual(os.getcwd(), cwd) os.removedirs(tempdir)