Skip to content

Commit

Permalink
fix: Return JSON on environment creation dry run (#3627)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
  • Loading branch information
jjerphan authored Nov 22, 2024
1 parent fcb007a commit b163ce9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libmamba/src/api/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <iostream>

#include "mamba/api/configuration.hpp"
#include "mamba/api/create.hpp"
#include "mamba/api/install.hpp"
Expand All @@ -29,6 +31,7 @@ namespace mamba

auto& create_specs = config.at("specs").value<std::vector<std::string>>();
auto& use_explicit = config.at("explicit_install").value<bool>();
auto& json_format = config.at("json").get_cli_config<bool>();

auto channel_context = ChannelContext::make_conda_compatible(ctx);

Expand Down Expand Up @@ -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)
{
Expand Down
15 changes: 15 additions & 0 deletions micromamba/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b163ce9

Please sign in to comment.