Skip to content

Commit e3e2b98

Browse files
AbrilRBSmemsharded
authored andcommitted
Let conan source reference the backup sources it generates in more cases (conan-io#18655)
* Let conan source reference the sources it downloads in more cases * Test that the cache backup-upload also tries to upload non unknown references
1 parent c509edc commit e3e2b98

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

conan/internal/rest/download_cache.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,17 @@ def update_backup_sources_json(cached_path, conanfile, urls):
123123
try:
124124
summary_key = str(conanfile.ref)
125125
except AttributeError:
126-
# The recipe path would be different between machines
127-
# So best we can do is to set this as unknown
128-
summary_key = "unknown"
126+
# If there's no node associated with the conanfile,
127+
# try to construct a reference from the conanfile itself.
128+
# We accept it if we have a name and a version at least.
129+
if conanfile.name and conanfile.version:
130+
user = f"@{conanfile.user}" if conanfile.user else ""
131+
channel = f"/{conanfile.channel}" if conanfile.channel else ""
132+
summary_key = f"{conanfile.name}/{conanfile.version}{user}{channel}"
133+
else:
134+
# The recipe path would be different between machines
135+
# So best we can do is to set this as unknown
136+
summary_key = "unknown"
129137

130138
if not isinstance(urls, (list, tuple)):
131139
urls = [urls]

test/integration/cache/backup_sources_test.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,16 @@ def source(self):
6262

6363
assert 2 == len(os.listdir(os.path.join(tmp_folder, "s")))
6464
content = json.loads(load(os.path.join(tmp_folder, "s", sha256 + ".json")))
65-
assert "http://localhost.mirror:5000/myfile.txt" in content["references"]["unknown"]
66-
assert "http://localhost:5000/myfile.txt" in content["references"]["unknown"]
67-
assert len(content["references"]["unknown"]) == 2
65+
assert "http://localhost.mirror:5000/myfile.txt" in content["references"]["pkg/1.0"]
66+
assert len(content["references"]["pkg/1.0"]) == 1
6867

6968
# Ensure the cache is working and we didn't break anything by modifying the summary
7069
client.run("source .")
7170
assert "Downloading file" not in client.out
7271

73-
client.run("create .")
72+
client.run("create . --user=barbarian")
7473
content = json.loads(load(os.path.join(tmp_folder, "s", sha256 + ".json")))
75-
assert content["references"]["pkg/1.0"] == \
74+
assert content["references"]["pkg/1.0@barbarian"] == \
7675
["http://localhost.mirror:5000/myfile.txt"]
7776

7877
client.run("create . --user=barbarian --channel=stable")
@@ -214,9 +213,9 @@ def test_unknown_handling(self):
214213
from conan.tools.files import download
215214
class Pkg2(ConanFile):
216215
name = "pkg"
217-
version = "1.0"
218216
def source(self):
219-
download(self, "{self.file_server.fake_url}/internet/myfile.txt", "myfile.txt",
217+
download(self, "{self.file_server.fake_url}/internet/myfile.txt",
218+
"myfile.txt",
220219
sha256="{sha256}")
221220
""")
222221

@@ -234,7 +233,7 @@ def source(self):
234233
s_folder = os.path.join(self.download_cache_folder, "s")
235234
assert len(os.listdir(s_folder)) == 2
236235

237-
self.client.run("export .")
236+
self.client.run("export . --version=1.0")
238237
self.client.run("upload * -c -r=default")
239238
assert "No backup sources files to upload" in self.client.out
240239

@@ -746,7 +745,8 @@ def source(self):
746745
# Ensure we are testing for an already uploaded recipe
747746
assert f"Recipe 'pkg/1.0#{exported_rev}' already in server, skipping upload" in self.client.out
748747

749-
def test_source_then_upload_workflow(self):
748+
@pytest.mark.parametrize("unknown", [True, False])
749+
def test_source_then_upload_workflow(self, unknown):
750750
mkdir(os.path.join(self.download_cache_folder, "s"))
751751

752752
http_server_base_folder_internet = os.path.join(self.file_server.store, "internet")
@@ -770,7 +770,12 @@ def source(self):
770770
f"core.sources:upload_url={self.file_server.fake_url}/backup/"})
771771

772772
self.client.save({"conanfile.py": conanfile})
773-
self.client.run("source .")
773+
ref_args = "" if unknown else "--name foo --version 1.0"
774+
reference_key = "unknown" if unknown else "foo/1.0"
775+
self.client.run(f"source . {ref_args}")
776+
content = json.loads(load(os.path.join(self.download_cache_folder, "s", sha256 + ".json")))
777+
assert len(content["references"][reference_key]) == 1
778+
774779
self.client.run("cache backup-upload")
775780
# This used to crash because we were trying to list a missing dir if only exports were made
776781
assert "[Errno 2] No such file or directory" not in self.client.out

0 commit comments

Comments
 (0)