import omni.kit.test
from .test_base import KitLaunchTest


class TestPortable(KitLaunchTest):
    async def test_kit_portable(self):

        # empty kit file
        kit_file = "[dependencies]\n"

        # script to check portable location is as passed in
        script = """
import omni.kit.app
import carb.tokens
from pathlib import Path
data = Path(carb.tokens.get_tokens_interface().resolve("${{data}}")).resolve()
portable_path = Path(r'{0}').resolve()

def is_subpath(path, root):
    return root in path.parents

if not is_subpath(data, portable_path):
    print(f"Fail, expected portable path: {{(portable_path)}}")
    omni.kit.app.get_app().post_quit(-1)
"""

        # kit.portable:
        portable_file = f"{self._temp_folder}/kit.portable"
        with open(portable_file, "w") as f:
            f.write("foo")

        portable_path = f"{self._temp_folder}/foo"

        await self._run_kit_with_script(
            script.format(portable_path), args=[], kit_file=kit_file, portable=False, kit_file_name="test_bar.kit"
        )

        # test_bar.portable (like app name) (overrides kit.portable too)
        portable_file = f"{self._temp_folder}/test_bar.portable"
        with open(portable_file, "w") as f:
            f.write("wow")

        portable_path = f"{self._temp_folder}/wow"
        await self._run_kit_with_script(
            script.format(portable_path), args=[], kit_file=kit_file, portable=False, kit_file_name="test_bar.kit"
        )

        # arg: `--portable-root` (overrides everything)
        portable_path = f"{self._temp_folder}/lake"
        await self._run_kit_with_script(
            script.format(portable_path),
            args=["--portable-root", portable_path],
            kit_file=kit_file,
            portable=False,
            kit_file_name="test_bar.kit",
        )
