|
4 | 4 | import logging |
5 | 5 | import tempfile |
6 | 6 | import shutil |
| 7 | +import os |
7 | 8 | from pathlib import Path |
8 | 9 | from datetime import datetime |
| 10 | +from unittest.mock import patch |
9 | 11 |
|
10 | 12 | from wobble.decorators import regression_test, integration_test, slow_test |
11 | 13 |
|
@@ -891,5 +893,99 @@ def mock_install(env_name, tag=None): |
891 | 893 | self.env_manager.python_env_manager.get_environment_info = original_get_info |
892 | 894 | self.env_manager._install_hatch_mcp_server = original_install |
893 | 895 |
|
| 896 | + # Non-TTY Handling Backward Compatibility Tests |
| 897 | + |
| 898 | + @regression_test |
| 899 | + @patch('sys.stdin.isatty', return_value=False) |
| 900 | + def test_add_package_non_tty_auto_approve(self, mock_isatty): |
| 901 | + """Test package addition in non-TTY environment (backward compatibility).""" |
| 902 | + # Create environment |
| 903 | + self.env_manager.create_environment("test_env", "Test environment", create_python_env=False) |
| 904 | + |
| 905 | + # Test existing auto_approve=True behavior is preserved |
| 906 | + from test_data_utils import TestDataLoader |
| 907 | + test_loader = TestDataLoader() |
| 908 | + base_pkg_path = test_loader.packages_dir / "basic" / "base_pkg" |
| 909 | + |
| 910 | + if not base_pkg_path.exists(): |
| 911 | + self.skipTest(f"Test package not found: {base_pkg_path}") |
| 912 | + |
| 913 | + result = self.env_manager.add_package_to_environment( |
| 914 | + str(base_pkg_path), |
| 915 | + "test_env", |
| 916 | + auto_approve=False # Should auto-approve due to non-TTY detection |
| 917 | + ) |
| 918 | + |
| 919 | + self.assertTrue(result, "Non-TTY environment should auto-approve even with auto_approve=False") |
| 920 | + mock_isatty.assert_called() # Verify TTY detection was called |
| 921 | + |
| 922 | + @regression_test |
| 923 | + @patch.dict(os.environ, {'HATCH_AUTO_APPROVE': '1'}) |
| 924 | + def test_add_package_environment_variable_compatibility(self): |
| 925 | + """Test new environment variable doesn't break existing workflows.""" |
| 926 | + # Verify existing auto_approve=False behavior with environment variable |
| 927 | + self.env_manager.create_environment("test_env", "Test environment", create_python_env=False) |
| 928 | + |
| 929 | + from test_data_utils import TestDataLoader |
| 930 | + test_loader = TestDataLoader() |
| 931 | + base_pkg_path = test_loader.packages_dir / "basic" / "base_pkg" |
| 932 | + |
| 933 | + if not base_pkg_path.exists(): |
| 934 | + self.skipTest(f"Test package not found: {base_pkg_path}") |
| 935 | + |
| 936 | + result = self.env_manager.add_package_to_environment( |
| 937 | + str(base_pkg_path), |
| 938 | + "test_env", |
| 939 | + auto_approve=False # Should be overridden by environment variable |
| 940 | + ) |
| 941 | + |
| 942 | + self.assertTrue(result, "Environment variable should enable auto-approval") |
| 943 | + |
| 944 | + @regression_test |
| 945 | + @patch('sys.stdin.isatty', return_value=False) |
| 946 | + def test_add_package_with_dependencies_non_tty(self, mock_isatty): |
| 947 | + """Test package with dependencies in non-TTY environment.""" |
| 948 | + # Create environment |
| 949 | + self.env_manager.create_environment("test_env", "Test environment", create_python_env=False) |
| 950 | + |
| 951 | + from test_data_utils import TestDataLoader |
| 952 | + test_loader = TestDataLoader() |
| 953 | + |
| 954 | + # Test with a package that has dependencies |
| 955 | + simple_pkg_path = test_loader.packages_dir / "dependencies" / "simple_dep_pkg" |
| 956 | + |
| 957 | + if not simple_pkg_path.exists(): |
| 958 | + self.skipTest(f"Test package not found: {simple_pkg_path}") |
| 959 | + |
| 960 | + result = self.env_manager.add_package_to_environment( |
| 961 | + str(simple_pkg_path), |
| 962 | + "test_env", |
| 963 | + auto_approve=False # Should auto-approve due to non-TTY |
| 964 | + ) |
| 965 | + |
| 966 | + self.assertTrue(result, "Package with dependencies should install in non-TTY") |
| 967 | + mock_isatty.assert_called() |
| 968 | + |
| 969 | + @regression_test |
| 970 | + @patch.dict(os.environ, {'HATCH_AUTO_APPROVE': 'yes'}) |
| 971 | + def test_environment_variable_case_variations(self): |
| 972 | + """Test environment variable with different case variations.""" |
| 973 | + self.env_manager.create_environment("test_env", "Test environment", create_python_env=False) |
| 974 | + |
| 975 | + from test_data_utils import TestDataLoader |
| 976 | + test_loader = TestDataLoader() |
| 977 | + base_pkg_path = test_loader.packages_dir / "basic" / "base_pkg" |
| 978 | + |
| 979 | + if not base_pkg_path.exists(): |
| 980 | + self.skipTest(f"Test package not found: {base_pkg_path}") |
| 981 | + |
| 982 | + result = self.env_manager.add_package_to_environment( |
| 983 | + str(base_pkg_path), |
| 984 | + "test_env", |
| 985 | + auto_approve=False |
| 986 | + ) |
| 987 | + |
| 988 | + self.assertTrue(result, "Environment variable 'yes' should enable auto-approval") |
| 989 | + |
894 | 990 | if __name__ == "__main__": |
895 | 991 | unittest.main() |
0 commit comments