|
| 1 | +import frontmatter |
| 2 | + |
| 3 | +from unittest import TestCase |
| 4 | +from pathlib import Path |
| 5 | +from tempfile import TemporaryDirectory |
| 6 | +from foliant.partial_copy import PartialCopy |
| 7 | +from foliant.cli.make import Cli |
| 8 | + |
| 9 | + |
| 10 | +class TestPartialCopy(TestCase): |
| 11 | + def setUp(self): |
| 12 | + # Create a temporary directory for testing |
| 13 | + self.test_dir = TemporaryDirectory() |
| 14 | + self.source_dir = Path(self.test_dir.name) / "source" |
| 15 | + self.destination_dir = Path(self.test_dir.name) / "destination" |
| 16 | + self.source_dir.mkdir() |
| 17 | + self.destination_dir.mkdir() |
| 18 | + |
| 19 | + # Create some test files |
| 20 | + (self.source_dir / "file1.txt").write_text("Hello, file1!") |
| 21 | + (self.source_dir / "file2.txt").write_text("Hello, file2!") |
| 22 | + (self.source_dir / "subfolder").mkdir() |
| 23 | + (self.source_dir / "subfolder" / "file3.txt").write_text("Hello, file3!") |
| 24 | + (self.source_dir / "file2.md").write_text("# Header\nSome content") |
| 25 | + (self.source_dir / "subfolder" / "file3.md").write_text("# Another Header\nMore content") |
| 26 | + (self.source_dir / "file4.md").write_text("---\nkey: value\n---\n# Header\nSome content") |
| 27 | + |
| 28 | + def tearDown(self): |
| 29 | + # Clean up the temporary directory |
| 30 | + self.test_dir.cleanup() |
| 31 | + |
| 32 | + def test_copy_single_file(self): |
| 33 | + # Test copying a single file |
| 34 | + source_file = self.source_dir / "file1.txt" |
| 35 | + PartialCopy.partial_copy(source_file, self.source_dir, self.destination_dir) |
| 36 | + |
| 37 | + # Check if the file was copied |
| 38 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 39 | + self.assertEqual((self.destination_dir / "file1.txt").read_text(), "Hello, file1!") |
| 40 | + |
| 41 | + def test_copy_list_of_files(self): |
| 42 | + # Test copying a list of files |
| 43 | + source_files = [ |
| 44 | + self.source_dir / "file1.txt", |
| 45 | + self.source_dir / "file2.txt" |
| 46 | + ] |
| 47 | + |
| 48 | + PartialCopy.partial_copy(source_files, self.source_dir, self.destination_dir) |
| 49 | + |
| 50 | + # Check if the files were copied |
| 51 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 52 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 53 | + |
| 54 | + def test_copy_list_of_files_two(self): |
| 55 | + # Test copying a list of files |
| 56 | + source_files = [ |
| 57 | + str(self.source_dir / "file1.txt"), |
| 58 | + str(self.source_dir / "file2.md") |
| 59 | + ] |
| 60 | + PartialCopy.partial_copy(source_files, self.source_dir, self.destination_dir) |
| 61 | + |
| 62 | + # Check if the files were copied |
| 63 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 64 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 65 | + |
| 66 | + def test_copy_glob_pattern(self): |
| 67 | + # Test copying files matching a glob pattern |
| 68 | + glob_pattern = str(self.source_dir / "*.txt") |
| 69 | + glob_pattern = Cli.prepare_list_of_file(glob_pattern, self.destination_dir) |
| 70 | + PartialCopy.partial_copy(glob_pattern, self.source_dir, self.destination_dir) |
| 71 | + |
| 72 | + # Check if the files were copied |
| 73 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 74 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 75 | + self.assertFalse((self.destination_dir / "file3.txt").exists()) # file3 is in a subfolder |
| 76 | + |
| 77 | + def test_copy_glob_pattern_md(self): |
| 78 | + # Test copying files matching a glob pattern |
| 79 | + glob_pattern = str(self.source_dir / '*2.md') |
| 80 | + glob_pattern = Cli.prepare_list_of_file(glob_pattern, self.destination_dir) |
| 81 | + PartialCopy.partial_copy(glob_pattern, self.source_dir, self.destination_dir) |
| 82 | + |
| 83 | + # Check if the files were copied |
| 84 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 85 | + # Markdown files should be processed (content removed, frontmatter added) |
| 86 | + content = (self.destination_dir / "file2.md").read_text() |
| 87 | + self.assertIn("# Header\nSome content", content) |
| 88 | + |
| 89 | + def test_copy_glob_pattern_recursive(self): |
| 90 | + # Test copying files matching a recursive glob pattern |
| 91 | + glob_pattern = str(self.source_dir / "**" / "*.txt") |
| 92 | + glob_pattern = Cli.prepare_list_of_file(glob_pattern, self.destination_dir) |
| 93 | + PartialCopy.partial_copy(glob_pattern, self.source_dir, self.destination_dir) |
| 94 | + |
| 95 | + # Check if the files were copied, including the one in the subfolder |
| 96 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 97 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 98 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.txt").exists()) |
| 99 | + |
| 100 | + def test_copy_directory_structure(self): |
| 101 | + # Test copying a file while preserving directory structure |
| 102 | + source_file = self.source_dir / "subfolder" / "file3.txt" |
| 103 | + PartialCopy.partial_copy(source_file, self.source_dir, self.destination_dir) |
| 104 | + |
| 105 | + # Check if the file was copied with the directory structure |
| 106 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.txt").exists()) |
| 107 | + |
| 108 | + def test_copy_nonexistent_file(self): |
| 109 | + # Test copying a nonexistent file (should not raise error, just warn) |
| 110 | + source_file = self.source_dir / "nonexistent.txt" |
| 111 | + try: |
| 112 | + PartialCopy.partial_copy(source_file, self.source_dir, self.destination_dir) |
| 113 | + # Should not raise error, just print warning |
| 114 | + except Exception as e: |
| 115 | + self.fail(f"partial_copy raised unexpected exception: {e}") |
| 116 | + |
| 117 | + def test_copy_nonexistent_glob(self): |
| 118 | + # Test copying with a glob pattern that matches no files |
| 119 | + glob_pattern = str(self.source_dir / "*_suffix.md") |
| 120 | + glob_pattern = Cli.prepare_list_of_file(glob_pattern, self.destination_dir) |
| 121 | + try: |
| 122 | + PartialCopy.partial_copy(glob_pattern, self.source_dir, self.destination_dir) |
| 123 | + # Should not raise error for empty glob results |
| 124 | + except Exception as e: |
| 125 | + self.fail(f"partial_copy raised unexpected exception: {e}") |
| 126 | + |
| 127 | + def test_copy_to_nonexistent_destination(self): |
| 128 | + # Test copying to a nonexistent destination (should create the destination folder) |
| 129 | + new_destination = self.destination_dir / "new_folder" |
| 130 | + PartialCopy.partial_copy(self.source_dir / "file1.txt", self.source_dir, new_destination) |
| 131 | + |
| 132 | + # Check if the file was copied and the destination folder was created |
| 133 | + self.assertTrue(new_destination.exists()) |
| 134 | + self.assertTrue((new_destination / "file1.txt").exists()) |
| 135 | + |
| 136 | + def test_copy_path_object(self): |
| 137 | + # Test copying using Path objects |
| 138 | + source_file = self.source_dir / "file1.txt" |
| 139 | + PartialCopy.partial_copy(source_file, self.source_dir, self.destination_dir) |
| 140 | + |
| 141 | + # Check if the file was copied |
| 142 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 143 | + |
| 144 | + def test_copy_text_file(self): |
| 145 | + # Test copying a text file |
| 146 | + PartialCopy.partial_copy(str(self.source_dir / "file1.txt"), self.source_dir, self.destination_dir) |
| 147 | + |
| 148 | + # Check if the file was copied |
| 149 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 150 | + self.assertEqual((self.destination_dir / "file1.txt").read_text(), "Hello, file1!") |
| 151 | + |
| 152 | + def test_copy_markdown_file(self): |
| 153 | + # Test copying a Markdown file |
| 154 | + PartialCopy.partial_copy(str(self.source_dir / "file2.md"), self.source_dir, self.destination_dir) |
| 155 | + |
| 156 | + # Check if the file was copied with frontmatter |
| 157 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 158 | + content = (self.destination_dir / "file2.md").read_text() |
| 159 | + self.assertIn("# Header\nSome content", content) |
| 160 | + |
| 161 | + def test_copy_directory_structure_with_header(self): |
| 162 | + # Test copying a file while preserving directory structure |
| 163 | + PartialCopy.partial_copy( |
| 164 | + str(self.source_dir / "subfolder" / "file3.md"), |
| 165 | + self.source_dir, |
| 166 | + self.destination_dir |
| 167 | + ) |
| 168 | + |
| 169 | + # Check if the file was copied with the directory structure |
| 170 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.md").exists()) |
| 171 | + content = (self.destination_dir / "subfolder" / "file3.md").read_text() |
| 172 | + self.assertIn("# Another Header\nMore content", content) |
| 173 | + |
| 174 | + def test_copy_referenced_images(self): |
| 175 | + # Create a Markdown file with image references |
| 176 | + md_content = "# Header\n\n" |
| 177 | + (self.source_dir / "file1.md").write_text(md_content) |
| 178 | + |
| 179 | + # Create referenced images |
| 180 | + (self.source_dir / "images").mkdir() |
| 181 | + (self.source_dir / "images" / "image1.png").write_text("Fake PNG content") |
| 182 | + (self.source_dir / "images" / "image2.jpg").write_text("Fake JPG content") |
| 183 | + |
| 184 | + # Copy files |
| 185 | + PartialCopy.partial_copy(str(self.source_dir / "file1.md"), self.source_dir, self.destination_dir) |
| 186 | + |
| 187 | + # Check if the Markdown file was copied |
| 188 | + self.assertTrue((self.destination_dir / "file1.md").exists()) |
| 189 | + |
| 190 | + # Check if referenced images were copied |
| 191 | + self.assertTrue((self.destination_dir / "images" / "image1.png").exists()) |
| 192 | + self.assertTrue((self.destination_dir / "images" / "image2.jpg").exists()) |
| 193 | + |
| 194 | + def test_copy_with_string_paths(self): |
| 195 | + # Test copying with string paths instead of Path objects |
| 196 | + source_path = str(self.source_dir / "file1.txt") |
| 197 | + root_path = str(self.source_dir) |
| 198 | + dest_path = str(self.destination_dir) |
| 199 | + |
| 200 | + PartialCopy.partial_copy(source_path, root_path, dest_path) |
| 201 | + |
| 202 | + # Check if the file was copied |
| 203 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 204 | + |
| 205 | + def test_copy_empty_list(self): |
| 206 | + # Test copying an empty list of files |
| 207 | + try: |
| 208 | + PartialCopy.partial_copy([], self.source_dir, self.destination_dir) |
| 209 | + # Should not raise error for empty list |
| 210 | + except Exception as e: |
| 211 | + self.fail(f"partial_copy raised unexpected exception for empty list: {e}") |
| 212 | + |
| 213 | + def test_has_frontmatter_method(self): |
| 214 | + content = (self.source_dir / "file4.md").read_text() |
| 215 | + post = frontmatter.loads(content) |
| 216 | + self.assertTrue(PartialCopy._has_frontmatter(post)) |
| 217 | + |
| 218 | + # Test without metadata |
| 219 | + content = (self.source_dir / "file2.md").read_text() |
| 220 | + post = frontmatter.loads(content) |
| 221 | + self.assertFalse(PartialCopy._has_frontmatter(post)) |
| 222 | + |
| 223 | + |
| 224 | +if __name__ == '__main__': |
| 225 | + import unittest |
| 226 | + unittest.main() |
0 commit comments