import omni.kit.test
import os
from pathlib import Path

from .test_base import KitLaunchTest


class TestScriptExec(KitLaunchTest):
    async def test_script_exec(self):
        kit_file = "\n"

        script_base = """
import omni.kit.app
import sys
def check(v, expected):
    if v != expected:
        print(f"[Test Fail] expected: {expected}, got: {v}")
        omni.kit.app.get_app().post_quit(-1)
"""

        # Regular run
        script = script_base + """
check(len(sys.argv), 1)
        """
        await self._run_kit_with_script(script, kit_file=kit_file)

        # Pass args
        script = script_base + """
check(len(sys.argv), 3)
check(sys.argv[1], "abc")
check(sys.argv[2], "42")
        """
        await self._run_kit_with_script(script, kit_file=kit_file, script_extra_args=" \"abc\" 42")

        # Subfolder with a space in path
        subfolder = f"{self._temp_folder}/abc def"
        os.makedirs(subfolder)
        script = script_base + """
check(len(sys.argv), 1)
        """
        script_file = f"\"{subfolder}/script_1.py\""
        await self._run_kit_with_script(script, kit_file=kit_file, script_file=script_file)

        # Subfolder with a space in path + args
        script = script_base + """
check(len(sys.argv), 3)
check(sys.argv[1], "abc")
check(sys.argv[2], "42")
        """
        script_file = f"\"{subfolder}/script_2.py\""
        await self._run_kit_with_script(script, kit_file=kit_file, script_file=script_file, script_extra_args=" \"abc\" 42")
