Skip to content

Commit 6d7fc36

Browse files
committed
std.Build.Step.ConfigHeader: handle undefined keys and values correctly
We intend to mimic the behavior of cmake which means we need to handle undefined keys and values the same
1 parent c04be63 commit 6d7fc36

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ fn expand_variables_cmake(
693693
}
694694

695695
const key = contents[curr + 1 .. close_pos];
696-
const value = values.get(key) orelse return error.MissingValue;
696+
const value = values.get(key) orelse .undef;
697697
const missing = contents[source_offset..curr];
698698
try result.appendSlice(missing);
699699
switch (value) {
@@ -748,10 +748,7 @@ fn expand_variables_cmake(
748748

749749
const key_start = open_pos.target + open_var.len;
750750
const key = result.items[key_start..];
751-
if (key.len == 0) {
752-
return error.MissingKey;
753-
}
754-
const value = values.get(key) orelse return error.MissingValue;
751+
const value = values.get(key) orelse .undef;
755752
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
756753
switch (value) {
757754
.undef, .defined => {},
@@ -952,8 +949,8 @@ test "expand_variables_cmake simple cases" {
952949
// line with misc content is preserved
953950
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
954951

955-
// empty ${} wrapper leads to an error
956-
try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values));
952+
// empty ${} wrapper is removed
953+
try testReplaceVariablesCMake(allocator, "${}", "", values);
957954

958955
// empty @ sigils are preserved
959956
try testReplaceVariablesCMake(allocator, "@", "@", values);
@@ -1016,9 +1013,9 @@ test "expand_variables_cmake simple cases" {
10161013
try testReplaceVariablesCMake(allocator, "undef@", "undef@", values);
10171014
try testReplaceVariablesCMake(allocator, "undef}", "undef}", values);
10181015

1019-
// unknown key leads to an error
1020-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@bad@", "", values));
1021-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${bad}", "", values));
1016+
// unknown key is removed
1017+
try testReplaceVariablesCMake(allocator, "@bad@", "", values);
1018+
try testReplaceVariablesCMake(allocator, "${bad}", "", values);
10221019
}
10231020

10241021
test "expand_variables_cmake edge cases" {
@@ -1063,17 +1060,17 @@ test "expand_variables_cmake edge cases" {
10631060
try testReplaceVariablesCMake(allocator, "@dollar@{@string@}", "${text}", values);
10641061

10651062
// when expanded variables contain invalid characters, they prevent further expansion
1066-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${${string_var}}", "", values));
1067-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${@string_var@}", "", values));
1063+
try testReplaceVariablesCMake(allocator, "${${string_var}}", "", values);
1064+
try testReplaceVariablesCMake(allocator, "${@string_var@}", "", values);
10681065

10691066
// nested expanded variables are expanded from the inside out
10701067
try testReplaceVariablesCMake(allocator, "${string${underscore}proxy}", "string", values);
10711068
try testReplaceVariablesCMake(allocator, "${string@underscore@proxy}", "string", values);
10721069

10731070
// nested vars are only expanded when ${} is closed
1074-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@underscore@proxy@", "", values));
1071+
try testReplaceVariablesCMake(allocator, "@nest@underscore@proxy@", "underscore", values);
10751072
try testReplaceVariablesCMake(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values);
1076-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values));
1073+
try testReplaceVariablesCMake(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "underscore", values);
10771074
try testReplaceVariablesCMake(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
10781075

10791076
// invalid characters lead to an error
@@ -1099,5 +1096,5 @@ test "expand_variables_cmake escaped characters" {
10991096
try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
11001097

11011098
// backslash is skipped when checking for invalid characters, yet it mangles the key
1102-
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${string\\}", "", values));
1099+
try testReplaceVariablesCMake(allocator, "${string\\}", "", values);
11031100
}

0 commit comments

Comments
 (0)