#!/usr/bin/env python3
"""Run tests in an isolated environment to ensure we test the installed package."""

import os
import sys
import shutil
import tempfile
import subprocess

def main():
    # Get the project directory
    project_dir = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    
    # Create a temporary directory
    with tempfile.TemporaryDirectory() as tmpdir:
        # Copy tests to temporary location
        test_src = os.path.join(project_dir, 'tests')
        test_dst = os.path.join(tmpdir, 'tests')
        shutil.copytree(test_src, test_dst)
        
        # Change to temp directory to ensure we're not in project directory
        os.chdir(tmpdir)
        
        # Run pytest on the copied tests
        result = subprocess.run([sys.executable, '-m', 'pytest', 'tests', '-v'], 
                                capture_output=False)
        
        # Exit with pytest's exit code
        sys.exit(result.returncode)

if __name__ == '__main__':
    main()