#!/usr/bin/env python3 """ Test script to validate the comprehensive uvicorn logging fix for TTS service. This script tests all layers of protection without starting the full service. """ import sys import os import subprocess import tempfile def test_import_safety(): """Test that imports work without logging conflicts""" print("๐Ÿงช Testing import safety...") test_code = ''' import sys print("[TEST] Starting import test") # This should trigger our SafeStream protection import os import warnings from typing import List, Dict, Any, Optional # Environment protection should be active print(f"[TEST] PYTHONWARNINGS: {os.environ.get('PYTHONWARNINGS', 'not set')}") print(f"[TEST] GRADIO_ANALYTICS_ENABLED: {os.environ.get('GRADIO_ANALYTICS_ENABLED', 'not set')}") # Logging should be completely disabled import logging print(f"[TEST] Logging disabled: {logging.root.disabled}") # Safe imports that previously caused conflicts try: import gradio as gr print("[TEST] โœ… Gradio import successful") except Exception as e: print(f"[TEST] โŒ Gradio import failed: {e}") try: import torch print("[TEST] โœ… PyTorch import successful") except Exception as e: print(f"[TEST] โŒ PyTorch import failed: {e}") try: import transformers print("[TEST] โœ… Transformers import successful") except Exception as e: print(f"[TEST] โŒ Transformers import failed: {e}") print("[TEST] Import test completed") ''' # Write test code to temporary file with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: f.write(test_code) temp_file = f.name try: # Run the test code using the TTS app imports result = subprocess.run([ sys.executable, '-c', f'exec(open("{os.path.abspath("app.py")}").read()[:2000]); exec(open("{temp_file}").read())' ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) print("STDOUT:") print(result.stdout) if result.stderr: print("STDERR:") print(result.stderr) if result.returncode == 0: print("โœ… Import safety test PASSED") return True else: print("โŒ Import safety test FAILED") return False finally: os.unlink(temp_file) def test_safe_log_function(): """Test the safe_log function""" print("\n๐Ÿงช Testing safe_log function...") test_code = ''' # Import the safe_log function from app.py import sys import os exec(open("app.py").read()[:3000]) # Import first part including safe_log # Test safe_log function try: safe_log("info", "Test info message") safe_log("warning", "Test warning message") safe_log("error", "Test error message") print("[TEST] โœ… safe_log function working correctly") except Exception as e: print(f"[TEST] โŒ safe_log function failed: {e}") ''' result = subprocess.run([ sys.executable, '-c', test_code ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) print("STDOUT:") print(result.stdout) if result.stderr: print("STDERR:") print(result.stderr) if result.returncode == 0 and "[TTS-INFO]" in result.stdout: print("โœ… safe_log function test PASSED") return True else: print("โŒ safe_log function test FAILED") return False def test_syntax_validation(): """Test Python syntax validation""" print("\n๐Ÿงช Testing syntax validation...") result = subprocess.run([ sys.executable, '-m', 'py_compile', 'app.py' ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) if result.returncode == 0: print("โœ… Syntax validation PASSED") return True else: print("โŒ Syntax validation FAILED") print("STDERR:", result.stderr) return False def test_stream_protection(): """Test SafeStream protection""" print("\n๐Ÿงช Testing SafeStream protection...") test_code = ''' import sys import os # Test SafeStream class from app.py exec(open("app.py").read()[:1000]) # Get SafeStream class # Test SafeStream functionality try: stream = SafeStream('stdout') stream.write("Test write operation") stream.flush() print("[TEST] โœ… SafeStream write/flush successful") # Test error conditions try: stream.fileno() print("[TEST] โŒ SafeStream should raise OSError for fileno()") except OSError: print("[TEST] โœ… SafeStream correctly raises OSError for fileno()") print(f"[TEST] SafeStream isatty(): {stream.isatty()}") except Exception as e: print(f"[TEST] โŒ SafeStream test failed: {e}") ''' result = subprocess.run([ sys.executable, '-c', test_code ], capture_output=True, text=True, cwd=os.path.dirname(os.path.abspath(__file__))) print("STDOUT:") print(result.stdout) if result.stderr: print("STDERR:") print(result.stderr) if result.returncode == 0 and "SafeStream write/flush successful" in result.stdout: print("โœ… SafeStream protection test PASSED") return True else: print("โŒ SafeStream protection test FAILED") return False def main(): """Run all logging fix tests""" print("๐Ÿš€ TTS Service - Comprehensive Logging Fix Validation") print("=" * 60) tests = [ ("Syntax Validation", test_syntax_validation), ("SafeStream Protection", test_stream_protection), ("safe_log Function", test_safe_log_function), ("Import Safety", test_import_safety), ] results = [] for test_name, test_func in tests: print(f"\n๐Ÿ“‹ Running {test_name}...") try: result = test_func() results.append((test_name, result)) except Exception as e: print(f"โŒ {test_name} failed with exception: {e}") results.append((test_name, False)) print("\n" + "=" * 60) print("๐ŸŽฏ TEST SUMMARY") print("=" * 60) passed = 0 total = len(results) for test_name, result in results: status = "โœ… PASSED" if result else "โŒ FAILED" print(f"{test_name:.<40} {status}") if result: passed += 1 print("-" * 60) print(f"Total: {passed}/{total} tests passed") if passed == total: print("\n๐ŸŽ‰ ALL TESTS PASSED - TTS logging fix is working correctly!") return True else: print(f"\nโš ๏ธ {total - passed} tests failed - review output above") return False if __name__ == "__main__": success = main() sys.exit(0 if success else 1)