# HG changeset patch # User Franz Glasner # Date 1739903440 -3600 # Node ID b4137ebd8e795dcdd073a92c5d52d7ab65b906d9 # Parent 6fe88de236cb9c7b1cd9adfb32fe354df6c0d8ef Unittests for WalkDirEntry: is_dir, is_symlink and basic stat diff -r 6fe88de236cb -r b4137ebd8e79 tests/test_walk.py --- a/tests/test_walk.py Tue Feb 18 18:24:16 2025 +0100 +++ b/tests/test_walk.py Tue Feb 18 19:30:40 2025 +0100 @@ -5,10 +5,11 @@ from __future__ import absolute_import, print_function +import os import sys import unittest -import _test_setup # noqa: F401 imported but unused +from _test_setup import DATADIR from cutils.util import walk @@ -196,5 +197,60 @@ self.assertEqual(b"tests\\xc5/test", entry.alt_u8path) +# https://stackoverflow.com/questions/1854/how-to-identify-which-os-python-is-running-on/58071295#58071295 +@unittest.skipIf( + os.name != "posix", "Only supported on POSIX (needs real symlinks)") +class SymlinkTests(unittest.TestCase): + + def test_symlink_to_directory(self): + entry = walk.WalkDirEntry.from_path_name( + DATADIR, "tree-1-s") + self.assertTrue(entry.is_symlink) + self.assertTrue(entry.is_dir) + self.assertIsNotNone(entry.stat) + + def test_broken_symlink_to_directory(self): + entry = walk.WalkDirEntry.from_path_name( + DATADIR, "tree-nn-s") + self.assertTrue(entry.is_symlink) + self.assertFalse(entry.is_dir) + self.assertIsNone(entry.stat) + + def test_real_directory(self): + entry = walk.WalkDirEntry.from_path_name( + DATADIR, "tree-1") + self.assertFalse(entry.is_symlink) + self.assertTrue(entry.is_dir) + self.assertIsNotNone(entry.stat) + + def test_symlink_to_file(self): + entry = walk.WalkDirEntry.from_path_name( + os.path.join(DATADIR, "tree-1"), "file-1-1-s.txt") + self.assertTrue(entry.is_symlink) + self.assertFalse(entry.is_dir) + self.assertIsNotNone(entry.stat) + + def test_broken_symlink_to_file(self): + entry = walk.WalkDirEntry.from_path_name( + os.path.join(DATADIR, "tree-1"), "file-1-nn-s.txt") + self.assertTrue(entry.is_symlink) + self.assertFalse(entry.is_dir) + self.assertIsNone(entry.stat) + + def test_real_file(self): + entry = walk.WalkDirEntry.from_path_name( + os.path.join(DATADIR, "tree-1"), "file-1-1.txt") + self.assertFalse(entry.is_symlink) + self.assertFalse(entry.is_dir) + self.assertIsNotNone(entry.stat) + + def test_real_file_via_dir_symlink(self): + entry = walk.WalkDirEntry.from_path_name( + os.path.join(DATADIR, "tree-1-s"), "file-1-1.txt") + self.assertFalse(entry.is_symlink) + self.assertFalse(entry.is_dir) + self.assertIsNotNone(entry.stat) + + if __name__ == "__main__": sys.exit(unittest.main())