Skip to content

Commit

Permalink
Revert "remove CodeTaskFactory tests to see if Windows Core and Windo…
Browse files Browse the repository at this point in the history
…s Full fail or not"

This reverts commit aea4ee7.
  • Loading branch information
surayya-MS committed Feb 16, 2024
1 parent aea4ee7 commit 8021d78
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions src/Tasks.UnitTests/CodeTaskFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Microsoft.Build.UnitTests
#if FEATURE_CODETASKFACTORY

using System.CodeDom.Compiler;
using System.IO.Compression;
using Microsoft.Build.Logging;
using Shouldly;

public sealed class CodeTaskFactoryTests
{
Expand Down Expand Up @@ -1040,6 +1043,164 @@ public override bool Execute()
}
}

[Fact]
public void EmbedsSourceFileInBinlog()
{
string sourceFileContent = @"
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
namespace Microsoft.Build.NonShippingTasks
{
public class LogNameValue_ClassSourcesTest : Task
{
private string variableName;
private string variableValue;
[Required]
public string Name
{
get { return variableName; }
set { variableName = value; }
}
public string Value
{
get { return variableValue; }
set { variableValue = value; }
}
public override bool Execute()
{
// Set the process environment
Log.LogMessage(""Setting {0}={1}"", this.variableName, this.variableValue);
return true;
}
}
}
";

string tempFileDirectory = Path.GetTempPath();
string tempFileName = Guid.NewGuid().ToString() + ".cs";
string tempSourceFile = Path.Combine(tempFileDirectory, tempFileName);
File.WriteAllText(tempSourceFile, sourceFileContent);

try
{
string projectFileContents = @"
<Project ToolsVersion='msbuilddefaulttoolsversion'>
<UsingTask TaskName=`LogNameValue_ClassSourcesTest` TaskFactory=`CodeTaskFactory` AssemblyFile=`$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll`>
<ParameterGroup>
<Name ParameterType='System.String' />
<Value ParameterType='System.String' />
</ParameterGroup>
<Task>
<Code Source='" + tempSourceFile + @"'/>
</Task>
</UsingTask>
<Target Name=`Build`>
<LogNameValue_ClassSourcesTest Name='MyName' Value='MyValue'/>
</Target>
</Project>";

string binaryLogFile = Path.Combine(tempFileDirectory, "output.binlog");
var binaryLogger = new BinaryLogger()
{
Parameters = $"LogFile={binaryLogFile}",
CollectProjectImports = BinaryLogger.ProjectImportsCollectionMode.ZipFile,
};

Helpers.BuildProjectWithNewOMAndBinaryLogger(projectFileContents, binaryLogger, out bool result);

result.ShouldBeTrue();

string projectImportsZipPath = Path.ChangeExtension(binaryLogFile, ".ProjectImports.zip");
using var fileStream = new FileStream(projectImportsZipPath, FileMode.Open);
using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read);

// Can't just compare `Name` because `ZipArchive` does not handle unix directory separators well
// thus producing garbled fully qualified paths in the actual .ProjectImports.zip entries
zipArchive.Entries.ShouldContain(zE => zE.Name.EndsWith(tempFileName),
"");
}
finally
{
if (File.Exists(tempSourceFile))
{
File.Delete(tempSourceFile);
}
}
}

[Fact]
public void EmbedsSourceFileInBinlogWhenFailsToCompile()
{
string sourceFileContentThatFailsToCompile = @"
namespace Microsoft.Build.NonShippingTasks
{
public class LogNameValue_ClassSourcesTest : Task
{
private string
";

string tempFileDirectory = Path.GetTempPath();
string tempFileName = Guid.NewGuid().ToString() + ".cs";
string tempSourceFile = Path.Combine(tempFileDirectory, tempFileName);
File.WriteAllText(tempSourceFile, sourceFileContentThatFailsToCompile);

try
{
string projectFileContents = @"
<Project ToolsVersion='msbuilddefaulttoolsversion'>
<UsingTask TaskName=`LogNameValue_ClassSourcesTest` TaskFactory=`CodeTaskFactory` AssemblyFile=`$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll`>
<ParameterGroup>
<Name ParameterType='System.String' />
<Value ParameterType='System.String' />
</ParameterGroup>
<Task>
<Code Source='" + tempSourceFile + @"'/>
</Task>
</UsingTask>
<Target Name=`Build`>
<LogNameValue_ClassSourcesTest Name='MyName' Value='MyValue'/>
</Target>
</Project>";

string binaryLogFile = Path.Combine(tempFileDirectory, "output.binlog");
var binaryLogger = new BinaryLogger()
{
Parameters = $"LogFile={binaryLogFile}",
CollectProjectImports = BinaryLogger.ProjectImportsCollectionMode.ZipFile,
};

Helpers.BuildProjectWithNewOMAndBinaryLogger(projectFileContents, binaryLogger, out bool result);

result.ShouldBeFalse();

string projectImportsZipPath = Path.ChangeExtension(binaryLogFile, ".ProjectImports.zip");
using var fileStream = new FileStream(projectImportsZipPath, FileMode.Open);
using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read);

// Can't just compare `Name` because `ZipArchive` does not handle unix directory separators well
// thus producing garbled fully qualified paths in the actual .ProjectImports.zip entries
zipArchive.Entries.ShouldContain(zE => zE.Name.EndsWith(tempFileName),
"");
}
finally
{
if (File.Exists(tempSourceFile))
{
File.Delete(tempSourceFile);
}
}
}

/// <summary>
/// Code factory test where the TMP directory does not exist.
/// See https://github.com/dotnet/msbuild/issues/328 for details.
Expand Down

0 comments on commit 8021d78

Please sign in to comment.