From 289acf10d3b2ee88486a2a3de9724534c60f1e7c Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Mon, 22 Jan 2018 20:57:39 -0200 Subject: [PATCH] Fix limited shared option regex - Now is possible to validate shared recipe when using multiple lines - Added unit tests to validate os name and if is CI running Signed-off-by: Uilian Ries --- bincrafters/__init__.py | 2 +- bincrafters/build_shared.py | 11 ++- bincrafters/requirements.txt | 2 +- tests/test_package_tools.py | 9 ++ tests/test_shared_option.py | 156 +++++++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 tests/test_shared_option.py diff --git a/bincrafters/__init__.py b/bincrafters/__init__.py index 9d1bb72..e754a83 100644 --- a/bincrafters/__init__.py +++ b/bincrafters/__init__.py @@ -1 +1 @@ -__version__ = '0.10.0' +__version__ = '0.10.1' diff --git a/bincrafters/build_shared.py b/bincrafters/build_shared.py index 6c12f2c..3f5c044 100644 --- a/bincrafters/build_shared.py +++ b/bincrafters/build_shared.py @@ -7,8 +7,8 @@ import platform -def get_value_from_recipe(search_string): - with open("conanfile.py", "r") as conanfile: +def get_value_from_recipe(search_string, recipe="conanfile.py"): + with open(recipe, "r") as conanfile: contents = conanfile.read() result = re.search(search_string, contents) return result @@ -23,11 +23,14 @@ def get_version_from_recipe(): def is_shared(): - return "shared" in get_value_from_recipe(r'''options\s*=\s*(.*)''').groups()[0] + match = get_value_from_recipe(r'''options.*=([\s\S]*?)(?=}|$)''') + if match is None: + return False + return "shared" in match.groups()[0] def is_ci_running(): - return os.getenv("APPVEYOR_REPO_NAME", "") or os.getenv("TRAVIS_REPO_SLUG", "") + return (os.getenv("APPVEYOR_REPO_NAME", None) or os.getenv("TRAVIS_REPO_SLUG", None)) is not None def get_repo_name_from_ci(): diff --git a/bincrafters/requirements.txt b/bincrafters/requirements.txt index 0d52c34..54889da 100644 --- a/bincrafters/requirements.txt +++ b/bincrafters/requirements.txt @@ -1 +1 @@ -conan_package_tools>=0.8.4 +conan_package_tools>=0.9.0 diff --git a/tests/test_package_tools.py b/tests/test_package_tools.py index 71aab40..0c4c98b 100644 --- a/tests/test_package_tools.py +++ b/tests/test_package_tools.py @@ -8,6 +8,7 @@ from bincrafters import build_template_header_only from bincrafters import build_template_installer import os +import platform os.environ["CONAN_GCC_VERSIONS"] = "7" @@ -67,3 +68,11 @@ def test_build_boost_header_only(): assert 0 == len(options) assert 1 == len(builder.items) assert "" == _get_upload_when_stable() + +def test_get_os(): + expected_os = platform.system() + assert expected_os == build_shared.get_os() + +def test_ci_is_running(): + expected = True if os.getenv("CI", None) is not None else False + assert expected == build_shared.is_ci_running() diff --git a/tests/test_shared_option.py b/tests/test_shared_option.py new file mode 100644 index 0000000..a6ff16f --- /dev/null +++ b/tests/test_shared_option.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import tempfile +import contextlib +import os +from bincrafters import build_shared + + +recipe_with_shared_1 = """ +class FoobarConan(ConanFile): + name = "foobar" + version = "0.1.0" + url = "https://github.com/bincrafters/foobar" + description = "Just another foobar " + license = "MIT" + exports = ["LICENSE.md"] + exports_sources = ["CMakeLists.txt"] + generators = "cmake" + settings = "os", "arch", "compiler", "build_type" + options = {"with_zlib": [True, False], + "build_tests": [True, False], + "build_binaries": [True, False], + "static_rt": [True, False], + "shared": [True, False], # Just another comment + } + default_options = "with_zlib=False", "build_tests=False", "static_rt=True", "build_binaries=True", "shared=False" + source_subfolder = "source_subfolder" + build_subfolder = "build_subfolder" + + def requirements(self): + if self.options.with_zlib: + self.requires("zlib/1.2.11@conan/stable") + + def source(self): + repo_url = "https://github.com/bincrafters/foobar.git" + self.run("git clone -b v{0} {1} {2}".format(self.version, repo_url, self.source_subfolder)) + + def build(self): + cmake = CMake(self) + cmake.configure(build_folder=self.build_subfolder) + cmake.build() + cmake.install() + + def package_info(self): + self.cpp_info.libs = tools.collect_libs(self) +""" + +recipe_with_shared_2 = """ +class FoobarConan(ConanFile): + name="foobar" + version="0.1.0" + url="https://github.com/bincrafters/foobar" + description="Just another foobar " + license="MIT" + exports="LICENSE.md" + exports_sources="CMakeLists.txt" + generators="cmake" + settings="os", "arch", "compiler", "build_type" + options={"shared": [True, False]} + default_options="shared=False" + + def source(self): + repo_url = "https://github.com/bincrafters/foobar.git" + self.run("git clone -b v{0} {1} {2}".format(self.version, repo_url, self.source_subfolder)) + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + cmake.install() + + def package_info(self): + self.cpp_info.libs = tools.collect_libs(self) +""" + +recipe_with_no_shared_1 = """ + name = "foobar" + version = "0.1.0" + no_copy_source = True + + def source(self): + self.run("git clone ...") + tools.download("url", "file.zip") + tools.unzip("file.zip" ) + + def package(self): + self.copy("*.h", "include") + + + def package_id(self): + self.info.header_only() +""" + +recipe_with_no_shared_2 = """ +class FoobarConan(ConanFile): + name = "foobar" + version = "0.1.0" + url = "https://github.com/bincrafters/foobar" + description = "Just another foobar " + license = "MIT" + exports = ["LICENSE.md"] + exports_sources = ["CMakeLists.txt"] + generators = "cmake" + settings = "os", "arch", "compiler", "build_type" + options = {"with_zlib": [True, False], + "build_tests": [True, False], + "build_binaries": [True, False], + "static_rt": [True, False] + } + default_options = "with_zlib=False", "build_tests=False", "static_rt=True", "build_binaries=True" + source_subfolder = "source_subfolder" + build_subfolder = "build_subfolder" + + def requirements(self): + if self.options.with_zlib: + self.requires("zlib/1.2.11@conan/stable") + + def source(self): + repo_url = "https://github.com/bincrafters/foobar.git" + self.run("git clone -b v{0} {1} {2}".format(self.version, repo_url, self.source_subfolder)) + + def build(self): + cmake = CMake(self) + cmake.configure(build_folder=self.build_subfolder) + cmake.build() + cmake.install() + + def package_info(self): + self.cpp_info.libs = tools.collect_libs(self) +""" + +@contextlib.contextmanager +def chdir(path): + prev_cwd = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(prev_cwd) + +def create_and_validate(recipe_buffer, expected_value): + tempdir = tempfile.mkdtemp() + with chdir(tempdir): + with open("conanfile.py", "w") as fd: + fd.write(recipe_buffer) + fd.flush() + assert expected_value == build_shared.is_shared() + +def test_recipe_with_shared_option(): + create_and_validate(recipe_with_shared_1, True) + create_and_validate(recipe_with_shared_2, True) + +def test_recipe_with_no_shared_option(): + create_and_validate(recipe_with_no_shared_1, False) + create_and_validate(recipe_with_no_shared_2, False)