From 7212d251d1c7ea613269ba9aa700563b1b023650 Mon Sep 17 00:00:00 2001 From: truePhilipp Date: Fri, 10 May 2024 08:00:22 +0000 Subject: [PATCH] added more converter tests; removed conversion to absolute paths --- html_to_django/converter.py | 13 +++-- tests/tests_converter.py | 96 +++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 10 deletions(-) diff --git a/html_to_django/converter.py b/html_to_django/converter.py index f2a1322..3ddf0ae 100644 --- a/html_to_django/converter.py +++ b/html_to_django/converter.py @@ -106,7 +106,7 @@ def parse_args(name: Literal["html_to_django", "html_to_django_r"]) -> Namespace return parser.parse_args() -def convert_path(path: str, overwrite: bool) -> None: +def convert_path(path: str, overwrite: bool = False) -> None: """ Converts the special attributes in the HTML files at the given path to Django template tags. @@ -123,13 +123,12 @@ def convert_path(path: str, overwrite: bool) -> None: converted Django templates are written to new files, and the original HTML files are left unchanged. """ - absolute_path = os.path.abspath(path) - if os.path.isdir(absolute_path): - convert_dir(absolute_path, overwrite) - elif os.path.isfile(absolute_path): - convert_file(absolute_path, overwrite) + if os.path.isdir(path): + convert_dir(path, overwrite) + elif os.path.isfile(path): + convert_file(path, overwrite) else: - sys.exit(f'The path "{absolute_path}" does not exist.') + sys.exit(f'The path "{path}" does not exist.') def command_entry() -> None: diff --git a/tests/tests_converter.py b/tests/tests_converter.py index 4642217..316e89e 100644 --- a/tests/tests_converter.py +++ b/tests/tests_converter.py @@ -2,10 +2,12 @@ import os import filecmp import shutil import tempfile +import sys +from io import StringIO from unittest import TestCase from bs4 import BeautifulSoup from glob import glob -from html_to_django.converter import get_required_libraries, convert_file, convert_dir +from html_to_django.converter import get_required_libraries, convert_file, convert_dir, convert_path class ConverterTests(TestCase): @@ -58,11 +60,11 @@ class ConverterTests(TestCase): def test_convert_file_nonexistent(self) -> None: with self.assertRaises(FileNotFoundError): - convert_file(os.path.abspath("tests/test/project/nonexistent_file.html")) + convert_file("tests/test/project/nonexistent_file.html") def test_convert_file_dir_passed(self) -> None: with self.assertRaises(FileNotFoundError): - convert_file(os.path.abspath("tests/test_project")) + convert_file("tests/test_project") def test_convert_dir(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: @@ -117,3 +119,91 @@ class ConverterTests(TestCase): def test_convert_dir_file_passed(self) -> None: with self.assertRaises(FileNotFoundError): convert_dir("tests/test_project/test.html") + + def test_convert_path_file(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_path(f"{tmpdir}/test.html") + # verify that the new file exists and has the expected value + self.assertTrue(filecmp.cmp( + "tests/test_project_expected_result/test.html", + f"{tmpdir}/test.n.html" + )) + # verify that the source file is unchanged + self.assertTrue(filecmp.cmp( + "tests/test_project/test.html", + f"{tmpdir}/test.html" + )) + + def test_convert_path_file_overwrite(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_path(f"{tmpdir}/test.html", True) + # verify that the source file has been overwriten + self.assertTrue(filecmp.cmp( + "tests/test_project_expected_result/test.html", + f"{tmpdir}/test.html" + )) + # verify that no new file was created + self.assertFalse(os.path.exists(f"{tmpdir}/test.n.html")) + + def test_convert_path_dir(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_path(f"{tmpdir}") + + for conversion_result in glob(f"{tmpdir}/**/*.n.html", recursive=True): + conversion_source = conversion_result.replace(".n.html", ".html") + + # verify that the new file exists and has the expected value + expected_result = conversion_source.replace(tmpdir, "tests/test_project_expected_result") + self.assertTrue(filecmp.cmp( + expected_result, + conversion_result + )) + + # verify that the source file is unchanged + original_source = conversion_source.replace(tmpdir, "tests/test_project") + self.assertTrue(filecmp.cmp( + original_source, + conversion_source + )) + + def test_convert_path_dir_overwrite(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_path(f"{tmpdir}", True) + + for conversion_result in glob(f"{tmpdir}/**/*.html", recursive=True): + # verify that the source file has been overwritten + expected_result = conversion_result.replace(tmpdir, "tests/test_project_expected_result") + self.assertTrue(filecmp.cmp( + expected_result, + conversion_result + )) + + # verify that no new file was created + self.assertFalse(os.path.exists(conversion_result.replace(".html", ".n.html"))) + + def test_convert_path_nonexistent(self) -> None: + with self.assertRaises(SystemExit) as cm: + convert_path("tests/test/project/nonexistent_file.html") + + expected_result = 'The path "tests/test/project/nonexistent_file.html" does not exist.' + self.assertIn(expected_result, cm.exception.args)