Skip to content

Commit

Permalink
Add mock alloyignore to project root and adjust unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
OLILHR committed Aug 3, 2024
1 parent 919206a commit fa5cca3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .alloyignore.mock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# required for unittests

.png
.svg
2 changes: 2 additions & 0 deletions alloy/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def read_alloyignore(project_root, extension_filter):

# pylint: disable=too-many-return-statements
def exclude_files(file_path):
file_path = file_path.replace(os.sep, "/")
if extension_filter:
_, file_extension = os.path.splitext(file_path)
if file_extension[1:] in extension_filter:
return False

for pattern in ignore_list:
pattern = pattern.replace(os.sep, "/")
if pattern.startswith("/"): # covers absolute paths from the root
if file_path.startswith(pattern[1:]):
return True
Expand Down
31 changes: 24 additions & 7 deletions unittests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@
import pytest


@pytest.fixture
def unittests_directory(request):
unittests_dir = os.path.dirname(request.module.__file__)
@pytest.fixture(scope="session")
def project_root():
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


@pytest.fixture(scope="session")
def unittests_directory(project_root):
return {
"test_data": os.path.join(unittests_dir, "data"),
"mock_data": os.path.join(project_root, "unittests", "data"),
}


@pytest.fixture
def alloyignore_path(unittests_directory):
return os.path.join(unittests_directory["test_data"], ".alloyignore")
@pytest.fixture(scope="function")
def mock_alloyignore(monkeypatch, project_root):
"""
Ensures unittests use the .alloyignore.mock file and returns its path.
"""
mock_alloyignore_path = os.path.join(project_root, ".alloyignore.mock")
original_join = os.path.join

def mock_join(*args):
if args[-1] == ".alloyignore":
return mock_alloyignore_path
return original_join(*args)

monkeypatch.setattr(os.path, "join", mock_join)

return mock_alloyignore_path
2 changes: 0 additions & 2 deletions unittests/data/.alloyignore

This file was deleted.

14 changes: 8 additions & 6 deletions unittests/test_file_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from alloy.filter import read_alloyignore


def test_read_alloyignore(unittests_directory, alloyignore_path):
exclude = read_alloyignore(unittests_directory["test_data"], [])
with open(alloyignore_path, encoding="utf-8") as f:
def test_read_alloyignore(project_root, mock_alloyignore):
exclude = read_alloyignore(project_root, [])

with open(mock_alloyignore, encoding="utf-8") as f:
alloyignore = f.read()

assert ".png" in alloyignore
Expand All @@ -18,9 +19,10 @@ def test_read_alloyignore(unittests_directory, alloyignore_path):
assert exclude("test.yml") is False


def test_consolidate_excludes_png_and_svg(unittests_directory, alloyignore_path):
codebase = consolidate(unittests_directory["test_data"])
with open(alloyignore_path, encoding="utf-8") as f:
def test_consolidate_excludes_png_and_svg(unittests_directory, mock_alloyignore):
codebase = consolidate(unittests_directory["mock_data"])

with open(mock_alloyignore, encoding="utf-8") as f:
alloyignore = f.read()

assert "dummy_md.md" in codebase
Expand Down
12 changes: 6 additions & 6 deletions unittests/test_file_filter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from alloy.collector import consolidate


def test_consolidate_specified_filter_extensions(unittests_directory, alloyignore_path):
filtered_codebase = consolidate(unittests_directory["test_data"], extensions=["md", "txt"])
with open(alloyignore_path, encoding="utf-8") as f:
def test_consolidate_specified_filter_extensions(unittests_directory, mock_alloyignore):
filtered_codebase = consolidate(unittests_directory["mock_data"], extensions=["md", "txt"])
with open(mock_alloyignore, encoding="utf-8") as f:
alloyignore = f.read()

assert not any(ext in alloyignore for ext in [".md", ".txt", ".py", ".yml"])
Expand All @@ -18,9 +18,9 @@ def test_consolidate_specified_filter_extensions(unittests_directory, alloyignor
assert "dummy_svg.svg" not in filtered_codebase


def test_extension_filter_bypasses_alloyignore(unittests_directory, alloyignore_path):
filtered_codebase = consolidate(unittests_directory["test_data"], extensions=["svg"])
with open(alloyignore_path, encoding="utf-8") as f:
def test_extension_filter_bypasses_alloyignore(unittests_directory, mock_alloyignore):
filtered_codebase = consolidate(unittests_directory["mock_data"], extensions=["svg"])
with open(mock_alloyignore, encoding="utf-8") as f:
alloyignore = f.read()

assert ".svg" in alloyignore
Expand Down

0 comments on commit fa5cca3

Please sign in to comment.