class ProfileTest(BaseTest):
    def test_build_context_support(self):
        conanfile = textwrap.dedent("""
                from conans import ConanFile
                class Pkg(ConanFile):
                    pass
                """)
        self.save_conanfile(conanfile)
        build_profile = textwrap.dedent("""
                [settings]
                os=Linux
                arch=x86_64
                compiler=gcc
                compiler.version=8
                compiler.libcxx=libstdc++
                build_type=Release
                [options]
                [build_requires]
                [env]
                [conf]
                """)
        build_profile_path = os.path.join(self.tmp_folder, "build_profile")
        tools.save(build_profile_path, build_profile)

        host_profile = textwrap.dedent("""
                        [settings]
                        arch=armv7hf
                        build_type=Release
                        [options]
                        [build_requires]
                        [env]
                        [conf]
                        """)
        host_profile_path = os.path.join(self.tmp_folder, "host_profile")
        tools.save(host_profile_path, host_profile)
        self.packager = ConanMultiPackager(
            username="******",
            reference="tizona/1.0.40@elcidcampeador/testing",
            out=self.output.write)
        self.packager.add_common_builds()
        self.packager.run_builds(curpage=1,
                                 total_pages=1,
                                 base_profile_name=host_profile_path,
                                 base_profile_build_name=build_profile_path)
        self.assertIn("Using specified default base profile", self.output)
        self.assertIn("Using specified build profile", self.output)
    def test_32bits_images(self):
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      use_docker=True,
                                      docker_32_images=True,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertIn("docker pull conanio/gcc6-x86", self.runner.calls[0])

        self.runner.reset()
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      conan_api=self.conan_api,
                                      use_docker=True,
                                      docker_32_images=False,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertNotIn("docker pull conanio/gcc6-i386", self.runner.calls[0])

        self.runner.reset()
        with tools.environment_append({"CONAN_DOCKER_32_IMAGES": "1"}):
            packager = ConanMultiPackager(username="******",
                                          channel="mychannel",
                                          runner=self.runner,
                                          conan_api=self.conan_api,
                                          use_docker=True,
                                          reference="zlib/1.2.11",
                                          ci_manager=self.ci_manager)

            packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
            packager.run_builds(1, 1)
            self.assertIn("docker pull conanio/gcc6-x86", self.runner.calls[0])

        self.runner.reset()
        # Test the opossite
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      conan_api=self.conan_api,
                                      use_docker=True,
                                      docker_32_images=False,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertIn("docker pull conanio/gcc6", self.runner.calls[0])
    def test_remotes(self):
        runner = MockRunner()
        builder = ConanMultiPackager(username="******",
                                     remotes=["url1", "url2"],
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[1], "url1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[3].args[1], "url2")
        self.assertEquals(self.conan_api.calls[3].kwargs["insert"], -1)

        runner = MockRunner()
        self.conan_api = MockConanAPI()
        builder = ConanMultiPackager(username="******",
                                     remotes="myurl1",
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[1], "myurl1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)

        # Named remotes, with SSL flag
        runner = MockRunner()
        self.conan_api = MockConanAPI()
        remotes = [("u1", True, "my_cool_name1"),
                   ("u2", False, "my_cool_name2")]
        builder = ConanMultiPackager(username="******",
                                     remotes=remotes,
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[0], "my_cool_name1")
        self.assertEquals(self.conan_api.calls[1].args[1], "u1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[1].kwargs["verify_ssl"], True)

        self.assertEquals(self.conan_api.calls[3].args[0], "my_cool_name2")
        self.assertEquals(self.conan_api.calls[3].args[1], "u2")
        self.assertEquals(self.conan_api.calls[3].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[3].kwargs["verify_ssl"], False)
class AppTest(unittest.TestCase):

    def setUp(self):
        self.runner = MockRunner()
        self.conan_api = MockConanAPI()
        self.ci_manager = MockCIManager()
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           reference="lib/1.0",
                                           ci_manager=self.ci_manager)
        if "APPVEYOR" in os.environ:
            del os.environ["APPVEYOR"]
        if "TRAVIS" in os.environ:
            del os.environ["TRAVIS"]

    def _add_build(self, number, compiler=None, version=None):
        self.packager.add({"os": "os%d" % number, "compiler": compiler or "compiler%d" % number,
                           "compiler.version": version or "4.3"},
                          {"option%d" % number: "value%d" % number,
                           "option%d" % number: "value%d" % number})

    def test_remove_build_if(self):
        self.packager.add({"arch": "x86", "build_type": "Release", "compiler": "gcc", "compiler.version": "6"})
        self.packager.add({"arch": "x86", "build_type": "Debug", "compiler": "gcc", "compiler.version": "6"})
        self.packager.add({"arch": "x86", "build_type": "Release", "compiler": "gcc", "compiler.version": "7"})
        self.packager.add({"arch": "x86", "build_type": "Debug", "compiler": "gcc", "compiler.version": "7"})

        self.packager.remove_build_if(lambda build: build.settings["compiler.version"] == "6")

        packager_expected = ConanMultiPackager("lasote", "mychannel",
                                               runner=self.runner,
                                               conan_api=self.conan_api,
                                               reference="lib/1.0",
                                               ci_manager=self.ci_manager)

        packager_expected.add({"arch": "x86", "build_type": "Release", "compiler": "gcc", "compiler.version": "7"})
        packager_expected.add({"arch": "x86", "build_type": "Debug", "compiler": "gcc", "compiler.version": "7"})

        self.assertEqual([tuple(a) for a in self.packager.items], packager_expected.items)

    def test_update_build_if(self):
        self.packager.add({"os": "Windows"})
        self.packager.add({"os": "Linux"})

        self.packager.update_build_if(lambda build: build.settings["os"] == "Windows",
                                      new_build_requires={"*": ["7zip_installer/0.1.0@conan/stable"]})

        packager_expected = ConanMultiPackager("lasote", "mychannel",
                                               runner=self.runner,
                                               conan_api=self.conan_api,
                                               reference="lib/1.0",
                                               ci_manager=self.ci_manager)

        packager_expected.add({"os": "Windows"}, {}, {}, {"*": ["7zip_installer/0.1.0@conan/stable"]})
        packager_expected.add({"os": "Linux"})

        self.assertEqual([tuple(a) for a in self.packager.items], packager_expected.items)

    def test_full_profile(self):
        self.packager.add({"os": "Windows", "compiler": "gcc"},
                          {"option1": "One"},
                          {"VAR_1": "ONE",
                           "VAR_2": "TWO"},
                          {"*": ["myreference/1.0@lasote/testing"]})
        self.packager.run_builds(1, 1)
        profile = self.conan_api.get_profile_from_call_index(1)
        self.assertEquals(profile.settings["os"], "Windows")
        self.assertEquals(profile.settings["compiler"], "gcc")
        self.assertEquals(profile.options.as_list(), [("option1", "One")])
        self.assertEquals(profile.env_values.data[None]["VAR_1"], "ONE")
        self.assertEquals(profile.env_values.data[None]["VAR_2"], "TWO")
        self.assertEquals(profile.build_requires["*"],
                          [ConanFileReference.loads("myreference/1.0@lasote/testing")])

    def test_profile_environ(self):
        self.packager.add({"os": "Windows", "compiler": "gcc"},
                          {"option1": "One"},
                          {"VAR_1": "ONE",
                           "VAR_2": "TWO"},
                          {"*": ["myreference/1.0@lasote/testing"]})
        with tools.environment_append({"CONAN_BUILD_REQUIRES": "br1/1.0@conan/testing"}):
            self.packager.run_builds(1, 1)
            profile = self.conan_api.get_profile_from_call_index(1)
            self.assertEquals(profile.build_requires["*"],
                              [ConanFileReference.loads("myreference/1.0@lasote/testing"),
                               ConanFileReference.loads("br1/1.0@conan/testing")])

    def test_pages(self):
        for number in range(10):
            self._add_build(number)

        # 10 pages, 1 per build
        self.packager.run_builds(1, 10)
        self.conan_api.assert_tests_for([0])

        # 2 pages, 5 per build
        self.conan_api.reset()
        self.packager.run_builds(1, 2)
        self.conan_api.assert_tests_for([0, 2, 4, 6, 8])

        self.conan_api.reset()
        self.packager.run_builds(2, 2)
        self.conan_api.assert_tests_for([1, 3, 5, 7, 9])

        # 3 pages, 4 builds in page 1 and 3 in the rest of pages
        self.conan_api.reset()
        self.packager.run_builds(1, 3)
        self.conan_api.assert_tests_for([0, 3, 6, 9])

        self.conan_api.reset()
        self.packager.run_builds(2, 3)
        self.conan_api.assert_tests_for([1, 4, 7])

        self.conan_api.reset()
        self.packager.run_builds(3, 3)
        self.conan_api.assert_tests_for([2, 5, 8])

    def test_deprecation_gcc(self):

        with self.assertRaisesRegexp(Exception, "DEPRECATED GCC MINOR VERSIONS!"):
            ConanMultiPackager(username="******",
                               channel="mychannel",
                               runner=self.runner,
                               conan_api=self.conan_api,
                               gcc_versions=["4.3", "5.4"],
                               use_docker=True,
                               reference="zlib/1.2.11",
                               ci_manager=self.ci_manager)

    def test_32bits_images(self):
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      use_docker=True,
                                      docker_32_images=True,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertIn("docker pull conanio/gcc6-x86", self.runner.calls[0])

        self.runner.reset()
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      conan_api=self.conan_api,
                                      use_docker=True,
                                      docker_32_images=False,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertNotIn("docker pull conanio/gcc6-i386", self.runner.calls[0])

        self.runner.reset()
        with tools.environment_append({"CONAN_DOCKER_32_IMAGES": "1"}):
            packager = ConanMultiPackager(username="******",
                                          channel="mychannel",
                                          runner=self.runner,
                                          conan_api=self.conan_api,
                                          use_docker=True,
                                          reference="zlib/1.2.11",
                                          ci_manager=self.ci_manager)

            packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
            packager.run_builds(1, 1)
            self.assertIn("docker pull conanio/gcc6-x86", self.runner.calls[0])

        self.runner.reset()
        # Test the opossite
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      runner=self.runner,
                                      conan_api=self.conan_api,
                                      use_docker=True,
                                      docker_32_images=False,
                                      reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)

        packager.add({"arch": "x86", "compiler": "gcc", "compiler.version": "6"})
        packager.run_builds(1, 1)
        self.assertIn("docker pull conanio/gcc6", self.runner.calls[0])

    def test_docker_gcc(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           gcc_versions=["4.3", "5"],
                                           use_docker=True,
                                           reference="zlib/1.2.11",
                                           ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "4.3")
        self._add_build(2, "gcc", "4.3")
        self._add_build(3, "gcc", "4.3")

        self.packager.run_builds(1, 2)
        self.assertIn("docker pull conanio/gcc43", self.runner.calls[0])
        self.assertIn('docker run ', self.runner.calls[1])
        self.assertNotIn('sudo pip', self.runner.calls[1])
        self.assertIn('pip install', self.runner.calls[1])
        self.assertIn('os=os1', self.runner.calls[4])
        self.packager.run_builds(1, 2)
        self.assertIn("docker pull conanio/gcc43", self.runner.calls[0])

        # Next build from 4.3 is cached, not pulls are performed
        self.assertIn('os=os3', self.runner.calls[5])

        for the_bool in ["True", "False"]:
            self.runner.reset()
            with tools.environment_append({"CONAN_DOCKER_USE_SUDO": the_bool}):
                self.packager = ConanMultiPackager(username="******",
                                                   channel="mychannel",
                                                   runner=self.runner,
                                                   conan_api=self.conan_api,
                                                   gcc_versions=["4.3", "5"],
                                                   use_docker=True,
                                                   reference="zlib/1.2.11",
                                                   ci_manager=self.ci_manager)
                self._add_build(1, "gcc", "4.3")
                self.packager.run_builds(1, 2)
                if the_bool == "True":
                    self.assertIn("sudo -E docker run", self.runner.calls[-1])
                else:
                    self.assertNotIn("sudo -E docker run", self.runner.calls[-1])
                    self.assertIn("docker run", self.runner.calls[-1])
            self.runner.reset()
            with tools.environment_append({"CONAN_PIP_USE_SUDO": the_bool}):
                self.packager = ConanMultiPackager(username="******",
                                                   channel="mychannel",
                                                   runner=self.runner,
                                                   conan_api=self.conan_api,
                                                   gcc_versions=["4.3", "5"],
                                                   use_docker=True,
                                                   reference="zlib/1.2.11",
                                                   ci_manager=self.ci_manager)
                self._add_build(1, "gcc", "4.3")
                self.packager.run_builds(1, 2)
                if the_bool == "True":
                    self.assertIn("sudo -E pip", self.runner.calls[1])
                else:
                    self.assertNotIn("sudo -E pip", self.runner.calls[1])
                    self.assertIn("pip", self.runner.calls[1])

    def test_docker_clang(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           clang_versions=["3.8", "4.0"],
                                           use_docker=True,
                                           reference="zlib/1.2.11",
                                           ci_manager=self.ci_manager)

        self._add_build(1, "clang", "3.8")
        self._add_build(2, "clang", "3.8")
        self._add_build(3, "clang", "3.8")

        self.packager.run_builds(1, 2)
        self.assertIn("docker pull conanio/clang38", self.runner.calls[0])
        self.assertIn('docker run ', self.runner.calls[1])
        self.assertIn('os=os1', self.runner.calls[4])

        # Next build from 3.8 is cached, not pulls are performed
        self.assertIn('os=os3', self.runner.calls[5])

    def test_docker_gcc_and_clang(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           gcc_versions=["5", "6"],
                                           clang_versions=["3.9", "4.0"],
                                           use_docker=True,
                                           reference="zlib/1.2.11",
                                           ci_manager=self.ci_manager)

        self._add_build(1, "gcc", "5")
        self._add_build(2, "gcc", "5")
        self._add_build(3, "gcc", "5")
        self._add_build(4, "clang", "3.9")
        self._add_build(5, "clang", "3.9")
        self._add_build(6, "clang", "3.9")

        self.packager.run_builds(1, 2)
        self.assertIn("docker pull conanio/gcc5", self.runner.calls[0])
        self.assertIn('docker run ', self.runner.calls[1])

        self.assertIn('os=os1', self.runner.calls[4])
        self.assertIn('os=os3', self.runner.calls[5])

        self.packager.run_builds(2, 2)
        self.assertIn("docker pull conanio/clang39", self.runner.calls[16])
        self.assertIn('docker run ', self.runner.calls[17])
        self.assertIn('os=os4', self.runner.calls[20])
        self.assertIn('os=os6', self.runner.calls[21])

    def test_upload_false(self):
        packager = ConanMultiPackager(username="******",
                                      channel="mychannel",
                                      upload=False, reference="zlib/1.2.11",
                                      ci_manager=self.ci_manager)
        self.assertFalse(packager._upload_enabled())

    def test_docker_env_propagated(self):
        # test env
        with tools.environment_append({"CONAN_FAKE_VAR": "32"}):
            self.packager = ConanMultiPackager(username="******",
                                               channel="mychannel",
                                               runner=self.runner,
                                               conan_api=self.conan_api,
                                               gcc_versions=["5", "6"],
                                               clang_versions=["3.9", "4.0"],
                                               use_docker=True,
                                               reference="zlib/1.2.11",
                                               ci_manager=self.ci_manager)
            self._add_build(1, "gcc", "5")
            self.packager.run_builds(1, 1)
            self.assertIn('-e CONAN_FAKE_VAR="32"', self.runner.calls[-1])

    def test_docker_home_env(self):
        with tools.environment_append({"CONAN_DOCKER_HOME": "/some/dir"}):
            self.packager = ConanMultiPackager(username="******",
                                               channel="mychannel",
                                               runner=self.runner,
                                               conan_api=self.conan_api,
                                               gcc_versions=["5", "6"],
                                               clang_versions=["3.9", "4.0"],
                                               use_docker=True,
                                               reference="zlib/1.2.11",
                                               ci_manager=self.ci_manager)
            self._add_build(1, "gcc", "5")
            self.packager.run_builds(1, 1)
            self.assertIn('-e CONAN_DOCKER_HOME="/some/dir"',
                          self.runner.calls[-1])
            self.assertEquals(self.packager.docker_conan_home, "/some/dir")

    def test_docker_home_opt(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           gcc_versions=["5", "6"],
                                           clang_versions=["3.9", "4.0"],
                                           use_docker=True,
                                           docker_conan_home="/some/dir",
                                           reference="zlib/1.2.11",
                                           ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "5")
        self.packager.run_builds(1, 1)
        self.assertEquals(self.packager.docker_conan_home, "/some/dir")

    def test_docker_invalid(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           use_docker=True,
                                           reference="zlib/1.2.11",
                                           ci_manager=self.ci_manager)

        self._add_build(1, "msvc", "10")

        # Only clang and gcc have docker images
        self.assertRaises(Exception, self.packager.run_builds)

    def test_assign_builds_retrocompatibility(self):
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           runner=self.runner,
                                           conan_api=self.conan_api,
                                           gcc_versions=["4.3", "5"],
                                           use_docker=True,
                                           reference="lib/1.0",
                                           ci_manager=self.ci_manager)
        self.packager.add_common_builds()
        self.packager.builds = [({"os": "Windows"}, {"option": "value"})]
        self.assertEquals(self.packager.items, [BuildConf(settings={'os': 'Windows'},
                                                          options={'option': 'value'},
                                                          env_vars={}, build_requires={},
                                                          reference="lib/1.0@lasote/mychannel")])

    def test_only_mingw(self):

        mingw_configurations = [("4.9", "x86_64", "seh", "posix")]
        builder = ConanMultiPackager(mingw_configurations=mingw_configurations, visual_versions=[],
                                     username="******", platform_info=platform_mock_for("Windows"),
                                     reference="lib/1.0", ci_manager=self.ci_manager)
        builder.add_common_builds(shared_option_name="zlib:shared", pure_c=True)
        expected = [({'compiler.exception': 'seh', 'compiler.libcxx': "libstdc++",
                      'compiler.threads': 'posix', 'compiler.version': '4.9', 'arch': 'x86_64',
                      'build_type': 'Release', 'compiler': 'gcc'},
                     {'zlib:shared': True},
                     {},
                     {'*': [ConanFileReference.loads("mingw_installer/1.0@conan/stable")]}),
                    ({'compiler.exception': 'seh', 'compiler.libcxx': "libstdc++", 'arch': 'x86_64',
                      'compiler.threads': 'posix', 'compiler.version': '4.9', 'build_type': 'Debug',
                      'compiler': 'gcc'},
                     {'zlib:shared': True},
                     {},
                     {'*': [ConanFileReference.loads("mingw_installer/1.0@conan/stable")]}),

                    ({'compiler.exception': 'seh', 'compiler.libcxx': "libstdc++",
                      'compiler.threads': 'posix', 'compiler.version': '4.9', 'arch': 'x86_64',
                      'build_type': 'Release', 'compiler': 'gcc'},
                     {'zlib:shared': False},
                     {},
                     {'*': [ConanFileReference.loads("mingw_installer/1.0@conan/stable")]}),
                    ({'compiler.exception': 'seh', 'compiler.libcxx': "libstdc++", 'arch': 'x86_64',
                      'compiler.threads': 'posix', 'compiler.version': '4.9', 'build_type': 'Debug',
                      'compiler': 'gcc'},
                     {'zlib:shared': False},
                     {},
                     {'*': [ConanFileReference.loads("mingw_installer/1.0@conan/stable")]})]

        self.assertEquals([tuple(a) for a in builder.builds], expected)

    def test_named_pages(self):
        builder = ConanMultiPackager(username="******", reference="zlib/1.2.11",
                                     ci_manager=self.ci_manager)
        named_builds = defaultdict(list)
        builder.add_common_builds(shared_option_name="zlib:shared", pure_c=True)
        for settings, options, env_vars, build_requires, _ in builder.items:
            named_builds[settings['arch']].append([settings, options, env_vars, build_requires])
        builder.named_builds = named_builds

        self.assertEquals(builder.builds, [])
        if platform.system() == "Darwin":  # Not default x86 in Macos
            self.assertEquals(len(builder.named_builds), 1)
            self.assertFalse("x86" in builder.named_builds)
            self.assertTrue("x86_64" in builder.named_builds)
        else:
            self.assertEquals(len(builder.named_builds), 2)
            self.assertTrue("x86" in builder.named_builds)
            self.assertTrue("x86_64" in builder.named_builds)

    def test_remotes(self):
        runner = MockRunner()
        builder = ConanMultiPackager(username="******",
                                     remotes=["url1", "url2"],
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[1], "url1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[3].args[1], "url2")
        self.assertEquals(self.conan_api.calls[3].kwargs["insert"], -1)

        runner = MockRunner()
        self.conan_api = MockConanAPI()
        builder = ConanMultiPackager(username="******",
                                     remotes="myurl1",
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[1], "myurl1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)

        # Named remotes, with SSL flag
        runner = MockRunner()
        self.conan_api = MockConanAPI()
        remotes = [("u1", True, "my_cool_name1"),
                   ("u2", False, "my_cool_name2")]
        builder = ConanMultiPackager(username="******",
                                     remotes=remotes,
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     reference="lib/1.0@lasote/mychannel",
                                     ci_manager=self.ci_manager)

        builder.add({}, {}, {}, {})
        builder.run_builds()
        self.assertEquals(self.conan_api.calls[1].args[0], "my_cool_name1")
        self.assertEquals(self.conan_api.calls[1].args[1], "u1")
        self.assertEquals(self.conan_api.calls[1].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[1].kwargs["verify_ssl"], True)

        self.assertEquals(self.conan_api.calls[3].args[0], "my_cool_name2")
        self.assertEquals(self.conan_api.calls[3].args[1], "u2")
        self.assertEquals(self.conan_api.calls[3].kwargs["insert"], -1)
        self.assertEquals(self.conan_api.calls[3].kwargs["verify_ssl"], False)

    def test_visual_defaults(self):

        with tools.environment_append({"CONAN_VISUAL_VERSIONS": "10"}):
            builder = ConanMultiPackager(username="******",
                                         platform_info=platform_mock_for("Windows"),
                                         reference="lib/1.0@lasote/mychannel",
                                         ci_manager=self.ci_manager)
            builder.add_common_builds()
            for settings, _, _, _, _ in builder.items:
                self.assertEquals(settings["compiler"], "Visual Studio")
                self.assertEquals(settings["compiler.version"], "10")

        with tools.environment_append({"CONAN_VISUAL_VERSIONS": "10",
                                       "MINGW_CONFIGURATIONS": "4.9@x86_64@seh@posix"}):

            builder = ConanMultiPackager(username="******",
                                         platform_info=platform_mock_for("Windows"),
                                         reference="lib/1.0@lasote/mychannel",
                                         ci_manager=self.ci_manager)
            builder.add_common_builds()
            for settings, _, _, _, _ in builder.items:
                self.assertEquals(settings["compiler"], "gcc")
                self.assertEquals(settings["compiler.version"], "4.9")

    def select_defaults_test(self):
        with tools.environment_append({"CONAN_REFERENCE": "zlib/1.2.8"}):
            builder = ConanMultiPackager(platform_info=platform_mock_for("Linux"),
                                         gcc_versions=["4.8", "5"],
                                         username="******",
                                         reference="lib/1.0@lasote/mychannel",
                                         ci_manager=self.ci_manager)

            self.assertEquals(builder.build_generator._clang_versions, [])

        with tools.environment_append({"CONAN_GCC_VERSIONS": "4.8, 5",
                                       "CONAN_REFERENCE": "zlib/1.2.8"}):
            builder = ConanMultiPackager(platform_info=platform_mock_for("Linux"),
                                         username="******",
                                         reference="lib/1.0@lasote/mychannel",
                                         ci_manager=self.ci_manager)

            self.assertEquals(builder.build_generator._clang_versions, [])
            self.assertEquals(builder.build_generator._gcc_versions, ["4.8", "5"])

        builder = ConanMultiPackager(platform_info=platform_mock_for("Linux"),
                                     clang_versions=["4.8", "5"],
                                     username="******",
                                     reference="lib/1.0",
                                     ci_manager=self.ci_manager)

        self.assertEquals(builder.build_generator._gcc_versions, [])

        with tools.environment_append({"CONAN_CLANG_VERSIONS": "4.8, 5",
                                       "CONAN_APPLE_CLANG_VERSIONS": " "}):
            builder = ConanMultiPackager(platform_info=platform_mock_for("Linux"),
                                         username="******",
                                         reference="lib/1.0",
                                         ci_manager=self.ci_manager)

            self.assertEquals(builder.build_generator._gcc_versions, [])
            self.assertEquals(builder.build_generator._clang_versions, ["4.8", "5"])
            self.assertEquals(builder.build_generator._clang_versions, ["4.8", "5"])
            self.assertEquals(builder.build_generator._apple_clang_versions, [])

    def test_upload(self):
        runner = MockRunner()
        runner.output = "arepo: myurl"
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload="myurl", visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     remotes="myurl, otherurl",
                                     platform_info=platform_mock_for("Darwin"),
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()

        # Duplicated upload remote puts upload repo first (in the remotes order)
        self.assertEqual(self.conan_api.calls[1].args[0], 'upload_repo')
        self.assertEqual(self.conan_api.calls[3].args[0], 'remote1')

        # Now check that the upload remote order is preserved if we specify it in the remotes
        runner = MockRunner()
        self.conan_api = MockConanAPI()
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload="myurl", visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     remotes="otherurl, myurl, moreurl",
                                     platform_info=platform_mock_for("Darwin"),
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()

        self.assertEqual(self.conan_api.calls[1].args[0], 'remote0')
        self.assertEqual(self.conan_api.calls[3].args[0], 'upload_repo')
        self.assertEqual(self.conan_api.calls[5].args[0], 'remote2')

        runner = MockRunner()
        self.conan_api = MockConanAPI()
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload="myurl", visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=runner,
                                     conan_api=self.conan_api,
                                     remotes="otherurl",
                                     platform_info=platform_mock_for("Darwin"),
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()

        self.assertEqual(self.conan_api.calls[1].args[0], 'remote0')
        self.assertEqual(self.conan_api.calls[3].args[0], 'upload_repo')

    def test_build_policy(self):
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=self.runner,
                                     conan_api=self.conan_api,
                                     remotes="otherurl",
                                     platform_info=platform_mock_for("Darwin"),
                                     build_policy="outdated",
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()
        self.assertEquals(["outdated"], self.conan_api.calls[-1].kwargs["build_modes"])

        with tools.environment_append({"CONAN_BUILD_POLICY": "missing"}):
            self.conan_api = MockConanAPI()
            builder = ConanMultiPackager(username="******", channel="testing",
                                         reference="Hello/0.1", password="******",
                                         visual_versions=[], gcc_versions=[],
                                         apple_clang_versions=[],
                                         runner=self.runner,
                                         conan_api=self.conan_api,
                                         remotes="otherurl",
                                         platform_info=platform_mock_for("Darwin"),
                                         build_policy="missing",
                                         ci_manager=self.ci_manager)
            builder.add_common_builds()
            builder.run()
            self.assertEquals(["missing"], self.conan_api.calls[-1].kwargs["build_modes"])

    def test_test_folder(self):
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=self.runner,
                                     conan_api=self.conan_api,
                                     remotes="otherurl",
                                     platform_info=platform_mock_for("Darwin"),
                                     test_folder="foobar",
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()
        self.assertEquals("foobar", self.conan_api.calls[-1].kwargs["test_folder"])

        with tools.environment_append({"CONAN_BUILD_POLICY": "missing"}):
            self.conan_api = MockConanAPI()
            builder = ConanMultiPackager(username="******", channel="testing",
                                         reference="Hello/0.1", password="******",
                                         visual_versions=[], gcc_versions=[],
                                         apple_clang_versions=[],
                                         runner=self.runner,
                                         conan_api=self.conan_api,
                                         remotes="otherurl",
                                         platform_info=platform_mock_for("Darwin"),
                                         build_policy=None,
                                         ci_manager=self.ci_manager)
            builder.add_common_builds()
            builder.run()
            self.assertEquals(None, self.conan_api.calls[-1].kwargs["test_folder"])

    def test_check_credentials(self):

        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload="myurl", visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=self.runner,
                                     conan_api=self.conan_api,
                                     platform_info=platform_mock_for("Darwin"),
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()

        # When activated, check credentials before to create the profiles
        self.assertEqual(self.conan_api.calls[2].name, 'authenticate')
        self.assertEqual(self.conan_api.calls[5].name, 'create_profile')

        self.conan_api = MockConanAPI()
        # If we skip the credentials check, the login will be performed just before the upload
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload="myurl", visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=self.runner,
                                     conan_api=self.conan_api,
                                     platform_info=platform_mock_for("Darwin"),
                                     skip_check_credentials=True,
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()
        self.assertNotEqual(self.conan_api.calls[0].name, 'authenticate')

        # No upload, no authenticate
        self.conan_api = MockConanAPI()
        builder = ConanMultiPackager(username="******", channel="testing",
                                     reference="Hello/0.1", password="******",
                                     upload=None, visual_versions=[], gcc_versions=[],
                                     apple_clang_versions=[],
                                     runner=self.runner,
                                     conan_api=self.conan_api,
                                     platform_info=platform_mock_for("Darwin"),
                                     skip_check_credentials=True,
                                     ci_manager=self.ci_manager)
        builder.add_common_builds()
        builder.run()
        for action in self.conan_api.calls:
            self.assertNotEqual(action.name, 'authenticate')
            self.assertNotEqual(action.name, 'upload')

    def channel_detector_test(self):

        for branch, expected_channel in [("testing", "a_channel"),
                                         ("dummy", "a_channel"),
                                         ("stable", "stable"),
                                         ("stable/something", "stable"),
                                         ("release", "stable"),
                                         ("release/something", "stable"),
                                         ("master", "stable"),
                                         ("master/something", "a_channel")]:
            builder = ConanMultiPackager(username="******",
                                         channel="a_channel",
                                         reference="lib/1.0",
                                         ci_manager=MockCIManager(current_branch=branch))

            self.assertEquals(builder.channel, expected_channel, "Not match for branch %s" % branch)

    def test_pip_conanio_image(self):
        self.packager = ConanMultiPackager(username="******",
                                            channel="mychannel",
                                            runner=self.runner,
                                            conan_api=self.conan_api,
                                            gcc_versions=["4.3", "5"],
                                            use_docker=True,
                                            docker_image='conanio/gcc43',
                                            reference="zlib/1.2.11",
                                            ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "4.3")
        self.packager.run_builds(1, 2)
        self.assertNotIn("sudo -E pip", self.runner.calls[1])
        self.assertIn("pip", self.runner.calls[1])

        self.runner.reset()
        self.packager = ConanMultiPackager(username="******",
                                            channel="mychannel",
                                            runner=self.runner,
                                            conan_api=self.conan_api,
                                            gcc_versions=["4.3", "5"],
                                            docker_image='conanio/gcc43',
                                            reference="zlib/1.2.11",
                                            ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "4.3")
        self.packager.run_builds(1, 2)
        self.assertNotIn("sudo -E pip", self.runner.calls[1])
        self.assertIn("pip", self.runner.calls[1])


    @unittest.skipIf(sys.platform.startswith("win"), "Requires Linux")
    def test_pip_docker_sudo(self):
        self.packager = ConanMultiPackager(username="******",
                                            channel="mychannel",
                                            runner=self.runner,
                                            conan_api=self.conan_api,
                                            gcc_versions=["4.3", "5"],
                                            docker_image='foobar/gcc43',
                                            reference="zlib/1.2.11",
                                            ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "4.3")
        self.packager.run_builds(1, 2)
        self.assertIn("sudo -E pip", self.runner.calls[1])

        self.runner.reset()
        with tools.environment_append({"CONAN_PIP_USE_SUDO": "True"}):
            self.packager = ConanMultiPackager(username="******",
                                                channel="mychannel",
                                                runner=self.runner,
                                                conan_api=self.conan_api,
                                                gcc_versions=["4.3", "5"],
                                                docker_image='conanio/gcc43',
                                                reference="zlib/1.2.11",
                                                ci_manager=self.ci_manager)
        self._add_build(1, "gcc", "4.3")
        self.packager.run_builds(1, 2)
        self.assertIn("sudo -E pip", self.runner.calls[1])
Esempio n. 5
0
class SimpleTest(BaseTest):
    def test_missing_full_reference(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    pass
"""
        self.save_conanfile(conanfile)
        mp = ConanMultiPackager(username="******")
        with self.assertRaisesRegexp(
                Exception, "Specify a CONAN_REFERENCE or name and version"):
            mp.add_common_builds()

    def test_missing_username(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    options = {"shared": [True, False]}
    default_options = "shared=False"

"""
        self.save_conanfile(conanfile)

        with self.assertRaisesRegexp(
                Exception, "Instance ConanMultiPackage with 'username' "
                "parameter or use CONAN_USERNAME env variable"):
            ConanMultiPackager()

    @unittest.skipUnless(sys.platform.startswith("win"), "Requires Windows")
    def test_msvc(self):
        conanfile = """from conans import ConanFile
import os

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

    def build(self):
        assert("WindowsLibPath" in os.environ)

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           visual_versions=[15],
                                           archs=["x86"],
                                           build_types=["Release"],
                                           visual_runtimes=["MD"],
                                           reference="zlib/1.2.2")
        self.packager.add_common_builds()
        self.packager.run_builds(1, 1)

    def test_msvc_exclude_precommand(self):
        conanfile = """from conans import ConanFile
import os

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

    def build(self):
        assert("WindowsLibPath" not in os.environ)

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           visual_versions=[15],
                                           archs=["x86"],
                                           build_types=["Release"],
                                           visual_runtimes=["MD"],
                                           exclude_vcvars_precommand=True,
                                           reference="zlib/1.2.2")
        self.packager.add_common_builds()
        self.packager.run_builds(1, 1)

    def test_shared_option_auto_managed(self):
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******")
        self.packager.add_common_builds()
        self.assertIn("lib:shared", self.packager.items[0].options)

        # Even without name and version but reference
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}


"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           reference="lib2/1.0")
        self.packager.add_common_builds()
        self.assertIn("lib2:shared", self.packager.items[0].options)

        self.packager = ConanMultiPackager(username="******",
                                           reference="lib2/1.0")
        self.packager.add_common_builds(shared_option_name=False)
        self.assertNotIn("lib2:shared", self.packager.items[0].options)

    def test_exported_files(self):
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    settings = "os"
    exports = "*"
    exports_sources = "source*"

"""
        ci_manager = MockCIManager()
        self.save_conanfile(conanfile)
        tools.save(os.path.join(self.tmp_folder, "other_file"),
                   "Dummy contents")
        tools.save(os.path.join(self.tmp_folder, "source.cpp"),
                   "Dummy contents")
        self.packager = ConanMultiPackager(username="******",
                                           reference="lib/1.0",
                                           ci_manager=ci_manager)
        self.packager.add({}, {}, {}, {})
        self.packager.run()

        pf = self.client_cache.export(
            ConanFileReference.loads("lib/1.0@lasote/testing"))
        found_in_export = False
        for exported in os.listdir(pf):
            if "other_file" == exported:
                found_in_export = True
                break

        self.assertTrue(found_in_export)

        pf = self.client_cache.export_sources(
            ConanFileReference.loads("lib/1.0@lasote/testing"))
        found_in_export_sources = False
        for exported in os.listdir(pf):
            if "source.cpp" == exported:
                found_in_export_sources = True
                break

        self.assertTrue(found_in_export_sources)

    def test_build_policy(self):
        ci_manager = MockCIManager(build_policy="outdated")
        conanfile = """from conans import ConanFile
import os

class Pkg(ConanFile):
    name = "lib"
    version = "1.2"
    settings = "os", "compiler", "build_type", "arch"

"""
        self.save_conanfile(conanfile)
        with tools.environment_append({"CONAN_USERNAME": "******"}):
            self.packager = ConanMultiPackager(channel="mychannel",
                                               gcc_versions=["6"],
                                               visual_versions=["12"],
                                               archs=["x86", "x86_64"],
                                               build_types=["Release"],
                                               ci_manager=ci_manager)
            self.packager.add_common_builds()
            self.packager.run()
Esempio n. 6
0
class SimpleTest(BaseTest):

    def test_missing_full_reference(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    pass
"""
        self.save_conanfile(conanfile)
        mp = ConanMultiPackager(username="******")
        with self.assertRaisesRegexp(Exception, "Specify a CONAN_REFERENCE or name and version"):
            mp.add_common_builds()

    @unittest.skipUnless(sys.platform.startswith("win"), "Requires Windows")
    def test_msvc(self):
        conanfile = """from conans import ConanFile
import os

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

    def build(self):
        assert("WindowsLibPath" in os.environ)

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           visual_versions=[15],
                                           archs=["x86"],
                                           build_types=["Release"],
                                           visual_runtimes=["MD"],
                                           reference="zlib/1.2.2")
        self.packager.add_common_builds()
        self.packager.run_builds(1, 1)

    def test_msvc_exclude_precommand(self):
        conanfile = """from conans import ConanFile
import os

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

    def build(self):
        assert("WindowsLibPath" not in os.environ)

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           visual_versions=[15],
                                           archs=["x86"],
                                           build_types=["Release"],
                                           visual_runtimes=["MD"],
                                           exclude_vcvars_precommand=True,
                                           reference="zlib/1.2.2")
        self.packager.add_common_builds()
        self.packager.run_builds(1, 1)

    def test_shared_option_auto_managed(self):
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}

