|
| 1 | +from unittest import TestCase |
| 2 | +from pathlib import Path |
| 3 | +from tempfile import TemporaryDirectory |
| 4 | + |
| 5 | +from foliant.backends.base import BaseBackend |
| 6 | + |
| 7 | +class TestBackendCopyFiles(TestCase): |
| 8 | + def setUp(self): |
| 9 | + # Create a temporary directory for testing |
| 10 | + self.test_dir = TemporaryDirectory() |
| 11 | + self.source_dir = Path(self.test_dir.name) / "source" |
| 12 | + self.destination_dir = Path(self.test_dir.name) / "destination" |
| 13 | + self.source_dir.mkdir() |
| 14 | + self.destination_dir.mkdir() |
| 15 | + |
| 16 | + # Create some test files |
| 17 | + (self.source_dir / "file1.txt").write_text("Hello, file1!") |
| 18 | + (self.source_dir / "file2.txt").write_text("Hello, file2!") |
| 19 | + (self.source_dir / "subfolder").mkdir() |
| 20 | + (self.source_dir / "subfolder" / "file3.txt").write_text("Hello, file3!") |
| 21 | + (self.source_dir / "file2.md").write_text("# Header\nSome content") |
| 22 | + (self.source_dir / "subfolder" / "file3.md").write_text("# Another Header\nMore content") |
| 23 | + |
| 24 | + def tearDtown(self): |
| 25 | + # Clean up the temporary directory |
| 26 | + self.test_dir.cleanup() |
| 27 | + |
| 28 | + def test_copy_single_file(self): |
| 29 | + # Test copying a single file |
| 30 | + source_file = self.source_dir / "file1.txt" |
| 31 | + BaseBackend.partial_copy(source_file, self.destination_dir, self.source_dir) |
| 32 | + |
| 33 | + # Check if the file was copied |
| 34 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 35 | + self.assertEqual((self.destination_dir / "file1.txt").read_text(), "Hello, file1!") |
| 36 | + |
| 37 | + def test_copy_list_of_files(self): |
| 38 | + # Test copying a list of files |
| 39 | + source_files = [ |
| 40 | + self.source_dir / "file1.txt", |
| 41 | + self.source_dir / "file2.txt" |
| 42 | + ] |
| 43 | + BaseBackend.partial_copy(source_files, self.destination_dir, self.source_dir) |
| 44 | + |
| 45 | + # Check if the files were copied |
| 46 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 47 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 48 | + |
| 49 | + |
| 50 | + def test_copy_list_of_files_two(self): |
| 51 | + # Test copying a list of files |
| 52 | + source_files = [ |
| 53 | + str(self.source_dir / "file1.txt"), |
| 54 | + str(self.source_dir / "file2.md") |
| 55 | + ] |
| 56 | + BaseBackend.partial_copy(source_files, self.destination_dir, self.source_dir) |
| 57 | + |
| 58 | + # Check if the files were copied |
| 59 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 60 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 61 | + |
| 62 | + def test_copy_glob_pattern(self): |
| 63 | + # Test copying files matching a glob pattern |
| 64 | + glob_pattern = str(self.source_dir / "*.txt") |
| 65 | + BaseBackend.partial_copy(glob_pattern, self.destination_dir, self.source_dir) |
| 66 | + |
| 67 | + # Check if the files were copied |
| 68 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 69 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 70 | + self.assertFalse((self.destination_dir / "file3.txt").exists()) # file3 is in a subfolder |
| 71 | + |
| 72 | + def test_copy_glob_pattern_md(self): |
| 73 | + # Test copying files matching a glob pattern |
| 74 | + glob_pattern = str(self.source_dir / '*2.md') |
| 75 | + BaseBackend.partial_copy(glob_pattern, self.destination_dir, self.source_dir) |
| 76 | + |
| 77 | + # Check if the files were copied |
| 78 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 79 | + self.assertEqual((self.destination_dir / "file2.md").read_text(), "# Header\nSome content") |
| 80 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.md").exists()) |
| 81 | + self.assertEqual((self.destination_dir / "subfolder" / "file3.md").read_text(), "# Another Header\n") # Only '*2.md' files should be copied with content |
| 82 | + |
| 83 | + def test_copy_glob_pattern_recursive(self): |
| 84 | + # Test copying files matching a recursive glob pattern |
| 85 | + glob_pattern = str(self.source_dir / "**" / "*.txt") |
| 86 | + BaseBackend.partial_copy(glob_pattern, self.destination_dir, self.source_dir) |
| 87 | + |
| 88 | + # Check if the files were copied, including the one in the subfolder |
| 89 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 90 | + self.assertTrue((self.destination_dir / "file2.txt").exists()) |
| 91 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.txt").exists()) |
| 92 | + |
| 93 | + def test_copy_directory_structure(self): |
| 94 | + # Test copying a file while preserving directory structure |
| 95 | + source_file = self.source_dir / "subfolder" / "file3.txt" |
| 96 | + BaseBackend.partial_copy(source_file, self.destination_dir, self.source_dir) |
| 97 | + |
| 98 | + # Check if the file was copied with the directory structure |
| 99 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.txt").exists()) |
| 100 | + |
| 101 | + def test_copy_nonexistent_file(self): |
| 102 | + # Test copying a nonexistent file (should raise FileNotFoundError) |
| 103 | + source_file = self.source_dir / "nonexistent.txt" |
| 104 | + with self.assertRaises(FileNotFoundError): |
| 105 | + BaseBackend.partial_copy(source_file, self.destination_dir, self.source_dir) |
| 106 | + |
| 107 | + def test_copy_nonexistent_glob(self): |
| 108 | + # Test copying with a glob pattern that matches no files |
| 109 | + glob_pattern = str(self.source_dir / "*_suffix.md") |
| 110 | + BaseBackend.partial_copy(glob_pattern, self.destination_dir, self.source_dir) |
| 111 | + |
| 112 | + # Check that no files were copied |
| 113 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 114 | + self.assertEqual((self.destination_dir / "file2.md").read_text(), "# Header\n") |
| 115 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.md").exists()) |
| 116 | + self.assertEqual((self.destination_dir / "subfolder" / "file3.md").read_text(), "# Another Header\n") |
| 117 | + |
| 118 | + def test_copy_to_nonexistent_destination(self): |
| 119 | + # Test copying to a nonexistent destination (should create the destination folder) |
| 120 | + new_destination = self.destination_dir / "new_folder" |
| 121 | + BaseBackend.partial_copy(self.source_dir / "file1.txt", new_destination, self.source_dir) |
| 122 | + |
| 123 | + # Check if the file was copied and the destination folder was created |
| 124 | + self.assertTrue(new_destination.exists()) |
| 125 | + self.assertTrue((new_destination / "file1.txt").exists()) |
| 126 | + |
| 127 | + def test_copy_path_object(self): |
| 128 | + # Test copying using Path objects |
| 129 | + source_file = self.source_dir / "file1.txt" |
| 130 | + destination = self.destination_dir / "file1.txt" |
| 131 | + BaseBackend.partial_copy(source_file, destination, self.source_dir) |
| 132 | + |
| 133 | + # Check if the file was copied |
| 134 | + self.assertTrue(destination.exists()) |
| 135 | + |
| 136 | + def test_copy_text_file(self): |
| 137 | + # Test copying a text file |
| 138 | + BaseBackend.partial_copy(str(self.source_dir / "file1.txt"), self.destination_dir, self.source_dir) |
| 139 | + |
| 140 | + # Check if the file was copied |
| 141 | + self.assertTrue((self.destination_dir / "file1.txt").exists()) |
| 142 | + self.assertEqual((self.destination_dir / "file1.txt").read_text(), "Hello, file1!") |
| 143 | + |
| 144 | + def test_copy_markdown_file(self): |
| 145 | + # Test copying a Markdown file |
| 146 | + BaseBackend.partial_copy(str(self.source_dir / "file2.md"), self.destination_dir, self.source_dir) |
| 147 | + |
| 148 | + # Check if only the header was copied |
| 149 | + self.assertTrue((self.destination_dir / "file2.md").exists()) |
| 150 | + self.assertEqual((self.destination_dir / "file2.md").read_text(), "# Header\nSome content") |
| 151 | + |
| 152 | + def test_copy_directory_structure_with_header(self): |
| 153 | + # Test copying a file while preserving directory structure |
| 154 | + BaseBackend.partial_copy(str(self.source_dir / "subfolder" / "file3.md"), self.destination_dir, self.source_dir) |
| 155 | + |
| 156 | + # Check if the file was copied with the directory structure |
| 157 | + self.assertTrue((self.destination_dir / "subfolder" / "file3.md").exists()) |
| 158 | + self.assertEqual((self.destination_dir / "subfolder" / "file3.md").read_text(), "# Another Header\nMore content") |
0 commit comments