| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 3 |
# |
| 4 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 5 |
# later. See the COPYING file in the top-level directory. |
| 6 |
|
| 7 |
""" |
| 8 |
Test case for VFS |
| 9 |
""" |
| 10 |
|
| 11 |
import os |
| 12 |
|
| 13 |
from tests import common |
| 14 |
|
| 15 |
from bleachbit import IS_POSIX, IS_WINDOWS |
| 16 |
from bleachbit.VFS import ListVFS, RealVFS, VFS |
| 17 |
|
| 18 |
|
| 19 |
class VFSTestCase(common.BleachbitTestCase): |
| 20 |
"""Test case for VFS.""" |
| 21 |
|
| 22 |
def test_VFS_abstract(self): |
| 23 |
"""Test VFS base class is abstract""" |
| 24 |
vfs = VFS() |
| 25 |
with self.assertRaises(NotImplementedError): |
| 26 |
vfs.listdir('/') |
| 27 |
with self.assertRaises(NotImplementedError): |
| 28 |
vfs.isdir('/') |
| 29 |
|
| 30 |
def test_RealVFS(self): |
| 31 |
"""Test RealVFS against the real filesystem""" |
| 32 |
vfs = RealVFS() |
| 33 |
# Test against the actual root |
| 34 |
self.assertTrue(vfs.isdir('/')) |
| 35 |
root_children = vfs.listdir('/' if IS_POSIX else 'C:\\') |
| 36 |
self.assertIsInstance(root_children, list) |
| 37 |
self.assertGreater(len(root_children), 0) |
| 38 |
self.assertTrue(all(isinstance(child, str) for child in root_children)) |
| 39 |
if IS_POSIX: |
| 40 |
self.assertIn('tmp', root_children) |
| 41 |
if IS_WINDOWS: |
| 42 |
self.assertIn('Users', root_children) |
| 43 |
|
| 44 |
# Test against the temporary directory |
| 45 |
self.assertTrue(vfs.isdir(self.tempdir)) |
| 46 |
self.assertFalse(vfs.isdir(os.path.join(self.tempdir, 'nonexistent'))) |
| 47 |
|
| 48 |
def test_ListVFS_basic(self): |
| 49 |
"""Test ListVFS with a basic path list""" |
| 50 |
vfs = ListVFS(['/foo/bar', '/foo/baz']) |
| 51 |
|
| 52 |
self.assertTrue(vfs.isdir('/')) |
| 53 |
self.assertTrue(vfs.isdir('/foo')) |
| 54 |
self.assertFalse(vfs.isdir('/foo/bar')) |
| 55 |
self.assertFalse(vfs.isdir('/foo/baz')) |
| 56 |
self.assertFalse(vfs.isdir('/foo/qux')) |
| 57 |
|
| 58 |
self.assertEqual(vfs.listdir('/'), ['foo']) |
| 59 |
self.assertEqual(vfs.listdir('/foo'), ['bar', 'baz']) |
| 60 |
with self.assertRaises(NotADirectoryError): |
| 61 |
vfs.listdir('/foo/bar') |
| 62 |
|
| 63 |
def test_ListVFS_comments_and_blanks(self): |
| 64 |
"""Test ListVFS ignores comments and blank lines""" |
| 65 |
vfs = ListVFS(['', '# comment', '/file1']) |
| 66 |
self.assertEqual(vfs.listdir('/'), ['file1']) |
| 67 |
self.assertFalse(vfs.isdir('/file1')) |
| 68 |
self.assertFalse(vfs.isdir('# comment')) |
| 69 |
|
| 70 |
def test_ListVFS_no_leading_slash(self): |
| 71 |
"""Test ListVFS adds leading slash if missing""" |
| 72 |
vfs = ListVFS(['foo/bar']) |
| 73 |
self.assertTrue(vfs.isdir('/foo')) |
| 74 |
self.assertFalse(vfs.isdir('/foo/bar')) |
| 75 |
|
| 76 |
def test_ListVFS_trailing_slash(self): |
| 77 |
"""Test ListVFS handles trailing slashes""" |
| 78 |
vfs = ListVFS(['/foo/bar/']) |
| 79 |
self.assertTrue(vfs.isdir('/foo')) |
| 80 |
self.assertTrue(vfs.isdir('/foo/bar')) |
| 81 |
self.assertEqual(vfs.listdir('/foo'), ['bar']) |
| 82 |
|
| 83 |
def test_ListVFS_empty_path(self): |
| 84 |
"""Test ListVFS with an empty path list""" |
| 85 |
vfs = ListVFS([]) |
| 86 |
self.assertTrue(vfs.isdir('/')) |
| 87 |
self.assertEqual(vfs.listdir('/'), []) |
| 88 |
|
| 89 |
def test_ListVFS_listdir_non_directory(self): |
| 90 |
"""Test ListVFS raises for files and non-existent directories""" |
| 91 |
vfs = ListVFS(['/foo/bar']) |
| 92 |
with self.assertRaises(NotADirectoryError): |
| 93 |
vfs.listdir('/foo/bar') |
| 94 |
with self.assertRaises(FileNotFoundError): |
| 95 |
vfs.listdir('/foo/qux') |
| 96 |
|
| 97 |
def test_ListVFS_isdir_false(self): |
| 98 |
"""Test ListVFS.isdir returns False for files and non-existent paths""" |
| 99 |
vfs = ListVFS(['/foo/bar']) |
| 100 |
self.assertFalse(vfs.isdir('/foo/bar')) |
| 101 |
self.assertFalse(vfs.isdir('/foo/qux')) |
| 102 |
self.assertFalse(vfs.isdir('/baz')) |