diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..6babd9e --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,56 @@ +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 + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + + #---------------------------------------------- + # load cached venv if cache exists + #---------------------------------------------- + - name: Load cached venv + id: cached-poetry-dependencies + uses: actions/cache@v3 + with: + path: .venv + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} + #---------------------------------------------- + # install dependencies if cache does not exist + #---------------------------------------------- + - name: Install dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' + run: poetry install --no-interaction --no-root + #---------------------------------------------- + # install your root project, if required + #---------------------------------------------- + - name: Install project + run: poetry install --no-interaction + #---------------------------------------------- + # run test suite + #---------------------------------------------- +# - name: Run tests +# run: | +# source .venv/bin/activate +# pytest tests/ +# coverage report + + - name: Run tests + run: | + python -m unittest discover -s tests diff --git a/README.md b/README.md index c7d3da0..d0fcaac 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_ooxml_files.py b/tests/test_ooxml_files.py index 7141cdf..d64301a 100644 --- a/tests/test_ooxml_files.py +++ b/tests/test_ooxml_files.py @@ -8,144 +8,87 @@ All tests are written for and conducted using pytest. """ -from subprocess import check_output -from os import remove, rename +import unittest +import os +from subprocess import check_output, CalledProcessError from shutil import copyfile -program_dir = "../docubleach/" +class OOXMLFilesTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.cur_dir = os.getcwd() + cls.test_dir = os.path.join(cls.cur_dir, 'tests') + cls.test_resources_dir = os.path.join(cls.test_dir, 'test_files') + cls.program_dir = os.path.join(cls.cur_dir, "docubleach", "bleach.py") + # Verify the existence of the paths + if not os.path.exists(cls.program_dir): + raise FileNotFoundError(f"Program file not found: {cls.program_dir}") -def test_word_document(): - output = check_output(f"python {program_dir}bleach.py test_files/word_document.docx -c", encoding='utf-8') + def run_test(self, filename, expected_output): + try: + output = check_output( + ["python", self.program_dir, os.path.join(self.test_resources_dir, filename), "-c"], + encoding='utf-8' + ) + self.assertEqual(output, expected_output) + except CalledProcessError as e: + self.fail(f"Subprocess failed with error: {e}") - assert output == "" + def run_macro_test(self, filename, expected_output): + backup_file = os.path.join(self.test_resources_dir, f"{filename}.bak") + test_file = os.path.join(self.test_resources_dir, filename) + copyfile(test_file, backup_file) + try: + self.run_test(filename, expected_output) + finally: + os.remove(test_file) + os.rename(backup_file, test_file) -def test_word_document_with_macros(): - copyfile("test_files/word_document_with_macros.docm", "test_files/word_document_with_macros.docm.bak") + def test_word_document(self): + self.run_test('word_document.docx', "") - output = check_output(f"python {program_dir}bleach.py test_files/word_document_with_macros.docm -c", - encoding='utf-8') + def test_word_document_with_macros(self): + self.run_macro_test('word_document_with_macros.docm', "Macros detected and removed.\n") - remove("test_files/word_document_with_macros.docm") - rename("test_files/word_document_with_macros.docm.bak", "test_files/word_document_with_macros.docm") + def test_word_template(self): + self.run_test('word_template.dotx', "") - assert output == "Macros detected and removed.\n" + def test_word_template_with_macros(self): + self.run_macro_test('word_template_with_macros.dotm', "Macros detected and removed.\n") + def test_powerpoint_presentation(self): + self.run_test('powerpoint_presentation.pptx', "") -def test_word_template(): - output = check_output(f"python {program_dir}bleach.py test_files/word_template.dotx -c", encoding='utf-8') + def test_powerpoint_presentation_with_macros(self): + self.run_macro_test('powerpoint_presentation_with_macros.pptm', "Macros detected and removed.\n") - assert output == "" + def test_powerpoint_template(self): + self.run_test('powerpoint_template.potx', "") + def test_powerpoint_template_with_macros(self): + self.run_macro_test('powerpoint_template_with_macros.potm', "Macros detected and removed.\n") -def test_word_template_with_macros(): - copyfile("test_files/word_template_with_macros.dotm", "test_files/word_template_with_macros.dotm.bak") + def test_powerpoint_show(self): + self.run_test('powerpoint_show.ppsx', "") - output = check_output(f"python {program_dir}bleach.py test_files/word_template_with_macros.dotm -c", - encoding='utf-8') + def test_powerpoint_show_with_macros(self): + self.run_macro_test('powerpoint_show_with_macros.ppsm', "Macros detected and removed.\n") - remove("test_files/word_template_with_macros.dotm") - rename("test_files/word_template_with_macros.dotm.bak", "test_files/word_template_with_macros.dotm") + def test_excel_spreadsheet(self): + self.run_test('excel_spreadsheet.xlsx', "") - assert output == "Macros detected and removed.\n" + def test_excel_spreadsheet_with_macros(self): + self.run_macro_test('excel_spreadsheet_with_macros.xlsm', "Macros detected and removed.\n") + def test_excel_template(self): + self.run_test('excel_template.xltx', "") -def test_powerpoint_presentation(): - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_presentation.pptx -c", - encoding='utf-8') + def test_excel_template_with_macros(self): + self.run_macro_test('excel_template_with_macros.xltm', "Macros detected and removed.\n") - assert output == "" - -def test_powerpoint_presentation_with_macros(): - copyfile("test_files/powerpoint_presentation_with_macros.pptm", - "test_files/powerpoint_presentation_with_macros.pptm.bak") - - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_presentation_with_macros.pptm -c", - encoding='utf-8') - - remove("test_files/powerpoint_presentation_with_macros.pptm") - rename("test_files/powerpoint_presentation_with_macros.pptm.bak", - "test_files/powerpoint_presentation_with_macros.pptm") - - assert output == "Macros detected and removed.\n" - - -def test_powerpoint_template(): - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_template.potx -c", - encoding='utf-8') - - assert output == "" - - -def test_powerpoint_template_with_macros(): - copyfile("test_files/powerpoint_template_with_macros.potm", - "test_files/powerpoint_template_with_macros.potm.bak") - - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_template_with_macros.potm -c", - encoding='utf-8') - - remove("test_files/powerpoint_template_with_macros.potm") - rename("test_files/powerpoint_template_with_macros.potm.bak", - "test_files/powerpoint_template_with_macros.potm") - - assert output == "Macros detected and removed.\n" - - -def test_powerpoint_show(): - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_show.ppsx -c", - encoding='utf-8') - - assert output == "" - - -def test_powerpoint_show_with_macros(): - copyfile("test_files/powerpoint_show_with_macros.ppsm", "test_files/powerpoint_show_with_macros.ppsm.bak") - - output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_show_with_macros.ppsm -c", - encoding='utf-8') - - remove("test_files/powerpoint_show_with_macros.ppsm") - rename("test_files/powerpoint_show_with_macros.ppsm.bak", "test_files/powerpoint_show_with_macros.ppsm") - - assert output == "Macros detected and removed.\n" - - -def test_excel_spreadsheet(): - output = check_output(f"python {program_dir}bleach.py test_files/excel_spreadsheet.xlsx -c", - encoding='utf-8') - - assert output == "" - - -def test_excel_spreadsheet_with_macros(): - copyfile("test_files/excel_spreadsheet_with_macros.xlsm", - "test_files/excel_spreadsheet_with_macros.xlsm.bak") - - output = check_output(f"python {program_dir}bleach.py test_files/excel_spreadsheet_with_macros.xlsm -c", - encoding='utf-8') - - remove("test_files/excel_spreadsheet_with_macros.xlsm") - rename("test_files/excel_spreadsheet_with_macros.xlsm.bak", "test_files/excel_spreadsheet_with_macros.xlsm") - - assert output == "Macros detected and removed.\n" - - -def test_excel_template(): - output = check_output(f"python {program_dir}bleach.py test_files/excel_template.xltx -c", encoding='utf-8') - - assert output == "" - - -def test_excel_template_with_macros(): - copyfile("test_files/excel_template_with_macros.xltm", "test_files/excel_template_with_macros.xltm.bak") - - output = check_output(f"python {program_dir}bleach.py test_files/excel_template_with_macros.xltm -c", - encoding='utf-8') - - remove("test_files/excel_template_with_macros.xltm") - rename("test_files/excel_template_with_macros.xltm.bak", "test_files/excel_template_with_macros.xltm") - - assert output == "Macros detected and removed.\n" +if __name__ == '__main__': + unittest.main()