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
This commit is contained in:
Jan200101 2025-05-29 21:39:56 +02:00
parent 854774d468
commit d3645b70d0
No known key found for this signature in database
GPG key ID: 5B71B1D78B882E05

View file

@ -650,7 +650,7 @@ fn expand_variables_cmake(
}
const key = contents[curr + 1 .. close_pos];
const value = values.get(key) orelse return error.MissingValue;
const value = values.get(key) orelse .undef;
const missing = contents[source_offset..curr];
try result.appendSlice(missing);
switch (value) {
@ -705,10 +705,7 @@ fn expand_variables_cmake(
const key_start = open_pos.target + open_var.len;
const key = result.items[key_start..];
if (key.len == 0) {
return error.MissingKey;
}
const value = values.get(key) orelse return error.MissingValue;
const value = values.get(key) orelse .undef;
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
switch (value) {
.undef, .defined => {},
@ -909,8 +906,8 @@ test "expand_variables_cmake simple cases" {
// line with misc content is preserved
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
// empty ${} wrapper leads to an error
try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values));
// empty ${} wrapper is removed
try testReplaceVariablesCMake(allocator, "${}", "", values);
// empty @ sigils are preserved
try testReplaceVariablesCMake(allocator, "@", "@", values);
@ -973,9 +970,9 @@ test "expand_variables_cmake simple cases" {
try testReplaceVariablesCMake(allocator, "undef@", "undef@", values);
try testReplaceVariablesCMake(allocator, "undef}", "undef}", values);
// unknown key leads to an error
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@bad@", "", values));
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${bad}", "", values));
// unknown key is removed
try testReplaceVariablesCMake(allocator, "@bad@", "", values);
try testReplaceVariablesCMake(allocator, "${bad}", "", values);
}
test "expand_variables_cmake edge cases" {
@ -1020,17 +1017,17 @@ test "expand_variables_cmake edge cases" {
try testReplaceVariablesCMake(allocator, "@dollar@{@string@}", "${text}", values);
// when expanded variables contain invalid characters, they prevent further expansion
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${${string_var}}", "", values));
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${@string_var@}", "", values));
try testReplaceVariablesCMake(allocator, "${${string_var}}", "", values);
try testReplaceVariablesCMake(allocator, "${@string_var@}", "", values);
// nested expanded variables are expanded from the inside out
try testReplaceVariablesCMake(allocator, "${string${underscore}proxy}", "string", values);
try testReplaceVariablesCMake(allocator, "${string@underscore@proxy}", "string", values);
// nested vars are only expanded when ${} is closed
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@underscore@proxy@", "", values));
try testReplaceVariablesCMake(allocator, "@nest@underscore@proxy@", "underscore", values);
try testReplaceVariablesCMake(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values);
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values));
try testReplaceVariablesCMake(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "underscore", values);
try testReplaceVariablesCMake(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
// invalid characters lead to an error
@ -1056,5 +1053,5 @@ test "expand_variables_cmake escaped characters" {
try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
// backslash is skipped when checking for invalid characters, yet it mangles the key
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${string\\}", "", values));
try testReplaceVariablesCMake(allocator, "${string\\}", "", values);
}