From e6854a774873e9d28f20d37b76192a96db413de6 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 11 Jul 2023 22:05:09 +0200 Subject: [PATCH] Propagate RunEnvironmentInfo --- appimage/appimage.bzl | 6 +++++- tests/BUILD | 26 +++++++++++++++++++++++++- tests/test.cc | 23 +++++++++++++++++++++++ tests/test.py | 9 +++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/test.cc diff --git a/appimage/appimage.bzl b/appimage/appimage.bzl index a81b3a9..c6843c8 100644 --- a/appimage/appimage.bzl +++ b/appimage/appimage.bzl @@ -33,13 +33,17 @@ def _appimage_impl(ctx): tools = tools, ) + env = ctx.attr.env + if RunEnvironmentInfo in ctx.attr.binary: + env.update(ctx.attr.binary[RunEnvironmentInfo].environment) + return [ DefaultInfo( executable = ctx.outputs.executable, files = depset([ctx.outputs.executable]), runfiles = ctx.runfiles(files = [ctx.outputs.executable]), ), - testing.TestEnvironment(ctx.attr.env), + RunEnvironmentInfo(env), ] _ATTRS = { diff --git a/tests/BUILD b/tests/BUILD index a08976b..e92c648 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -27,6 +27,23 @@ appimage_test( tags = ["requires-fakeroot"], # This helps tests failing with `fusermount3: mount failed: Operation not permitted` ) +cc_binary( + name = "test_cc", + srcs = ["test.cc"], + env = { + "MY_BINARY_ENV": "not lost", + "MY_APPIMAGE_ENV": "original", + }, +) + +appimage_test( + name = "appimage_test_cc", + size = "small", + binary = ":test_cc", + env = {"MY_APPIMAGE_ENV": "overwritten"}, # environment variables are propagated from the binary target env attr + tags = ["requires-fakeroot"], +) + py_binary( name = "test_py", srcs = ["test.py"], @@ -36,6 +53,10 @@ py_binary( ":external_bin.appimage", ":symlink_and_emptyfile", ], + env = { + "MY_BINARY_ENV": "not lost", + "MY_APPIMAGE_ENV": "original", + }, main = "test.py", ) @@ -43,7 +64,10 @@ appimage_test( name = "appimage_test_py", size = "small", binary = ":test_py", - env = {"APPIMAGE_EXTRACT_AND_RUN": "1"}, # Another way to run if no libfuse2 is available + env = { + "APPIMAGE_EXTRACT_AND_RUN": "1", # Another way to run if no libfuse2 is available + "MY_APPIMAGE_ENV": "overwritten", + }, ) appimage( diff --git a/tests/test.cc b/tests/test.cc new file mode 100644 index 0000000..4616342 --- /dev/null +++ b/tests/test.cc @@ -0,0 +1,23 @@ +#include +#include +#include + +int main(int argc, char** argv, char** envp) { + // Go through the environment variables and find the one we set in the BUILD. + // When running inside the appimage, we want the env to not be lost. + bool have_binary_env = false; + bool have_appimage_env = false; + for (char** env = envp; *env != 0; env++) { + char* thisEnv = *env; + std::cout << thisEnv << std::endl; + if (std::string(thisEnv) == "MY_BINARY_ENV=not lost") { + have_binary_env = true; + } else if (std::string(thisEnv) == "MY_APPIMAGE_ENV=overwritten") { + have_appimage_env = true; + } + } + if (have_binary_env && have_appimage_env) { + return EXIT_SUCCESS; + } + return EXIT_FAILURE; +} diff --git a/tests/test.py b/tests/test.py index 1198300..35a8270 100644 --- a/tests/test.py +++ b/tests/test.py @@ -69,6 +69,14 @@ def test_runfiles_symlinks() -> None: assert runfiles_symlink.resolve().is_file() +def test_binary_env() -> None: + """Test that env attr on the binary target is handled.""" + # Unfortunately rules_python does not seem to set the RunEnvironmentInfo provider. + # See https://github.com/bazelbuild/rules_python/issues/901 + assert "MY_BINARY_ENV" not in os.environ + # assert os.environ["MY_BINARY_ENV"] == "not lost" + + def greeter() -> None: """Greet the user.""" parser = argparse.ArgumentParser() @@ -82,4 +90,5 @@ def greeter() -> None: test_external_bin() test_symlinks() test_runfiles_symlinks() + test_binary_env() greeter()