#!/usr/bin/env python3
"""
배치 파일 생성 테스트
"""

import os
import tempfile

def test_batch_creation():
    """배치 파일 생성 테스트"""
    print("=" * 60)
    print("Batch File Creation Test")
    print("=" * 60)
    
    # 테스트용 경로
    test_dir = tempfile.mkdtemp()
    exe_dir = test_dir
    exe_name = "test_app.exe"
    
    print(f"Test directory: {test_dir}")
    print(f"Target exe: {exe_name}")
    
    try:
        # 방법 1: f-string with triple quotes (문제가 될 수 있는 방법)
        print("\n--- Method 1: f-string with triple quotes ---")
        try:
            batch_content_1 = f"""@echo off
echo Starting test application...
cd /d "{exe_dir}"
start "" "{exe_name}"
echo Done
"""
            batch_path_1 = os.path.join(test_dir, "method1.bat")
            with open(batch_path_1, 'w', encoding='cp949') as f:
                f.write(batch_content_1)
            
            if os.path.exists(batch_path_1):
                size = os.path.getsize(batch_path_1)
                print(f"✓ Method 1 success: {size} bytes")
                
                # 내용 확인
                with open(batch_path_1, 'r', encoding='cp949') as f:
                    content = f.read()
                    print(f"Content preview: {content[:50]}...")
            else:
                print("✗ Method 1 failed: file not created")
                
        except Exception as e:
            print(f"✗ Method 1 error: {e}")
        
        # 방법 2: 라인별 생성 (권장 방법)
        print("\n--- Method 2: Line-by-line construction ---")
        try:
            batch_lines = [
                "@echo off",
                "echo Starting test application...",
                f'cd /d "{exe_dir}"',
                f'start "" "{exe_name}"',
                "echo Done"
            ]
            batch_content_2 = "\n".join(batch_lines)
            batch_path_2 = os.path.join(test_dir, "method2.bat")
            
            with open(batch_path_2, 'w', encoding='cp949') as f:
                f.write(batch_content_2)
                f.flush()
            
            if os.path.exists(batch_path_2):
                size = os.path.getsize(batch_path_2)
                print(f"✓ Method 2 success: {size} bytes")
                
                # 내용 확인
                with open(batch_path_2, 'r', encoding='cp949') as f:
                    content = f.read()
                    print(f"Content preview: {content[:50]}...")
            else:
                print("✗ Method 2 failed: file not created")
                
        except Exception as e:
            print(f"✗ Method 2 error: {e}")
        
        # 방법 3: 간단한 문자열 조합 (fallback)
        print("\n--- Method 3: Simple string concatenation ---")
        try:
            batch_content_3 = "@echo off\n"
            batch_content_3 += "echo Starting test application...\n"
            batch_content_3 += f'cd /d "{exe_dir}"\n'
            batch_content_3 += f'start "" "{exe_name}"\n'
            batch_content_3 += "echo Done\n"
            
            batch_path_3 = os.path.join(test_dir, "method3.bat")
            with open(batch_path_3, 'w', encoding='cp949') as f:
                f.write(batch_content_3)
            
            if os.path.exists(batch_path_3):
                size = os.path.getsize(batch_path_3)
                print(f"✓ Method 3 success: {size} bytes")
                
                # 내용 확인
                with open(batch_path_3, 'r', encoding='cp949') as f:
                    content = f.read()
                    print(f"Content preview: {content[:50]}...")
            else:
                print("✗ Method 3 failed: file not created")
                
        except Exception as e:
            print(f"✗ Method 3 error: {e}")
    
    finally:
        # 정리
        import shutil
        try:
            shutil.rmtree(test_dir)
            print(f"\nTest directory cleaned up")
        except:
            pass
    
    print("\n--- Recommendations ---")
    print("✓ Use line-by-line construction for reliability")
    print("✓ Always use flush() when writing files")
    print("✓ Verify file creation before attempting to execute")

if __name__ == "__main__":
    test_batch_creation()