|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +import shutil |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from nemoguardrails import RailsConfig |
| 22 | +from nemoguardrails.utils import get_railsignore_path, get_railsignore_patterns |
| 23 | + |
| 24 | +CONFIGS_FOLDER = os.path.join(os.path.dirname(__file__), ".", "test_configs") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="function") |
| 28 | +def cleanup(): |
| 29 | + # Copy current rails ignore and prepare for tests |
| 30 | + railsignore_path = get_railsignore_path() |
| 31 | + |
| 32 | + temp_file_path = str(railsignore_path) + "-copy" |
| 33 | + |
| 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}") |
| 37 | + |
| 38 | + # Clean railsignore file before |
| 39 | + cleanup_railsignore() |
| 40 | + |
| 41 | + # Yield control to test |
| 42 | + yield |
| 43 | + |
| 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}") |
| 55 | + |
| 56 | + |
| 57 | +def test_railsignore_config_loading(cleanup): |
| 58 | + # Setup railsignore |
| 59 | + append_railsignore("ignored_config.co") |
| 60 | + |
| 61 | + # Load config |
| 62 | + config = RailsConfig.from_path(os.path.join(CONFIGS_FOLDER, "railsignore_config")) |
| 63 | + |
| 64 | + config_string = str(config) |
| 65 | + # Assert .railsignore successfully ignores |
| 66 | + assert "ignored_config.co" not in config_string |
| 67 | + |
| 68 | + # Other files should load successfully |
| 69 | + assert "config_to_load.co" in config_string |
| 70 | + |
| 71 | + |
| 72 | +def test_get_railsignore_files(cleanup): |
| 73 | + # Empty railsignore |
| 74 | + ignored_files = get_railsignore_patterns() |
| 75 | + |
| 76 | + assert "ignored_module.py" not in ignored_files |
| 77 | + assert "ignored_colang.co" not in ignored_files |
| 78 | + |
| 79 | + # Append files to railsignore |
| 80 | + append_railsignore("ignored_module.py") |
| 81 | + append_railsignore("ignored_colang.co") |
| 82 | + |
| 83 | + # Grab ignored files |
| 84 | + ignored_files = get_railsignore_patterns() |
| 85 | + |
| 86 | + # Check files exist |
| 87 | + assert "ignored_module.py" in ignored_files |
| 88 | + assert "ignored_colang.co" in ignored_files |
| 89 | + |
| 90 | + # Append comment and whitespace |
| 91 | + append_railsignore("# This_is_a_comment.py") |
| 92 | + append_railsignore(" ") |
| 93 | + append_railsignore("") |
| 94 | + |
| 95 | + # Grab ignored files |
| 96 | + ignored_files = get_railsignore_patterns() |
| 97 | + |
| 98 | + # Comments and whitespace not retrieved |
| 99 | + assert "# This_is_a_comment.py" not in ignored_files |
| 100 | + assert " " not in ignored_files |
| 101 | + assert "" not in ignored_files |
| 102 | + |
| 103 | + # Assert files still exist |
| 104 | + assert "ignored_module.py" in ignored_files |
| 105 | + assert "ignored_colang.co" in ignored_files |
| 106 | + |
| 107 | + |
| 108 | +def cleanup_railsignore(): |
| 109 | + """ |
| 110 | + Helper for clearing a railsignore file. |
| 111 | + """ |
| 112 | + railsignore_path = get_railsignore_path() |
| 113 | + |
| 114 | + try: |
| 115 | + with open(railsignore_path, "w") as f: |
| 116 | + pass |
| 117 | + except OSError as e: |
| 118 | + print(f"Error: Unable to create {railsignore_path}. {e}") |
| 119 | + else: |
| 120 | + print(f"Successfully cleaned up .railsignore: {railsignore_path}") |
| 121 | + |
| 122 | + |
| 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 | + |
| 129 | + try: |
| 130 | + with open(railsignore_path, "a") as f: |
| 131 | + f.write(file_name + "\n") |
| 132 | + except FileNotFoundError: |
| 133 | + print(f"No {railsignore_path} found in the current directory.") |
| 134 | + except OSError as e: |
| 135 | + print(f"Error: Failed to write to {railsignore_path}. {e}") |
0 commit comments