Mercurial > hgrepos > Python > apps > py-cutils
comparison tests/test_walk.py @ 272:b4137ebd8e79
Unittests for WalkDirEntry: is_dir, is_symlink and basic stat
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 18 Feb 2025 19:30:40 +0100 |
| parents | 58d93453c307 |
| children | c02a57df2a29 |
comparison
equal
deleted
inserted
replaced
| 271:6fe88de236cb | 272:b4137ebd8e79 |
|---|---|
| 3 | 3 |
| 4 """ | 4 """ |
| 5 | 5 |
| 6 from __future__ import absolute_import, print_function | 6 from __future__ import absolute_import, print_function |
| 7 | 7 |
| 8 import os | |
| 8 import sys | 9 import sys |
| 9 import unittest | 10 import unittest |
| 10 | 11 |
| 11 import _test_setup # noqa: F401 imported but unused | 12 from _test_setup import DATADIR |
| 12 | 13 |
| 13 from cutils.util import walk | 14 from cutils.util import walk |
| 14 | 15 |
| 15 | 16 |
| 16 class SurrogateEscapeTests(unittest.TestCase): | 17 class SurrogateEscapeTests(unittest.TestCase): |
| 194 | 195 |
| 195 self.assertEqual(b"test", entry.alt_u8name) | 196 self.assertEqual(b"test", entry.alt_u8name) |
| 196 self.assertEqual(b"tests\\xc5/test", entry.alt_u8path) | 197 self.assertEqual(b"tests\\xc5/test", entry.alt_u8path) |
| 197 | 198 |
| 198 | 199 |
| 200 # https://stackoverflow.com/questions/1854/how-to-identify-which-os-python-is-running-on/58071295#58071295 | |
| 201 @unittest.skipIf( | |
| 202 os.name != "posix", "Only supported on POSIX (needs real symlinks)") | |
| 203 class SymlinkTests(unittest.TestCase): | |
| 204 | |
| 205 def test_symlink_to_directory(self): | |
| 206 entry = walk.WalkDirEntry.from_path_name( | |
| 207 DATADIR, "tree-1-s") | |
| 208 self.assertTrue(entry.is_symlink) | |
| 209 self.assertTrue(entry.is_dir) | |
| 210 self.assertIsNotNone(entry.stat) | |
| 211 | |
| 212 def test_broken_symlink_to_directory(self): | |
| 213 entry = walk.WalkDirEntry.from_path_name( | |
| 214 DATADIR, "tree-nn-s") | |
| 215 self.assertTrue(entry.is_symlink) | |
| 216 self.assertFalse(entry.is_dir) | |
| 217 self.assertIsNone(entry.stat) | |
| 218 | |
| 219 def test_real_directory(self): | |
| 220 entry = walk.WalkDirEntry.from_path_name( | |
| 221 DATADIR, "tree-1") | |
| 222 self.assertFalse(entry.is_symlink) | |
| 223 self.assertTrue(entry.is_dir) | |
| 224 self.assertIsNotNone(entry.stat) | |
| 225 | |
| 226 def test_symlink_to_file(self): | |
| 227 entry = walk.WalkDirEntry.from_path_name( | |
| 228 os.path.join(DATADIR, "tree-1"), "file-1-1-s.txt") | |
| 229 self.assertTrue(entry.is_symlink) | |
| 230 self.assertFalse(entry.is_dir) | |
| 231 self.assertIsNotNone(entry.stat) | |
| 232 | |
| 233 def test_broken_symlink_to_file(self): | |
| 234 entry = walk.WalkDirEntry.from_path_name( | |
| 235 os.path.join(DATADIR, "tree-1"), "file-1-nn-s.txt") | |
| 236 self.assertTrue(entry.is_symlink) | |
| 237 self.assertFalse(entry.is_dir) | |
| 238 self.assertIsNone(entry.stat) | |
| 239 | |
| 240 def test_real_file(self): | |
| 241 entry = walk.WalkDirEntry.from_path_name( | |
| 242 os.path.join(DATADIR, "tree-1"), "file-1-1.txt") | |
| 243 self.assertFalse(entry.is_symlink) | |
| 244 self.assertFalse(entry.is_dir) | |
| 245 self.assertIsNotNone(entry.stat) | |
| 246 | |
| 247 def test_real_file_via_dir_symlink(self): | |
| 248 entry = walk.WalkDirEntry.from_path_name( | |
| 249 os.path.join(DATADIR, "tree-1-s"), "file-1-1.txt") | |
| 250 self.assertFalse(entry.is_symlink) | |
| 251 self.assertFalse(entry.is_dir) | |
| 252 self.assertIsNotNone(entry.stat) | |
| 253 | |
| 254 | |
| 199 if __name__ == "__main__": | 255 if __name__ == "__main__": |
| 200 sys.exit(unittest.main()) | 256 sys.exit(unittest.main()) |
