class LatexProjectGenerationTests(TestCase): def setUp(self): self._file_system = InMemoryFileSystem() self._directory = "home" def test_setup_a_single_file_project(self): self._do_test_setup(LatexProject(TexFile("main.tex", "blabla"))) def test_setup_a_two_files_project(self): self._do_test_setup( LatexProject(TexFile("main.tex", "blabla"), TexFile("result.tex", "Some results"))) def test_setup_a_project_with_subdirectories(self): self._do_test_setup( LatexProject(TexFile("main.tex", "blabla"), TexFile("sections/introduction.tex", "introduction"), TexFile("sections/conclusions.tex", "conclusions"), TexFile("images/results.pdf", "PDF"))) def _do_test_setup(self, project): project.setup(self._file_system, Path.fromText(self._directory)) self._verify(project) def _verify(self, project): for (path, file) in project.files.items(): file_on_disk = self._file_system.open( Path.fromText(self._directory) / path) self.assertEqual(file.content, file_on_disk.content())
class LatexProjectGenerationTests(TestCase): def setUp(self): self._file_system = InMemoryFileSystem() self._directory = "home" def test_setup_a_single_file_project(self): self._do_test_setup( LatexProject(TexFile("main.tex", "blabla")) ) def test_setup_a_two_files_project(self): self._do_test_setup(LatexProject( TexFile("main.tex", "blabla"), TexFile("result.tex", "Some results")) ) def test_setup_a_project_with_subdirectories(self): self._do_test_setup(LatexProject( TexFile("main.tex", "blabla"), TexFile("sections/introduction.tex", "introduction"), TexFile("sections/conclusions.tex", "conclusions"), TexFile("images/results.pdf", "PDF")) ) def _do_test_setup(self, project): project.setup(self._file_system, Path.fromText(self._directory)) self._verify(project) def _verify(self, project): for (path, file) in project.files.items(): file_on_disk = self._file_system.open(Path.fromText(self._directory) / path) self.assertEqual(file.content, file_on_disk.content())
class LatexProjectExtractionTests(TestCase): def setUp(self): self._file_system = InMemoryFileSystem() self._directory = "home/" self._files = [] def _create_files(self, *files): self._files = files for (path, content) in files: complete_path = Path.fromText(self._directory + path) self._file_system.create_file(complete_path, content) def _extract_project(self): root = self._file_system.open(Path.fromText(self._directory)) return LatexProject.extract_from_directory(root) def _verify(self, project): self._expected().assert_is_equivalent_to(project) def _do_test_with_files(self, *files): self._create_files(*files) project = self._extract_project() self._verify(project) def _expected(self): tex_files = [] for (path, content) in self._files: tex_files.append(TexFile(path, content)) return LatexProject(*tex_files) def test_extracting_a_simple_file(self): self._do_test_with_files(("main.tex", "content")) def test_extracting_a_two_files_project(self): self._do_test_with_files( ("main.tex", "content"), ("result.tex", "Here are some results")) def test_extracting_files_in_a_subdirectory(self): self._do_test_with_files( ("main.tex", "content"), ("test/result.tex", "Here are some results")) def test_extracting_a_complete_project(self): self._do_test_with_files( ("main.tex", "blablabla"), ("sections/introduction.tex", "Here are some results"), ("sections/development.tex", "Some more details"), ("sections/conclusions.tex", "Some more hindsight"), ("images/results.pdf", "PDF CONTENT"), ("images/sources/results.svg", "SVG CODE"), ("article.bib", "The bibliography"))
class LatexProjectExtractionTests(TestCase): def setUp(self): self._file_system = InMemoryFileSystem() self._directory = "home/" self._files = [] def _create_files(self, *files): self._files = files for (path, content) in files: complete_path = Path.fromText(self._directory + path) self._file_system.create_file(complete_path, content) def _extract_project(self): root = self._file_system.open(Path.fromText(self._directory)) return LatexProject.extract_from_directory(root) def _verify(self, project): self._expected().assert_is_equivalent_to(project) def _do_test_with_files(self, *files): self._create_files(*files) project = self._extract_project() self._verify(project) def _expected(self): tex_files = [] for (path, content) in self._files: tex_files.append(TexFile(path, content)) return LatexProject(*tex_files) def test_extracting_a_simple_file(self): self._do_test_with_files(("main.tex", "content")) def test_extracting_a_two_files_project(self): self._do_test_with_files(("main.tex", "content"), ("result.tex", "Here are some results")) def test_extracting_files_in_a_subdirectory(self): self._do_test_with_files(("main.tex", "content"), ("test/result.tex", "Here are some results")) def test_extracting_a_complete_project(self): self._do_test_with_files( ("main.tex", "blablabla"), ("sections/introduction.tex", "Here are some results"), ("sections/development.tex", "Some more details"), ("sections/conclusions.tex", "Some more hindsight"), ("images/results.pdf", "PDF CONTENT"), ("images/sources/results.svg", "SVG CODE"), ("article.bib", "The bibliography"))
class InMemoryFileSystemTest(unittest.TestCase): def setUp(self): self.fileSystem = InMemoryFileSystem() self.current_directory = Path.fromText("/temp") def test_file_creation(self): path = Path.fromText("dir/test/source.tex") self.fileSystem.create_file(path, "blah") file = self.fileSystem.open(Path.fromText("dir/test/source.tex")) self.assertTrue(file.exists()) self.assertTrue(file.contains("blah")) def test_create_file_rejects_content_that_is_not_text(self): path = Path.fromText("dir/test.txt") with self.assertRaises(ValueError): self.fileSystem.create_file(path, [1, 2, 3, 4]) def testThatMissingFileDoNotExist(self): path = Path.fromText("file\\that\\do\\not\\exist.txt") file = self.fileSystem.open(path) self.assertFalse(file.exists()) def testContainingDirectoryIsAvailable(self): path = Path.fromText("my\\dir\\test.txt") self.fileSystem.create_file(path, "data") file = self.fileSystem.open(path) self.assertEqual(file.container().path(), ROOT / "my" / "dir") def testFullNameIsAvailable(self): path = Path.fromText("/my/dir/test.txt") self.fileSystem.create_file(path, "data") file = self.fileSystem.open(path) self.assertEqual(file.fullname(), "test.txt") def testBasenameIsAvailable(self): path = Path.fromText("my/dir/test.txt") self.fileSystem.create_file(path, "whatever") file = self.fileSystem.open(path) self.assertEqual(file.basename(), "test") def testDirectoryContainsFiles(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") file = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(file.files()), 2) def testDirectoryContainsOnlyItsDirectContent(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") self.fileSystem.create_file(Path.fromText("dir/more/test.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(directory.files()), 3, [ str(file.path()) for file in directory.files()]) def testFilteringFilesInDirectory(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") self.fileSystem.create_file(Path.fromText("dir/blah"), "z") file = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(file.files_that_matches("test")), 1) def testCopyingFile(self): source = Path.fromText("dir/test.txt") self.fileSystem.create_file(source, "whatever") file = self.fileSystem.open(source) destination = Path.fromText("dir2/clone") self.fileSystem.copy(file, destination) copy = self.fileSystem.open(destination / "test.txt") self.assertTrue(copy.exists()) self.assertEqual(copy.content(), "whatever") def test_copy_while_renaming(self): source = Path.fromText("dir/test.txt") self.fileSystem.create_file(source, "whatever") file = self.fileSystem.open(source) destination = Path.fromText("dir2/clone/test_copy.txt") self.fileSystem.copy(file, destination) copy = self.fileSystem.open(destination) self.assertTrue(copy.exists()) self.assertEqual(copy.content(), "whatever") def test_files_that_match(self): self.fileSystem.create_file(Path.fromText("dir/foo/bar/test.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir/foo")) results = directory.files_that_matches("bar/test") self.assertEqual(len(results), 1) def test_files_that_match_with_multiple_matches(self): self.fileSystem.create_file(Path.fromText("dir/foo/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/foo/subtest.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir/foo")) results = directory.files_that_matches("test") self.assertEqual(len(results), 1) def test_finding_files_in_the_current_directory(self): path = Path.fromText("/root/foo/bar/test.txt") self.fileSystem.create_file(path, "blahblah blah") self.fileSystem.move_to_directory(Path.fromText("/root/foo")) file = self.fileSystem.open(Path.fromText("bar/test.txt")) self.assertEqual(file.content(), "blahblah blah")
class InMemoryFileSystemTest(unittest.TestCase): def setUp(self): self.fileSystem = InMemoryFileSystem() self.current_directory = Path.fromText("/temp") def test_file_creation(self): path = Path.fromText("dir/test/source.tex") self.fileSystem.create_file(path, "blah") file = self.fileSystem.open(Path.fromText("dir/test/source.tex")) self.assertTrue(file.exists()) self.assertTrue(file.contains("blah")) def test_create_file_rejects_content_that_is_not_text(self): path = Path.fromText("dir/test.txt") with self.assertRaises(ValueError): self.fileSystem.create_file(path, [1, 2, 3, 4]) def testThatMissingFileDoNotExist(self): path = Path.fromText("file\\that\\do\\not\\exist.txt") file = self.fileSystem.open(path) self.assertFalse(file.exists()) def testContainingDirectoryIsAvailable(self): path = Path.fromText("my\\dir\\test.txt") self.fileSystem.create_file(path, "data") file = self.fileSystem.open(path) self.assertEqual(file.container().path(), ROOT / "my" / "dir") def testFullNameIsAvailable(self): path = Path.fromText("/my/dir/test.txt") self.fileSystem.create_file(path, "data") file = self.fileSystem.open(path) self.assertEqual(file.fullname(), "test.txt") def testBasenameIsAvailable(self): path = Path.fromText("my/dir/test.txt") self.fileSystem.create_file(path, "whatever") file = self.fileSystem.open(path) self.assertEqual(file.basename(), "test") def testDirectoryContainsFiles(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") file = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(file.files()), 2) def testDirectoryContainsOnlyItsDirectContent(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") self.fileSystem.create_file(Path.fromText("dir/more/test.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(directory.files()), 3, [ str(file.path()) for file in directory.files() ]) def testFilteringFilesInDirectory(self): self.fileSystem.create_file(Path.fromText("dir/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/test2.txt"), "y") self.fileSystem.create_file(Path.fromText("dir/blah"), "z") file = self.fileSystem.open(Path.fromText("dir")) self.assertEqual(len(file.files_that_matches("test")), 2) def testCopyingFile(self): source = Path.fromText("dir/test.txt") self.fileSystem.create_file(source, "whatever") file = self.fileSystem.open(source) destination = Path.fromText("dir2/clone") self.fileSystem.copy(file, destination) copy = self.fileSystem.open(destination / "test.txt") self.assertTrue(copy.exists()) self.assertEqual(copy.content(), "whatever") def test_copy_while_renaming(self): source = Path.fromText("dir/test.txt") self.fileSystem.create_file(source, "whatever") file = self.fileSystem.open(source) destination = Path.fromText("dir2/clone/test_copy.txt") self.fileSystem.copy(file, destination) copy = self.fileSystem.open(destination) self.assertTrue(copy.exists()) self.assertEqual(copy.content(), "whatever") def test_files_that_match(self): self.fileSystem.create_file(Path.fromText("dir/foo/bar/test.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir/foo")) results = directory.files_that_matches("bar/test") self.assertEqual(len(results), 1) def test_files_that_match_with_multiple_matches(self): self.fileSystem.create_file(Path.fromText("dir/foo/test.txt"), "x") self.fileSystem.create_file(Path.fromText("dir/foo/subtest.txt"), "x") directory = self.fileSystem.open(Path.fromText("dir/foo")) results = directory.files_that_matches("test") self.assertEqual(len(results), 1) def test_finding_files_in_the_current_directory(self): path = Path.fromText("/root/foo/bar/test.txt") self.fileSystem.create_file(path, "blahblah blah") self.fileSystem.move_to_directory(Path.fromText("/root/foo")) file = self.fileSystem.open(Path.fromText("bar/test.txt")) self.assertEqual(file.content(), "blahblah blah")