From f72ea274fa0e15d522d6d9161421884d2641a555 Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:09:19 -0400 Subject: [PATCH 1/8] refactor(build): Remove single use function. --- build.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/build.py b/build.py index 2ef76c9..922bdc7 100755 --- a/build.py +++ b/build.py @@ -32,15 +32,6 @@ def copy_and_apply_func(src_dir: str, dst_dir: str, func: Callable[[str], None]) shutil.copy2(src_path, dst_path) func(dst_path) -def get_layout() -> str: - """ - Get the layout file. - - Returns: - str: The path to the layout file. - """ - return os.path.join("src", "layout.html") - def read_file(path: str) -> str: """ Read the contents of a file. @@ -121,9 +112,7 @@ def main() -> None: """ The main entry point for the script. """ - layout = read_file( - get_layout() - ) + layout = read_file(os.path.join("src", "layout.html")) delete_directory("dist") copy_and_apply_func( From 891025563473ff31ff8929294b12373d67c65a4b Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:37:03 -0400 Subject: [PATCH 2/8] chore(ignore): Updated .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7773828..0b84c84 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -dist/ \ No newline at end of file +dist/ +*.pyc +.coverage From 689cab9be61d49e04a384a9e0278380f56f644f0 Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:37:16 -0400 Subject: [PATCH 3/8] test(build): Test copy_file --- tests/test_build.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test_build.py diff --git a/tests/test_build.py b/tests/test_build.py new file mode 100644 index 0000000..31aeae7 --- /dev/null +++ b/tests/test_build.py @@ -0,0 +1,49 @@ +# Copyright (c) 2021-2023 Johnathan P. Irvin +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +from unittest.mock import call, mock_open, patch + +import pytest + +from build import copy_file, read_file + + +def test_read_file(): + """ + Test the read_file function. + """ + with patch("build.open", mock_open(read_data="data")) as mock_file: + assert read_file("path") == "data" + mock_file.assert_called_once_with("path") + +def test_copy_file(): + """ + Test the copy_file function. + """ + with patch("build.open", mock_open()) as mock_file: + copy_file("src", "dst") + + mock_file.assert_has_calls( + [ + call("src", "rb"), + call("dst", "wb"), + ], + any_order=True + ) From 9276d8715bfa9666bddcca64d7ccf4a8c7b06f44 Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:39:35 -0400 Subject: [PATCH 4/8] test(build): Tested delete directory --- tests/test_build.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index 31aeae7..080662b 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -22,7 +22,7 @@ import pytest -from build import copy_file, read_file +from build import copy_file, delete_directory, read_file def test_read_file(): @@ -47,3 +47,22 @@ def test_copy_file(): ], any_order=True ) + +@pytest.mark.parametrize( + "path, exists", + [ + ("path", True), + ("path", False), + ] +) +def test_delete_directory(path, exists): + with patch("build.os.path.exists", return_value=exists) as mock_exists: + with patch("build.shutil.rmtree") as mock_rmtree: + delete_directory(path) + + mock_exists.assert_called_once_with("path") + + if exists: + mock_rmtree.assert_called_once_with("path") + else: + mock_rmtree.assert_not_called() From 4f0857769acbb40c457893d1ac281aeebedb0052 Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:40:14 -0400 Subject: [PATCH 5/8] test(build): Tested write file --- tests/test_build.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index 080662b..dcc15fc 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -22,7 +22,7 @@ import pytest -from build import copy_file, delete_directory, read_file +from build import copy_file, delete_directory, read_file, write_file def test_read_file(): @@ -33,6 +33,17 @@ def test_read_file(): assert read_file("path") == "data" mock_file.assert_called_once_with("path") +def test_write_file(): + """ + Test the write_file function. + """ + with patch("build.open", mock_open()) as mock_file: + write_file("path", "data") + + mock_file.assert_called_once_with("path", "w") + mock_file().write.assert_called_once_with("data") + + def test_copy_file(): """ Test the copy_file function. From 006a0b47e8869f2cdc4cbfbcee9b6540691e50e8 Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:42:47 -0400 Subject: [PATCH 6/8] test(build): Tested minimize function --- tests/test_build.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index dcc15fc..d8c91a6 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -22,7 +22,7 @@ import pytest -from build import copy_file, delete_directory, read_file, write_file +from build import copy_file, delete_directory, minimize, read_file, write_file def test_read_file(): @@ -77,3 +77,20 @@ def test_delete_directory(path, exists): mock_rmtree.assert_called_once_with("path") else: mock_rmtree.assert_not_called() + +@pytest.mark.parametrize( + "unminimized, minimized", + [ + ("data", "data"), + ("data\n", "data"), + ("data\t", "data"), + ("data ", "data"), + ("data\n\t", "data"), + ("data\n ", "data"), + ("data\t ", "data"), + ("data\n\t ", "data"), + ("data\n\t data", "data data"), + ] +) +def test_minimize(unminimized, minimized): + assert minimize(unminimized) == minimized From 03ee7f828e7c5672391b4a6f0b7dd6bf600aee7b Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:43:51 -0400 Subject: [PATCH 7/8] fix(build): Fixed double spaces not leaving single space behind. --- build.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/build.py b/build.py index 922bdc7..5ea1307 100755 --- a/build.py +++ b/build.py @@ -106,7 +106,12 @@ def minimize(html: str) -> str: Returns: str: The minimized HTML. """ - return html.replace("\n", "").replace("\t", "").replace(" ", "") + return " ".join( + html + .replace("\n", "") + .replace("\t", "") + .split() + ) def main() -> None: """ From f30bacd28cac70f08df61e3c93731b8f967fa2fe Mon Sep 17 00:00:00 2001 From: Johnathan Irvin Date: Tue, 6 Jun 2023 17:46:39 -0400 Subject: [PATCH 8/8] test(build): Tested inject --- tests/test_build.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index d8c91a6..6ef423f 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -22,7 +22,8 @@ import pytest -from build import copy_file, delete_directory, minimize, read_file, write_file +from build import (copy_file, delete_directory, inject, minimize, read_file, + write_file) def test_read_file(): @@ -94,3 +95,15 @@ def test_delete_directory(path, exists): ) def test_minimize(unminimized, minimized): assert minimize(unminimized) == minimized + + +@pytest.mark.parametrize( + "template, content, tag, injected", + [ + ("{% body %}", "content", "{% body %}", "content"), + ("{% body %}", "content", "{% nope %}", "{% body %}"), + ("Empty Template", "content", "{% body %}", "Empty Template"), + ] +) +def test_inject(template, content, tag, injected): + assert inject(template, content, tag) == injected