|
| 1 | +import tempfile |
| 2 | +import unittest |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from .bases import ActionTestsBase |
| 6 | + |
| 7 | + |
| 8 | +def create_filesystem_tree(*args, **kwargs): |
| 9 | + from contextshell.backends.filesystem import FilesystemTree |
| 10 | + return FilesystemTree(*args, **kwargs) |
| 11 | + |
| 12 | + |
| 13 | +class FilesystemActionsTestsBase(ActionTestsBase): |
| 14 | + """Test base for tests wishing to use actual filesystem directory""" |
| 15 | + def _make_test_path(self, relative_path): |
| 16 | + return Path(self.test_directory.name).joinpath(relative_path) |
| 17 | + |
| 18 | + def create_file(self, path: str, contents: str=None): |
| 19 | + full_path = self._make_test_path(path) |
| 20 | + full_path.parent.mkdir(parents=True, exist_ok=True) |
| 21 | + with open(full_path, 'w') as file: |
| 22 | + file.write(contents if contents else '') |
| 23 | + |
| 24 | + def create_directory(self, path): |
| 25 | + full_path = self._make_test_path(path) |
| 26 | + full_path.mkdir(parents=True) |
| 27 | + |
| 28 | + def create_backend(self): |
| 29 | + return create_filesystem_tree(self.test_directory.name) |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + temp_dir_suffix = type(self).__name__ |
| 33 | + self.test_directory = tempfile.TemporaryDirectory(temp_dir_suffix) |
| 34 | + |
| 35 | + super().setUp() |
| 36 | + |
| 37 | + def tearDown(self): |
| 38 | + self.test_directory.cleanup() |
| 39 | + super().tearDown() |
| 40 | + |
| 41 | + |
| 42 | +class ContainsActionTests(FilesystemActionsTestsBase): |
| 43 | + def test_existing_file(self): |
| 44 | + self.create_file('file') |
| 45 | + |
| 46 | + exists = self.execute(".", "contains", 'file') |
| 47 | + |
| 48 | + self.assertTrue(exists) |
| 49 | + |
| 50 | + def test_existing_file_nested(self): |
| 51 | + self.create_file('parent/file') |
| 52 | + |
| 53 | + exists = self.execute(".", "contains", 'parent.file') |
| 54 | + |
| 55 | + self.assertTrue(exists) |
| 56 | + |
| 57 | + def test_existing_directory(self): |
| 58 | + self.create_directory('dir') |
| 59 | + |
| 60 | + exists = self.execute(".", "contains", 'dir') |
| 61 | + |
| 62 | + self.assertTrue(exists) |
| 63 | + |
| 64 | + def test_nonexistent(self): |
| 65 | + exists = self.execute(".", "contains", 'nonexistent') |
| 66 | + |
| 67 | + self.assertFalse(exists) |
| 68 | + |
| 69 | + def test_nonexistent_nested(self): |
| 70 | + self.create_directory('parent') |
| 71 | + |
| 72 | + exists = self.execute(".", "contains", 'parent.path') |
| 73 | + |
| 74 | + self.assertFalse(exists) |
| 75 | + |
| 76 | + |
| 77 | +class GetActionTests(FilesystemActionsTestsBase): |
| 78 | + def test_empty_file(self): |
| 79 | + self.create_file('file') |
| 80 | + |
| 81 | + file_value = self.execute(".file", "get") |
| 82 | + |
| 83 | + self.assertEqual(file_value, '') |
| 84 | + |
| 85 | + def test_file(self): |
| 86 | + self.create_file('file', 'DATA') |
| 87 | + |
| 88 | + file_value = self.execute(".file", "get") |
| 89 | + |
| 90 | + self.assertEqual(file_value, 'DATA') |
| 91 | + |
| 92 | + def test_directory(self): |
| 93 | + self.create_directory('dir') |
| 94 | + |
| 95 | + dir_value = self.execute(".dir", "get") |
| 96 | + |
| 97 | + self.assertEqual(dir_value, None) |
| 98 | + |
| 99 | + |
| 100 | +class IsActionTests(FilesystemActionsTestsBase): |
| 101 | + def test_directory_is_file(self): |
| 102 | + self.create_directory('dir') |
| 103 | + |
| 104 | + is_file = self.execute(".dir", "is.file") |
| 105 | + |
| 106 | + self.assertFalse(is_file) |
| 107 | + |
| 108 | + def test_file_is_file(self): |
| 109 | + self.create_file('file') |
| 110 | + |
| 111 | + is_file = self.execute(".file", "is.file") |
| 112 | + |
| 113 | + self.assertTrue(is_file) |
| 114 | + |
| 115 | + def test_directory_is_directory(self): |
| 116 | + self.create_directory('dir') |
| 117 | + |
| 118 | + is_dir = self.execute(".dir", "is.directory") |
| 119 | + |
| 120 | + self.assertTrue(is_dir) |
| 121 | + |
| 122 | + def test_file_is_directory(self): |
| 123 | + self.create_file('file') |
| 124 | + |
| 125 | + is_dir = self.execute(".file", "is.directory") |
| 126 | + |
| 127 | + self.assertFalse(is_dir) |
| 128 | + |
| 129 | + |
| 130 | +class ListActionTests(FilesystemActionsTestsBase): |
| 131 | + def test_list_file(self): |
| 132 | + self.create_file('file') |
| 133 | + |
| 134 | + file_list = self.execute(".file", "list") |
| 135 | + |
| 136 | + self.assertEqual(file_list, []) |
| 137 | + |
| 138 | + def test_list_empty_directory(self): |
| 139 | + self.create_directory('dir') |
| 140 | + |
| 141 | + file_list = self.execute(".dir", "list") |
| 142 | + |
| 143 | + self.assertEqual(file_list, []) |
| 144 | + |
| 145 | + def test_list_directory_with_file_and_dirs(self): |
| 146 | + self.create_directory('parent/dir') |
| 147 | + self.create_file('parent/file') |
| 148 | + |
| 149 | + file_list = self.execute(".parent", "list") |
| 150 | + |
| 151 | + self.assertIn('dir', file_list) |
| 152 | + self.assertIn('file', file_list) |
| 153 | + |
| 154 | + @unittest.skip("Re-enable when path uses '/' as separator") |
| 155 | + def test_list_hidden(self): |
| 156 | + self.create_directory('parent/.dir') |
| 157 | + self.create_file('parent/.file') |
| 158 | + |
| 159 | + file_list = self.execute(".parent", "list") |
| 160 | + |
| 161 | + self.assertListEqual(file_list, []) |
| 162 | + |
| 163 | + @unittest.skip("Re-enable when path uses '/' as separator") |
| 164 | + def test_list_all(self): |
| 165 | + self.create_directory('parent/.dir') |
| 166 | + self.create_file('parent/.file') |
| 167 | + |
| 168 | + file_list = self.execute(".parent", "list.all") |
| 169 | + |
| 170 | + self.assertIn('@dir', file_list) |
| 171 | + self.assertIn('@file', file_list) |
| 172 | + |
| 173 | + |
| 174 | +class ListActionsActionTests(FilesystemActionsTestsBase): |
| 175 | + def test_list_all(self): |
| 176 | + action_list = self.execute(".", "list.actions") |
| 177 | + |
| 178 | + self.assertSetEqual({'contains', 'get', 'is', 'list'}, set(action_list)) |
| 179 | + |
| 180 | + def test_list_nested(self): |
| 181 | + action_list = self.execute(".", "list.actions", "is") |
| 182 | + |
| 183 | + self.assertSetEqual({'is.file', 'is.directory'}, set(action_list)) |
0 commit comments