From bfec09034e1417ccc1d1b386f2c43fb7b144b068 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Tue, 7 Jun 2022 03:45:42 +0200 Subject: [PATCH] First draft of the preparser test --- src/bootstraparse/modules/sitecrawler.py | 4 +- src/bootstraparse/modules/sitecreator.py | 2 +- tests/modules/test_sitecrawler.py | 91 +++++++++++++++++++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/bootstraparse/modules/sitecrawler.py b/src/bootstraparse/modules/sitecrawler.py index 69c6a6a..cea2916 100644 --- a/src/bootstraparse/modules/sitecrawler.py +++ b/src/bootstraparse/modules/sitecrawler.py @@ -68,7 +68,7 @@ class SiteCrawler: ) else: os.remove(path) - open(path, "w+").close() + open(path, "a").close() return path def __iter__(self): @@ -76,7 +76,7 @@ class SiteCrawler: yield pre -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover from bootstraparse.modules import config site_path = pathresolver.b_path("../../example_userfiles/test.bpr") config_path = pathresolver.b_path("../../example_userfiles/configs/") diff --git a/src/bootstraparse/modules/sitecreator.py b/src/bootstraparse/modules/sitecreator.py index 197afcc..6f1e4de 100644 --- a/src/bootstraparse/modules/sitecreator.py +++ b/src/bootstraparse/modules/sitecreator.py @@ -57,7 +57,7 @@ def save(list_of_containers, destination, env): output_file.write(export.ContextConverter(list_of_containers, env.export_mngr).process_pile().read()) -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover xpath = pathresolver.b_path("../../example_userfiles") dpath = pathresolver.b_path("../../example_output") create_website(xpath, dpath) diff --git a/tests/modules/test_sitecrawler.py b/tests/modules/test_sitecrawler.py index dbd3f33..1ca74d2 100644 --- a/tests/modules/test_sitecrawler.py +++ b/tests/modules/test_sitecrawler.py @@ -1,8 +1,95 @@ +import os +import tempfile + +import pytest +import rich + import bootstraparse.modules.sitecrawler as sitecrawler +from bootstraparse.modules import environment, config, pathresolver, export + +_TEMP_DIRECTORY = tempfile.TemporaryDirectory() +_BASE = os.path.join(_TEMP_DIRECTORY.name, "base") +_DEST = os.path.join(_TEMP_DIRECTORY.name, "dest") +files = [ + ("base/test1.bpr", ""), + ("base/test2.bpr", ""), + ("base/test3.bpr", ""), + ("base/_noimport.bpr", ""), + ("base/subtests/test4.bpr", ""), + ("base/subtests/test5.bpr", ""), + ("base/config/test6.yml", ""), + ("base/config/test7.yml", ""), + ("base/templates/test8.yml", ""), +] -def test_sitecrawler(): +def make_new_file(path, content="", mode="w+"): + """ + Make a new file + """ + name = os.path.join(_TEMP_DIRECTORY.name, path) + os.makedirs(os.path.dirname(name), exist_ok=True) + # print(f"Creating : {name}") + # print(f"Content : {content}") + # print(f"Location : {os.path.dirname(name)}") + # print() + with open(name, mode=mode) as f: + f.write(content) + return name + + +@pytest.fixture(scope="module") +def list_files(): + """ + List all files in the test directory + """ + return [make_new_file(file, content) for file, content in files] + + +@pytest.fixture(scope="module") +def env(): + env = environment.Environment() + env.config = config.ConfigLoader(pathresolver.b_path("configs")) + if os.path.exists(os.path.join(_BASE, "configs")): + env.config.add_folder(os.path.join(_BASE, "configs")) + + env.template = config.ConfigLoader(pathresolver.b_path("templates")) + if os.path.exists(os.path.join(_BASE, "templates")): + env.template.add_folder(os.path.join(_BASE, "templates")) + + env.export_mngr = export.ExportManager(env.config, env.template) + env.origin = _BASE + env.destination = _DEST + return env + + +def test_bad_site_creator(env): + with pytest.raises(FileNotFoundError): + sitecrawler.SiteCrawler("NONEXISTENT", _DEST, env) + + +def test_sitecrawler(list_files, env): """ Test the sitecrawler.SiteCrawler class """ - assert sitecrawler + + crw = sitecrawler.SiteCrawler(_BASE, _DEST, env) + rich.inspect(crw) + crw.set_all_preparsers() + for pre, dest in crw: + assert os.path.exists(dest) + assert os.path.isfile(dest) + + crw.force_rewrite = False + with pytest.raises(FileExistsError): + crw.set_all_preparsers() + crw.force_rewrite = True + crw.set_all_preparsers() + + +@pytest.mark.skip(reason="Not implemented") +def test_result_crawler(list_files, env): + """ + Test the sitecrawler.ResultCrawler class + """ + pass