diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index d8586de..046720c 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -2,9 +2,11 @@ name: Pylint on: push: - branches: [ "main" ] + branches: + - "**" pull_request: - branches: [ "main" ] + branches: + - "**" jobs: build: @@ -13,18 +15,18 @@ jobs: matrix: python-version: ["3.9"] steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - cd backend - pip install -r requirements.txt - pip install pylint - - name: Analysing the code with pylint - run: | - cd backend - pylint --fail-under=7.0 $(git ls-files '*.py') + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + cd backend + pip install -r requirements.txt + pip install pylint + - name: Analysing the code with pylint + run: | + cd backend + pylint --fail-under=7.0 $(git ls-files '*.py') diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 715320a..c60fdaf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,35 +4,36 @@ name: Python application on: push: - branches: [ "main" ] + branches: + - "**" pull_request: - branches: [ "main" ] + branches: + - "**" permissions: contents: read jobs: build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Set up Python 3.9 - uses: actions/setup-python@v3 - with: - python-version: "3.9" - - name: Install dependencies - run: | - python -m pip install --upgrade pip - cd backend - pip install -r requirements.txt - - name: Test - run: | - cd backend - python -m unittest discover . - - name: View Coverage - run: | - cd backend - coverage run --source=code -m unittest discover -s . - coverage report -m + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.9 + uses: actions/setup-python@v3 + with: + python-version: "3.9" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + cd backend + pip install -r requirements.txt + - name: Test + run: | + cd backend + python -m unittest discover . + - name: View Coverage + run: | + cd backend + coverage run --source=code -m unittest discover -s . + coverage report -m diff --git a/backend/code/utils.py b/backend/code/utils.py index fdb2126..f97d0a9 100644 --- a/backend/code/utils.py +++ b/backend/code/utils.py @@ -13,7 +13,7 @@ from config import SOURCES_FOLDER, UPLOADS_FOLDER -def custom_encoding(normal_input: any) -> bytes: +def custom_encoding(normal_input: any) -> str: """ Using utf-8 encoding. This is our custom encoding function we use in the entire project, we should not use the inbuilt functions. diff --git a/backend/test/common.py b/backend/test/common.py new file mode 100644 index 0000000..60b0c92 --- /dev/null +++ b/backend/test/common.py @@ -0,0 +1,34 @@ +""" +Common Code for testing class +""" + +import os + +from code.file_tokenizer import hash_file_blocks +from config import UPLOADS_FOLDER, SOURCES_FOLDER, HASH_EXTENSION + + +def write_to_testing_file_and_create_hackthehill(function_name: str, testing_file: str) -> None: + """ + Write the function name to the testing file (which is generated as a side effect) and + generate the hackthehill function (also a side effect). + """ + + with open(testing_file, "x", encoding="utf-8") as f: + f.write(function_name) + hash_file_blocks(testing_file) + + +def remove_files(function_name: str): + """ + Remove the files generated while testing components + """ + + testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt") + hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION) + + if os.path.exists(testing_file): + os.remove(testing_file) + + if os.path.exists(hackthehill_file): + os.remove(hackthehill_file) diff --git a/backend/test/test_file_tokenizer.py b/backend/test/test_file_tokenizer.py index 109114d..339c045 100644 --- a/backend/test/test_file_tokenizer.py +++ b/backend/test/test_file_tokenizer.py @@ -8,6 +8,7 @@ from code.file_tokenizer import hash_file_blocks, get_block_content from code.utils import custom_encoding from config import UPLOADS_FOLDER, SOURCES_FOLDER, HASH_EXTENSION +from test.common import write_to_testing_file_and_create_hackthehill, remove_files class TestFileTokenizer(unittest.TestCase): @@ -45,8 +46,7 @@ def test_hash_file_blocks_with_empty_file_returns_correct_value(self): self.assertEqual(hackthehill_file_hashed_content, custom_encoding(json.dumps(hackthehill_file_content))) - os.remove(testing_file) - os.remove(hackthehill_file) + remove_files(function_name) def test_hash_file_blocks_with_non_empty_file_returns_correct_value(self): """ @@ -91,8 +91,7 @@ def test_hash_file_blocks_with_non_empty_file_returns_correct_value(self): self.assertEqual(hackthehill_file_hashed_content, custom_encoding(json.dumps(hackthehill_file_content))) - os.remove(testing_file) - os.remove(hackthehill_file) + remove_files(function_name) def test_get_block_content_block_out_index_throws_error(self): """ @@ -103,16 +102,12 @@ def test_get_block_content_block_out_index_throws_error(self): testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt") hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION) - with open(testing_file, "x", encoding="utf-8") as f: - f.write(function_name) - - hash_file_blocks(testing_file) + write_to_testing_file_and_create_hackthehill(function_name, testing_file) self.assertRaises(ValueError, get_block_content, hackthehill_file, -1) self.assertRaises(ValueError, get_block_content, hackthehill_file, 1) - os.remove(testing_file) - os.remove(hackthehill_file) + remove_files(function_name) def test_get_block_content_returns_correct_value(self): """ @@ -135,5 +130,4 @@ def test_get_block_content_returns_correct_value(self): self.assertEqual(get_block_content(hackthehill_file, 0), encoded_text) - os.remove(testing_file) - os.remove(hackthehill_file) + remove_files(function_name) diff --git a/backend/test/test_utils.py b/backend/test/test_utils.py index 15e5e03..d18c4cb 100644 --- a/backend/test/test_utils.py +++ b/backend/test/test_utils.py @@ -1,7 +1,7 @@ """ Testing the Utilities class """ -import hashlib + import os.path import unittest @@ -9,6 +9,7 @@ from code.utils import find_file, custom_encoding, get_filename_by_file_id, custom_decoding from config import SOURCES_FOLDER, CODE_FOLDER, UPLOADS_FOLDER, HASH_EXTENSION +from test.common import write_to_testing_file_and_create_hackthehill, remove_files class TestUtils(unittest.TestCase): @@ -74,7 +75,7 @@ def test_get_filename_by_file_id_with_no_matching_id_returns_none(self): self.assertEqual(get_filename_by_file_id(file_id), None) - os.remove(testing_file) + remove_files(function_name) def test_get_filename_by_file_id_with_matching_id_returns_tuple(self): """ @@ -86,17 +87,14 @@ def test_get_filename_by_file_id_with_matching_id_returns_tuple(self): testing_file = os.path.join(UPLOADS_FOLDER, f"{function_name}.txt") hackthehill_file = os.path.join(SOURCES_FOLDER, function_name + HASH_EXTENSION) - with open(testing_file, "x", encoding="utf-8") as f: - f.write(function_name) - hash_file_blocks(testing_file) + write_to_testing_file_and_create_hackthehill(function_name, testing_file) - with open(hackthehill_file, "r", encoding="utf-8") as g: - hackthehill_file_content = g.read() - file_id = custom_encoding(hackthehill_file_content) + with open(hackthehill_file, "r", encoding="utf-8") as g: + hackthehill_file_content = g.read() + file_id = custom_encoding(hackthehill_file_content) - self.assertEqual(get_filename_by_file_id(file_id), - (os.path.basename(testing_file), - os.path.basename(hackthehill_file))) + self.assertEqual(get_filename_by_file_id(file_id), + (os.path.basename(testing_file), + os.path.basename(hackthehill_file))) - os.remove(testing_file) - os.remove(hackthehill_file) + remove_files(function_name)