Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for symlink files embedding to binlog #8213

Merged
merged 11 commits into from
Jan 3, 2023
49 changes: 49 additions & 0 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,55 @@ public void BinaryLoggerShouldEmbedFilesViaTaskOutput()
zipArchive.Entries.ShouldContain(zE => zE.Name.EndsWith("testtaskoutputfile.txt"));
}

#if FEATURE_SYMLINK_TARGET
[Fact]
public void BinaryLoggerShouldEmbedSymlinkFilesViaTaskOutput()
{
string testFileName = "foobar.txt";
TransientTestFolder testFolder = _env.DefaultTestDirectory.CreateDirectory("TestDir");
TransientTestFolder testFolder2 = _env.DefaultTestDirectory.CreateDirectory("TestDir2");
TransientTestFile testFile = testFolder.CreateFile(testFileName, string.Join(Environment.NewLine, new[] { "123", "456" }));
string symlinkPath = Path.Combine(testFolder2.Path, testFileName);

File.CreateSymbolicLink(symlinkPath, testFile.Path);

using var buildManager = new BuildManager();
var binaryLogger = new BinaryLogger()
{
Parameters = $"LogFile={_logFile}",
CollectProjectImports = BinaryLogger.ProjectImportsCollectionMode.ZipFile,
};
var testProjectFmt = @"
<Project>
<Target Name=""Build"" Inputs=""{0}"" Outputs=""testtaskoutputfile.txt"">
<ReadLinesFromFile
File=""{0}"" >
<Output
TaskParameter=""Lines""
ItemName=""ItemsFromFile""/>
</ReadLinesFromFile>
<WriteLinesToFile File=""testtaskoutputfile.txt"" Lines=""@(ItemsFromFile);abc;def;ghi""/>
<CreateItem Include=""testtaskoutputfile.txt"">
<Output TaskParameter=""Include"" ItemName=""EmbedInBinlog"" />
</CreateItem>
<CreateItem Include=""{0}"">
<Output TaskParameter=""Include"" ItemName=""EmbedInBinlog"" />
</CreateItem>
</Target>
</Project>";
var testProject = string.Format(testProjectFmt, symlinkPath);
ObjectModelHelpers.BuildProjectExpectSuccess(testProject, binaryLogger);
var projectImportsZipPath = Path.ChangeExtension(_logFile, ".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("testtaskoutputfile.txt"));
zipArchive.Entries.ShouldContain(zE => zE.Name.EndsWith(testFileName));
}
#endif

[Fact]
public void BinaryLoggerShouldNotThrowWhenMetadataCannotBeExpanded()
{
Expand Down
8 changes: 8 additions & 0 deletions src/Build/Logging/BinaryLogger/ProjectImportsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ private void AddFileCore(string filePath)
}

var fileInfo = new FileInfo(filePath);

#if FEATURE_SYMLINK_TARGET
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved
if (fileInfo.Length == 0 && fileInfo.Exists && !string.IsNullOrEmpty(fileInfo.LinkTarget))
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved
{
fileInfo = new FileInfo(fileInfo.LinkTarget);
JanKrivanek marked this conversation as resolved.
Show resolved Hide resolved
}
#endif

if (!fileInfo.Exists || fileInfo.Length == 0)
{
_processedFiles.Add(filePath);
Expand Down