Skip to content

Commit

Permalink
tests: Add tests for SOURCE_DATE_EPOCH
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Aug 3, 2023
1 parent 48e5a4a commit 901092c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Environment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ test Environment {
var env3: Environment = .{};
try std.testing.expectEqual(default_value, env3.get(.SOURCE_DATE_EPOCH));
}

test getSourceEpoch {
var env: Environment = .{};
try std.testing.expectEqual(@as(?i64, null), try env.getSourceEpoch(100));

env.set(.SOURCE_DATE_EPOCH, "123");
try std.testing.expectEqual(@as(?i64, 123), try env.getSourceEpoch(123));
try std.testing.expectError(error.InvalidEpoch, env.getSourceEpoch(100));

env.set(.SOURCE_DATE_EPOCH, "-1");
try std.testing.expectError(error.InvalidEpoch, env.getSourceEpoch(100));
}
1 change: 1 addition & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub const CodeGen = @import("CodeGen.zig");
pub const Compilation = @import("Compilation.zig");
pub const Diagnostics = @import("Diagnostics.zig");
pub const Driver = @import("Driver.zig");
pub const Environment = @import("Environment.zig");
pub const Interner = @import("Interner.zig");
pub const Ir = @import("Ir.zig");
pub const Object = @import("Object.zig");
Expand Down
6 changes: 6 additions & 0 deletions test/cases/expanded/source epoch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@



"Jan 1 1970"
"00:00:00"
"Thu Jan 1 00:00:00 1970"
4 changes: 4 additions & 0 deletions test/cases/invalid epoch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//aro-env SOURCE_DATE_EPOCH=abc

#define EXPECTED_ERRORS "error: environment variable SOURCE_DATE_EPOCH must expand to a non-negative integer less than or equal to 253402300799" \

6 changes: 6 additions & 0 deletions test/cases/source epoch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//aro-args -E
//aro-env SOURCE_DATE_EPOCH=0

__DATE__
__TIME__
__TIMESTAMP__
19 changes: 17 additions & 2 deletions test/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const build_options = @import("build_options");
const print = std.debug.print;
const aro = @import("aro");
const Codegen = aro.Codegen;
const Environment = aro.Environment;
const Tree = aro.Tree;
const Token = Tree.Token;
const NodeIndex = Tree.NodeIndex;
Expand All @@ -12,6 +13,7 @@ var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};

/// Returns true if saw -E
fn addCommandLineArgs(comp: *aro.Compilation, file: aro.Source, macro_buf: anytype) !bool {
var only_preprocess = false;
if (std.mem.startsWith(u8, file.buf, "//aro-args")) {
var test_args = std.ArrayList([]const u8).init(comp.gpa);
defer test_args.deinit();
Expand All @@ -22,9 +24,22 @@ fn addCommandLineArgs(comp: *aro.Compilation, file: aro.Source, macro_buf: anyty
var driver: aro.Driver = .{ .comp = comp };
defer driver.deinit();
_ = try driver.parseArgs(std.io.null_writer, macro_buf, test_args.items);
return driver.only_preprocess;
only_preprocess = driver.only_preprocess;
}
return false;
if (std.mem.indexOf(u8, file.buf, "//aro-env")) |idx| {
const buf = file.buf[idx..];
const nl = std.mem.indexOfAny(u8, buf, "\n\r") orelse buf.len;
var it = std.mem.tokenize(u8, buf[0..nl], " ");
while (it.next()) |some| {
var parts = std.mem.splitScalar(u8, some, '=');
const name = parts.next().?;
const val = parts.next() orelse "";
const varname = std.meta.stringToEnum(Environment.Name, name) orelse continue;
comp.environment.set(varname, val);
}
}

return only_preprocess;
}

fn testOne(allocator: std.mem.Allocator, path: []const u8) !void {
Expand Down

0 comments on commit 901092c

Please sign in to comment.