added more converter tests; removed conversion to absolute paths

This commit is contained in:
truePhilipp committed 2024-05-10 08:00:49 +00:00
1 parent cd9f10c030
commit 7212d251d1
2 files changed
+99 -10

No files matched your search

+6 -7
View File
@@ -106,7 +106,7 @@ def parse_args(name: Literal["html_to_django", "html_to_django_r"]) -> Namespace
return parser.parse_args() 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. 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 converted Django templates are written to new files, and the original HTML files are left
unchanged. unchanged.
""" """
absolute_path = os.path.abspath(path) if os.path.isdir(path):
if os.path.isdir(absolute_path): convert_dir(path, overwrite)
convert_dir(absolute_path, overwrite) elif os.path.isfile(path):
elif os.path.isfile(absolute_path): convert_file(path, overwrite)
convert_file(absolute_path, overwrite)
else: else:
sys.exit(f'The path "{absolute_path}" does not exist.') sys.exit(f'The path "{path}" does not exist.')
def command_entry() -> None: def command_entry() -> None:
+93 -3
View File
@@ -2,10 +2,12 @@ import os
import filecmp import filecmp
import shutil import shutil
import tempfile import tempfile
import sys
from io import StringIO
from unittest import TestCase from unittest import TestCase
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from glob import glob 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): class ConverterTests(TestCase):
@@ -58,11 +60,11 @@ class ConverterTests(TestCase):
def test_convert_file_nonexistent(self) -> None: def test_convert_file_nonexistent(self) -> None:
with self.assertRaises(FileNotFoundError): 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: def test_convert_file_dir_passed(self) -> None:
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):
convert_file(os.path.abspath("tests/test_project")) convert_file("tests/test_project")
def test_convert_dir(self) -> None: def test_convert_dir(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
@@ -117,3 +119,91 @@ class ConverterTests(TestCase):
def test_convert_dir_file_passed(self) -> None: def test_convert_dir_file_passed(self) -> None:
with self.assertRaises(FileNotFoundError): with self.assertRaises(FileNotFoundError):
convert_dir("tests/test_project/test.html") 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)