#!/usr/bin/env python3 """ Test script to verify that the load_env_from_file function correctly finds the .env file at the project root directory, regardless of the current working directory. """ import os import sys import shutil import tempfile import subprocess # Add the parent directory to the path so we can import from oedb sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) from oedb.utils.db import load_env_from_file from oedb.utils.logging import logger def test_load_env_from_file_in_root(): """ Test the load_env_from_file function when run from the project root directory. """ print("\n=== Testing load_env_from_file from project root ===") # Backup the .env file if it exists env_exists = os.path.exists('.env') if env_exists: print("Backing up existing .env file...") shutil.copy('.env', '.env.backup') # Create a test .env file print("Creating test .env file at project root...") with open('.env', 'w') as f: f.write("TEST_VAR_ROOT=test_value_root\n") # Test the function print("Testing load_env_from_file()...") result = load_env_from_file() print(f"load_env_from_file() returned: {result}") print(f"TEST_VAR_ROOT environment variable: {os.getenv('TEST_VAR_ROOT')}") # Clean up os.remove('.env') # Restore the original .env file if it existed if env_exists: print("Restoring original .env file...") shutil.move('.env.backup', '.env') def test_load_env_from_file_in_subdir(): """ Test the load_env_from_file function when run from a subdirectory. """ print("\n=== Testing load_env_from_file from a subdirectory ===") # Create a temporary subdirectory with tempfile.TemporaryDirectory(dir='.') as temp_dir: print(f"Created temporary subdirectory: {temp_dir}") # Backup the .env file if it exists env_exists = os.path.exists('.env') if env_exists: print("Backing up existing .env file...") shutil.copy('.env', '.env.backup') # Create a test .env file at the project root print("Creating test .env file at project root...") with open('.env', 'w') as f: f.write("TEST_VAR_SUBDIR=test_value_subdir\n") # Create a test script in the subdirectory test_script_path = os.path.join(temp_dir, 'test_script.py') print(f"Creating test script at {test_script_path}...") with open(test_script_path, 'w') as f: f.write("""#!/usr/bin/env python3 import os import sys # Add the project root to the path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from oedb.utils.db import load_env_from_file # Test the function result = load_env_from_file() print(f"load_env_from_file() returned: {result}") print(f"TEST_VAR_SUBDIR environment variable: {os.getenv('TEST_VAR_SUBDIR')}") """) # Make the test script executable os.chmod(test_script_path, 0o755) # Run the test script from the subdirectory print(f"Running test script from {temp_dir}...") subprocess.run([sys.executable, test_script_path], cwd=temp_dir) # Clean up os.remove('.env') # Restore the original .env file if it existed if env_exists: print("Restoring original .env file...") shutil.move('.env.backup', '.env') def test_osm_cal_script(): """ Test the osm_cal.py script to ensure it can find the .env file. """ print("\n=== Testing osm_cal.py script ===") # Backup the .env file if it exists env_exists = os.path.exists('.env') if env_exists: print("Backing up existing .env file...") shutil.copy('.env', '.env.backup') # Create a test .env file with minimal required variables print("Creating test .env file at project root...") with open('.env', 'w') as f: f.write("DB_NAME=test_db\n") f.write("DB_HOST=localhost\n") f.write("DB_USER=test_user\n") f.write("POSTGRES_PASSWORD=test_password\n") # Test the osm_cal.py script print("Testing osm_cal.py script...") try: # We'll use a very short timeout since we expect it to try to connect to the database # and fail, but we just want to verify it gets past the .env check result = subprocess.run( [sys.executable, 'extractors/osm_cal.py'], capture_output=True, text=True, timeout=2 # Short timeout ) print(f"Exit code: {result.returncode}") print(f"Output: {result.stdout}") print(f"Error: {result.stderr}") except subprocess.TimeoutExpired: print("Process timed out - this is expected as it's trying to connect to the database") # Clean up os.remove('.env') # Restore the original .env file if it existed if env_exists: print("Restoring original .env file...") shutil.move('.env.backup', '.env') if __name__ == "__main__": test_load_env_from_file_in_root() test_load_env_from_file_in_subdir() test_osm_cal_script()