Skip to content

Commit

Permalink
[command][cache path] Raising error if folder path does not exist (#1…
Browse files Browse the repository at this point in the history
…5257)

* Wip

* Fixed test

* Fixed

* Better checking from API

* Removed useless import
  • Loading branch information
franramirez688 authored Dec 18, 2023
1 parent 97b8b6d commit 9ffd71d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
20 changes: 13 additions & 7 deletions conan/api/subapi/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,43 @@ def export_path(self, ref: RecipeReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
ref.revision = None if ref.revision == "latest" else ref.revision
ref_layout = app.cache.recipe_layout(ref)
return ref_layout.export()
return _check_folder_existence(ref, "export", ref_layout.export())

def recipe_metadata_path(self, ref: RecipeReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
ref = _resolve_latest_ref(app, ref)
ref_layout = app.cache.recipe_layout(ref)
return ref_layout.metadata()
return _check_folder_existence(ref, "metadata", ref_layout.metadata())

def export_source_path(self, ref: RecipeReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
ref.revision = None if ref.revision == "latest" else ref.revision
ref_layout = app.cache.recipe_layout(ref)
return ref_layout.export_sources()
return _check_folder_existence(ref, "export_sources", ref_layout.export_sources())

def source_path(self, ref: RecipeReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
ref.revision = None if ref.revision == "latest" else ref.revision
ref_layout = app.cache.recipe_layout(ref)
return ref_layout.source()
return _check_folder_existence(ref, "source", ref_layout.source())

def build_path(self, pref: PkgReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
pref = _resolve_latest_pref(app, pref)
ref_layout = app.cache.pkg_layout(pref)
return ref_layout.build()
return _check_folder_existence(pref, "build", ref_layout.build())

def package_metadata_path(self, pref: PkgReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
pref = _resolve_latest_pref(app, pref)
ref_layout = app.cache.pkg_layout(pref)
return ref_layout.metadata()
return _check_folder_existence(pref, "metadata", ref_layout.metadata())

def package_path(self, pref: PkgReference):
app = ConanApp(self.conan_api.cache_folder, self.conan_api.config.global_conf)
pref = _resolve_latest_pref(app, pref)
ref_layout = app.cache.pkg_layout(pref)
return ref_layout.package()
return _check_folder_existence(pref, "package", ref_layout.package())

def check_integrity(self, package_list):
"""Check if the recipes and packages are corrupted (it will raise a ConanExcepcion)"""
Expand Down Expand Up @@ -202,3 +202,9 @@ def _resolve_latest_pref(app, pref):
raise ConanException(f"'{pref.repr_notime()}' not found in cache")
pref = result
return pref


def _check_folder_existence(ref, folder_name, folder_path):
if not os.path.exists(folder_path):
raise ConanException(f"'{folder_name}' folder does not exist for the reference {ref}")
return folder_path
14 changes: 14 additions & 0 deletions conans/test/integration/command_v2/test_cache_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ def test_cache_path_arg_errors():
# source, cannot obtain build without pref
t.run("cache path foo/1.0:pid --folder source", assert_error=True)
assert "ERROR: '--folder source' requires a recipe reference" in t.out


def test_cache_path_does_not_exist_folder():
client = TestClient(default_server_user=True)
conanfile = GenConanfile()
client.save({"conanfile.py": conanfile})
client.run("create . --name=mypkg --version=0.1")
pref = client.created_package_reference("mypkg/0.1")
client.run("upload * --confirm -r default")
client.run("remove * -c")

client.run(f"install --requires mypkg/0.1")
client.run(f"cache path {pref} --folder build", assert_error=True)
assert f"ERROR: 'build' folder does not exist for the reference {pref}" in client.out
15 changes: 8 additions & 7 deletions conans/test/integration/metadata/test_metadata_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def test_upload(self, create_conan_pkg):
# Add some metadata
self.save_metadata_file(c, "pkg/0.1", "mylogs.txt")
self.save_metadata_file(c, f"pkg/0.1:{pid}", "mybuildlogs.txt")

# Now upload everything
c.run("upload * -c -r=default")
assert "pkg/0.1: Recipe metadata: 1 files" in c.out
Expand All @@ -47,15 +46,17 @@ def test_upload(self, create_conan_pkg):

c.run("remove * -c")
c.run("install --requires=pkg/0.1") # wont install metadata by default
c.run("cache path pkg/0.1 --folder=metadata")
metadata_path = str(c.stdout).strip()
c.run(f"cache path pkg/0.1:{pid} --folder=metadata")
pkg_metadata_path = str(c.stdout).strip()
assert not os.path.exists(metadata_path)
assert not os.path.exists(pkg_metadata_path)
c.run("cache path pkg/0.1 --folder=metadata", assert_error=True)
assert "'metadata' folder does not exist for the reference pkg/0.1" in c.out
c.run(f"cache path pkg/0.1:{pid} --folder=metadata", assert_error=True)
assert f"'metadata' folder does not exist for the reference pkg/0.1:{pid}" in c.out

# Forcing the download of the metadata of cache-existing things with the "download" command
c.run("download pkg/0.1 -r=default --metadata=*")
c.run(f"cache path pkg/0.1 --folder=metadata")
metadata_path = str(c.stdout).strip()
c.run(f"cache path pkg/0.1:{pid} --folder=metadata")
pkg_metadata_path = str(c.stdout).strip()
for f in "logs/mylogs.txt", "logs/mylogs2.txt":
assert os.path.isfile(os.path.join(metadata_path, f))
for f in "logs/mybuildlogs.txt", "logs/mybuildlogs2.txt":
Expand Down

0 comments on commit 9ffd71d

Please sign in to comment.