From 704f0400aff37c0243ddd26f4627ff10818a01c8 Mon Sep 17 00:00:00 2001 From: pshubham Date: Thu, 13 Jun 2024 14:41:33 +0530 Subject: [PATCH 01/46] feat: Adding Github Unittests workflow --- .github/workflows/unit-tests.yml | 26 ++++++++++++++++++++++++++ README.md | 2 ++ tests/test_file_validation.py | 3 +-- tests/test_ooxml_files.py | 3 +-- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/unit-tests.yml diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..8845b7b --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,26 @@ +name: Unit Tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + id: setup-python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint poetry + poetry install + + - name: Run tests + run: poetry run pytest diff --git a/README.md b/README.md index 3ff020f..de67771 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ A command-line tool designed to detect and purge any and all macros and dynamic content from commonly used office document formats (including MS Office Files, PDFs, etc.). [![Super-Linter](https://github.com/Anti-Malware-Alliance/ms-office-macro-bleach/actions/workflows/pre-commit-actions.yml/badge.svg)](https://github.com/marketplace/actions/super-linter) +![Unit Tests](https://github.com/Anti-Malware-Alliance/ms-office-macro-bleach/actions/workflows/unit-tests.yml/badge.svg) + ## Supported formats diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 2932f99..6d5e67d 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -14,10 +14,9 @@ All tests are written for and conducted using pytest. """ - -from subprocess import check_output from os import remove, rename from shutil import copyfile +from subprocess import check_output prog_dir = "docubleach/" test_dir = "tests/test_files/" diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index d6f0515..b194531 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -8,10 +8,9 @@ All tests are written for and conducted using pytest. """ -from subprocess import check_output from os import remove, rename from shutil import copyfile - +from subprocess import check_output prog_dir = "docubleach/" test_dir = "tests/test_files/" From db0e0a9e53a324e4bc62ea1bd874bacf30db4fd9 Mon Sep 17 00:00:00 2001 From: pshubham Date: Tue, 18 Jun 2024 17:08:37 +0530 Subject: [PATCH 02/46] Using pytest for workflow --- .github/workflows/unit-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8845b7b..debe677 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -19,7 +19,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pylint poetry + pip install pylint poetry pytest poetry install - name: Run tests From 81d94313fa29de16279848048fb697fde5906de9 Mon Sep 17 00:00:00 2001 From: pshubham Date: Wed, 19 Jun 2024 11:24:45 +0530 Subject: [PATCH 03/46] Reverting tests refactoring --- tests/test_file_validation.py | 3 ++- tests/test_ooxml_files.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 6d5e67d..2932f99 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -14,9 +14,10 @@ All tests are written for and conducted using pytest. """ + +from subprocess import check_output from os import remove, rename from shutil import copyfile -from subprocess import check_output prog_dir = "docubleach/" test_dir = "tests/test_files/" diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index b194531..d6f0515 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -8,9 +8,10 @@ All tests are written for and conducted using pytest. """ +from subprocess import check_output from os import remove, rename from shutil import copyfile -from subprocess import check_output + prog_dir = "docubleach/" test_dir = "tests/test_files/" From 1f4a9af094911d7f1bf98d8cecb42619caad39bd Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 08:38:54 +0100 Subject: [PATCH 04/46] fix: add setup/teardown to tests and test dependencies to workflow --- .github/workflows/unit-tests.yml | 4 +-- tests/test_file_validation.py | 26 ++++++++------- tests/test_ooxml_files.py | 55 ++++++++------------------------ 3 files changed, 31 insertions(+), 54 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index debe677..8642e34 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -19,8 +19,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pylint poetry pytest - poetry install + pip install pylint poetry + poetry install --with test - name: Run tests run: poetry run pytest diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 2932f99..f41dd33 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -16,33 +16,37 @@ """ from subprocess import check_output -from os import remove, rename +from os import remove, rename, listdir from shutil import copyfile + prog_dir = "docubleach/" test_dir = "tests/test_files/" -def test_valid_file_with_macros(): - copyfile(f"{test_dir}valid_file_with_macros.docm", f"{test_dir}valid_file_with_macros.bak") +def setup_module(): + for file in listdir(test_dir): + copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros.docm", encoding='utf-8') - remove(f"{test_dir}valid_file_with_macros.docm") - rename(f"{test_dir}valid_file_with_macros.bak", f"{test_dir}valid_file_with_macros.docm") +def teardown_module(): + for file in listdir(test_dir): + if file[-4:] != '.bak': + remove(f"{test_dir}{file}") + else: + rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) + + +def test_valid_file_with_macros(): + output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros.docm", encoding='utf-8') assert output == "" def test_valid_file_with_macros_with_check(): - copyfile(f"{test_dir}valid_file_with_macros_check.docm", f"{test_dir}valid_file_with_macros_check.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros_check.docm -c", encoding='utf-8') - remove(f"{test_dir}valid_file_with_macros_check.docm") - rename(f"{test_dir}valid_file_with_macros_check.bak", f"{test_dir}valid_file_with_macros_check.docm") - assert output == "Macros detected and removed.\n" diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index d6f0515..4cb2906 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,7 +9,7 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename +from os import remove, rename, listdir from shutil import copyfile @@ -17,6 +17,19 @@ test_dir = "tests/test_files/" +def setup_module(): + for file in listdir(test_dir): + copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") + + +def teardown_module(): + for file in listdir(test_dir): + if file[-4:] != '.bak': + remove(f"{test_dir}{file}") + else: + rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) + + def test_word_document(): output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document.docx -c", encoding='utf-8') @@ -24,14 +37,9 @@ def test_word_document(): def test_word_document_with_macros(): - copyfile(f"{test_dir}word_document_with_macros.docm", f"{test_dir}word_document_with_macros.docm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document_with_macros.docm -c", encoding='utf-8') - remove(f"{test_dir}word_document_with_macros.docm") - rename(f"{test_dir}word_document_with_macros.docm.bak", f"{test_dir}word_document_with_macros.docm") - assert output == "Macros detected and removed.\n" @@ -42,14 +50,9 @@ def test_word_template(): def test_word_template_with_macros(): - copyfile(f"{test_dir}word_template_with_macros.dotm", f"{test_dir}word_template_with_macros.dotm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_template_with_macros.dotm -c", encoding='utf-8') - remove(f"{test_dir}word_template_with_macros.dotm") - rename(f"{test_dir}word_template_with_macros.dotm.bak", f"{test_dir}word_template_with_macros.dotm") - assert output == "Macros detected and removed.\n" @@ -61,16 +64,9 @@ def test_powerpoint_presentation(): def test_powerpoint_presentation_with_macros(): - copyfile(f"{test_dir}powerpoint_presentation_with_macros.pptm", - f"{test_dir}powerpoint_presentation_with_macros.pptm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_presentation_with_macros.pptm -c", encoding='utf-8') - remove(f"{test_dir}powerpoint_presentation_with_macros.pptm") - rename(f"{test_dir}powerpoint_presentation_with_macros.pptm.bak", - f"{test_dir}powerpoint_presentation_with_macros.pptm") - assert output == "Macros detected and removed.\n" @@ -82,16 +78,9 @@ def test_powerpoint_template(): def test_powerpoint_template_with_macros(): - copyfile(f"{test_dir}powerpoint_template_with_macros.potm", - f"{test_dir}powerpoint_template_with_macros.potm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_template_with_macros.potm -c", encoding='utf-8') - remove(f"{test_dir}powerpoint_template_with_macros.potm") - rename(f"{test_dir}powerpoint_template_with_macros.potm.bak", - f"{test_dir}powerpoint_template_with_macros.potm") - assert output == "Macros detected and removed.\n" @@ -103,14 +92,9 @@ def test_powerpoint_show(): def test_powerpoint_show_with_macros(): - copyfile(f"{test_dir}powerpoint_show_with_macros.ppsm", f"{test_dir}powerpoint_show_with_macros.ppsm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_show_with_macros.ppsm -c", encoding='utf-8') - remove(f"{test_dir}powerpoint_show_with_macros.ppsm") - rename(f"{test_dir}powerpoint_show_with_macros.ppsm.bak", f"{test_dir}powerpoint_show_with_macros.ppsm") - assert output == "Macros detected and removed.\n" @@ -122,15 +106,9 @@ def test_excel_spreadsheet(): def test_excel_spreadsheet_with_macros(): - copyfile(f"{test_dir}excel_spreadsheet_with_macros.xlsm", - f"{test_dir}excel_spreadsheet_with_macros.xlsm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_spreadsheet_with_macros.xlsm -c", encoding='utf-8') - remove(f"{test_dir}excel_spreadsheet_with_macros.xlsm") - rename(f"{test_dir}excel_spreadsheet_with_macros.xlsm.bak", f"{test_dir}excel_spreadsheet_with_macros.xlsm") - assert output == "Macros detected and removed.\n" @@ -141,12 +119,7 @@ def test_excel_template(): def test_excel_template_with_macros(): - copyfile(f"{test_dir}excel_template_with_macros.xltm", f"{test_dir}excel_template_with_macros.xltm.bak") - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_template_with_macros.xltm -c", encoding='utf-8') - remove(f"{test_dir}excel_template_with_macros.xltm") - rename(f"{test_dir}excel_template_with_macros.xltm.bak", f"{test_dir}excel_template_with_macros.xltm") - assert output == "Macros detected and removed.\n" From fda99cbcc001623085634c929b04831c7d72d8e1 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 08:43:12 +0100 Subject: [PATCH 05/46] fix: edit path slashes to work with linux --- tests/test_file_validation.py | 4 ++-- tests/test_ooxml_files.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index f41dd33..d3fe481 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -20,8 +20,8 @@ from shutil import copyfile -prog_dir = "docubleach/" -test_dir = "tests/test_files/" +prog_dir = "docubleach\\" +test_dir = "tests\\test_files\\" def setup_module(): diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 4cb2906..cc47fa8 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -13,8 +13,8 @@ from shutil import copyfile -prog_dir = "docubleach/" -test_dir = "tests/test_files/" +prog_dir = "docubleach\\" +test_dir = "tests\\test_files\\" def setup_module(): From 1c2116051f18cb17034f354463de7ce0af0c39fb Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 08:46:55 +0100 Subject: [PATCH 06/46] revert: path slashes --- tests/test_file_validation.py | 4 ++-- tests/test_ooxml_files.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index d3fe481..f41dd33 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -20,8 +20,8 @@ from shutil import copyfile -prog_dir = "docubleach\\" -test_dir = "tests\\test_files\\" +prog_dir = "docubleach/" +test_dir = "tests/test_files/" def setup_module(): diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index cc47fa8..5727ddb 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -13,8 +13,8 @@ from shutil import copyfile -prog_dir = "docubleach\\" -test_dir = "tests\\test_files\\" +prog_dir = "docubleach/" +test_dir = "tests\\test_files/" def setup_module(): From c69537556a76ea5c23ec16b9c61769dfa6e3ec5a Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 08:51:05 +0100 Subject: [PATCH 07/46] fix: restore all path slashes --- tests/test_ooxml_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 5727ddb..4cb2906 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -14,7 +14,7 @@ prog_dir = "docubleach/" -test_dir = "tests\\test_files/" +test_dir = "tests/test_files/" def setup_module(): From 0ab37223030daac5002db908790ef55447f182c0 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 08:56:51 +0100 Subject: [PATCH 08/46] fix: add pythonpath to .toml --- pyproject.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ba6aabb..e139faf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,12 @@ optional = true pytest = "^8.2.2" +[tool.pytest.ini_options] +pythonpath = [ + "." +] + + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" From 1f9f59026bef084c1d073ca9eb006507236074ed Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 09:09:56 +0100 Subject: [PATCH 09/46] fix: command format for validation tests --- tests/test_file_validation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index f41dd33..c130e2e 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -38,20 +38,20 @@ def teardown_module(): def test_valid_file_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros.docm", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"], encoding='utf-8') assert output == "" def test_valid_file_with_macros_with_check(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros_check.docm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros_check.docm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_valid_file_without_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_without_macros.docx", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"], encoding='utf-8') assert output == "" @@ -68,7 +68,7 @@ def test_invalid_file_size(): with open(f"{test_dir}invalid_file_size.docx", "wb") as out: out.truncate(262144000) - output = check_output(f"python {prog_dir}bleach.py {test_dir}invalid_file_size.docx", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_size.docx"], encoding='utf-8') remove(f"{test_dir}invalid_file_size.docx") From a002fd122dade655aca1d1aec9e9e43b4bbae87f Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 09:22:38 +0100 Subject: [PATCH 10/46] fix: command format for all tests --- tests/test_file_validation.py | 8 +++++--- tests/test_ooxml_files.py | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index c130e2e..82d3ad0 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -38,7 +38,8 @@ def teardown_module(): def test_valid_file_with_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"], encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"], + encoding='utf-8') assert output == "" @@ -51,13 +52,14 @@ def test_valid_file_with_macros_with_check(): def test_valid_file_without_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"], encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"], + encoding='utf-8') assert output == "" def test_invalid_file_type(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}invalid_file_type.txt", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_type.txt"], encoding='utf-8') assert output == "Unsupported file format.\n" diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 4cb2906..a33d021 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -31,95 +31,95 @@ def teardown_module(): def test_word_document(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document.docx -c", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') assert output == "" def test_word_document_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document_with_macros.docm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document_with_macros.docm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_word_template(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_template.dotx -c", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template.dotx", "-c"], encoding='utf-8') assert output == "" def test_word_template_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}word_template_with_macros.dotm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_powerpoint_presentation(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_presentation.pptx -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation.pptx", "-c"], encoding='utf-8') assert output == "" def test_powerpoint_presentation_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_presentation_with_macros.pptm -c", - encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation_with_macros.pptm", + "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_powerpoint_template(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_template.potx -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_template.potx", "-c"], encoding='utf-8') assert output == "" def test_powerpoint_template_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_template_with_macros.potm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_template_with_macros.potm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_powerpoint_show(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_show.ppsx -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_show.ppsx", "-c"], encoding='utf-8') assert output == "" def test_powerpoint_show_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_show_with_macros.ppsm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_show_with_macros.ppsm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_excel_spreadsheet(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_spreadsheet.xlsx -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_spreadsheet.xlsx", "-c"], encoding='utf-8') assert output == "" def test_excel_spreadsheet_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_spreadsheet_with_macros.xlsm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_spreadsheet_with_macros.xlsm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" def test_excel_template(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_template.xltx -c", encoding='utf-8') + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_template.xltx", "-c"], encoding='utf-8') assert output == "" def test_excel_template_with_macros(): - output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_template_with_macros.xltm -c", + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_template_with_macros.xltm", "-c"], encoding='utf-8') assert output == "Macros detected and removed.\n" From a30f3fb8e8ebd6eca04912eebbb1bafb29c73b08 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 09:31:33 +0100 Subject: [PATCH 11/46] fix: remove pyproject changes --- pyproject.toml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e139faf..ba6aabb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,12 +24,6 @@ optional = true pytest = "^8.2.2" -[tool.pytest.ini_options] -pythonpath = [ - "." -] - - [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" From 965771d2143b5cdf60f7171814651db805f6dc70 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 09:51:13 +0100 Subject: [PATCH 12/46] revert: restore pyproject changes --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ba6aabb..b6e0f78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,10 @@ optional = true pytest = "^8.2.2" +[tool.pytest.ini_options] +pythonpath = ["."] + + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" From b87c77fd014359043ee8c60e504d314ac30db71f Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 12:28:08 +0100 Subject: [PATCH 13/46] fix: alter pytest pythonpath --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b6e0f78..ec75540 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ pytest = "^8.2.2" [tool.pytest.ini_options] -pythonpath = ["."] +pythonpath = ["./docubleach"] [build-system] From 9673014eb64c0b9a9ec67b8b453d8bc49ee9e4d7 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 12:32:27 +0100 Subject: [PATCH 14/46] fix: comment out erroneous code --- docubleach/bleach.py | 4 +++- pyproject.toml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 35ce323..d7d6981 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -141,11 +141,13 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: + """ if getsize(file) < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") - return False + return False""" + return True else: print("Unsupported file format.") return False diff --git a/pyproject.toml b/pyproject.toml index ec75540..b6e0f78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ pytest = "^8.2.2" [tool.pytest.ini_options] -pythonpath = ["./docubleach"] +pythonpath = ["."] [build-system] From b171bfe6a4440acf8775dc7e04d9bc8c457200c2 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 13:03:51 +0100 Subject: [PATCH 15/46] revert: uncomment code --- docubleach/bleach.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index d7d6981..35ce323 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -141,13 +141,11 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: - """ if getsize(file) < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") - return False""" - return True + return False else: print("Unsupported file format.") return False From 24e66075aaa09700e63f0e999596d96fe36355b7 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 13:21:32 +0100 Subject: [PATCH 16/46] fix: change file size check method --- docubleach/bleach.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 35ce323..519424e 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -18,8 +18,7 @@ about our organisation and projects. """ from argparse import ArgumentParser -from os import rename, path, remove -from os.path import getsize +from os import rename, path, remove, stat from zipfile import ZipFile from shutil import make_archive, rmtree from olefile import OleFileIO @@ -141,7 +140,7 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: - if getsize(file) < FILESIZE_LIMIT: + if stat(file).st_size < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") From 67428398b4bbcbdb79a1adc6db79f190e3cca52c Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 13:32:15 +0100 Subject: [PATCH 17/46] revert: getsize method re-added --- docubleach/bleach.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 519424e..35ce323 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -18,7 +18,8 @@ about our organisation and projects. """ from argparse import ArgumentParser -from os import rename, path, remove, stat +from os import rename, path, remove +from os.path import getsize from zipfile import ZipFile from shutil import make_archive, rmtree from olefile import OleFileIO @@ -140,7 +141,7 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: - if stat(file).st_size < FILESIZE_LIMIT: + if getsize(file) < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") From 9c119ec1ad2d7970ac74811b70000bdcd7e170fa Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 13:46:46 +0100 Subject: [PATCH 18/46] fix: add testpaths to pyproject.toml --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index b6e0f78..ed1c87d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ pytest = "^8.2.2" [tool.pytest.ini_options] pythonpath = ["."] +testpaths = ["tests"] [build-system] From 1c2cb53f0ef1a218ae663782b75f3a4628edf48d Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 13:54:21 +0100 Subject: [PATCH 19/46] fix: remove pythonpath from pyproject.toml --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ed1c87d..eb4ce66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,6 @@ pytest = "^8.2.2" [tool.pytest.ini_options] -pythonpath = ["."] testpaths = ["tests"] From 7cfc24abffb91e5b73a6e6528aa4e0e613280792 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:08:39 +0100 Subject: [PATCH 20/46] revert: return pythonpath to pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index eb4ce66..b6e0f78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ pytest = "^8.2.2" [tool.pytest.ini_options] -testpaths = ["tests"] +pythonpath = ["."] [build-system] From 38707019b23fd44f7dfb6ff2d28320c3324a0c47 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:25:15 +0100 Subject: [PATCH 21/46] fix: add absolute path detection --- docubleach/bleach.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 35ce323..094d1c4 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -19,7 +19,7 @@ """ from argparse import ArgumentParser from os import rename, path, remove -from os.path import getsize +from os.path import getsize, abspath from zipfile import ZipFile from shutil import make_archive, rmtree from olefile import OleFileIO @@ -158,8 +158,10 @@ def main(): "content is found", action="store_true") args = parser.parse_args() - if validate_file(args.file): - remove_macros(args.file, args.c) + file = abspath(args.file) + + if validate_file(file): + remove_macros(file, args.c) if __name__ == "__main__": From 5a9cad5d012673a47668c0c58c3c7504e44c57df Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:38:12 +0100 Subject: [PATCH 22/46] fix: file backup and restore bug --- tests/test_ooxml_files.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index a33d021..32a9c18 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -24,9 +24,8 @@ def setup_module(): def teardown_module(): for file in listdir(test_dir): - if file[-4:] != '.bak': - remove(f"{test_dir}{file}") - else: + if file[-4:] == '.bak': + remove(f"{test_dir}{file}"[:-4]) rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) From 0229a86ccc8d5f814787716fbc9da50b59252d1d Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:45:53 +0100 Subject: [PATCH 23/46] fix: comment out size check for bug fixing --- docubleach/bleach.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 094d1c4..9cf398f 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -141,11 +141,14 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: - if getsize(file) < FILESIZE_LIMIT: + """ + if getsize(file) < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") return False + """ + return True else: print("Unsupported file format.") return False From 7fe69874a02e14150db0b392caba9b0d7469c9ba Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:51:30 +0100 Subject: [PATCH 24/46] fix: remove setup and teardown --- docubleach/bleach.py | 5 +---- tests/test_ooxml_files.py | 9 +++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 9cf398f..094d1c4 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -141,14 +141,11 @@ def validate_file(file): filetype = file.split(".")[-1].lower() if filetype in ooxml_formats or filetype in bff_formats: - """ - if getsize(file) < FILESIZE_LIMIT: + if getsize(file) < FILESIZE_LIMIT: return True else: print("File exceeds size limit.") return False - """ - return True else: print("Unsupported file format.") return False diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 32a9c18..51270e7 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -16,7 +16,7 @@ prog_dir = "docubleach/" test_dir = "tests/test_files/" - +""" def setup_module(): for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") @@ -24,10 +24,11 @@ def setup_module(): def teardown_module(): for file in listdir(test_dir): - if file[-4:] == '.bak': - remove(f"{test_dir}{file}"[:-4]) + if file[-4:] != '.bak': + remove(f"{test_dir}{file}") + else: rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) - +""" def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') From 8074d1f545406d63afd93311e5f550a5e6c9f619 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 14:53:41 +0100 Subject: [PATCH 25/46] fix: restore setup and teardown --- tests/test_ooxml_files.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 51270e7..a33d021 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -16,7 +16,7 @@ prog_dir = "docubleach/" test_dir = "tests/test_files/" -""" + def setup_module(): for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") @@ -28,7 +28,7 @@ def teardown_module(): remove(f"{test_dir}{file}") else: rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) -""" + def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') From d2dc333ecb4521c8efb63c4bf3982b66cd13d9a5 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 16:40:15 +0100 Subject: [PATCH 26/46] fix: add ls statement to workflow for debug --- .github/workflows/unit-tests.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8642e34..895bf61 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -21,6 +21,12 @@ jobs: python -m pip install --upgrade pip pip install pylint poetry poetry install --with test + + + - name: List directory structure + run: | + ls -R + - name: Run tests run: poetry run pytest From 393ad2461a22930ed2d7ec05cbed371233a86f40 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 16:46:07 +0100 Subject: [PATCH 27/46] fix: add working directory to yml --- .github/workflows/unit-tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 895bf61..3e9d803 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -30,3 +30,4 @@ jobs: - name: Run tests run: poetry run pytest + working-directory: . From 01957a38db314a3e9817f93f6e6dc313f65aa8f8 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:07:26 +0100 Subject: [PATCH 28/46] fix: add absolute path to test scripts --- .github/workflows/unit-tests.yml | 7 ------- docubleach/bleach.py | 6 ++---- tests/test_file_validation.py | 5 +++-- tests/test_ooxml_files.py | 5 +++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 3e9d803..8642e34 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -21,13 +21,6 @@ jobs: python -m pip install --upgrade pip pip install pylint poetry poetry install --with test - - - - name: List directory structure - run: | - ls -R - - name: Run tests run: poetry run pytest - working-directory: . diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 094d1c4..45cbe5f 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -158,10 +158,8 @@ def main(): "content is found", action="store_true") args = parser.parse_args() - file = abspath(args.file) - - if validate_file(file): - remove_macros(file, args.c) + if validate_file(args.file): + remove_macros(args.file, args.c) if __name__ == "__main__": diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 82d3ad0..225ae37 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -17,11 +17,12 @@ from subprocess import check_output from os import remove, rename, listdir +from os.path import abspath from shutil import copyfile -prog_dir = "docubleach/" -test_dir = "tests/test_files/" +prog_dir = abspath("./docubleach/") + "/" +test_dir = abspath("./tests/test_files/") + "/" def setup_module(): diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index a33d021..129a2dd 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -10,11 +10,12 @@ """ from subprocess import check_output from os import remove, rename, listdir +from os.path import abspath from shutil import copyfile -prog_dir = "docubleach/" -test_dir = "tests/test_files/" +prog_dir = abspath("./docubleach/") + "/" +test_dir = abspath("./tests/test_files/") + "/" def setup_module(): From 121d54b7233ebbc653eb6340ba10c7ca1a0dfc79 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:25:34 +0100 Subject: [PATCH 29/46] fix: add cwd test --- tests/test_ooxml_files.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 129a2dd..6be4961 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,7 +9,7 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename, listdir +from os import remove, rename, listdir, getcwd from os.path import abspath from shutil import copyfile @@ -17,6 +17,8 @@ prog_dir = abspath("./docubleach/") + "/" test_dir = abspath("./tests/test_files/") + "/" +cwd = getcwd() + def setup_module(): for file in listdir(test_dir): @@ -32,6 +34,8 @@ def teardown_module(): def test_word_document(): + assert getcwd() == cwd + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') assert output == "" From c369dfe3d0a3d489130365f275a2ff8344bea652 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:32:56 +0100 Subject: [PATCH 30/46] fix: add function setup --- tests/test_ooxml_files.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 6be4961..be6f6a5 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,9 +9,10 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename, listdir, getcwd +from os import remove, rename, listdir, getcwd, chdir from os.path import abspath from shutil import copyfile +import webbrowser prog_dir = abspath("./docubleach/") + "/" @@ -33,9 +34,11 @@ def teardown_module(): rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) -def test_word_document(): - assert getcwd() == cwd +def setup_function(): + chdir(cwd) + +def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') assert output == "" From 47f87fd6c9a98d833d2c3ac3497ed816557f9c18 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:34:52 +0100 Subject: [PATCH 31/46] fix: add function teardown --- tests/test_ooxml_files.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index be6f6a5..be8167c 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -38,6 +38,10 @@ def setup_function(): chdir(cwd) +def teardown_function(): + chdir(cwd) + + def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') From 0a15f6c06126d0658debbfd5321badf08b182bd2 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:36:59 +0100 Subject: [PATCH 32/46] fix: add set cwd to setup module function --- tests/test_ooxml_files.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index be8167c..726e3a9 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -8,6 +8,7 @@ All tests are written for and conducted using pytest. """ +import os from subprocess import check_output from os import remove, rename, listdir, getcwd, chdir from os.path import abspath @@ -18,10 +19,12 @@ prog_dir = abspath("./docubleach/") + "/" test_dir = abspath("./tests/test_files/") + "/" -cwd = getcwd() +cwd = "" def setup_module(): + global cwd + cwd = os.getcwd() for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") From 557e86a2625faea3e90f6796ce3983d7a15d5ea7 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 17:39:31 +0100 Subject: [PATCH 33/46] revert: remove attempts to set cwd as problem is elsewhere --- tests/test_ooxml_files.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 726e3a9..55f36d1 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -8,23 +8,17 @@ All tests are written for and conducted using pytest. """ -import os from subprocess import check_output -from os import remove, rename, listdir, getcwd, chdir +from os import remove, rename, listdir from os.path import abspath from shutil import copyfile -import webbrowser -prog_dir = abspath("./docubleach/") + "/" -test_dir = abspath("./tests/test_files/") + "/" - -cwd = "" +prog_dir = abspath("./docubleach") + "/" +test_dir = abspath("./tests/test_files") + "/" def setup_module(): - global cwd - cwd = os.getcwd() for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") @@ -37,14 +31,6 @@ def teardown_module(): rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) -def setup_function(): - chdir(cwd) - - -def teardown_function(): - chdir(cwd) - - def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') From 9629d10a54551738fd9edbabe9ea5fc3b50a39c8 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 18:05:46 +0100 Subject: [PATCH 34/46] revert: add absolute paths for files (again) --- docubleach/bleach.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 45cbe5f..094d1c4 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -158,8 +158,10 @@ def main(): "content is found", action="store_true") args = parser.parse_args() - if validate_file(args.file): - remove_macros(args.file, args.c) + file = abspath(args.file) + + if validate_file(file): + remove_macros(file, args.c) if __name__ == "__main__": From 989d3828e7fcba6a06b4d8d85f83aa22e05d8ca8 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 18:12:49 +0100 Subject: [PATCH 35/46] revert: add ls command to workflow (again) --- .github/workflows/unit-tests.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8642e34..1a86b4d 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -22,5 +22,9 @@ jobs: pip install pylint poetry poetry install --with test + - name: List directory structure + run: | + ls -R + - name: Run tests run: poetry run pytest From e62e3c4b1df308483ccad93467eb13962ec71122 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 18:18:20 +0100 Subject: [PATCH 36/46] revert: remove absolute paths --- docubleach/bleach.py | 8 +++----- pyproject.toml | 4 ---- tests/test_file_validation.py | 5 ++--- tests/test_ooxml_files.py | 5 ++--- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/docubleach/bleach.py b/docubleach/bleach.py index 094d1c4..35ce323 100644 --- a/docubleach/bleach.py +++ b/docubleach/bleach.py @@ -19,7 +19,7 @@ """ from argparse import ArgumentParser from os import rename, path, remove -from os.path import getsize, abspath +from os.path import getsize from zipfile import ZipFile from shutil import make_archive, rmtree from olefile import OleFileIO @@ -158,10 +158,8 @@ def main(): "content is found", action="store_true") args = parser.parse_args() - file = abspath(args.file) - - if validate_file(file): - remove_macros(file, args.c) + if validate_file(args.file): + remove_macros(args.file, args.c) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index b6e0f78..ba6aabb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,10 +24,6 @@ optional = true pytest = "^8.2.2" -[tool.pytest.ini_options] -pythonpath = ["."] - - [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 225ae37..6505c4d 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -17,12 +17,11 @@ from subprocess import check_output from os import remove, rename, listdir -from os.path import abspath from shutil import copyfile -prog_dir = abspath("./docubleach/") + "/" -test_dir = abspath("./tests/test_files/") + "/" +prog_dir = "./docubleach/" +test_dir = "./tests/test_files/" def setup_module(): diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 55f36d1..068fb60 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -10,12 +10,11 @@ """ from subprocess import check_output from os import remove, rename, listdir -from os.path import abspath from shutil import copyfile -prog_dir = abspath("./docubleach") + "/" -test_dir = abspath("./tests/test_files") + "/" +prog_dir = "./docubleach/" +test_dir = "./tests/test_files/" def setup_module(): From 0c19456ce13b946e22d2712186dd0ee064081664 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 18:22:12 +0100 Subject: [PATCH 37/46] revert: remove erroneous path characters --- tests/test_file_validation.py | 4 ++-- tests/test_ooxml_files.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 6505c4d..82d3ad0 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -20,8 +20,8 @@ from shutil import copyfile -prog_dir = "./docubleach/" -test_dir = "./tests/test_files/" +prog_dir = "docubleach/" +test_dir = "tests/test_files/" def setup_module(): diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 068fb60..a33d021 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -13,8 +13,8 @@ from shutil import copyfile -prog_dir = "./docubleach/" -test_dir = "./tests/test_files/" +prog_dir = "docubleach/" +test_dir = "tests/test_files/" def setup_module(): From 0f3558621f0460bbdda999b4cac53edf2bc4a84f Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 18:35:44 +0100 Subject: [PATCH 38/46] revert: re-add pythonpath --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ba6aabb..b6e0f78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,10 @@ optional = true pytest = "^8.2.2" +[tool.pytest.ini_options] +pythonpath = ["."] + + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" From 91be6cd07b7f1e6d3db6acc1c91679891a03506b Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:01:56 +0100 Subject: [PATCH 39/46] fix: attempt absolute path in test function --- tests/test_ooxml_files.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index a33d021..45a16ec 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,10 +9,11 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename, listdir +from os import remove, rename, listdir, getcwd +from os.path import abspath from shutil import copyfile - +working_dir = getcwd() prog_dir = "docubleach/" test_dir = "tests/test_files/" @@ -31,7 +32,9 @@ def teardown_module(): def test_word_document(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') + file_path = abspath(f"{test_dir}word_document.docx") + + output = check_output(["python", f"{prog_dir}bleach.py", file_path, "-c"], encoding='utf-8') assert output == "" From 24004bf59d5805c4177c5e3385103865541d1239 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:21:51 +0100 Subject: [PATCH 40/46] fix: change test order --- tests/test_files/__init__.py | 0 tests/test_ooxml_files.py | 23 ++++++++++------------- 2 files changed, 10 insertions(+), 13 deletions(-) create mode 100644 tests/test_files/__init__.py diff --git a/tests/test_files/__init__.py b/tests/test_files/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 45a16ec..7ae296c 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,11 +9,10 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename, listdir, getcwd -from os.path import abspath +from os import remove, rename, listdir from shutil import copyfile -working_dir = getcwd() + prog_dir = "docubleach/" test_dir = "tests/test_files/" @@ -31,10 +30,15 @@ def teardown_module(): rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) -def test_word_document(): - file_path = abspath(f"{test_dir}word_document.docx") +def test_word_template_with_macros(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"], + encoding='utf-8') + + assert output == "Macros detected and removed.\n" + - output = check_output(["python", f"{prog_dir}bleach.py", file_path, "-c"], encoding='utf-8') +def test_word_document(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') assert output == "" @@ -52,13 +56,6 @@ def test_word_template(): assert output == "" -def test_word_template_with_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"], - encoding='utf-8') - - assert output == "Macros detected and removed.\n" - - def test_powerpoint_presentation(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation.pptx", "-c"], encoding='utf-8') From 956284dd9bc19a4f3a16b2eda0a0337da664f5de Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:28:49 +0100 Subject: [PATCH 41/46] fix: comment out setup/teardown --- tests/test_files/__init__.py | 0 tests/test_ooxml_files.py | 17 +++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 tests/test_files/__init__.py diff --git a/tests/test_files/__init__.py b/tests/test_files/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 7ae296c..330c19e 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -17,6 +17,7 @@ test_dir = "tests/test_files/" +""" def setup_module(): for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") @@ -28,14 +29,7 @@ def teardown_module(): remove(f"{test_dir}{file}") else: rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) - - -def test_word_template_with_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"], - encoding='utf-8') - - assert output == "Macros detected and removed.\n" - +""" def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') @@ -56,6 +50,13 @@ def test_word_template(): assert output == "" +def test_word_template_with_macros(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"], + encoding='utf-8') + + assert output == "Macros detected and removed.\n" + + def test_powerpoint_presentation(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation.pptx", "-c"], encoding='utf-8') From c508c05cb6d6a31e85bcb729db67c7abedd1f162 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:30:30 +0100 Subject: [PATCH 42/46] revert: uncomment setup/teardown --- tests/test_ooxml_files.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index 330c19e..a33d021 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -17,7 +17,6 @@ test_dir = "tests/test_files/" -""" def setup_module(): for file in listdir(test_dir): copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") @@ -29,7 +28,7 @@ def teardown_module(): remove(f"{test_dir}{file}") else: rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) -""" + def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8') From ad6f49eccae867467aec359a7605e1e6419d5d13 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:41:43 +0100 Subject: [PATCH 43/46] fix: move function into working file --- tests/test_file_validation.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index 82d3ad0..72a9af1 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -44,6 +44,13 @@ def test_valid_file_with_macros(): assert output == "" +def test_word_document_with_macros(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document_with_macros.docm", "-c"], + encoding='utf-8') + + assert output == "Macros detected and removed.\n" + + def test_valid_file_with_macros_with_check(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros_check.docm", "-c"], encoding='utf-8') From ec0bba286da7228f37dd61060d08514c30c2ce09 Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:44:01 +0100 Subject: [PATCH 44/46] fix: remove first test file --- tests/test_file_validation.py | 84 ----------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 tests/test_file_validation.py diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py deleted file mode 100644 index 72a9af1..0000000 --- a/tests/test_file_validation.py +++ /dev/null @@ -1,84 +0,0 @@ -"""These tests are designed to check file validation when various valid and invalid files are given as arguments. - -Each test runs the bleaching function on their file and records the console output. - -These are then compared against the correct outputs. - -For the invalid file size test, a temporary file exceeding the file size limit is generated. - -After output is recorded, it is subsequently deleted as to not occupy storage unnecessarily. - -Valid files containing macros are restored to their original form after testing to ensure test repeatability. - -This is because the purpose of these tests is to check file validation, not macro removal. - -All tests are written for and conducted using pytest. -""" - -from subprocess import check_output -from os import remove, rename, listdir -from shutil import copyfile - - -prog_dir = "docubleach/" -test_dir = "tests/test_files/" - - -def setup_module(): - for file in listdir(test_dir): - copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") - - -def teardown_module(): - for file in listdir(test_dir): - if file[-4:] != '.bak': - remove(f"{test_dir}{file}") - else: - rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) - - -def test_valid_file_with_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"], - encoding='utf-8') - - assert output == "" - - -def test_word_document_with_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document_with_macros.docm", "-c"], - encoding='utf-8') - - assert output == "Macros detected and removed.\n" - - -def test_valid_file_with_macros_with_check(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros_check.docm", "-c"], - encoding='utf-8') - - assert output == "Macros detected and removed.\n" - - -def test_valid_file_without_macros(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"], - encoding='utf-8') - - assert output == "" - - -def test_invalid_file_type(): - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_type.txt"], encoding='utf-8') - - assert output == "Unsupported file format.\n" - - -def test_invalid_file_size(): - - # Create temporary file exceeding 200MB limit - with open(f"{test_dir}invalid_file_size.docx", "wb") as out: - out.truncate(262144000) - - output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_size.docx"], encoding='utf-8') - - remove(f"{test_dir}invalid_file_size.docx") - - assert output == "File exceeds size limit.\n" From 4db3246d1b06d02a6ca62d51d2918571dc68f60a Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:55:12 +0100 Subject: [PATCH 45/46] revert: restore test file --- tests/test_file_validation.py | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/test_file_validation.py diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py new file mode 100644 index 0000000..fad0a0c --- /dev/null +++ b/tests/test_file_validation.py @@ -0,0 +1,64 @@ +"""These tests are designed to check file validation when various valid and invalid files are given as arguments. + +Each test runs the bleaching function on their file and records the console output. + +These are then compared against the correct outputs. + +For the invalid file size test, a temporary file exceeding the file size limit is generated. + +After output is recorded, it is subsequently deleted as to not occupy storage unnecessarily. + +Valid files containing macros are restored to their original form after testing to ensure test repeatability. + +This is because the purpose of these tests is to check file validation, not macro removal. + +All tests are written for and conducted using pytest. +""" + +from subprocess import check_output +from os import remove, rename, listdir +from shutil import copyfile + + +prog_dir = "docubleach/" +test_dir = "tests/test_files/" + + +def test_valid_file_with_macros(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"], + encoding='utf-8') + + assert output == "" + + +def test_valid_file_with_macros_with_check(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros_check.docm", "-c"], + encoding='utf-8') + + assert output == "Macros detected and removed.\n" + + +def test_valid_file_without_macros(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"], + encoding='utf-8') + + assert output == "" + + +def test_invalid_file_type(): + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_type.txt"], encoding='utf-8') + + assert output == "Unsupported file format.\n" + + +def test_invalid_file_size(): + + # Create temporary file exceeding 200MB limit + with open(f"{test_dir}invalid_file_size.docx", "wb") as out: + out.truncate(262144000) + + output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_size.docx"], encoding='utf-8') + + remove(f"{test_dir}invalid_file_size.docx") + + assert output == "File exceeds size limit.\n" From 66ad502e36b2538e25ae99aaf1ea06e41583240a Mon Sep 17 00:00:00 2001 From: Patterbear Date: Wed, 19 Jun 2024 19:58:16 +0100 Subject: [PATCH 46/46] fix: remove error-causing setup/teardown --- tests/test_file_validation.py | 3 +-- tests/test_ooxml_files.py | 15 --------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/tests/test_file_validation.py b/tests/test_file_validation.py index fad0a0c..84c8c85 100644 --- a/tests/test_file_validation.py +++ b/tests/test_file_validation.py @@ -16,8 +16,7 @@ """ from subprocess import check_output -from os import remove, rename, listdir -from shutil import copyfile +from os import remove prog_dir = "docubleach/" diff --git a/tests/test_ooxml_files.py b/tests/test_ooxml_files.py index a33d021..657039b 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -9,27 +9,12 @@ All tests are written for and conducted using pytest. """ from subprocess import check_output -from os import remove, rename, listdir -from shutil import copyfile prog_dir = "docubleach/" test_dir = "tests/test_files/" -def setup_module(): - for file in listdir(test_dir): - copyfile(f"{test_dir}{file}", f"{test_dir}{file}.bak") - - -def teardown_module(): - for file in listdir(test_dir): - if file[-4:] != '.bak': - remove(f"{test_dir}{file}") - else: - rename(f"{test_dir}{file}", f"{test_dir}{file}"[:-4]) - - def test_word_document(): output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8')