/usr/lib/python3/dist-packages/pathspec/tests/test_util.py is in python3-pathspec 0.3.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | # encoding: utf-8
"""
This script tests utility functions.
"""
import os
import os.path
import shutil
import sys
import tempfile
import unittest
from pathspec.util import iter_tree, RecursionError
class IterTreeTest(unittest.TestCase):
"""
The ``IterTreeTest`` class tests `pathspec.util.iter_tree()`.
"""
def make_dirs(self, dirs):
"""
Create the specified directories.
"""
for dir in dirs:
os.mkdir(os.path.join(self.temp_dir, self.ospath(dir)))
def make_files(self, files):
"""
Create the specified files.
"""
for file in files:
self.mkfile(os.path.join(self.temp_dir, self.ospath(file)))
def make_links(self, links):
"""
Create the specified links.
"""
for link, node in links:
os.symlink(os.path.join(self.temp_dir, self.ospath(node)), os.path.join(self.temp_dir, self.ospath(link)))
@staticmethod
def mkfile(file):
"""
Creates an empty file.
"""
with open(file, 'wb'):
pass
@staticmethod
def ospath(path):
"""
Convert the POSIX path to a native OS path.
"""
return os.path.join(*path.split('/'))
def require_realpath(self):
"""
Skips the test if `os.path.realpath` does not properly support
symlinks.
"""
if self.broken_realpath:
raise unittest.SkipTest("`os.path.realpath` is broken.")
def require_symlink(self):
"""
Skips the test if `os.symlink` is not supported.
"""
if self.no_symlink:
raise unittest.SkipTest("`os.symlink` is not supported.")
def setUp(self):
"""
Called before each test.
"""
self.temp_dir = tempfile.mkdtemp()
def tearDown(self):
"""
Called after each test.
"""
shutil.rmtree(self.temp_dir)
def test_1_files(self):
"""
Tests to make sure all files are found.
"""
self.make_dirs([
'Empty',
'Dir',
'Dir/Inner',
])
self.make_files([
'a',
'b',
'Dir/c',
'Dir/d',
'Dir/Inner/e',
'Dir/Inner/f',
])
results = set(iter_tree(self.temp_dir))
self.assertEqual(results, set(map(self.ospath, {
'a',
'b',
'Dir/c',
'Dir/d',
'Dir/Inner/e',
'Dir/Inner/f',
})))
def test_2_0_check_symlink(self):
"""
Tests whether links can be created.
"""
no_symlink = None
try:
file = os.path.join(self.temp_dir, 'file')
link = os.path.join(self.temp_dir, 'link')
self.mkfile(file)
try:
os.symlink(file, link)
except (AttributeError, NotImplementedError):
no_symlink = True
raise
no_symlink = False
finally:
self.__class__.no_symlink = no_symlink
def test_2_1_check_realpath(self):
"""
Tests whether `os.path.realpath` works properly with symlinks.
"""
broken_realpath = None
try:
self.require_symlink()
file = os.path.join(self.temp_dir, 'file')
link = os.path.join(self.temp_dir, 'link')
self.mkfile(file)
os.symlink(file, link)
try:
self.assertEqual(os.path.realpath(file), os.path.realpath(link))
except AssertionError:
broken_realpath = True
raise
broken_realpath = False
finally:
self.__class__.broken_realpath = broken_realpath
def test_2_2_links(self):
"""
Tests to make sure links to directories and files work.
"""
self.require_symlink()
self.make_dirs([
'Dir'
])
self.make_files([
'a',
'b',
'Dir/c',
'Dir/d',
])
self.make_links([
('ax', 'a'),
('bx', 'b'),
('Dir/cx', 'Dir/c'),
('Dir/dx', 'Dir/d'),
])
results = set(iter_tree(self.temp_dir))
self.assertEqual(results, set(map(self.ospath, {
'a',
'ax',
'b',
'bx',
'Dir/c',
'Dir/cx',
'Dir/d',
'Dir/dx',
})))
def test_2_3_sideways_links(self):
"""
Tests to make sure the same directory can be encountered multiple
times via links.
"""
self.require_symlink()
self.make_dirs([
'Dir',
'Dir/Target',
])
self.make_files([
'Dir/Target/file',
])
self.make_links([
('Ax', 'Dir'),
('Bx', 'Dir'),
('Cx', 'Dir/Target'),
('Dx', 'Dir/Target'),
('Dir/Ex', 'Dir/Target'),
('Dir/Fx', 'Dir/Target'),
])
results = set(iter_tree(self.temp_dir))
self.assertEqual(results, set(map(self.ospath, {
'Ax/Ex/file',
'Ax/Fx/file',
'Ax/Target/file',
'Bx/Ex/file',
'Bx/Fx/file',
'Bx/Target/file',
'Cx/file',
'Dx/file',
'Dir/Ex/file',
'Dir/Fx/file',
'Dir/Target/file',
})))
def test_2_4_recursive_links(self):
"""
Tests detection of recursive links.
"""
self.require_symlink()
self.require_realpath()
self.make_dirs([
'Dir',
])
self.make_files([
'Dir/file',
])
self.make_links([
('Dir/Self', 'Dir'),
])
with self.assertRaises(RecursionError) as context:
set(iter_tree(self.temp_dir))
self.assertEqual(context.exception.first_path, 'Dir')
self.assertEqual(context.exception.second_path, self.ospath('Dir/Self'))
def test_2_5_recursive_circular_links(self):
"""
Tests detection of recursion through circular links.
"""
self.require_symlink()
self.require_realpath()
self.make_dirs([
'A',
'B',
'C',
])
self.make_files([
'A/d',
'B/e',
'C/f'
])
self.make_links([
('A/Bx', 'B'),
('B/Cx', 'C'),
('C/Ax', 'A'),
])
with self.assertRaises(RecursionError) as context:
set(iter_tree(self.temp_dir))
self.assertIn(context.exception.first_path, ('A', 'B', 'C'))
self.assertEqual(context.exception.second_path, {
'A': self.ospath('A/Bx/Cx/Ax'),
'B': self.ospath('B/Cx/Ax/Bx'),
'C': self.ospath('C/Ax/Bx/Cx'),
}[context.exception.first_path])
|