Skip to content

Commit

Permalink
Propagate RunEnvironmentInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lalten committed Jul 11, 2023
1 parent 3802ffd commit e6854a7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
6 changes: 5 additions & 1 deletion appimage/appimage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
26 changes: 25 additions & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -36,14 +53,21 @@ py_binary(
":external_bin.appimage",
":symlink_and_emptyfile",
],
env = {
"MY_BINARY_ENV": "not lost",
"MY_APPIMAGE_ENV": "original",
},
main = "test.py",
)

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(
Expand Down
23 changes: 23 additions & 0 deletions tests/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cstdlib>
#include <iostream>
#include <string>

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;
}
9 changes: 9 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -82,4 +90,5 @@ def greeter() -> None:
test_external_bin()
test_symlinks()
test_runfiles_symlinks()
test_binary_env()
greeter()

0 comments on commit e6854a7

Please sign in to comment.