forked from conan-io/conan
/
export_test.py
495 lines (421 loc) · 21.8 KB
/
export_test.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import os
import platform
import stat
import textwrap
import unittest
import pytest
from parameterized import parameterized
from conans.model.manifest import FileTreeManifest
from conans.model.ref import ConanFileReference
from conans.paths import CONANFILE, CONAN_MANIFEST
from conans.test.assets.cpp_test_files import cpp_hello_conan_files
from conans.test.utils.tools import TestClient, GenConanfile
from conans.util.files import load, save
class ExportSettingsTest(unittest.TestCase):
def test_basic(self):
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
settings = {"os": ["Linux"]}
""")
client.save({"conanfile.py": conanfile})
client.run("export . lasote/stable")
self.assertIn("WARN: Conanfile doesn't have 'license'", client.out)
client.run("install Hello/1.2@lasote/stable -s os=Windows", assert_error=True)
self.assertIn("'Windows' is not a valid 'settings.os' value", client.out)
self.assertIn("Possible values are ['Linux']", client.out)
def test_export_without_full_reference(self):
client = TestClient()
client.save({"conanfile.py": GenConanfile()})
client.run("export . lasote/stable", assert_error=True)
self.assertIn("conanfile didn't specify name", client.out)
client.save({"conanfile.py": GenConanfile("Lib")})
client.run("export . lasote/stable", assert_error=True)
self.assertIn("conanfile didn't specify version", client.out)
client.save({"conanfile.py": GenConanfile()})
client.run("export . lib/1.0@lasote/channel")
self.assertIn("lib/1.0@lasote/channel: A new conanfile.py version was exported", client.out)
client.save({"conanfile.py": GenConanfile("Lib", "1.0")})
client.run("export . lasote", assert_error=True)
self.assertIn("Invalid parameter 'lasote', specify the full reference or user/channel",
client.out)
def test_export_read_only(self):
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports = "file1.txt"
exports_sources = "file2.txt"
""")
ref = ConanFileReference.loads("Hello/1.2@lasote/stable")
export_path = client.cache.package_layout(ref).export()
export_src_path = client.cache.package_layout(ref).export_sources()
client.save({CONANFILE: conanfile,
"file1.txt": "",
"file2.txt": ""})
mode1 = os.stat(os.path.join(client.current_folder, "file1.txt")).st_mode
mode2 = os.stat(os.path.join(client.current_folder, "file2.txt")).st_mode
os.chmod(os.path.join(client.current_folder, "file1.txt"), mode1 & ~stat.S_IWRITE)
os.chmod(os.path.join(client.current_folder, "file2.txt"), mode2 & ~stat.S_IWRITE)
client.run("export . lasote/stable")
self.assertEqual(load(os.path.join(export_path, "file1.txt")), "")
self.assertEqual(load(os.path.join(export_src_path, "file2.txt")), "")
with self.assertRaises(IOError):
save(os.path.join(export_path, "file1.txt"), "")
with self.assertRaises(IOError):
save(os.path.join(export_src_path, "file2.txt"), "")
self.assertIn("WARN: Conanfile doesn't have 'license'", client.out)
os.chmod(os.path.join(client.current_folder, "file1.txt"), mode1 | stat.S_IWRITE)
os.chmod(os.path.join(client.current_folder, "file2.txt"), mode2 | stat.S_IWRITE)
client.save({CONANFILE: conanfile,
"file1.txt": "file1",
"file2.txt": "file2"})
client.run("export . lasote/stable")
self.assertEqual(load(os.path.join(export_path, "file1.txt")), "file1")
self.assertEqual(load(os.path.join(export_src_path, "file2.txt")), "file2")
client.run("install Hello/1.2@lasote/stable --build=missing")
self.assertIn("Hello/1.2@lasote/stable: Generating the package", client.out)
client.save({CONANFILE: conanfile,
"file1.txt": "",
"file2.txt": ""})
os.chmod(os.path.join(client.current_folder, "file1.txt"), mode1 & ~stat.S_IWRITE)
os.chmod(os.path.join(client.current_folder, "file2.txt"), mode2 & ~stat.S_IWRITE)
client.run("export . lasote/stable")
self.assertEqual(load(os.path.join(export_path, "file1.txt")), "")
self.assertEqual(load(os.path.join(export_src_path, "file2.txt")), "")
client.run("install Hello/1.2@lasote/stable --build=Hello")
self.assertIn("Hello/1.2@lasote/stable: Generating the package", client.out)
def test_code_parent(self):
# when referencing the parent, the relative folder "sibling" will be kept
base = """
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports = "../*.txt"
"""
for conanfile in (base, base.replace("../*.txt", "../sibling*")):
client = TestClient()
client.save({"recipe/conanfile.py": conanfile,
"sibling/file.txt": "Hello World!"})
client.current_folder = os.path.join(client.current_folder, "recipe")
client.run("export . lasote/stable")
ref = ConanFileReference("Hello", "1.2", "lasote", "stable")
export_path = client.cache.package_layout(ref).export()
content = load(os.path.join(export_path, "sibling/file.txt"))
self.assertEqual("Hello World!", content)
def test_code_sibling(self):
# if provided a path with slash, it will use as a export base
client = TestClient()
conanfile = """
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports = "../sibling/*.txt"
"""
files = {"recipe/conanfile.py": conanfile,
"sibling/file.txt": "Hello World!"}
client.save(files)
client.current_folder = os.path.join(client.current_folder, "recipe")
client.run("export . lasote/stable")
ref = ConanFileReference("Hello", "1.2", "lasote", "stable")
export_path = client.cache.package_layout(ref).export()
content = load(os.path.join(export_path, "file.txt"))
self.assertEqual("Hello World!", content)
def test_code_several_sibling(self):
# if provided a path with slash, it will use as a export base
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports_sources = "../test/src/*", "../cpp/*", "../include/*"
""")
client.save({"recipe/conanfile.py": conanfile,
"test/src/file.txt": "Hello World!",
"cpp/file.cpp": "Hello World!",
"include/file.h": "Hello World!"})
client.current_folder = os.path.join(client.current_folder, "recipe")
client.run("export . lasote/stable")
ref = ConanFileReference("Hello", "1.2", "lasote", "stable")
export_path = client.cache.package_layout(ref).export_sources()
self.assertEqual(sorted(['file.txt', 'file.cpp', 'file.h']),
sorted(os.listdir(export_path)))
@parameterized.expand([("myconanfile.py", ), ("Conanfile.py", )])
def test_filename(self, filename):
client = TestClient()
client.save({filename: GenConanfile("Hello", "1.2")})
client.run("export %s user/stable" % filename)
self.assertIn("Hello/1.2@user/stable: A new conanfile.py version was exported", client.out)
ref = ConanFileReference("Hello", "1.2", "user", "stable")
export_path = client.cache.package_layout(ref).export()
conanfile = load(os.path.join(export_path, "conanfile.py"))
self.assertIn("name = 'Hello'", conanfile)
manifest = load(os.path.join(export_path, "conanmanifest.txt"))
self.assertIn('conanfile.py: aa4e485651a4281b79d343af7d7b20e4', manifest)
def test_exclude_basic(self):
client = TestClient()
conanfile = """
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports = "*.txt", "!*file1.txt"
exports_sources = "*.cpp", "!*temp.cpp"
"""
client.save({CONANFILE: conanfile,
"file.txt": "",
"file1.txt": "",
"file.cpp": "",
"file_temp.cpp": ""})
client.run("export . lasote/stable")
ref = ConanFileReference("Hello", "1.2", "lasote", "stable")
export_path = client.cache.package_layout(ref).export()
exports_sources_path = client.cache.package_layout(ref).export_sources()
self.assertTrue(os.path.exists(os.path.join(export_path, "file.txt")))
self.assertFalse(os.path.exists(os.path.join(export_path, "file1.txt")))
self.assertTrue(os.path.exists(os.path.join(exports_sources_path, "file.cpp")))
self.assertFalse(os.path.exists(os.path.join(exports_sources_path, "file_temp.cpp")))
def test_exclude_folders(self):
client = TestClient()
conanfile = """
from conans import ConanFile
class TestConan(ConanFile):
name = "Hello"
version = "1.2"
exports = "*.txt", "!*/temp/*"
"""
client.save({CONANFILE: conanfile,
"file.txt": "",
"any/temp/file1.txt": "",
"other/sub/file2.txt": ""})
client.run("export . lasote/stable")
ref = ConanFileReference("Hello", "1.2", "lasote", "stable")
export_path = client.cache.package_layout(ref).export()
self.assertTrue(os.path.exists(os.path.join(export_path, "file.txt")))
self.assertFalse(os.path.exists(os.path.join(export_path, "any/temp/file1.txt")))
self.assertTrue(os.path.exists(os.path.join(export_path, "other/sub/file2.txt")))
class ExportTest(unittest.TestCase):
def setUp(self):
self.client = TestClient()
self.files = cpp_hello_conan_files("Hello0", "0.1")
self.ref = ConanFileReference("Hello0", "0.1", "lasote", "stable")
self.client.save(self.files)
self.client.run("export . lasote/stable")
def test_basic(self):
reg_path = self.client.cache.package_layout(self.ref).export()
manif = FileTreeManifest.load(self.client.cache.package_layout(self.ref).export())
self.assertIn('%s: A new conanfile.py version was exported' % str(self.ref),
self.client.out)
self.assertIn('%s: Folder: %s' % (str(self.ref), reg_path), self.client.out)
self.assertTrue(os.path.exists(reg_path))
for name in list(self.files.keys()):
self.assertTrue(os.path.exists(os.path.join(reg_path, name)))
expected_sums = {'hello.cpp': '4f005274b2fdb25e6113b69774dac184',
'main.cpp': '0479f3c223c9a656a718f3148e044124',
'CMakeLists.txt': '10d907c160c360b28f6991397a5aa9b4',
'conanfile.py': '1ad1f4b995ae7ffdb00d53ff49e1366f',
'executable': '68b329da9893e34099c7d8ad5cb9c940',
'helloHello0.h': 'd0a6868b5df17a6ae6e61ebddb0c9eb3'}
self.assertEqual(expected_sums, manif.file_sums)
def test_case_sensitive(self):
self.files = cpp_hello_conan_files("hello0", "0.1")
self.ref = ConanFileReference("hello0", "0.1", "lasote", "stable")
self.client.save(self.files)
self.client.run("export . lasote/stable", assert_error=True)
self.assertIn("ERROR: Cannot export package with same name but different case",
self.client.out)
def test_export_filter(self):
self.client.save({CONANFILE: GenConanfile("openssl", "2.0.1")})
self.client.run("export . lasote/stable")
ref = ConanFileReference.loads('openssl/2.0.1@lasote/stable')
reg_path = self.client.cache.package_layout(ref).export()
self.assertEqual(sorted(os.listdir(reg_path)),
[CONANFILE, CONAN_MANIFEST])
content = """
from conans import ConanFile
class OpenSSLConan(ConanFile):
name = "openssl"
version = "2.0.1"
exports = ('*.txt', '*.h')
"""
self.client.save({CONANFILE: content})
self.client.run("export . lasote/stable")
self.assertEqual(sorted(os.listdir(reg_path)),
['CMakeLists.txt', CONANFILE, CONAN_MANIFEST, 'helloHello0.h'])
# Now exports being a list instead a tuple
content = """
from conans import ConanFile
class OpenSSLConan(ConanFile):
name = "openssl"
version = "2.0.1"
exports = ['*.txt', '*.h']
"""
self.client.save({CONANFILE: content})
self.client.run("export . lasote/stable")
self.assertEqual(sorted(os.listdir(reg_path)),
['CMakeLists.txt', CONANFILE, CONAN_MANIFEST, 'helloHello0.h'])
def test_export_the_same_code(self):
file_list = self._create_packages_and_builds()
# Export the same conans
# Do not adjust cpu_count, it is reusing a cache
client2 = TestClient(self.client.cache_folder, cpu_count=False)
files2 = cpp_hello_conan_files("Hello0", "0.1")
client2.save(files2)
client2.run("export . lasote/stable")
reg_path2 = client2.cache.package_layout(self.ref).export()
digest2 = FileTreeManifest.load(client2.cache.package_layout(self.ref).export())
self.assertNotIn('A new Conan version was exported', client2.out)
self.assertNotIn('Cleaning the old builds ...', client2.out)
self.assertNotIn('Cleaning the old packs ...', client2.out)
self.assertNotIn('All the previous packs were cleaned', client2.out)
self.assertIn('%s: A new conanfile.py version was exported' % str(self.ref),
self.client.out)
self.assertIn('%s: Folder: %s' % (str(self.ref), reg_path2), self.client.out)
self.assertTrue(os.path.exists(reg_path2))
for name in list(files2.keys()):
self.assertTrue(os.path.exists(os.path.join(reg_path2, name)))
expected_sums = {'hello.cpp': '4f005274b2fdb25e6113b69774dac184',
'main.cpp': '0479f3c223c9a656a718f3148e044124',
'CMakeLists.txt': '10d907c160c360b28f6991397a5aa9b4',
'conanfile.py': '1ad1f4b995ae7ffdb00d53ff49e1366f',
'executable': '68b329da9893e34099c7d8ad5cb9c940',
'helloHello0.h': 'd0a6868b5df17a6ae6e61ebddb0c9eb3'}
self.assertEqual(expected_sums, digest2.file_sums)
for f in file_list:
self.assertTrue(os.path.exists(f))
def test_export_a_new_version(self):
self._create_packages_and_builds()
# Export an update of the same conans
# Do not adjust cpu_count, it is reusing a cache
client2 = TestClient(self.client.cache_folder, cpu_count=False)
files2 = cpp_hello_conan_files("Hello0", "0.1")
files2[CONANFILE] = "# insert comment\n %s" % files2[CONANFILE]
client2.save(files2)
client2.run("export . lasote/stable")
reg_path3 = client2.cache.package_layout(self.ref).export()
digest3 = FileTreeManifest.load(client2.cache.package_layout(self.ref).export())
self.assertIn('%s: A new conanfile.py version was exported' % str(self.ref),
self.client.out)
self.assertIn('%s: Folder: %s' % (str(self.ref), reg_path3), self.client.out)
self.assertTrue(os.path.exists(reg_path3))
for name in list(files2.keys()):
self.assertTrue(os.path.exists(os.path.join(reg_path3, name)))
expected_sums = {'hello.cpp': '4f005274b2fdb25e6113b69774dac184',
'main.cpp': '0479f3c223c9a656a718f3148e044124',
'CMakeLists.txt': '10d907c160c360b28f6991397a5aa9b4',
'conanfile.py': 'e309305959502e16f8a57439bb6a4107',
'executable': '68b329da9893e34099c7d8ad5cb9c940',
'helloHello0.h': 'd0a6868b5df17a6ae6e61ebddb0c9eb3'}
self.assertEqual(expected_sums, digest3.file_sums)
# for f in file_list:
# self.assertFalse(os.path.exists(f))
def _create_packages_and_builds(self):
reg_builds = self.client.cache.package_layout(self.ref).builds()
reg_packs = self.client.cache.package_layout(self.ref).packages()
folders = [os.path.join(reg_builds, '342525g4f52f35f'),
os.path.join(reg_builds, 'ew9o8asdf908asdf80'),
os.path.join(reg_packs, '342525g4f52f35f'),
os.path.join(reg_packs, 'ew9o8asdf908asdf80')]
file_list = []
for f in folders:
for name, content in {'file1.h': 'asddfasdf', 'file1.dll': 'asddfasdf'}.items():
file_path = os.path.join(f, name)
save(file_path, content)
file_list.append(file_path)
return file_list
class ExportMetadataTest(unittest.TestCase):
conanfile = textwrap.dedent("""
from conans import ConanFile
class Lib(ConanFile):
revision_mode = "{revision_mode}"
""")
summary_hash = "bfe8b4a6a2a74966c0c4e0b34705004a"
def test_revision_mode_hash(self):
t = TestClient()
t.save({'conanfile.py': self.conanfile.format(revision_mode="hash")})
ref = ConanFileReference.loads("name/version@user/channel")
t.run("export . {}".format(ref))
meta = t.cache.package_layout(ref, short_paths=False).load_metadata()
self.assertEqual(meta.recipe.revision, self.summary_hash)
@pytest.mark.tool_git
def test_revision_mode_scm(self):
t = TestClient()
commit = t.init_git_repo({'conanfile.py': self.conanfile.format(revision_mode="scm")})
ref = ConanFileReference.loads("name/version@user/channel")
t.run("export . {}".format(ref))
meta = t.cache.package_layout(ref, short_paths=False).load_metadata()
self.assertEqual(meta.recipe.revision, commit)
def test_revision_mode_invalid(self):
conanfile = self.conanfile.format(revision_mode="auto")
t = TestClient()
t.save({'conanfile.py': conanfile})
ref = ConanFileReference.loads("name/version@user/channel")
t.run("export . {}".format(ref), assert_error=True)
self.assertIn("ERROR: Revision mode should be one of 'hash' (default) or 'scm'", t.out)
def test_export_no_params(self):
client = TestClient()
client.save({"conanfile.py": GenConanfile().with_name("lib").with_version("1.0")})
client.run('export .')
client.cache.package_layout(ConanFileReference.loads("lib/1.0@")).export()
self.assertIn("lib/1.0: A new conanfile.py version was exported", client.out)
# Do it twice
client.run('export . ')
self.assertIn("lib/1.0: The stored package has not changed", client.out)
def test_export_with_name_and_version(self):
client = TestClient()
client.save({"conanfile.py": GenConanfile()})
client.run('export . lib/1.0@')
self.assertIn("lib/1.0: A new conanfile.py version was exported", client.out)
def test_export_with_only_user_channel(self):
"""This should be the recommended way and only from Conan 2.0"""
client = TestClient()
client.save({"conanfile.py": GenConanfile().with_name("lib").with_version("1.0")})
client.run('export . @user/channel')
self.assertIn("lib/1.0@user/channel: A new conanfile.py version was exported", client.out)
def test_export_conflict_no_user_channel(self):
client = TestClient()
client.save({"conanfile.py": GenConanfile()})
client.run('export . pkg/0.1@user/channel')
self.assertIn("pkg/0.1@user/channel: A new conanfile.py version was exported", client.out)
client.run('export . pkg/0.1@other/stable')
self.assertIn("pkg/0.1@other/stable: A new conanfile.py version was exported", client.out)
client.run('export . pkg/0.1@')
self.assertIn("pkg/0.1: A new conanfile.py version was exported", client.out)
client.run('export . Pkg/0.1@', assert_error=True)
self.assertIn("ERROR: Cannot export package with same name but different case", client.out)
@pytest.mark.skipif(platform.system() != "Linux", reason="Needs case-sensitive filesystem")
def test_export_casing():
# https://github.com/conan-io/conan/issues/8583
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
exports = "file1", "FILE1"
exports_sources = "test", "TEST"
""")
client.save({"conanfile.py": conanfile,
"test": "some lowercase",
"TEST": "some UPPERCASE",
"file1": "file1 lowercase",
"FILE1": "file1 UPPERCASE"
})
assert client.load("test") == "some lowercase"
assert client.load("TEST") == "some UPPERCASE"
assert client.load("file1") == "file1 lowercase"
assert client.load("FILE1") == "file1 UPPERCASE"
client.run("export . pkg/0.1@")
ref = ConanFileReference.loads("pkg/0.1@")
export_src_folder = client.cache.package_layout(ref).export_sources()
assert load(os.path.join(export_src_folder, "test")) == "some lowercase"
assert load(os.path.join(export_src_folder, "TEST")) == "some UPPERCASE"
exports_folder = client.cache.package_layout(ref).export()
assert load(os.path.join(exports_folder, "file1")) == "file1 lowercase"
assert load(os.path.join(exports_folder, "FILE1")) == "file1 UPPERCASE"