Skip to content

Commit 08b9029

Browse files
committed
Test is_file, is_dir, and walk on symlinks
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 0f35f3f commit 08b9029

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

tests/commoncode/data/symlink/test

Whitespace-only changes.

tests/commoncode/test_filetype.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,25 @@ def test_get_file_count(self):
209209
for test_file, count in tests:
210210
result = filetype.get_file_count(os.path.join(test_dir, test_file))
211211
assert count == result
212+
213+
214+
def SymlinkTest(FileBasedTesting):
215+
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
216+
217+
@skipIf(on_windows, 'os.symlink does not work on Windows')
218+
def test_is_file(self):
219+
test_file = self.get_test_loc('symlink/test', copy=True)
220+
temp_dir = fileutils.get_temp_dir()
221+
test_link = join(temp_dir, 'test-link')
222+
os.symlink(test_file, test_link)
223+
assert filetype.is_file(test_link, allow_symlinks=True)
224+
assert not filetype.is_file(test_link, allow_symlinks=False)
225+
226+
@skipIf(on_windows, 'os.symlink does not work on Windows')
227+
def test_is_dir(self):
228+
test_dir = self.get_test_loc('symlink', copy=True)
229+
temp_dir = fileutils.get_temp_dir()
230+
test_link = join(temp_dir, 'test-dir-link')
231+
os.symlink(test_dir, test_link)
232+
assert filetype.is_dir(test_link, allow_symlinks=True)
233+
assert not filetype.is_dir(test_link, allow_symlinks=False)

tests/commoncode/test_fileutils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,20 @@ def test_os_walk_can_walk_non_utf8_path_from_unicode_path(self):
384384
_dirpath, _dirnames, filenames = result
385385
assert 18 == len(filenames)
386386

387+
@skipIf(on_windows, 'os.symlink does not work on Windows')
388+
def test_walk_on_symlinks(self):
389+
test_dir = self.get_test_loc('symlink', copy=True)
390+
temp_dir = fileutils.get_temp_dir()
391+
test_link = join(temp_dir, 'test-dir-link')
392+
os.symlink(test_dir, test_link)
393+
results = list(fileutils.walk(test_link, allow_symlinks=True))
394+
assert len(results) == 1
395+
top, dirs, files = results[0]
396+
top = os.path.basename(top)
397+
assert 'test-dir-link' == top
398+
assert [] == dirs
399+
assert ['test'] == files
400+
387401

388402
class TestFileUtilsIter(FileBasedTesting):
389403
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

0 commit comments

Comments
 (0)