Skip to content

Commit

Permalink
refactor(tests): use system temp dir for .railsignore
Browse files Browse the repository at this point in the history
Moved the creation of the temporary .railsignore file to the system's temporary directory in the cleanup fixture. This change ensures compatibility across different operating systems, the test was failing on windows.
  • Loading branch information
Pouyanpi committed Oct 26, 2024
1 parent 12b05c1 commit 4c3563c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tests/test_railsignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os
import shutil
import tempfile
from pathlib import Path
from unittest.mock import patch

Expand All @@ -26,13 +27,28 @@
CONFIGS_FOLDER = os.path.join(os.path.dirname(__file__), ".", "test_configs")


def cleanup_railsignore(railsignore_path):
"""Helper for clearing a railsignore file."""
try:
with open(railsignore_path, "w") as f:
pass
except OSError as e:
print(f"Error: Unable to create {railsignore_path}. {e}")
else:
print(f"Successfully cleaned up .railsignore: {railsignore_path}")


@pytest.fixture(scope="function")
def cleanup():
# Get the system's temporary directory
temp_dir = Path(tempfile.gettempdir())
# Create a path for the .railsignore file in the temp directory
railsignore_path = temp_dir / ".railsignore"

# Mock the path to the .railsignore file
with patch(
"nemoguardrails.utils.get_railsignore_path"
) as mock_get_railsignore_path:
railsignore_path = Path("/tmp/.railsignore")
mock_get_railsignore_path.return_value = railsignore_path

# Ensure the mock file exists
Expand Down Expand Up @@ -120,17 +136,6 @@ def test_is_ignored_by_railsignore(cleanup):
assert not is_ignored_by_railsignore("not_ignored.py", ignored_files)


def cleanup_railsignore(railsignore_path):
"""Helper for clearing a railsignore file."""
try:
with open(railsignore_path, "w") as f:
pass
except OSError as e:
print(f"Error: Unable to create {railsignore_path}. {e}")
else:
print(f"Successfully cleaned up .railsignore: {railsignore_path}")


def append_railsignore(railsignore_path: str, file_name: str) -> None:
"""Helper for appending to a railsignore file."""
try:
Expand Down

0 comments on commit 4c3563c

Please sign in to comment.