authorgravatar for cbilz@posteo.netConstantin Bilz <cbilz@posteo.net> 2025-02-06 15:16:31+01:00
committergravatar for cbilz@posteo.netConstantin Bilz <cbilz@posteo.net> 2025-02-21 15:22:39+01:00
logaf2b6893e2f8629ba191ea9503ca63ea72e0d35d
tree1c31c726beb179087275174a2e48f583d21e4f30
parent62e388e73e71ed70bb5f059796035be6f59e72b3

std.Build.Step.ConfigHeader: Add tests for Autoconf `@FOO@` variables


1 files changed, 205 insertions(+), 74 deletions(-)

lib/std/Build/Step/ConfigHeader.zig+205-74
......@@ -782,7 +782,26 @@ fn expand_variables_cmake(
782782 return result.toOwnedSlice();
783783}
784784
785fn testReplaceVariables(
785fn testReplaceVariablesAutoconfAt(
786 allocator: Allocator,
787 contents: []const u8,
788 expected: []const u8,
789 values: std.StringArrayHashMap(Value),
790) !void {
791 var output = std.ArrayList(u8).init(allocator);
792 defer output.deinit();
793
794 const used = try allocator.alloc(bool, values.count());
795 for (used) |*u| u.* = false;
796 defer allocator.free(used);
797
798 try expand_variables_autoconf_at(&output, contents, values, used);
799
800 for (used) |u| if (!u) return error.UnusedValue;
801 try std.testing.expectEqualStrings(expected, output.items);
802}
803
804fn testReplaceVariablesCMake(
786805 allocator: Allocator,
787806 contents: []const u8,
788807 expected: []const u8,
......@@ -794,6 +813,118 @@ fn testReplaceVariables(
794813 try std.testing.expectEqualStrings(expected, actual);
795814}
796815
816test "expand_variables_autoconf_at simple cases" {
817 const allocator = std.testing.allocator;
818 var values = std.StringArrayHashMap(Value).init(allocator);
819 defer values.deinit();
820
821 // empty strings are preserved
822 try testReplaceVariablesAutoconfAt(allocator, "", "", values);
823
824 // line with misc content is preserved
825 try testReplaceVariablesAutoconfAt(allocator, "no substitution", "no substitution", values);
826
827 // empty @ sigils are preserved
828 try testReplaceVariablesAutoconfAt(allocator, "@", "@", values);
829 try testReplaceVariablesAutoconfAt(allocator, "@@", "@@", values);
830 try testReplaceVariablesAutoconfAt(allocator, "@@@", "@@@", values);
831 try testReplaceVariablesAutoconfAt(allocator, "@@@@", "@@@@", values);
832
833 // simple substitution
834 try values.putNoClobber("undef", .undef);
835 try testReplaceVariablesAutoconfAt(allocator, "@undef@", "", values);
836 values.clearRetainingCapacity();
837
838 try values.putNoClobber("defined", .defined);
839 try testReplaceVariablesAutoconfAt(allocator, "@defined@", "", values);
840 values.clearRetainingCapacity();
841
842 try values.putNoClobber("true", Value{ .boolean = true });
843 try testReplaceVariablesAutoconfAt(allocator, "@true@", "1", values);
844 values.clearRetainingCapacity();
845
846 try values.putNoClobber("false", Value{ .boolean = false });
847 try testReplaceVariablesAutoconfAt(allocator, "@false@", "0", values);
848 values.clearRetainingCapacity();
849
850 try values.putNoClobber("int", Value{ .int = 42 });
851 try testReplaceVariablesAutoconfAt(allocator, "@int@", "42", values);
852 values.clearRetainingCapacity();
853
854 try values.putNoClobber("ident", Value{ .string = "value" });
855 try testReplaceVariablesAutoconfAt(allocator, "@ident@", "value", values);
856 values.clearRetainingCapacity();
857
858 try values.putNoClobber("string", Value{ .string = "text" });
859 try testReplaceVariablesAutoconfAt(allocator, "@string@", "text", values);
860 values.clearRetainingCapacity();
861
862 // double packed substitution
863 try values.putNoClobber("string", Value{ .string = "text" });
864 try testReplaceVariablesAutoconfAt(allocator, "@string@@string@", "texttext", values);
865 values.clearRetainingCapacity();
866
867 // triple packed substitution
868 try values.putNoClobber("int", Value{ .int = 42 });
869 try values.putNoClobber("string", Value{ .string = "text" });
870 try testReplaceVariablesAutoconfAt(allocator, "@string@@int@@string@", "text42text", values);
871 values.clearRetainingCapacity();
872
873 // double separated substitution
874 try values.putNoClobber("int", Value{ .int = 42 });
875 try testReplaceVariablesAutoconfAt(allocator, "@int@.@int@", "42.42", values);
876 values.clearRetainingCapacity();
877
878 // triple separated substitution
879 try values.putNoClobber("true", Value{ .boolean = true });
880 try values.putNoClobber("int", Value{ .int = 42 });
881 try testReplaceVariablesAutoconfAt(allocator, "@int@.@true@.@int@", "42.1.42", values);
882 values.clearRetainingCapacity();
883
884 // misc prefix is preserved
885 try values.putNoClobber("false", Value{ .boolean = false });
886 try testReplaceVariablesAutoconfAt(allocator, "false is @false@", "false is 0", values);
887 values.clearRetainingCapacity();
888
889 // misc suffix is preserved
890 try values.putNoClobber("true", Value{ .boolean = true });
891 try testReplaceVariablesAutoconfAt(allocator, "@true@ is true", "1 is true", values);
892 values.clearRetainingCapacity();
893
894 // surrounding content is preserved
895 try values.putNoClobber("int", Value{ .int = 42 });
896 try testReplaceVariablesAutoconfAt(allocator, "what is 6*7? @int@!", "what is 6*7? 42!", values);
897 values.clearRetainingCapacity();
898
899 // incomplete key is preserved
900 try testReplaceVariablesAutoconfAt(allocator, "@undef", "@undef", values);
901
902 // unknown key leads to an error
903 try std.testing.expectError(error.MissingValue, testReplaceVariablesAutoconfAt(allocator, "@bad@", "", values));
904
905 // unused key leads to an error
906 try values.putNoClobber("int", Value{ .int = 42 });
907 try values.putNoClobber("false", Value{ .boolean = false });
908 try std.testing.expectError(error.UnusedValue, testReplaceVariablesAutoconfAt(allocator, "@int", "", values));
909 values.clearRetainingCapacity();
910}
911
912test "expand_variables_autoconf_at edge cases" {
913 const allocator = std.testing.allocator;
914 var values = std.StringArrayHashMap(Value).init(allocator);
915 defer values.deinit();
916
917 // @-vars resolved only when they wrap valid characters, otherwise considered literals
918 try values.putNoClobber("string", Value{ .string = "text" });
919 try testReplaceVariablesAutoconfAt(allocator, "@@string@@", "@text@", values);
920 values.clearRetainingCapacity();
921
922 // expanded variables are considered strings after expansion
923 try values.putNoClobber("string_at", Value{ .string = "@string@" });
924 try testReplaceVariablesAutoconfAt(allocator, "@string_at@", "@string@", values);
925 values.clearRetainingCapacity();
926}
927
797928test "expand_variables_cmake simple cases" {
798929 const allocator = std.testing.allocator;
799930 var values = std.StringArrayHashMap(Value).init(allocator);
......@@ -808,78 +939,78 @@ test "expand_variables_cmake simple cases" {
808939 try values.putNoClobber("string", Value{ .string = "text" });
809940
810941 // empty strings are preserved
811 try testReplaceVariables(allocator, "", "", values);
942 try testReplaceVariablesCMake(allocator, "", "", values);
812943
813944 // line with misc content is preserved
814 try testReplaceVariables(allocator, "no substitution", "no substitution", values);
945 try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
815946
816947 // empty ${} wrapper leads to an error
817 try std.testing.expectError(error.MissingKey, testReplaceVariables(allocator, "${}", "", values));
948 try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values));
818949
819950 // empty @ sigils are preserved
820 try testReplaceVariables(allocator, "@", "@", values);
821 try testReplaceVariables(allocator, "@@", "@@", values);
822 try testReplaceVariables(allocator, "@@@", "@@@", values);
823 try testReplaceVariables(allocator, "@@@@", "@@@@", values);
951 try testReplaceVariablesCMake(allocator, "@", "@", values);
952 try testReplaceVariablesCMake(allocator, "@@", "@@", values);
953 try testReplaceVariablesCMake(allocator, "@@@", "@@@", values);
954 try testReplaceVariablesCMake(allocator, "@@@@", "@@@@", values);
824955
825956 // simple substitution
826 try testReplaceVariables(allocator, "@undef@", "", values);
827 try testReplaceVariables(allocator, "${undef}", "", values);
828 try testReplaceVariables(allocator, "@defined@", "", values);
829 try testReplaceVariables(allocator, "${defined}", "", values);
830 try testReplaceVariables(allocator, "@true@", "1", values);
831 try testReplaceVariables(allocator, "${true}", "1", values);
832 try testReplaceVariables(allocator, "@false@", "0", values);
833 try testReplaceVariables(allocator, "${false}", "0", values);
834 try testReplaceVariables(allocator, "@int@", "42", values);
835 try testReplaceVariables(allocator, "${int}", "42", values);
836 try testReplaceVariables(allocator, "@ident@", "value", values);
837 try testReplaceVariables(allocator, "${ident}", "value", values);
838 try testReplaceVariables(allocator, "@string@", "text", values);
839 try testReplaceVariables(allocator, "${string}", "text", values);
957 try testReplaceVariablesCMake(allocator, "@undef@", "", values);
958 try testReplaceVariablesCMake(allocator, "${undef}", "", values);
959 try testReplaceVariablesCMake(allocator, "@defined@", "", values);
960 try testReplaceVariablesCMake(allocator, "${defined}", "", values);
961 try testReplaceVariablesCMake(allocator, "@true@", "1", values);
962 try testReplaceVariablesCMake(allocator, "${true}", "1", values);
963 try testReplaceVariablesCMake(allocator, "@false@", "0", values);
964 try testReplaceVariablesCMake(allocator, "${false}", "0", values);
965 try testReplaceVariablesCMake(allocator, "@int@", "42", values);
966 try testReplaceVariablesCMake(allocator, "${int}", "42", values);
967 try testReplaceVariablesCMake(allocator, "@ident@", "value", values);
968 try testReplaceVariablesCMake(allocator, "${ident}", "value", values);
969 try testReplaceVariablesCMake(allocator, "@string@", "text", values);
970 try testReplaceVariablesCMake(allocator, "${string}", "text", values);
840971
841972 // double packed substitution
842 try testReplaceVariables(allocator, "@string@@string@", "texttext", values);
843 try testReplaceVariables(allocator, "${string}${string}", "texttext", values);
973 try testReplaceVariablesCMake(allocator, "@string@@string@", "texttext", values);
974 try testReplaceVariablesCMake(allocator, "${string}${string}", "texttext", values);
844975
845976 // triple packed substitution
846 try testReplaceVariables(allocator, "@string@@int@@string@", "text42text", values);
847 try testReplaceVariables(allocator, "@string@${int}@string@", "text42text", values);
848 try testReplaceVariables(allocator, "${string}@int@${string}", "text42text", values);
849 try testReplaceVariables(allocator, "${string}${int}${string}", "text42text", values);
977 try testReplaceVariablesCMake(allocator, "@string@@int@@string@", "text42text", values);
978 try testReplaceVariablesCMake(allocator, "@string@${int}@string@", "text42text", values);
979 try testReplaceVariablesCMake(allocator, "${string}@int@${string}", "text42text", values);
980 try testReplaceVariablesCMake(allocator, "${string}${int}${string}", "text42text", values);
850981
851982 // double separated substitution
852 try testReplaceVariables(allocator, "@int@.@int@", "42.42", values);
853 try testReplaceVariables(allocator, "${int}.${int}", "42.42", values);
983 try testReplaceVariablesCMake(allocator, "@int@.@int@", "42.42", values);
984 try testReplaceVariablesCMake(allocator, "${int}.${int}", "42.42", values);
854985
855986 // triple separated substitution
856 try testReplaceVariables(allocator, "@int@.@true@.@int@", "42.1.42", values);
857 try testReplaceVariables(allocator, "@int@.${true}.@int@", "42.1.42", values);
858 try testReplaceVariables(allocator, "${int}.@true@.${int}", "42.1.42", values);
859 try testReplaceVariables(allocator, "${int}.${true}.${int}", "42.1.42", values);
987 try testReplaceVariablesCMake(allocator, "@int@.@true@.@int@", "42.1.42", values);
988 try testReplaceVariablesCMake(allocator, "@int@.${true}.@int@", "42.1.42", values);
989 try testReplaceVariablesCMake(allocator, "${int}.@true@.${int}", "42.1.42", values);
990 try testReplaceVariablesCMake(allocator, "${int}.${true}.${int}", "42.1.42", values);
860991
861992 // misc prefix is preserved
862 try testReplaceVariables(allocator, "false is @false@", "false is 0", values);
863 try testReplaceVariables(allocator, "false is ${false}", "false is 0", values);
993 try testReplaceVariablesCMake(allocator, "false is @false@", "false is 0", values);
994 try testReplaceVariablesCMake(allocator, "false is ${false}", "false is 0", values);
864995
865996 // misc suffix is preserved
866 try testReplaceVariables(allocator, "@true@ is true", "1 is true", values);
867 try testReplaceVariables(allocator, "${true} is true", "1 is true", values);
997 try testReplaceVariablesCMake(allocator, "@true@ is true", "1 is true", values);
998 try testReplaceVariablesCMake(allocator, "${true} is true", "1 is true", values);
868999
8691000 // surrounding content is preserved
870 try testReplaceVariables(allocator, "what is 6*7? @int@!", "what is 6*7? 42!", values);
871 try testReplaceVariables(allocator, "what is 6*7? ${int}!", "what is 6*7? 42!", values);
1001 try testReplaceVariablesCMake(allocator, "what is 6*7? @int@!", "what is 6*7? 42!", values);
1002 try testReplaceVariablesCMake(allocator, "what is 6*7? ${int}!", "what is 6*7? 42!", values);
8721003
8731004 // incomplete key is preserved
874 try testReplaceVariables(allocator, "@undef", "@undef", values);
875 try testReplaceVariables(allocator, "${undef", "${undef", values);
876 try testReplaceVariables(allocator, "{undef}", "{undef}", values);
877 try testReplaceVariables(allocator, "undef@", "undef@", values);
878 try testReplaceVariables(allocator, "undef}", "undef}", values);
1005 try testReplaceVariablesCMake(allocator, "@undef", "@undef", values);
1006 try testReplaceVariablesCMake(allocator, "${undef", "${undef", values);
1007 try testReplaceVariablesCMake(allocator, "{undef}", "{undef}", values);
1008 try testReplaceVariablesCMake(allocator, "undef@", "undef@", values);
1009 try testReplaceVariablesCMake(allocator, "undef}", "undef}", values);
8791010
8801011 // unknown key leads to an error
881 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@bad@", "", values));
882 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${bad}", "", values));
1012 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@bad@", "", values));
1013 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${bad}", "", values));
8831014}
8841015
8851016test "expand_variables_cmake edge cases" {
......@@ -906,41 +1037,41 @@ test "expand_variables_cmake edge cases" {
9061037 try values.putNoClobber("nest_proxy", Value{ .string = "nest_underscore_proxy" });
9071038
9081039 // @-vars resolved only when they wrap valid characters, otherwise considered literals
909 try testReplaceVariables(allocator, "@@string@@", "@text@", values);
910 try testReplaceVariables(allocator, "@${string}@", "@text@", values);
1040 try testReplaceVariablesCMake(allocator, "@@string@@", "@text@", values);
1041 try testReplaceVariablesCMake(allocator, "@${string}@", "@text@", values);
9111042
9121043 // @-vars are resolved inside ${}-vars
913 try testReplaceVariables(allocator, "${@string_proxy@}", "text", values);
1044 try testReplaceVariablesCMake(allocator, "${@string_proxy@}", "text", values);
9141045
9151046 // expanded variables are considered strings after expansion
916 try testReplaceVariables(allocator, "@string_at@", "@string@", values);
917 try testReplaceVariables(allocator, "${string_at}", "@string@", values);
918 try testReplaceVariables(allocator, "$@string_curly@", "${string}", values);
919 try testReplaceVariables(allocator, "$${string_curly}", "${string}", values);
920 try testReplaceVariables(allocator, "${string_var}", "${string}", values);
921 try testReplaceVariables(allocator, "@string_var@", "${string}", values);
922 try testReplaceVariables(allocator, "${dollar}{${string}}", "${text}", values);
923 try testReplaceVariables(allocator, "@dollar@{${string}}", "${text}", values);
924 try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values);
1047 try testReplaceVariablesCMake(allocator, "@string_at@", "@string@", values);
1048 try testReplaceVariablesCMake(allocator, "${string_at}", "@string@", values);
1049 try testReplaceVariablesCMake(allocator, "$@string_curly@", "${string}", values);
1050 try testReplaceVariablesCMake(allocator, "$${string_curly}", "${string}", values);
1051 try testReplaceVariablesCMake(allocator, "${string_var}", "${string}", values);
1052 try testReplaceVariablesCMake(allocator, "@string_var@", "${string}", values);
1053 try testReplaceVariablesCMake(allocator, "${dollar}{${string}}", "${text}", values);
1054 try testReplaceVariablesCMake(allocator, "@dollar@{${string}}", "${text}", values);
1055 try testReplaceVariablesCMake(allocator, "@dollar@{@string@}", "${text}", values);
9251056
9261057 // when expanded variables contain invalid characters, they prevent further expansion
927 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${${string_var}}", "", values));
928 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${@string_var@}", "", values));
1058 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${${string_var}}", "", values));
1059 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${@string_var@}", "", values));
9291060
9301061 // nested expanded variables are expanded from the inside out
931 try testReplaceVariables(allocator, "${string${underscore}proxy}", "string", values);
932 try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values);
1062 try testReplaceVariablesCMake(allocator, "${string${underscore}proxy}", "string", values);
1063 try testReplaceVariablesCMake(allocator, "${string@underscore@proxy}", "string", values);
9331064
9341065 // nested vars are only expanded when ${} is closed
935 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@underscore@proxy@", "", values));
936 try testReplaceVariables(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values);
937 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values));
938 try testReplaceVariables(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
1066 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@underscore@proxy@", "", values));
1067 try testReplaceVariablesCMake(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values);
1068 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values));
1069 try testReplaceVariablesCMake(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
9391070
9401071 // invalid characters lead to an error
941 try std.testing.expectError(error.InvalidCharacter, testReplaceVariables(allocator, "${str*ing}", "", values));
942 try std.testing.expectError(error.InvalidCharacter, testReplaceVariables(allocator, "${str$ing}", "", values));
943 try std.testing.expectError(error.InvalidCharacter, testReplaceVariables(allocator, "${str@ing}", "", values));
1072 try std.testing.expectError(error.InvalidCharacter, testReplaceVariablesCMake(allocator, "${str*ing}", "", values));
1073 try std.testing.expectError(error.InvalidCharacter, testReplaceVariablesCMake(allocator, "${str$ing}", "", values));
1074 try std.testing.expectError(error.InvalidCharacter, testReplaceVariablesCMake(allocator, "${str@ing}", "", values));
9441075}
9451076
9461077test "expand_variables_cmake escaped characters" {
......@@ -951,14 +1082,14 @@ test "expand_variables_cmake escaped characters" {
9511082 try values.putNoClobber("string", Value{ .string = "text" });
9521083
9531084 // backslash is an invalid character for @ lookup
954 try testReplaceVariables(allocator, "\\@string\\@", "\\@string\\@", values);
1085 try testReplaceVariablesCMake(allocator, "\\@string\\@", "\\@string\\@", values);
9551086
9561087 // backslash is preserved, but doesn't affect ${} variable expansion
957 try testReplaceVariables(allocator, "\\${string}", "\\text", values);
1088 try testReplaceVariablesCMake(allocator, "\\${string}", "\\text", values);
9581089
9591090 // backslash breaks ${} opening bracket identification
960 try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values);
1091 try testReplaceVariablesCMake(allocator, "$\\{string}", "$\\{string}", values);
9611092
9621093 // backslash is skipped when checking for invalid characters, yet it mangles the key
963 try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${string\\}", "", values));
1094 try std.testing.expectError(error.MissingValue, testReplaceVariablesCMake(allocator, "${string\\}", "", values));
9641095}