forked from conan-io/conan
/
test_in_subfolder.py
169 lines (144 loc) · 6.25 KB
/
test_in_subfolder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import textwrap
import pytest
from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.sources import gen_function_cpp
from conans.test.utils.tools import TestClient
def test_exports_sources_own_code_in_subfolder():
""" test that we can put the conanfile in a subfolder, and it can work. The key is
the exports_sources() method that can do:
os.path.join(self.recipe_folder, "..")
And the layout: self.folders.root = ".."
"""
c = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load, copy, save
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
def layout(self):
self.folders.root = ".."
self.folders.source = "."
self.folders.build = "build"
self.folders.generators = "conangen"
def generate(self):
save(self, "mygen.txt", "mycontent")
def export_sources(self):
source_folder = os.path.join(self.recipe_folder, "..")
copy(self, "*.txt", source_folder, self.export_sources_folder)
def source(self):
cmake = load(self, "CMakeLists.txt")
self.output.info("MYCMAKE-SRC: {}".format(cmake))
def build(self):
path = os.path.join(self.source_folder, "CMakeLists.txt")
cmake = load(self, path)
self.output.info("MYCMAKE-BUILD: {}".format(cmake))
save(self, "mylib.a", "mylib")
""")
c.save({"conan/conanfile.py": conanfile,
"CMakeLists.txt": "mycmake!"})
c.run("create conan")
assert "pkg/0.1: MYCMAKE-SRC: mycmake!" in c.out
assert "pkg/0.1: MYCMAKE-BUILD: mycmake!" in c.out
# Local flow
c.run("install conan")
assert c.load("conangen/mygen.txt") == "mycontent"
# SOURCE NOT CALLED! It doesnt make sense (will fail due to local exports)
c.run("build conan")
assert "conanfile.py (pkg/0.1): MYCMAKE-BUILD: mycmake!" in c.out
assert c.load("build/mylib.a") == "mylib"
def test_exports_sources_common_code():
""" very similar to the above, but intended for a multi-package project sharing some
common code
"""
c = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load, copy, save
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
def layout(self):
self.folders.root = ".."
self.folders.source = "pkg"
self.folders.build = "build"
def export_sources(self):
source_folder = os.path.join(self.recipe_folder, "..")
copy(self, "*.txt", source_folder, self.export_sources_folder)
copy(self, "*.cmake", source_folder, self.export_sources_folder)
def source(self):
cmake = load(self, "CMakeLists.txt")
self.output.info("MYCMAKE-SRC: {}".format(cmake))
def build(self):
path = os.path.join(self.source_folder, "CMakeLists.txt")
cmake = load(self, path)
self.output.info("MYCMAKE-BUILD: {}".format(cmake))
path = os.path.join(self.export_sources_folder, "common", "myutils.cmake")
cmake = load(self, path)
self.output.info("MYUTILS-BUILD: {}".format(cmake))
""")
c.save({"pkg/conanfile.py": conanfile,
"pkg/CMakeLists.txt": "mycmake!",
"common/myutils.cmake": "myutils!"})
c.run("create pkg")
assert "pkg/0.1: MYCMAKE-SRC: mycmake!" in c.out
assert "pkg/0.1: MYCMAKE-BUILD: mycmake!" in c.out
assert "pkg/0.1: MYUTILS-BUILD: myutils!" in c.out
# Local flow
c.run("install pkg")
# SOURCE NOT CALLED! It doesnt make sense (will fail due to local exports)
c.run("build pkg")
assert "conanfile.py (pkg/0.1): MYCMAKE-BUILD: mycmake!" in c.out
assert "conanfile.py (pkg/0.1): MYUTILS-BUILD: myutils!" in c.out
@pytest.mark.tool_cmake
def test_exports_sources_common_code_layout():
""" Equal to the previous test, but actually building and using cmake_layout
"""
c = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.cmake import cmake_layout, CMake
from conan.tools.files import load, copy, save
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain"
def layout(self):
self.folders.root = ".."
cmake_layout(self, subfolder="pkg")
def export_sources(self):
source_folder = os.path.join(self.recipe_folder, "..")
copy(self, "*", source_folder, self.export_sources_folder)
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
self.run(os.path.join(self.cpp.build.bindirs[0], "myapp"))
""")
cmake_include = "include(${CMAKE_CURRENT_LIST_DIR}/../common/myutils.cmake)"
c.save({"pkg/conanfile.py": conanfile,
"pkg/app.cpp": gen_function_cpp(name="main", includes=["../common/myheader"],
preprocessor=["MYDEFINE"]),
"pkg/CMakeLists.txt": gen_cmakelists(appsources=["app.cpp"],
custom_content=cmake_include),
"common/myutils.cmake": 'message(STATUS "MYUTILS.CMAKE!")',
"common/myheader.h": '#define MYDEFINE "MYDEFINEVALUE"'})
c.run("create pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Release!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out
# Local flow
c.run("install pkg")
c.run("build pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Release!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out
c.run("install pkg -s build_type=Debug")
c.run("build pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Debug!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out