generated from bazel-contrib/rules-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from aherrmann/env
feat: add `env` and `env_inherit` attributes
- Loading branch information
Showing
16 changed files
with
268 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,29 @@ | ||
load("@bazel_skylib//rules:diff_test.bzl", "diff_test") | ||
load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_test") | ||
|
||
zig_binary( | ||
name = "binary", | ||
env = {"ENV_ATTR": "42"}, | ||
main = "main.zig", | ||
) | ||
|
||
genrule( | ||
name = "output", | ||
outs = ["output.actual"], | ||
cmd = "ENV_GENRULE=21 $(execpath :binary) > $(OUTS)", | ||
tools = [":binary"], | ||
) | ||
|
||
diff_test( | ||
name = "output_test", | ||
size = "small", | ||
file1 = ":output.expected", | ||
file2 = ":output.actual", | ||
) | ||
|
||
zig_test( | ||
name = "test", | ||
size = "small", | ||
env = {"ENV_ATTR": "42"}, | ||
main = "main.zig", | ||
) |
This file contains 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,32 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
|
||
const allocator = arena.allocator(); | ||
|
||
const env_attr: ?[]const u8 = std.process.getEnvVarOwned(allocator, "ENV_ATTR") catch |e| switch (e) { | ||
error.EnvironmentVariableNotFound => null, | ||
else => |e_| return e_, | ||
}; | ||
defer if (env_attr) |value| allocator.free(value); | ||
|
||
const env_genrule: ?[]const u8 = std.process.getEnvVarOwned(allocator, "ENV_GENRULE") catch |e| switch (e) { | ||
error.EnvironmentVariableNotFound => null, | ||
else => |e_| return e_, | ||
}; | ||
defer if (env_genrule) |value| allocator.free(value); | ||
|
||
if (env_attr) |value| | ||
try std.io.getStdOut().writer().print("ENV_ATTR: '{s}'\n", .{value}); | ||
if (env_genrule) |value| | ||
try std.io.getStdOut().writer().print("ENV_GENRULE: '{s}'\n", .{value}); | ||
} | ||
|
||
test "bazel controlled env var" { | ||
const value = try std.process.getEnvVarOwned(std.testing.allocator, "ENV_ATTR"); | ||
defer std.testing.allocator.free(value); | ||
|
||
try std.testing.expectEqualStrings("42", value); | ||
} |
This file contains 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 @@ | ||
ENV_GENRULE: '21' |
This file contains 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 |
---|---|---|
|
@@ -10,5 +10,8 @@ zig_test( | |
], | ||
csrcs = ["main.c"], | ||
data = ["data.txt"], | ||
env = { | ||
"TARGET": "$(TARGET)", | ||
}, | ||
main = "main.zig", | ||
) |
This file contains 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 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 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 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 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.