@joebigelow / wix / commits / b6c0360f

Evaluate standalone variables for truthiness in the preprocessor

Fixes 4770

Rob Mensching committed Mar 2, 2022 at 14:00 UTC b6c0360f825c6f5dd11be04b4087a0a52d51a250
2 files changed +48 -2
src/wix/WixToolset.Core/Preprocessor.cs
+2 -2
@@ -1089,10 +1089,10 @@ namespace WixToolset.Core
1089 else if (operation.Length == 0)
1090 {
1091 // There is no right side of the equation.
1092 - // If the variable was evaluated, it exists, so the expression is true
1092 + // If the variable was evaluated, test to see if it is a "false" value.
1093 if (startsWithVariable)
1094 {
1095 - expressionValue = true;
1095 + expressionValue = !(leftValue == String.Empty || leftValue == "0" || leftValue == "false" || leftValue == "no");
1096 }
1097 else
1098 {
src/wix/test/WixToolsetTest.Core/PreprocessorFixture.cs
+46
@@ -61,6 +61,52 @@ namespace WixToolsetTest.Core
61 WixAssert.CompareLineByLine(expected, actual);
62 }
63
64 + [Fact]
65 + public void CanPreprocessFalsyCondition()
66 + {
67 + var input = String.Join(Environment.NewLine,
68 + "<Wix>",
69 + "<?define A=0?>",
70 + "<?if $(A) ?>",
71 + " <Fragment />",
72 + "<?endif?>",
73 + "</Wix>"
74 + );
75 + var expected = new[]
76 + {
77 + "<Wix />"
78 + };
79 +
80 + var result = PreprocessFromString(input);
81 +
82 + var actual = result.Document.ToString().Split("\r\n");
83 + WixAssert.CompareLineByLine(expected, actual);
84 + }
85 +
86 + [Fact]
87 + public void CanPreprocessTruthyCondition()
88 + {
89 + var input = String.Join(Environment.NewLine,
90 + "<Wix>",
91 + "<?define A=1?>",
92 + "<?if $(A) ?>",
93 + " <Fragment />",
94 + "<?endif?>",
95 + "</Wix>"
96 + );
97 + var expected = new[]
98 + {
99 + "<Wix>",
100 + " <Fragment />",
101 + "</Wix>"
102 + };
103 +
104 + var result = PreprocessFromString(input);
105 +
106 + var actual = result.Document.ToString().Split("\r\n");
107 + WixAssert.CompareLineByLine(expected, actual);
108 + }
109 +
110 private static IPreprocessResult PreprocessFromString(string xml)
111 {
112 using var stringReader = new StringReader(xml);