Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5261,5 +5261,101 @@ private void VerifyImportTargetRelativePath(string directory, string directory2,
FileUtilities.DeleteWithoutTrailingBackslash(directory, true);
}
}

/// <summary>
/// Tests the PropertyExists condition function that checks if a property is defined vs coerced to empty string
/// </summary>
[Fact]
public void PropertyExistsFunction()
{
string projectContents = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace'>
<PropertyGroup>
<ExistingProperty>SomeValue</ExistingProperty>
<EmptyProperty></EmptyProperty>
<!-- UndefinedProperty is not defined -->
</PropertyGroup>

<PropertyGroup>
<ResultExisting Condition=""PropertyExists('ExistingProperty')"">true</ResultExisting>
<ResultExisting Condition=""!PropertyExists('ExistingProperty')"">false</ResultExisting>

<ResultEmpty Condition=""PropertyExists('EmptyProperty')"">true</ResultEmpty>
<ResultEmpty Condition=""!PropertyExists('EmptyProperty')"">false</ResultEmpty>

<ResultUndefined Condition=""PropertyExists('UndefinedProperty')"">true</ResultUndefined>
<ResultUndefined Condition=""!PropertyExists('UndefinedProperty')"">false</ResultUndefined>
</PropertyGroup>
</Project>");

using ProjectFromString projectFromString = new(projectContents);
Project project = projectFromString.Project;

// Test that PropertyExists returns true for an existing property with a value
string resultExisting = project.GetPropertyValue("ResultExisting");
resultExisting.ShouldBe("true");

// Test that PropertyExists returns true for an existing property with empty value
string resultEmpty = project.GetPropertyValue("ResultEmpty");
resultEmpty.ShouldBe("true");

// Test that PropertyExists returns false for an undefined property
string resultUndefined = project.GetPropertyValue("ResultUndefined");
resultUndefined.ShouldBe("false");
}

/// <summary>
/// Tests PropertyExists function with property names that require expansion
/// </summary>
[Fact]
public void PropertyExistsFunctionWithExpansion()
{
string projectContents = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace'>
<PropertyGroup>
<PropertyName>ExistingProperty</PropertyName>
<ExistingProperty>SomeValue</ExistingProperty>
</PropertyGroup>

<PropertyGroup>
<Result Condition=""PropertyExists('$(PropertyName)')"">true</Result>
<Result Condition=""!PropertyExists('$(PropertyName)')"">false</Result>
</PropertyGroup>
</Project>");

using ProjectFromString projectFromString = new(projectContents);
Project project = projectFromString.Project;

// Test that PropertyExists works with expanded property names
string result = project.GetPropertyValue("Result");
result.ShouldBe("true");
}

/// <summary>
/// Tests PropertyExists function with invalid arguments
/// </summary>
[Fact]
public void PropertyExistsFunctionInvalidArguments()
{
// Test with no arguments
string projectContents1 = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace'>
<PropertyGroup Condition=""PropertyExists()"">
<ShouldNotBeSet>true</ShouldNotBeSet>
</PropertyGroup>
</Project>");

Should.Throw<InvalidProjectFileException>(() => new ProjectFromString(projectContents1));

// Test with too many arguments
string projectContents2 = ObjectModelHelpers.CleanupFileContents(@"
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns='msbuildnamespace'>
<PropertyGroup Condition=""PropertyExists('Prop1', 'Prop2')"">
<ShouldNotBeSet>true</ShouldNotBeSet>
</PropertyGroup>
</Project>");

Should.Throw<InvalidProjectFileException>(() => new ProjectFromString(projectContents2));
}
}
}
15 changes: 15 additions & 0 deletions src/Build/Evaluation/ConditionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ internal interface IConditionEvaluationState
/// </summary>
string ExpandIntoString(string expression);

/// <summary>
/// Checks whether a property exists (vs. being coerced to empty string).
/// </summary>
bool PropertyExists(string propertyName);

/// <summary>
/// PRE cache
/// </summary>
Expand Down Expand Up @@ -483,6 +488,16 @@ public string ExpandIntoString(string expression)

return expression;
}

/// <summary>
/// Checks whether a property exists (vs. being coerced to empty string).
/// </summary>
/// <param name="propertyName">The name of the property to check.</param>
/// <returns>True if the property exists, false otherwise.</returns>
public bool PropertyExists(string propertyName)
{
return _expander.PropertyExists(propertyName);
}
}
}
}
11 changes: 11 additions & 0 deletions src/Build/Evaluation/Conditionals/FunctionCallExpressionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ internal override bool BoolEvaluate(ConditionEvaluator.IConditionEvaluationState
return false;
}
}
else if (String.Equals(_functionName, "PropertyExists", StringComparison.OrdinalIgnoreCase))
{
// Check we only have one argument
VerifyArgumentCount(1, state);

// Get the property name from the argument without expanding it (we want the literal property name)
string propertyName = ExpandArgumentForScalarParameter("PropertyExists", _arguments[0], state, isFilePath: false);

// Check if the property exists
return state.PropertyExists(propertyName);
}
// We haven't implemented any other "functions"
else
{
Expand Down
9 changes: 9 additions & 0 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,15 @@ internal PropertiesUseTracker PropertiesUseTracker
set { _propertiesUseTracker = value; }
}

/// <summary>
/// Checks whether a property exists (vs. being coerced to empty string).
/// </summary>
internal bool PropertyExists(string propertyName)
{
P property = _properties.GetProperty(propertyName, 0, propertyName.Length - 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the IPropertyProvider<T>.GetProperty(string name) method, instead of doing the length calculations here?

T GetProperty(string name);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main motivation for this change would be to remove the propertyName.Length - 1 expression that looks like an off-by-one error, even though it isn't.

return property != null;
}

/// <summary>
/// Tests to see if the expression may contain expandable expressions, i.e.
/// contains $, % or @.
Expand Down
Loading