From b163ce9027b4583e97e2ff801690e3fc0da7cf31 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Fri, 22 Nov 2024 15:23:27 +0100 Subject: [PATCH] fix: Return JSON on environment creation dry run (#3627) Signed-off-by: Julien Jerphanion --- libmamba/src/api/create.cpp | 18 ++++++++++++++++++ micromamba/tests/test_create.py | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/libmamba/src/api/create.cpp b/libmamba/src/api/create.cpp index 835f244f56..ca25db2ab1 100644 --- a/libmamba/src/api/create.cpp +++ b/libmamba/src/api/create.cpp @@ -4,6 +4,8 @@ // // The full license is in the file LICENSE, distributed with this software. +#include + #include "mamba/api/configuration.hpp" #include "mamba/api/create.hpp" #include "mamba/api/install.hpp" @@ -29,6 +31,7 @@ namespace mamba auto& create_specs = config.at("specs").value>(); auto& use_explicit = config.at("explicit_install").value(); + auto& json_format = config.at("json").get_cli_config(); auto channel_context = ChannelContext::make_conda_compatible(ctx); @@ -78,6 +81,21 @@ namespace mamba ); } } + else + { + if (create_specs.empty() && json_format) + { + // Just print the JSON + nlohmann::json output; + output["actions"]["FETCH"] = nlohmann::json::array(); + output["actions"]["PREFIX"] = ctx.prefix_params.target_prefix; + output["dry_run"] = true; + output["prefix"] = ctx.prefix_params.target_prefix; + output["success"] = true; + std::cout << output.dump(2) << std::endl; + return; + } + } if (ctx.env_lockfile) { diff --git a/micromamba/tests/test_create.py b/micromamba/tests/test_create.py index 1987a8bcad..e26243daa9 100644 --- a/micromamba/tests/test_create.py +++ b/micromamba/tests/test_create.py @@ -1343,3 +1343,18 @@ def test_parsable_env_history_with_metadata(tmp_home, tmp_root_prefix, tmp_path) # Must not hang helpers.umamba_list("-p", env_prefix, "--json") + + +def test_create_dry_run_json(tmp_path): + # Non-regression test for https://github.com/mamba-org/mamba/issues/3583 + env_prefix = tmp_path / "env-create_dry_run_json" + res = helpers.create("-p", env_prefix, "--dry-run", "--json") + + expected_output = { + "actions": {"FETCH": [], "PREFIX": str(env_prefix)}, + "dry_run": True, + "prefix": str(env_prefix), + "success": True, + } + + assert res == expected_output