Spaces:
Paused
Paused
File size: 6,479 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #!/usr/bin/env python3
"""
Test script to verify that the configuration override fix works correctly.
This script tests that .env file settings are respected when not explicitly overridden by command line arguments.
"""
import os
import sys
import tempfile
def test_config_fix():
"""Test that environment variables from .env are respected."""
# Create a temporary .env file with test values
env_content = """
DEBUG_LOGS_ENABLED=true
TRACE_LOGS_ENABLED=true
AUTO_SAVE_AUTH=true
LAUNCH_MODE=normal
SERVER_LOG_LEVEL=DEBUG
"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".env", delete=False) as f:
f.write(env_content)
temp_env_path = f.name
try:
# Test 1: Check if launcher reads .env values correctly when no CLI args provided
print("=== Test 1: Reading .env values without CLI overrides ===")
# Set environment variables to simulate .env file loading
os.environ["DEBUG_LOGS_ENABLED"] = "true"
os.environ["TRACE_LOGS_ENABLED"] = "true"
os.environ["AUTO_SAVE_AUTH"] = "true"
os.environ["LAUNCH_MODE"] = "normal"
os.environ["SERVER_LOG_LEVEL"] = "DEBUG"
# Test the argument parsing by importing the launcher config
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
try:
from launcher.config import parse_args
# Test without CLI args - should use .env defaults
original_argv = sys.argv.copy()
sys.argv = ["launch_camoufox.py"]
args = parse_args()
print(f"DEBUG_LOGS_ENABLED from config: {args.debug_logs}")
print(f"TRACE_LOGS_ENABLED from config: {args.trace_logs}")
print(f"AUTO_SAVE_AUTH from config: {args.auto_save_auth}")
print(
f"debug_logs_from_cli flag: {getattr(args, 'debug_logs_from_cli', 'NOT SET')}"
)
print(
f"trace_logs_from_cli flag: {getattr(args, 'trace_logs_from_cli', 'NOT SET')}"
)
print(
f"auto_save_auth_from_cli flag: {getattr(args, 'auto_save_auth_from_cli', 'NOT SET')}"
)
# Verify the fix: args should reflect .env values when no CLI args provided
assert args.debug_logs, f"Expected debug_logs=True, got {args.debug_logs}"
assert args.trace_logs, f"Expected trace_logs=True, got {args.trace_logs}"
assert args.auto_save_auth, (
f"Expected auto_save_auth=True, got {args.auto_save_auth}"
)
assert not getattr(args, "debug_logs_from_cli", False), (
"debug_logs_from_cli should be False"
)
assert not getattr(args, "trace_logs_from_cli", False), (
"trace_logs_from_cli should be False"
)
assert not getattr(args, "auto_save_auth_from_cli", False), (
"auto_save_auth_from_cli should be False"
)
print("OK Test 1 PASSED: .env values correctly loaded")
except Exception as e:
print(f"X Test 1 FAILED: {e}")
return False
finally:
sys.argv = original_argv
if "launcher.config" in sys.modules:
del sys.modules["launcher.config"]
# Test 2: Check if CLI args override .env values
print("\n=== Test 2: CLI args override .env values ===")
try:
# Reset the module to force re-import
if "launcher.config" in sys.modules:
del sys.modules["launcher.config"]
# Test with CLI args - should override .env defaults
original_argv = sys.argv.copy()
sys.argv = [
"launch_camoufox.py",
"--debug-logs",
"--trace-logs",
"--auto-save-auth",
]
args = parse_args()
print(f"DEBUG_LOGS_ENABLED from config: {args.debug_logs}")
print(f"TRACE_LOGS_ENABLED from config: {args.trace_logs}")
print(f"AUTO_SAVE_AUTH from config: {args.auto_save_auth}")
print(
f"debug_logs_from_cli flag: {getattr(args, 'debug_logs_from_cli', 'NOT SET')}"
)
print(
f"trace_logs_from_cli flag: {getattr(args, 'trace_logs_from_cli', 'NOT SET')}"
)
print(
f"auto_save_auth_from_cli flag: {getattr(args, 'auto_save_auth_from_cli', 'NOT SET')}"
)
# Verify CLI override: args should be True and CLI flags should be True
assert args.debug_logs, f"Expected debug_logs=True, got {args.debug_logs}"
assert args.trace_logs, f"Expected trace_logs=True, got {args.trace_logs}"
assert args.auto_save_auth, (
f"Expected auto_save_auth=True, got {args.auto_save_auth}"
)
assert getattr(args, "debug_logs_from_cli", False), (
"debug_logs_from_cli should be True"
)
assert getattr(args, "trace_logs_from_cli", False), (
"trace_logs_from_cli should be True"
)
assert getattr(args, "auto_save_auth_from_cli", False), (
"auto_save_auth_from_cli should be True"
)
print("OK Test 2 PASSED: CLI args correctly override .env values")
except Exception as e:
print(f"X Test 2 FAILED: {e}")
return False
finally:
sys.argv = original_argv
if "launcher.config" in sys.modules:
del sys.modules["launcher.config"]
return True
finally:
# Clean up temporary file
try:
os.unlink(temp_env_path)
except OSError:
pass
if __name__ == "__main__":
print("Testing configuration override fix...")
print("=" * 60)
success = test_config_fix()
print("\n" + "=" * 60)
if success:
print("ALL TESTS PASSED! Configuration fix is working correctly.")
print("\nSummary of the fix:")
print("- .env file values are now respected when no CLI args are provided")
print("- CLI arguments still properly override .env values when specified")
print(
"- The launcher distinguishes between defaults and explicit user settings"
)
else:
print("TESTS FAILED! Configuration fix needs more work.")
sys.exit(1)
|