Skip to content

Commit

Permalink
Improve the message when UsingTask Reference Include is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
GangWang01 committed Nov 6, 2024
1 parent c5f2d38 commit a3d59cb
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 15 deletions.
20 changes: 12 additions & 8 deletions src/Tasks.UnitTests/CodeTaskFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,34 @@ public void EmptySource()
/// <summary>
/// Verify we get an error if a reference is missing an include attribute is set but it is empty
/// </summary>
[Fact]
public void EmptyReferenceInclude()
[Theory]
[InlineData("")]
[InlineData("Include=\"\"")]
[InlineData("Include=\" \"")]
public void EmptyReferenceInclude(string includeSetting)
{
string projectFileContents = @"
string taskName = "CustomTaskFromCodeFactory_EmptyReferenceInclude";
string projectFileContents = @$"
<Project ToolsVersion='msbuilddefaulttoolsversion'>
<UsingTask TaskName=`CustomTaskFromCodeFactory_EmptyReferenceInclude` TaskFactory=`CodeTaskFactory` AssemblyFile=`$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll` >
<UsingTask TaskName=`{taskName}` TaskFactory=`CodeTaskFactory` AssemblyFile=`$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll` >
<ParameterGroup>
<Text/>
</ParameterGroup>
<Task>
<Reference/>
<Reference {includeSetting}/>
<Code>
Log.LogMessage(MessageImportance.High, Text);
</Code>
</Task>
</UsingTask>
<Target Name=`Build`>
<CustomTaskFromCodeFactory_EmptyReferenceInclude/>
<{taskName}/>
</Target>
</Project>";

MockLogger mockLogger = Helpers.BuildProjectWithNewOMExpectFailure(projectFileContents, false);
string unformattedMessage = ResourceUtilities.GetResourceString("CodeTaskFactory.AttributeEmpty");
mockLogger.AssertLogContains(String.Format(unformattedMessage, "Include"));
string unformattedMessage = ResourceUtilities.GetResourceString("CodeTaskFactory.AttributeEmptyWithTaskElement");
mockLogger.AssertLogContains(String.Format(unformattedMessage, "Include", "Reference", taskName));
}

/// <summary>
Expand Down
11 changes: 7 additions & 4 deletions src/Tasks.UnitTests/RoslynCodeTaskFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,15 @@ public void EmptyCodeElement()
expectedErrorMessage: "You must specify source code within the Code element or a path to a file containing source code.");
}

[Fact]
public void EmptyIncludeAttributeOnReferenceElement()
[Theory]
[InlineData("")]
[InlineData("Include=\"\"")]
[InlineData("Include=\" \"")]
public void EmptyIncludeAttributeOnReferenceElement(string includeSetting)
{
TryLoadTaskBodyAndExpectFailure(
taskBody: "<Reference Include=\"\" />",
expectedErrorMessage: "The \"Include\" attribute of the <Reference> element has been set but is empty. If the \"Include\" attribute is set it must not be empty.");
taskBody: $"<Reference {includeSetting} />",
expectedErrorMessage: $"The \"Include\" attribute of the <Reference> element in the task \"{TaskName}\" has been set but is empty. Make sure the attribute has a proper value.");
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/CodeTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ private List<string> ExtractReferencedAssemblies()
return null;
}

if (attribute == null || attribute.Value.Length == 0)
if (string.IsNullOrWhiteSpace(attribute?.Value))
{
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmpty", "Include");
_log.LogErrorWithCodeFromResources("CodeTaskFactory.AttributeEmptyWithTaskElement", "Include", "Reference", _nameOfTask);
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Tasks/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,10 @@
<value>MSB3752: The "{0}" attribute of the &lt;{1}&gt; element has been set but is empty. If the "{0}" attribute is set it must not be empty.</value>
<comment>{StrBegin="MSB3752: "}</comment>
</data>
<data name="CodeTaskFactory.AttributeEmptyWithTaskElement" xml:space="preserve">
<value>MSB3752: The "{0}" attribute of the &lt;{1}&gt; element in the task "{2}" has been set but is empty. Make sure the attribute has a proper value.</value>
<comment>{StrBegin="MSB3752: "}</comment>
</data>
<data name="CodeTaskFactory.NeedsITaskInterface" xml:space="preserve">
<value>MSB3753: The task could not be instantiated because it does not implement the ITask interface. Make sure the task implements the Microsoft.Build.Framework.ITask interface.</value>
<comment>{StrBegin="MSB3753: "}</comment>
Expand Down
5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a3d59cb

Please sign in to comment.