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