1515
1616import os
1717import shutil
18+ from pathlib import Path
19+ from unittest .mock import patch
1820
1921import pytest
2022
2123from nemoguardrails import RailsConfig
22- from nemoguardrails .utils import get_railsignore_path , get_railsignore_patterns
24+ from nemoguardrails .utils import get_railsignore_patterns , is_ignored_by_railsignore
2325
2426CONFIGS_FOLDER = os .path .join (os .path .dirname (__file__ ), "." , "test_configs" )
2527
2628
2729@pytest .fixture (scope = "function" )
2830def cleanup ():
29- # Copy current rails ignore and prepare for tests
30- railsignore_path = get_railsignore_path ()
31+ # Mock the path to the .railsignore file
32+ with patch (
33+ "nemoguardrails.utils.get_railsignore_path"
34+ ) as mock_get_railsignore_path :
35+ railsignore_path = Path ("/tmp/.railsignore" )
36+ mock_get_railsignore_path .return_value = railsignore_path
3137
32- temp_file_path = str (railsignore_path ) + "-copy"
38+ # Ensure the mock file exists
39+ railsignore_path .touch ()
3340
34- # Copy the original .railsignore to a temporary file
35- shutil .copy (railsignore_path , temp_file_path )
36- print (f"Copied { railsignore_path } to { temp_file_path } " )
41+ # Clean railsignore file before
42+ cleanup_railsignore (railsignore_path )
3743
38- # Clean railsignore file before
39- cleanup_railsignore ()
44+ # Yield control to test
45+ yield railsignore_path
4046
41- # Yield control to test
42- yield
47+ # Clean railsignore file after
48+ cleanup_railsignore ( railsignore_path )
4349
44- # Clean railsignore file before
45- cleanup_railsignore ()
46-
47- # Restore the original .railsignore from the temporary copy
48- shutil .copy (temp_file_path , railsignore_path )
49- print (f"Restored { railsignore_path } from { temp_file_path } " )
50-
51- # Delete the temporary file
52- if os .path .exists (temp_file_path ):
53- os .remove (temp_file_path )
54- print (f"Deleted temporary file { temp_file_path } " )
50+ # Remove the mock file
51+ if railsignore_path .exists ():
52+ railsignore_path .unlink ()
5553
5654
5755def test_railsignore_config_loading (cleanup ):
56+ railsignore_path = cleanup
5857 # Setup railsignore
59- append_railsignore ("ignored_config.co" )
58+ append_railsignore (railsignore_path , "ignored_config.co" )
6059
6160 # Load config
6261 config = RailsConfig .from_path (os .path .join (CONFIGS_FOLDER , "railsignore_config" ))
@@ -69,31 +68,32 @@ def test_railsignore_config_loading(cleanup):
6968 assert "config_to_load.co" in config_string
7069
7170
72- def test_get_railsignore_files (cleanup ):
71+ def test_get_railsignore_patterns (cleanup ):
72+ railsignore_path = cleanup
7373 # Empty railsignore
74- ignored_files = get_railsignore_patterns ()
74+ ignored_files = get_railsignore_patterns (railsignore_path )
7575
7676 assert "ignored_module.py" not in ignored_files
7777 assert "ignored_colang.co" not in ignored_files
7878
7979 # Append files to railsignore
80- append_railsignore ("ignored_module.py" )
81- append_railsignore ("ignored_colang.co" )
80+ append_railsignore (railsignore_path , "ignored_module.py" )
81+ append_railsignore (railsignore_path , "ignored_colang.co" )
8282
8383 # Grab ignored files
84- ignored_files = get_railsignore_patterns ()
84+ ignored_files = get_railsignore_patterns (railsignore_path )
8585
8686 # Check files exist
8787 assert "ignored_module.py" in ignored_files
8888 assert "ignored_colang.co" in ignored_files
8989
9090 # Append comment and whitespace
91- append_railsignore ("# This_is_a_comment.py" )
92- append_railsignore (" " )
93- append_railsignore ("" )
91+ append_railsignore (railsignore_path , "# This_is_a_comment.py" )
92+ append_railsignore (railsignore_path , " " )
93+ append_railsignore (railsignore_path , "" )
9494
9595 # Grab ignored files
96- ignored_files = get_railsignore_patterns ()
96+ ignored_files = get_railsignore_patterns (railsignore_path )
9797
9898 # Comments and whitespace not retrieved
9999 assert "# This_is_a_comment.py" not in ignored_files
@@ -105,12 +105,23 @@ def test_get_railsignore_files(cleanup):
105105 assert "ignored_colang.co" in ignored_files
106106
107107
108- def cleanup_railsignore ():
109- """
110- Helper for clearing a railsignore file.
111- """
112- railsignore_path = get_railsignore_path ()
108+ def test_is_ignored_by_railsignore (cleanup ):
109+ railsignore_path = cleanup
110+ # Append files to railsignore
111+ append_railsignore (railsignore_path , "ignored_module.py" )
112+ append_railsignore (railsignore_path , "ignored_colang.co" )
113+
114+ # Grab ignored files
115+ ignored_files = get_railsignore_patterns (railsignore_path )
116+
117+ # Check if files are ignored
118+ assert is_ignored_by_railsignore ("ignored_module.py" , ignored_files )
119+ assert is_ignored_by_railsignore ("ignored_colang.co" , ignored_files )
120+ assert not is_ignored_by_railsignore ("not_ignored.py" , ignored_files )
121+
113122
123+ def cleanup_railsignore (railsignore_path ):
124+ """Helper for clearing a railsignore file."""
114125 try :
115126 with open (railsignore_path , "w" ) as f :
116127 pass
@@ -120,12 +131,8 @@ def cleanup_railsignore():
120131 print (f"Successfully cleaned up .railsignore: { railsignore_path } " )
121132
122133
123- def append_railsignore (file_name : str ) -> None :
124- """
125- Helper for appending to a railsignore file.
126- """
127- railsignore_path = get_railsignore_path ()
128-
134+ def append_railsignore (railsignore_path : str , file_name : str ) -> None :
135+ """Helper for appending to a railsignore file."""
129136 try :
130137 with open (railsignore_path , "a" ) as f :
131138 f .write (file_name + "\n " )
0 commit comments