forked from conan-io/conan
/
test_env.py
391 lines (348 loc) · 15.6 KB
/
test_env.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import os
import platform
import textwrap
import pytest
from conan.tools.env.environment import environment_wrap_command
from conans.test.utils.mocks import ConanFileMock
from conans.test.utils.tools import TestClient
from conans.util.files import save
@pytest.fixture()
def client():
openssl = textwrap.dedent(r"""
import os
from conans import ConanFile
from conans.tools import save, chdir
class Pkg(ConanFile):
settings = "os"
def package(self):
with chdir(self.package_folder):
echo = "@echo off\necho MYOPENSSL={}!!".format(self.settings.os)
save("bin/myopenssl.bat", echo)
save("bin/myopenssl.sh", echo)
os.chmod("bin/myopenssl.sh", 0o777)
""")
cmake = textwrap.dedent(r"""
import os
from conans import ConanFile
from conans.tools import save, chdir
class Pkg(ConanFile):
settings = "os"
requires = "openssl/1.0"
def package(self):
with chdir(self.package_folder):
echo = "@echo off\necho MYCMAKE={}!!".format(self.settings.os)
save("mycmake.bat", echo + "\ncall myopenssl.bat")
save("mycmake.sh", echo + "\n myopenssl.sh")
os.chmod("mycmake.sh", 0o777)
def package_info(self):
# Custom buildenv not defined by cpp_info
self.buildenv_info.prepend_path("PATH", self.package_folder)
self.buildenv_info.define("MYCMAKEVAR", "MYCMAKEVALUE!!")
""")
gtest = textwrap.dedent(r"""
import os
from conans import ConanFile
from conans.tools import save, chdir
class Pkg(ConanFile):
settings = "os"
def package(self):
with chdir(self.package_folder):
prefix = "@echo off\n" if self.settings.os == "Windows" else ""
echo = "{}echo MYGTEST={}!!".format(prefix, self.settings.os)
save("bin/mygtest.bat", echo)
save("bin/mygtest.sh", echo)
os.chmod("bin/mygtest.sh", 0o777)
def package_info(self):
self.runenv_info.define("MYGTESTVAR", "MyGTestValue{}".format(self.settings.os))
""")
client = TestClient()
save(client.cache.new_config_path, "tools.env.virtualenv:auto_use=True")
client.save({"cmake/conanfile.py": cmake,
"gtest/conanfile.py": gtest,
"openssl/conanfile.py": openssl})
client.run("export openssl openssl/1.0@")
client.run("export cmake mycmake/1.0@")
client.run("export gtest mygtest/1.0@")
myrunner_bat = "@echo off\necho MYGTESTVAR=%MYGTESTVAR%!!\n"
myrunner_sh = "echo MYGTESTVAR=$MYGTESTVAR!!\n"
client.save({"myrunner.bat": myrunner_bat,
"myrunner.sh": myrunner_sh}, clean_first=True)
os.chmod(os.path.join(client.current_folder, "myrunner.sh"), 0o777)
return client
def test_complete(client):
conanfile = textwrap.dedent("""
import platform
from conans import ConanFile
class Pkg(ConanFile):
requires = "openssl/1.0"
build_requires = "mycmake/1.0"
apply_env = False
def build_requirements(self):
self.build_requires("mygtest/1.0", force_host_context=True)
def build(self):
mybuild_cmd = "mycmake.bat" if platform.system() == "Windows" else "mycmake.sh"
self.run(mybuild_cmd, env="conanbuildenv")
mytest_cmd = "mygtest.bat" if platform.system() == "Windows" else "mygtest.sh"
self.run(mytest_cmd, env="conanrunenv")
""")
client.save({"conanfile.py": conanfile})
client.run("install . -s:b os=Windows -s:h os=Linux --build=missing")
# Run the BUILD environment
ext = "bat" if platform.system() == "Windows" else "sh" # TODO: Decide on logic .bat vs .sh
cmd = environment_wrap_command(ConanFileMock(), "conanbuildenv", "mycmake.{}".format(ext),
cwd=client.current_folder)
client.run_command(cmd)
assert "MYCMAKE=Windows!!" in client.out
assert "MYOPENSSL=Windows!!" in client.out
# Run the RUN environment
cmd = environment_wrap_command(ConanFileMock(), "conanrunenv",
"mygtest.{ext} && .{sep}myrunner.{ext}".format(ext=ext,
sep=os.sep),
cwd=client.current_folder)
client.run_command(cmd)
assert "MYGTEST=Linux!!" in client.out
assert "MYGTESTVAR=MyGTestValueLinux!!" in client.out
client.run("build .")
assert "MYCMAKE=Windows!!" in client.out
assert "MYOPENSSL=Windows!!" in client.out
assert "MYGTEST=Linux!!" in client.out
def test_profile_included_multiple():
client = TestClient()
conanfile = textwrap.dedent("""\
import os, platform
from conans import ConanFile
class Pkg(ConanFile):
def generate(self):
buildenv = self.buildenv
self.output.info("MYVAR1: {}!!!".format(buildenv.get("MYVAR1")))
self.output.info("MYVAR2: {}!!!".format(buildenv.get("MYVAR2")))
self.output.info("MYVAR3: {}!!!".format(buildenv.get("MYVAR3")))
""")
myprofile = textwrap.dedent("""
[buildenv]
MYVAR1=MyVal1
MYVAR3+=MyVal3
""")
other_profile = textwrap.dedent("""
[buildenv]
MYVAR1=MyValOther1
MYVAR2=MyValOther2
MYVAR3=MyValOther3
""")
client.save({"conanfile.py": conanfile,
"myprofile": myprofile,
"myprofile_include": "include(other_profile)\n" + myprofile,
"other_profile": other_profile})
# The reference profile has priority
client.run("install . -pr=myprofile_include")
assert "MYVAR1: MyVal1!!!" in client.out
assert "MYVAR2: MyValOther2!!!" in client.out
assert "MYVAR3: MyValOther3 MyVal3!!!" in client.out
# Equivalent to include is to put it first, then the last has priority
client.run("install . -pr=other_profile -pr=myprofile")
assert "MYVAR1: MyVal1!!!" in client.out
assert "MYVAR2: MyValOther2!!!" in client.out
assert "MYVAR3: MyValOther3 MyVal3!!!" in client.out
def test_profile_buildenv():
client = TestClient()
save(client.cache.new_config_path, "tools.env.virtualenv:auto_use=True")
conanfile = textwrap.dedent("""\
import os, platform
from conans import ConanFile
class Pkg(ConanFile):
def generate(self):
self.buildenv.save_script("pkgenv")
if platform.system() != "Windows":
os.chmod("pkgenv.sh", 0o777)
""")
# Some scripts in a random system folders, path adding to the profile [env]
compiler_bat = "@echo off\necho MYCOMPILER!!\necho MYPATH=%PATH%"
compiler_sh = "echo MYCOMPILER!!\necho MYPATH=$PATH"
compiler2_bat = "@echo off\necho MYCOMPILER2!!\necho MYPATH2=%PATH%"
compiler2_sh = "echo MYCOMPILER2!!\necho MYPATH2=$PATH"
myprofile = textwrap.dedent("""
[buildenv]
PATH+=(path){}
mypkg*:PATH=!
mypkg*:PATH+=(path){}
""".format(os.path.join(client.current_folder, "compiler"),
os.path.join(client.current_folder, "compiler2")))
client.save({"conanfile.py": conanfile,
"myprofile": myprofile,
"compiler/mycompiler.bat": compiler_bat,
"compiler/mycompiler.sh": compiler_sh,
"compiler2/mycompiler.bat": compiler2_bat,
"compiler2/mycompiler.sh": compiler2_sh})
os.chmod(os.path.join(client.current_folder, "compiler", "mycompiler.sh"), 0o777)
os.chmod(os.path.join(client.current_folder, "compiler2", "mycompiler.sh"), 0o777)
client.run("install . -pr=myprofile")
# Run the BUILD environment
ext = "bat" if platform.system() == "Windows" else "sh" # TODO: Decide on logic .bat vs .sh
cmd = environment_wrap_command(ConanFileMock(), "conanbuildenv", "mycompiler.{}".format(ext),
cwd=client.current_folder)
client.run_command(cmd)
assert "MYCOMPILER!!" in client.out
assert "MYPATH=" in client.out
# Now with pkg-specific env-var
client.run("install . mypkg/1.0@ -pr=myprofile")
client.run_command(cmd)
assert "MYCOMPILER2!!" in client.out
assert "MYPATH2=" in client.out
def test_transitive_order():
gcc = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
def package_info(self):
self.runenv_info.append("MYVAR", "MyGCCValue")
""")
openssl = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os"
build_requires = "gcc/1.0"
def package_info(self):
self.runenv_info.append("MYVAR", "MyOpenSSL{}Value".format(self.settings.os))
""")
cmake = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
requires = "openssl/1.0"
build_requires = "gcc/1.0"
def package_info(self):
self.runenv_info.append("MYVAR", "MyCMakeRunValue")
self.buildenv_info.append("MYVAR", "MyCMakeBuildValue")
""")
client = TestClient()
client.save({"gcc/conanfile.py": gcc,
"cmake/conanfile.py": cmake,
"openssl/conanfile.py": openssl})
client.run("export gcc gcc/1.0@")
client.run("export openssl openssl/1.0@")
client.run("export cmake cmake/1.0@")
consumer = textwrap.dedent(r"""
from conans import ConanFile
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
class Pkg(ConanFile):
requires = "openssl/1.0"
build_requires = "cmake/1.0", "gcc/1.0"
def generate(self):
buildenv = VirtualBuildEnv(self).environment()
self.output.info("BUILDENV: {}!!!".format(buildenv.get("MYVAR")))
runenv = VirtualRunEnv(self).environment()
self.output.info("RUNENV: {}!!!".format(runenv.get("MYVAR")))
""")
client.save({"conanfile.py": consumer}, clean_first=True)
client.run("install . -s:b os=Windows -s:h os=Linux --build")
assert "BUILDENV: MyGCCValue MyOpenSSLWindowsValue "\
"MyCMakeRunValue MyCMakeBuildValue!!!" in client.out
assert "RUNENV: MyOpenSSLLinuxValue!!!" in client.out
# Even if the generator is duplicated in command line (it used to fail due to bugs)
client.run("install . -s:b os=Windows -s:h os=Linux --build -g VirtualRunEnv -g VirtualBuildEnv")
assert "BUILDENV: MyGCCValue MyOpenSSLWindowsValue "\
"MyCMakeRunValue MyCMakeBuildValue!!!" in client.out
assert "RUNENV: MyOpenSSLLinuxValue!!!" in client.out
def test_buildenv_from_requires():
openssl = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os"
def package_info(self):
self.buildenv_info.append("OpenSSL_ROOT",
"MyOpenSSL{}Value".format(self.settings.os))
""")
poco = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
requires = "openssl/1.0"
settings = "os"
def package_info(self):
self.buildenv_info.append("Poco_ROOT", "MyPoco{}Value".format(self.settings.os))
""")
client = TestClient()
client.save({"poco/conanfile.py": poco,
"openssl/conanfile.py": openssl})
client.run("export openssl openssl/1.0@")
client.run("export poco poco/1.0@")
consumer = textwrap.dedent(r"""
from conans import ConanFile
from conan.tools.env import VirtualBuildEnv
class Pkg(ConanFile):
requires = "poco/1.0"
def generate(self):
env = VirtualBuildEnv(self)
buildenv = env.environment()
self.output.info("BUILDENV POCO: {}!!!".format(buildenv.get("Poco_ROOT")))
self.output.info("BUILDENV OpenSSL: {}!!!".format(buildenv.get("OpenSSL_ROOT")))
""")
client.save({"conanfile.py": consumer}, clean_first=True)
client.run("install . -s:b os=Windows -s:h os=Linux --build -g VirtualBuildEnv")
assert "BUILDENV POCO: MyPocoLinuxValue!!!" in client.out
assert "BUILDENV OpenSSL: MyOpenSSLLinuxValue!!!" in client.out
def test_diamond_repeated():
pkga = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
def package_info(self):
self.runenv_info.define("MYVAR1", "PkgAValue1")
self.runenv_info.append("MYVAR2", "PkgAValue2")
self.runenv_info.prepend("MYVAR3", "PkgAValue3")
self.runenv_info.prepend("MYVAR4", "PkgAValue4")
""")
pkgb = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
requires = "pkga/1.0"
def package_info(self):
self.runenv_info.append("MYVAR1", "PkgBValue1")
self.runenv_info.append("MYVAR2", "PkgBValue2")
self.runenv_info.prepend("MYVAR3", "PkgBValue3")
self.runenv_info.prepend("MYVAR4", "PkgBValue4")
""")
pkgc = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
requires = "pkga/1.0"
def package_info(self):
self.runenv_info.append("MYVAR1", "PkgCValue1")
self.runenv_info.append("MYVAR2", "PkgCValue2")
self.runenv_info.prepend("MYVAR3", "PkgCValue3")
self.runenv_info.prepend("MYVAR4", "PkgCValue4")
""")
pkgd = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
requires = "pkgb/1.0", "pkgc/1.0"
def package_info(self):
self.runenv_info.append("MYVAR1", "PkgDValue1")
self.runenv_info.append("MYVAR2", "PkgDValue2")
self.runenv_info.prepend("MYVAR3", "PkgDValue3")
self.runenv_info.define("MYVAR4", "PkgDValue4")
""")
pkge = textwrap.dedent(r"""
from conans import ConanFile
from conan.tools.env import VirtualRunEnv
class Pkg(ConanFile):
requires = "pkgd/1.0"
def generate(self):
env = VirtualRunEnv(self)
runenv = env.environment()
self.output.info("MYVAR1: {}!!!".format(runenv.get("MYVAR1")))
self.output.info("MYVAR2: {}!!!".format(runenv.get("MYVAR2")))
self.output.info("MYVAR3: {}!!!".format(runenv.get("MYVAR3")))
self.output.info("MYVAR4: {}!!!".format(runenv.get("MYVAR4")))
""")
client = TestClient()
client.save({"pkga/conanfile.py": pkga,
"pkgb/conanfile.py": pkgb,
"pkgc/conanfile.py": pkgc,
"pkgd/conanfile.py": pkgd,
"pkge/conanfile.py": pkge})
client.run("export pkga pkga/1.0@")
client.run("export pkgb pkgb/1.0@")
client.run("export pkgc pkgc/1.0@")
client.run("export pkgd pkgd/1.0@")
client.run("install pkge --build")
assert "MYVAR1: PkgAValue1 PkgCValue1 PkgBValue1 PkgDValue1!!!" in client.out
assert "MYVAR2: PkgAValue2 PkgCValue2 PkgBValue2 PkgDValue2!!!" in client.out
assert "MYVAR3: PkgDValue3 PkgBValue3 PkgCValue3 PkgAValue3!!!" in client.out
assert "MYVAR4: PkgDValue4!!!" in client.out