"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******")
        self.packager.add_common_builds()
        self.assertIn("lib:shared", self.packager.items[0].options)

        # Even without name and version but reference
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}


"""
        self.save_conanfile(conanfile)
        self.packager = ConanMultiPackager(username="******", reference="lib2/1.0")
        self.packager.add_common_builds()
        self.assertIn("lib2:shared", self.packager.items[0].options)

        self.packager = ConanMultiPackager(username="******", reference="lib2/1.0")
        self.packager.add_common_builds(shared_option_name=False)
        self.assertNotIn("lib2:shared", self.packager.items[0].options)

    def test_auto_managed_subdirectory(self):
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}

"""
        cwd = os.path.join(self.tmp_folder, "subdirectory")
        tools.save(os.path.join(cwd, "conanfile.py"), conanfile)
        self.packager = ConanMultiPackager(username="******", cwd=cwd)
        self.packager.add_common_builds()
        self.assertGreater(len(self.packager.items), 0)
        self.assertIn("lib:shared", self.packager.items[0].options)

    def test_exported_files(self):
        conanfile = """from conans import ConanFile

class Pkg(ConanFile):
    name = "lib"
    version = "1.0"
    settings = "os"
    exports = "*"
    exports_sources = "source*"

"""
        ci_manager = MockCIManager()
        self.save_conanfile(conanfile)
        tools.save(os.path.join(self.tmp_folder, "other_file"), "Dummy contents")
        tools.save(os.path.join(self.tmp_folder, "source.cpp"), "Dummy contents")
        self.packager = ConanMultiPackager(username="******", reference="lib/1.0", ci_manager=ci_manager)
        self.packager.add({}, {}, {}, {})
        self.packager.run()

        ref = ConanFileReference.loads("lib/1.0@lasote/testing")
        pf = self.client_cache.package_layout(ref).export()
        found_in_export = False
        for exported in os.listdir(pf):
            if "other_file" == exported:
                found_in_export = True
                break

        self.assertTrue(found_in_export)

        pf = self.client_cache.package_layout(ref).export_sources()
        found_in_export_sources = False
        for exported in os.listdir(pf):
            if "source.cpp" == exported:
                found_in_export_sources = True
                break

        self.assertTrue(found_in_export_sources)

    def test_build_policy(self):
        ci_manager = MockCIManager()
        conanfile = """from conans import ConanFile
import os

class Pkg(ConanFile):
    name = "lib"
    version = "1.2"
    settings = "os", "compiler", "build_type", "arch"

"""
        self.save_conanfile(conanfile)
        with tools.environment_append({"CONAN_USERNAME": "******"}):
            self.packager = ConanMultiPackager(channel="mychannel",
                                               gcc_versions=["6"],
                                               visual_versions=["12"],
                                               archs=["x86", "x86_64"],
                                               build_types=["Release"],
                                               build_policy="outdated",
                                               ci_manager=ci_manager)
            self.packager.add_common_builds()
            self.packager.run()

        with tools.environment_append({"CONAN_USERNAME": "******",
                                       "CONAN_BUILD_POLICY": "outdated"}):
            self.packager = ConanMultiPackager(channel="mychannel",
                                               gcc_versions=["6"],
                                               visual_versions=["12"],
                                               archs=["x86", "x86_64"],
                                               build_types=["Release"],
                                               ci_manager=ci_manager)
            self.packager.add_common_builds()
            self.packager.run()

    def test_multiple_build_policy(self):
        ci_manager = MockCIManager()
        conanfile = """from conans import ConanFile
import os

class Pkg(ConanFile):
    name = "lib"
    version = "1.2"
    settings = "os", "compiler", "build_type", "arch"

"""
        self.save_conanfile(conanfile)
        with tools.environment_append({"CONAN_USERNAME": "******"}):
            self.packager = ConanMultiPackager(channel="mychannel",
                                               gcc_versions=["6"],
                                               visual_versions=["12"],
                                               archs=["x86", "x86_64"],
                                               build_types=["Release"],
                                               build_policy=["cascade", "outdated"],
                                               ci_manager=ci_manager)
            self.packager.add_common_builds()
            self.packager.run()

        with tools.environment_append({"CONAN_USERNAME": "******",
                                       "CONAN_BUILD_POLICY": "outdated, lib"}):
            self.packager = ConanMultiPackager(channel="mychannel",
                                               gcc_versions=["6"],
                                               visual_versions=["12"],
                                               archs=["x86", "x86_64"],
                                               build_types=["Release"],
                                               ci_manager=ci_manager)
            self.packager.add_common_builds()
            self.packager.run()

    def test_custom_conanfile(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "lib"
    version = "1.2"
    settings = "os", "compiler", "build_type", "arch"
"""
        tools.save(os.path.join(self.tmp_folder, "foobar.py"), conanfile)
        with tools.environment_append({"CONAN_CONANFILE": "foobar.py"}):
            self.packager = ConanMultiPackager(username="******",
                                               channel="mychannel",
                                               out=self.output.write)
            self.packager.add({}, {}, {}, {})
            self.packager.run()
        self.assertIn("conanfile                 | foobar.py", self.output)

        tools.save(os.path.join(self.tmp_folder, "custom_recipe.py"), conanfile)
        self.packager = ConanMultiPackager(username="******",
                                           channel="mychannel",
                                           conanfile="custom_recipe.py",
                                           out=self.output.write)
        self.packager.add({}, {}, {}, {})
        self.packager.run()
        self.assertIn("conanfile                 | custom_recipe.py", self.output)

    def test_partial_reference(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "foobar"
    version = "0.1.0"

    def configure(self):
        self.output.info("hello all")
"""
        tools.save(os.path.join(self.tmp_folder, "conanfile.py"), conanfile)
        with tools.environment_append({"CONAN_REFERENCE": "foobar/0.1.0@"}):
            self.packager = ConanMultiPackager(out=self.output.write)
            self.packager.add({}, {}, {}, {})
            self.packager.run()
        self.assertIn("partial_reference         | foobar/0.1.0@", self.output)

        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add({}, {}, {}, {})
        self.packager.run()
        self.assertIn("partial_reference         | foobar/0.1.0@", self.output)

    def test_save_packages_summary(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "foobar"
    version = "0.1.0"

    def configure(self):
        self.output.info("hello all")
"""
        json_file = 'cpt_summary_file.json'
        tools.save(os.path.join(self.tmp_folder, "conanfile.py"), conanfile)
        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add({}, {}, {}, {})
        self.packager.run(summary_file=json_file)
        self.assertTrue(os.path.isfile(json_file))
        with open(json_file) as json_content:
            json_data = json.load(json_content)
            self.assertFalse(json_data[0]["package"]["error"])

        json_file = "_" + json_file
        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add({}, {}, {}, {})
        self.packager.run()
        self.packager.save_packages_summary(json_file)
        self.assertTrue(os.path.isfile(json_file))
        with open(json_file) as json_content:
            json_data = json.load(json_content)
            self.assertFalse(json_data[0]["package"]["error"])

        json_file = "__" + json_file
        with tools.environment_append({"CPT_SUMMARY_FILE": json_file}):
            self.packager = ConanMultiPackager(out=self.output.write)
            self.packager.add({}, {}, {}, {})
            self.packager.run()
            self.assertTrue(os.path.isfile(json_file))
            with open(json_file) as json_content:
                json_data = json.load(json_content)
                self.assertFalse(json_data[0]["package"]["error"])

    def test_custom_name_version(self):
        conanfile = """from conans import ConanFile
from datetime import date
class Pkg(ConanFile):

    def configure(self):
        self.output.info("hello all")

    def set_name(self):
        self.name = "foobar"

    def set_version(self):
        today = date.today()
        self.version = today.strftime("%Y%B%d")
"""
        tools.save(os.path.join(self.tmp_folder, "conanfile.py"), conanfile)
        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add_common_builds(pure_c=False)
        self.packager.run()

    def test_header_only_option_true(self):
        header_only = self._test_header_only(False)
        self.assertEqual(header_only, 1)
        self.packager.run()

    def test_header_only_option_false(self):
        header_only = self._test_header_only(True)
        self.assertEqual(header_only, int(len(self.packager.builds) / 2))
        self.packager.run()

    def _test_header_only(self, default_value):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "qux"
    version = "0.1.0"
    settings = "os"
    options = {"header_only": [True, False], "shared": [True, False], "fPIC": [True, False]}
    default_options = {"header_only": %s, "shared": False, "fPIC": True}

    def configure(self):
        if self.options.header_only:
            del self.options.shared
            del self.options.fPIC

    def package_id(self):
        if self.options.header_only:
            self.info.header_only()
""" % default_value
        tools.save(os.path.join(self.tmp_folder, "conanfile.py"), conanfile)
        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add_common_builds(pure_c=False)

        header_only = 0
        for build in self.packager.builds:
            _, options, _, _ = build
            if options.get("qux:header_only") == (not default_value):
                header_only += 1
        return header_only

    def test_build_all_option_values(self):
        conanfile = """from conans import ConanFile
class Pkg(ConanFile):
    name = "qux"
    version = "0.1.0"
    options = {"shared": [True, False], "fPIC": [True, False],
               "header_only": [True, False], "foo": [True, False],
               "bar": ["baz", "qux", "foobar"], "blah": "ANY"}
    default_options = {"shared": False, "fPIC": True, "header_only": False,
                       "foo": False, "bar": "baz", "blah": 42}

    def configure(self):
        self.output.info("hello all")
"""
        tools.save(os.path.join(self.tmp_folder, "conanfile.py"), conanfile)
        self.packager = ConanMultiPackager(out=self.output.write)
        self.packager.add_common_builds(pure_c=False, build_all_options_values=["qux:foo", "qux:bar", "qux:blah"])
        self.packager.run()