-
Notifications
You must be signed in to change notification settings - Fork 440
Handle pre/post-link/unlink scripts #4313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
870e0ea
Add execution of pre-link and pre-unlink scripts
Hind-M eabc890
Fix linter
Hind-M a81e618
Comment offending test to validate assumption of failure
Hind-M 748cace
Try with is_mamba_exe as true
Hind-M 982a269
Create dummy conda_bat file for tests
Hind-M 8131cd6
Add dummy conda binary for unix as well
Hind-M e2edf53
Set SOURCE_DIR in envmap
Hind-M 59244a4
Fix warning for json output
Hind-M f434523
Adapt test instead of condition for warning
Hind-M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // Copyright (c) 2026, QuantStack and Mamba Contributors | ||
| // | ||
| // Distributed under the terms of the BSD 3-Clause License. | ||
| // | ||
| // The full license is in the file LICENSE, distributed with this software. | ||
|
|
||
| #include <fstream> | ||
|
|
||
| #include <catch2/catch_all.hpp> | ||
|
|
||
| #include "mamba/core/fsutil.hpp" | ||
| #include "mamba/core/util.hpp" | ||
| #include "mamba/core/util_os.hpp" | ||
| #include "mamba/specs/package_info.hpp" | ||
|
|
||
| #include "core/link.hpp" | ||
| #include "core/transaction_context.hpp" | ||
|
|
||
| #include "mambatests.hpp" | ||
|
|
||
| namespace mamba | ||
| { | ||
| namespace | ||
| { | ||
| TEST_CASE("run_scripts_in_transaction") | ||
| { | ||
| // Ensure Console is initialized to avoid singleton access errors | ||
| (void) mambatests::context(); | ||
|
|
||
| const auto tmp_dir = TemporaryDirectory(); | ||
| const fs::u8path prefix = tmp_dir.path() / "prefix"; | ||
| const fs::u8path cache_dir = tmp_dir.path() / "cache"; | ||
|
|
||
| specs::PackageInfo pkg("test_pkg"); | ||
| pkg.version = "1.0"; | ||
| pkg.build_string = "0"; | ||
|
|
||
| const fs::u8path pkg_source = cache_dir / pkg.str(); | ||
| const fs::u8path bin_dir_relative = get_bin_directory_short_path(); | ||
|
|
||
| fs::create_directories(prefix / "conda-meta"); | ||
| fs::create_directories(pkg_source / bin_dir_relative); | ||
| fs::create_directories(prefix / bin_dir_relative); | ||
|
|
||
| // Provide a dummy conda binary so wrap_call activation wrapper | ||
| // succeeds in the test environment | ||
| if (util::on_win) | ||
| { | ||
| fs::create_directories(prefix / "condabin"); | ||
| auto conda_bat_path = prefix / "condabin" | ||
| / (get_self_exe_path().stem().string() + ".bat"); | ||
| auto out = open_ofstream(conda_bat_path); | ||
| out << "@exit /b 0\n"; | ||
| } | ||
| else | ||
| { | ||
| fs::create_directories(prefix / "bin"); | ||
| auto conda_path = prefix / "bin" / "conda"; | ||
| auto out = open_ofstream(conda_path); | ||
| out << "#!/bin/sh\n"; | ||
|
Hind-M marked this conversation as resolved.
|
||
| out << "if [ \"$1\" = \"shell.posix\" ] && [ \"$2\" = \"hook\" ]; then\n"; | ||
| out << " echo 'conda() { :; }'\n"; // Define conda as a no-op function | ||
| out << "fi\n"; | ||
| make_executable(conda_path); | ||
| } | ||
|
|
||
| const auto make_tx_context = [&] | ||
| { | ||
| TransactionParams tx_params{ | ||
| .is_mamba_exe = false, | ||
| .json_output = false, | ||
| .verbosity = 0, | ||
| .shortcuts = false, | ||
| .envs_dirs = {}, | ||
| .platform = "linux-64", | ||
| .prefix_params = | ||
| PrefixParams{ | ||
| .target_prefix = prefix, | ||
| .root_prefix = prefix, | ||
| .conda_prefix = prefix, | ||
| .relocate_prefix = prefix, | ||
| }, | ||
| .link_params = {}, | ||
| .threads_params = {}, | ||
| }; | ||
|
|
||
| return TransactionContext( | ||
| tx_params, | ||
| { "3.14.4", "3.14.4" }, | ||
| "lib/python3.14/site-packages", | ||
| {} | ||
| ); | ||
| }; | ||
|
|
||
| std::string script_ext = util::on_win ? ".bat" : ".sh"; | ||
|
Hind-M marked this conversation as resolved.
|
||
| std::string pre_link_name = ".test_pkg-pre-link" + script_ext; | ||
| std::string post_link_name = ".test_pkg-post-link" + script_ext; | ||
| std::string pre_unlink_name = ".test_pkg-pre-unlink" + script_ext; | ||
| std::string post_unlink_name = ".test_pkg-post-unlink" + script_ext; | ||
|
|
||
| auto create_script = [](const fs::u8path& p, const fs::u8path& marker_path) | ||
| { | ||
| std::ofstream out = open_ofstream(p); | ||
| if (util::on_win) | ||
| { | ||
| out << "@echo off\n"; | ||
| out << "echo done > \"" << marker_path.string() << "\"\n"; | ||
| } | ||
| else | ||
| { | ||
| out << "#!/bin/sh\n"; | ||
| out << "echo done > \"" << marker_path.string() << "\"\n"; | ||
| } | ||
| }; | ||
|
|
||
| fs::u8path pre_link_marker = tmp_dir.path() / "pre_link_done.txt"; | ||
| fs::u8path post_link_marker = tmp_dir.path() / "post_link_done.txt"; | ||
| fs::u8path pre_unlink_marker = tmp_dir.path() / "pre_unlink_done.txt"; | ||
| fs::u8path post_unlink_marker = tmp_dir.path() / "post_unlink_done.txt"; | ||
|
|
||
| create_script(pkg_source / bin_dir_relative / pre_link_name, pre_link_marker); | ||
| create_script(prefix / bin_dir_relative / post_link_name, post_link_marker); | ||
| create_script(prefix / bin_dir_relative / pre_unlink_name, pre_unlink_marker); | ||
| create_script(prefix / bin_dir_relative / post_unlink_name, post_unlink_marker); | ||
|
|
||
| // Prepare metadata for unlinking | ||
| { | ||
| auto out = open_ofstream(prefix / "conda-meta" / (pkg.str() + ".json")); | ||
| out << R"({ | ||
| "name": "test_pkg", | ||
| "version": "1.0", | ||
| "build_string": "0", | ||
| "paths_data": { "paths": [], "paths_version": 1 } | ||
| })"; | ||
| } | ||
| // Prepare paths.json for linking | ||
| { | ||
| fs::create_directories(pkg_source / "info"); | ||
| auto out = open_ofstream(pkg_source / "info" / "paths.json"); | ||
| out << R"({ "paths": [], "paths_version": 1 })"; | ||
| } | ||
| // Prepare repodata_record.json for linking | ||
| { | ||
| auto out = open_ofstream(pkg_source / "info" / "repodata_record.json"); | ||
| out << R"({ "noarch": null })"; | ||
| } | ||
|
|
||
| SECTION("linking executes pre-link and post-link") | ||
| { | ||
| auto tx_context = make_tx_context(); | ||
| LinkPackage link_pkg(pkg, cache_dir, &tx_context); | ||
|
|
||
| REQUIRE(link_pkg.execute()); | ||
|
|
||
| REQUIRE(fs::exists(pre_link_marker)); | ||
| REQUIRE(fs::exists(post_link_marker)); | ||
| } | ||
|
|
||
| SECTION("unlinking executes pre-unlink and post-unlink") | ||
| { | ||
| auto tx_context = make_tx_context(); | ||
| UnlinkPackage unlink_pkg(pkg, cache_dir, &tx_context); | ||
|
|
||
| REQUIRE(unlink_pkg.execute()); | ||
|
|
||
| REQUIRE(fs::exists(pre_unlink_marker)); | ||
| // post-unlink script should not be executed as deprecated | ||
| REQUIRE(!fs::exists(post_unlink_marker)); | ||
| } | ||
| } | ||
| } | ||
| } // namespace mamba | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.