| ... | ... | @@ -568,7 +568,7 @@ fn expand_variables_cmake( |
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | const key = contents[curr + 1 .. close_pos]; |
| 571 | | const value = values.get(key) orelse .undef; |
| 571 | const value = values.get(key) orelse return error.MissingValue; |
| 572 | 572 | const missing = contents[source_offset..curr]; |
| 573 | 573 | try result.appendSlice(missing); |
| 574 | 574 | switch (value) { |
| ... | ... | @@ -623,7 +623,10 @@ fn expand_variables_cmake( |
| 623 | 623 | |
| 624 | 624 | const key_start = open_pos.target + open_var.len; |
| 625 | 625 | const key = result.items[key_start..]; |
| 626 | | const value = values.get(key) orelse .undef; |
| 626 | if (key.len == 0) { |
| 627 | return error.MissingKey; |
| 628 | } |
| 629 | const value = values.get(key) orelse return error.MissingValue; |
| 627 | 630 | result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len); |
| 628 | 631 | switch (value) { |
| 629 | 632 | .undef, .defined => {}, |
| ... | ... | @@ -693,8 +696,8 @@ test "expand_variables_cmake simple cases" { |
| 693 | 696 | // line with misc content is preserved |
| 694 | 697 | try testReplaceVariables(allocator, "no substitution", "no substitution", values); |
| 695 | 698 | |
| 696 | | // empty ${} wrapper is removed |
| 697 | | try testReplaceVariables(allocator, "${}", "", values); |
| 699 | // empty ${} wrapper leads to an error |
| 700 | try std.testing.expectError(error.MissingKey, testReplaceVariables(allocator, "${}", "", values)); |
| 698 | 701 | |
| 699 | 702 | // empty @ sigils are preserved |
| 700 | 703 | try testReplaceVariables(allocator, "@", "@", values); |
| ... | ... | @@ -757,9 +760,9 @@ test "expand_variables_cmake simple cases" { |
| 757 | 760 | try testReplaceVariables(allocator, "undef@", "undef@", values); |
| 758 | 761 | try testReplaceVariables(allocator, "undef}", "undef}", values); |
| 759 | 762 | |
| 760 | | // unknown key is removed |
| 761 | | try testReplaceVariables(allocator, "@bad@", "", values); |
| 762 | | try testReplaceVariables(allocator, "${bad}", "", values); |
| 763 | // unknown key leads to an error |
| 764 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@bad@", "", values)); |
| 765 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${bad}", "", values)); |
| 763 | 766 | } |
| 764 | 767 | |
| 765 | 768 | test "expand_variables_cmake edge cases" { |
| ... | ... | @@ -804,17 +807,17 @@ test "expand_variables_cmake edge cases" { |
| 804 | 807 | try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values); |
| 805 | 808 | |
| 806 | 809 | // when expanded variables contain invalid characters, they prevent further expansion |
| 807 | | try testReplaceVariables(allocator, "${${string_var}}", "", values); |
| 808 | | try testReplaceVariables(allocator, "${@string_var@}", "", values); |
| 810 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${${string_var}}", "", values)); |
| 811 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${@string_var@}", "", values)); |
| 809 | 812 | |
| 810 | 813 | // nested expanded variables are expanded from the inside out |
| 811 | 814 | try testReplaceVariables(allocator, "${string${underscore}proxy}", "string", values); |
| 812 | 815 | try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values); |
| 813 | 816 | |
| 814 | 817 | // nested vars are only expanded when ${} is closed |
| 815 | | try testReplaceVariables(allocator, "@nest@underscore@proxy@", "underscore", values); |
| 818 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@underscore@proxy@", "", values)); |
| 816 | 819 | try testReplaceVariables(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values); |
| 817 | | try testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "underscore", values); |
| 820 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values)); |
| 818 | 821 | try testReplaceVariables(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values); |
| 819 | 822 | |
| 820 | 823 | // invalid characters lead to an error |
| ... | ... | @@ -840,5 +843,5 @@ test "expand_variables_cmake escaped characters" { |
| 840 | 843 | try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values); |
| 841 | 844 | |
| 842 | 845 | // backslash is skipped when checking for invalid characters, yet it mangles the key |
| 843 | | try testReplaceVariables(allocator, "${string\\}", "", values); |
| 846 | try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${string\\}", "", values)); |
| 844 | 847 | } |