Skip to content

Commit

Permalink
Merge pull request conan-io#515 from tim-goto/extend-testing
Browse files Browse the repository at this point in the history
Extend testing
  • Loading branch information
memsharded authored May 21, 2023
2 parents f49e229 + 1d1fd3f commit c69227d
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 122 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/cmake_conan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ jobs:
- os: macos-latest
- os: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- uses: actions/checkout@v3.5.2
- name: Setup Python
uses: actions/setup-python@v4.6.0
with:
python-version: 3.7
python-version: "3.10"
- name: Install Conan
run: pip install conan pytest && conan --version
- name: Setup cmake
uses: jwlawson/actions-setup-cmake@v1.13
- name: Setup CMake and Ninja
uses: lukka/get-cmake@latest
with:
cmake-version: '3.25.x'
cmakeVersion: "~3.25.0"
ninjaVersion: "^1.11.1"
- name: Run Tests
run: pytest -rA -v
115 changes: 0 additions & 115 deletions test_smoke.py

This file was deleted.

8 changes: 8 additions & 0 deletions tests/resources/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.24)
project(MyApp CXX)

set(CMAKE_CXX_STANDARD 17)
find_package(hello REQUIRED)
find_package(bye REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app hello::hello bye::bye)
3 changes: 3 additions & 0 deletions tests/resources/basic/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[requires]
hello/0.1
bye/0.1
3 changes: 3 additions & 0 deletions tests/resources/basic/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "hello.h"
#include "bye.h"
int main(){hello();bye();}
9 changes: 9 additions & 0 deletions tests/resources/subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.24)
project(MyApp CXX)

set(CMAKE_CXX_STANDARD 17)
add_subdirectory(subdir)
find_package(hello REQUIRED)
find_package(bye REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app hello::hello bye::bye)
4 changes: 4 additions & 0 deletions tests/resources/subdir/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[requires]
hello/0.1
bye/0.1
subdir/0.1
6 changes: 6 additions & 0 deletions tests/resources/subdir/subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.24)
project(MySubdirApp)

find_package(subdir REQUIRED)
add_executable(appSubdir main.cpp)
target_link_libraries(appSubdir subdir::subdir)
2 changes: 2 additions & 0 deletions tests/resources/subdir/subdir/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "subdir.h"
int main(){subdir();}
176 changes: 176 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import os
import platform
import shutil
import subprocess
import tempfile
from contextlib import contextmanager
from pathlib import Path

import pytest

expected_conan_install_outputs = [
"first find_package() found. Installing dependencies with Conan",
"found, 'conan install' already ran"
]

expected_app_release_outputs = [
"hello/0.1: Hello World Release!",
"bye/0.1: Hello World Release!"
]

expected_app_debug_outputs = [
"hello/0.1: Hello World Debug!",
"bye/0.1: Hello World Debug!"
]

unix = pytest.mark.skipif(platform.system() != "Linux" and platform.system() != "Darwin", reason="Linux or Darwin only")
linux = pytest.mark.skipif(platform.system() != "Linux", reason="Linux only")
darwin = pytest.mark.skipif(platform.system() != "Darwin", reason="Darwin only")
windows = pytest.mark.skipif(platform.system() != "Windows", reason="Windows only")


def run(cmd, check=True):
subprocess.run(cmd, shell=True, check=check)


@contextmanager
def chdir(folder):
cwd = os.getcwd()
os.makedirs(folder, exist_ok=True)
os.chdir(folder)
try:
yield
finally:
os.chdir(cwd)


@pytest.fixture(scope="session")
def tmpdirs():
"""Always run all tests in the same tmp directory and set a custom conan
home to not pollute the cache of the user executing the tests locally.
"""
old_env = dict(os.environ)
conan_home = tempfile.mkdtemp(suffix="conan_home")
os.environ.update({"CONAN_HOME": conan_home})
conan_test_dir = tempfile.mkdtemp(suffix="conan_test_dir")
run(f"echo 'Current conan home: {conan_home}'")
run(f"echo 'Current conan test dir: {conan_test_dir}'")
with chdir(conan_test_dir):
yield
os.environ.clear()
os.environ.update(old_env)


@pytest.fixture(scope="session", autouse=True)
def basic_setup(tmpdirs):
"The packages created by this fixture are available to all tests."
run("conan profile detect -vquiet")
run("conan new cmake_lib -d name=hello -d version=0.1 -vquiet")
run("conan export . -vquiet")
run("conan new cmake_lib -d name=bye -d version=0.1 -f -vquiet")
run("conan export . -vquiet")
run("rm -rf *")
src_dir = Path(__file__).parent.parent
shutil.copy2(src_dir / 'conan_support.cmake', ".")
shutil.copy2(src_dir / 'conan_provider.cmake', ".")
shutil.copytree(src_dir / 'tests' / 'resources' / 'basic', ".", dirs_exist_ok=True)
yield


@pytest.fixture
def chdir_build():
with chdir("build"):
yield


@pytest.fixture
def chdir_build_multi():
with chdir("build-multi"):
yield


class TestBasic:
@windows
def test_windows(self, capfd, chdir_build):
"Conan installs once during configure and applications are created"
run("cmake .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_conan_install_outputs)
run("cmake --build . --config Release")
run(r"Release\app.exe")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
assert all(expected in out for expected in expected_app_release_outputs)
run("cmake --build . --config Debug")
run(r"Debug\app.exe")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
assert all(expected in out for expected in expected_app_debug_outputs)

@unix
def test_linux_single_config(self, capfd, chdir_build):
"Conan installs once during configure and applications are created"
run("cmake .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -DCMAKE_BUILD_TYPE=Release")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_conan_install_outputs)
run("cmake --build .")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
run("./app")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_app_release_outputs)

@unix
def test_linux_multi_config(self, capfd, chdir_build_multi):
"Conan installs once during configure and applications are created"
run("cmake .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -G'Ninja Multi-Config'")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_conan_install_outputs)
run("cmake --build . --config Release")
run("./Release/app")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
assert all(expected in out for expected in expected_app_release_outputs)
run("cmake --build . --config Debug")
run("./Debug/app")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
assert all(expected in out for expected in expected_app_debug_outputs)

@unix
def test_reconfigure_on_conanfile_changes(self, capfd, chdir_build):
"A conanfile change triggers conan install"
run("cmake --build .")
out, _ = capfd.readouterr()
assert all(expected not in out for expected in expected_conan_install_outputs)
p = Path("../conanfile.txt")
p.touch()
run("cmake --build .")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_conan_install_outputs)


class TestSubdir:
@pytest.fixture(scope="class", autouse=True)
def subdir_setup(self):
"Layout for subdir test"
run("conan new cmake_lib -d name=subdir -d version=0.1 -f -vquiet")
run("conan export . -vquiet")
run("rm -rf *")
src_dir = Path(__file__).parent.parent
shutil.copy2(src_dir / 'conan_support.cmake', ".")
shutil.copy2(src_dir / 'conan_provider.cmake', ".")
shutil.copytree(src_dir / 'tests' / 'resources' / 'basic', ".", dirs_exist_ok=True)
shutil.copytree(src_dir / 'tests' / 'resources' / 'subdir', ".", dirs_exist_ok=True)
yield

@unix
def test_add_subdirectory(self, capfd, chdir_build):
"The CMAKE_PREFIX_PATH is set for CMakeLists.txt included with add_subdirectory BEFORE the first find_package."
run("cmake .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -DCMAKE_BUILD_TYPE=Release")
out, _ = capfd.readouterr()
assert all(expected in out for expected in expected_conan_install_outputs)
run("cmake --build .")
run("./subdir/appSubdir")
out, _ = capfd.readouterr()
assert "subdir/0.1: Hello World Release!" in out

0 comments on commit c69227d

Please sign in to comment.