Skip to content

Commit

Permalink
Add format json option to conan cache path (#15613)
Browse files Browse the repository at this point in the history
* add cache path format json option

* Update conans/test/integration/command_v2/test_cache_path.py

Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>

* Update conans/test/integration/command_v2/test_cache_path.py

Co-authored-by: James <memsharded@gmail.com>

---------

Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>
Co-authored-by: James <memsharded@gmail.com>
  • Loading branch information
3 people authored Feb 7, 2024
1 parent 1c18f76 commit 9545dcc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion conan/cli/commands/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from conan.api.conan_api import ConanAPI
from conan.api.model import ListPattern, MultiPackagesList
from conan.api.output import cli_out_write, ConanOutput
Expand All @@ -9,6 +11,10 @@
from conans.model.recipe_ref import RecipeReference


def json_export(data):
cli_out_write(json.dumps({"cache_path": data}))


@conan_command(group="Consumer")
def cache(conan_api: ConanAPI, parser, *args):
"""
Expand All @@ -17,7 +23,7 @@ def cache(conan_api: ConanAPI, parser, *args):
pass


@conan_subcommand(formatters={"text": cli_out_write})
@conan_subcommand(formatters={"text": cli_out_write, "json": json_export})
def cache_path(conan_api: ConanAPI, parser, subparser, *args):
"""
Show the path to the Conan cache for a given reference.
Expand Down
13 changes: 13 additions & 0 deletions conans/test/integration/command_v2/test_cache_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
import os

import pytest

from conans.test.assets.genconanfile import GenConanfile
Expand Down Expand Up @@ -111,3 +114,13 @@ def test_cache_path_does_not_exist_folder():
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

def test_cache_path_output_json():
client = TestClient()
conanfile = GenConanfile("mypkg", "0.1")
client.save({"conanfile.py": conanfile})
client.run("export .")
layout = client.exported_layout()
client.run("cache path mypkg/0.1 --format=json")
output = json.loads(client.stdout)
assert output == {"cache_path": os.path.join(layout.base_folder, "e")}

0 comments on commit 9545dcc

Please sign in to comment.