import os

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.build import can_run


class  MfwMapTestConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"

    def requirements(self):
        self.requires(self.tested_reference_str)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        mfw_package_folder = str(self.dependencies[self.tested_reference_str].package_folder)
        tc.cache_variables["MFW_MAP_PACKAGE_PATH"] = mfw_package_folder
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
        args = [f'--prefix application']
        cmake.install(cli_args=args)

    def layout(self):
        cmake_layout(self)

    def test(self):
        if can_run(self):
            cmd = os.path.join(self.cpp.build.bindir, "application", "example")
            self.run(cmd + " -platform offscreen", env="conanrun")
