Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-28113: Convert Win32{Symlink,Junction}Tests to use support.TESTFN
  • Loading branch information
berkerpeksag committed Apr 22, 2019
commit 2cc093917a4e217dff20e39398991d2acee68748
57 changes: 21 additions & 36 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,26 +2148,25 @@ def test_bytes(self):
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
@support.skip_unless_symlink
class Win32SymlinkTests(unittest.TestCase):
filelink = 'filelinktest'
filelink_target = os.path.abspath(__file__)
dirlink = 'dirlinktest'
dirlink_target = os.path.dirname(filelink_target)
missing_link = 'missing link'
dirlink_target = os.path.join(support.TESTFN, 'win32_symlink_tests')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support.TESTFN is just a path, but not an existing directory.

Perhaps add something the following to setUp()?

        tempdir = tempfile.TemporaryDirectory(prefix=support.TESTFN)
        self.addCleanup(tempdir.cleanup)

        self.dirlink_target = os.path.join(tempdir , 'win32_symlink_tests')
        self.filelink_target = os.path.join(self.dirlink_target, support.TESTFN)
        self.dirlink = os.path.join(tempdir , 'dirlinktest')
        self.filelink = os.path.join(tempdir , 'filelinktest')
        self.missing_link = os.path.join(tempdir , 'missinglinktest')

With this all of the other existence checks and cleanups may be removed.

filelink_target = os.path.join(dirlink_target, support.TESTFN)
dirlink = os.path.join(support.TESTFN, 'dirlinktest')
filelink = os.path.join(support.TESTFN, 'filelinktest')
missing_link = os.path.join(support.TESTFN, 'missinglinktest')

def setUp(self):
assert os.path.exists(self.dirlink_target)
assert os.path.exists(self.filelink_target)
assert not os.path.exists(self.dirlink)
assert not os.path.exists(self.filelink)
assert not os.path.exists(self.missing_link)

def tearDown(self):
if os.path.exists(self.filelink):
os.remove(self.filelink)
if os.path.exists(self.dirlink):
os.rmdir(self.dirlink)
if os.path.lexists(self.missing_link):
os.remove(self.missing_link)
self.assertFalse(os.path.lexists(self.dirlink))
self.assertFalse(os.path.lexists(self.filelink))
self.assertFalse(os.path.lexists(self.missing_link))
self.assertFalse(os.path.lexists(self.dirlink_target))
self.assertFalse(os.path.lexists(self.filelink_target))
self.addCleanup(support.rmtree, self.dirlink_target)
self.addCleanup(support.rmdir, self.dirlink)
self.addCleanup(support.rmdir, self.missing_link)
self.addCleanup(support.unlink, self.missing_link)
self.addCleanup(support.unlink, self.filelink)
os.mkdir(self.dirlink_target)
create_file(self.filelink_target)

def test_directory_link(self):
os.symlink(self.dirlink_target, self.dirlink)
Expand Down Expand Up @@ -2200,16 +2199,12 @@ def test_remove_directory_link_to_missing_target(self):
# was created with target_is_dir==True.
os.remove(self.missing_link)

@unittest.skip("currently fails; consider for improvement")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this no longer skipped: has this behavior been implemented?

Likewise for the next test below.

def test_isdir_on_directory_link_to_missing_target(self):
self._create_missing_dir_link()
# consider having isdir return true for directory links
self.assertTrue(os.path.isdir(self.missing_link))

@unittest.skip("currently fails; consider for improvement")
def test_rmdir_on_directory_link_to_missing_target(self):
self._create_missing_dir_link()
# consider allowing rmdir to remove directory links
os.rmdir(self.missing_link)

def check_stat(self, link, target):
Expand All @@ -2221,21 +2216,13 @@ def check_stat(self, link, target):
self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link))

def test_12084(self):
level1 = os.path.abspath(support.TESTFN)
file1 = self.filelink_target
level1 = self.dirlink_target
level2 = os.path.join(level1, "level2")
level3 = os.path.join(level2, "level3")
self.addCleanup(support.rmtree, level1)

os.mkdir(level1)
os.mkdir(level2)
os.mkdir(level3)
os.makedirs(level3)

file1 = os.path.abspath(os.path.join(level1, "file1"))
create_file(file1)

orig_dir = os.getcwd()
try:
os.chdir(level2)
with support.change_cwd(level2):
link = os.path.join(level2, "link")
os.symlink(os.path.relpath(file1), "link")
self.assertIn("link", os.listdir(os.getcwd()))
Expand All @@ -2252,8 +2239,6 @@ def test_12084(self):
os.chdir(level3)
self.assertEqual(os.stat(file1),
os.stat(os.path.relpath(link)))
finally:
os.chdir(orig_dir)

@unittest.skipUnless(os.path.lexists(r'C:\Users\All Users')
and os.path.exists(r'C:\ProgramData'),
Expand Down