mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
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:
parent
854774d468
commit
d3645b70d0
1 changed files with 12 additions and 15 deletions
|
|
@ -650,7 +650,7 @@ fn expand_variables_cmake(
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = contents[curr + 1 .. close_pos];
|
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];
|
const missing = contents[source_offset..curr];
|
||||||
try result.appendSlice(missing);
|
try result.appendSlice(missing);
|
||||||
switch (value) {
|
switch (value) {
|
||||||
|
|
@ -705,10 +705,7 @@ fn expand_variables_cmake(
|
||||||
|
|
||||||
const key_start = open_pos.target + open_var.len;
|
const key_start = open_pos.target + open_var.len;
|
||||||
const key = result.items[key_start..];
|
const key = result.items[key_start..];
|
||||||
if (key.len == 0) {
|
const value = values.get(key) orelse .undef;
|
||||||
return error.MissingKey;
|
|
||||||
}
|
|
||||||
const value = values.get(key) orelse return error.MissingValue;
|
|
||||||
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
|
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
|
||||||
switch (value) {
|
switch (value) {
|
||||||
.undef, .defined => {},
|
.undef, .defined => {},
|
||||||
|
|
@ -909,8 +906,8 @@ test "expand_variables_cmake simple cases" {
|
||||||
// line with misc content is preserved
|
// line with misc content is preserved
|
||||||
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
|
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
|
||||||
|
|
||||||
// empty ${} wrapper leads to an error
|
// empty ${} wrapper is removed
|
||||||
try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values));
|
try testReplaceVariablesCMake(allocator, "${}", "", values);
|
||||||
|
|
||||||
// empty @ sigils are preserved
|
// empty @ sigils are preserved
|
||||||
try testReplaceVariablesCMake(allocator, "@", "@", values);
|
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);
|
||||||
try testReplaceVariablesCMake(allocator, "undef}", "undef}", values);
|
try testReplaceVariablesCMake(allocator, "undef}", "undef}", values);
|
||||||
|
|
||||||
// unknown key leads to an error
|
// unknown key is removed
|
||||||
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@bad@", "", values));
|
try testReplaceVariablesCMake(allocator, "@bad@", "", values);
|
||||||
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${bad}", "", values));
|
try testReplaceVariablesCMake(allocator, "${bad}", "", values);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "expand_variables_cmake edge cases" {
|
test "expand_variables_cmake edge cases" {
|
||||||
|
|
@ -1020,17 +1017,17 @@ test "expand_variables_cmake edge cases" {
|
||||||
try testReplaceVariablesCMake(allocator, "@dollar@{@string@}", "${text}", values);
|
try testReplaceVariablesCMake(allocator, "@dollar@{@string@}", "${text}", values);
|
||||||
|
|
||||||
// when expanded variables contain invalid characters, they prevent further expansion
|
// when expanded variables contain invalid characters, they prevent further expansion
|
||||||
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${${string_var}}", "", values));
|
try testReplaceVariablesCMake(allocator, "${${string_var}}", "", values);
|
||||||
try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${@string_var@}", "", values));
|
try testReplaceVariablesCMake(allocator, "${@string_var@}", "", values);
|
||||||
|
|
||||||
// nested expanded variables are expanded from the inside out
|
// 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);
|
||||||
try testReplaceVariablesCMake(allocator, "${string@underscore@proxy}", "string", values);
|
try testReplaceVariablesCMake(allocator, "${string@underscore@proxy}", "string", values);
|
||||||
|
|
||||||
// nested vars are only expanded when ${} is closed
|
// 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 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);
|
try testReplaceVariablesCMake(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
|
||||||
|
|
||||||
// invalid characters lead to an error
|
// invalid characters lead to an error
|
||||||
|
|
@ -1056,5 +1053,5 @@ test "expand_variables_cmake escaped characters" {
|
||||||
try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
|
try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
|
||||||
|
|
||||||
// backslash is skipped when checking for invalid characters, yet it mangles the key
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue