import os
import omni.kit.test
import omni.kit.extpath.git
import omni.kit.app
import unittest

ENABLE_TESTS_ENV = "OMNI_KIT_ENABLE_ONLINE_TESTS"
ENABLE_TESTS = bool(os.getenv(ENABLE_TESTS_ENV))

from unittest import mock

class TestExtPathGit(omni.kit.test.AsyncTestCase):
    @unittest.skipUnless(ENABLE_TESTS, f"Skip online tests, set {ENABLE_TESTS_ENV} env to enable.")
    async def test_git(self):
        manager = omni.kit.app.get_app_interface().get_extension_manager()

        URL = "git+https://gitlab-master.nvidia.com/anovoselov/test-git-extensions.git?dir=exts&branch=main"

        manager.add_path(URL)

        self.assertTrue(omni.kit.extpath.git.get_local_path(URL))

        await omni.kit.app.get_app().next_update_async()

        all_exts = {ext["id"] for ext in manager.get_extensions()}
        self.assertIn("omni.foo-0.3.55", all_exts)


    async def test_git_with_mock(self):
        manager = omni.kit.app.get_app_interface().get_extension_manager()

        with mock.patch("omni.kit.extpath.git.extpath_git._call_git", return_value=True):
            url1 = "git+https://some_1.git?dir=exts&branch=main"
            manager.add_path(url1)
            self.assertIsNotNone(omni.kit.extpath.git.get_local_path(url1))
