Skip to content

Commit

Permalink
C++
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Apr 9, 2023
1 parent 75788bb commit 7d8bea7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Copyright (c) .NET Foundation. All rights reserved.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- C++ projects currently do not import Microsoft.NET.Sdk.props. -->
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NET.Sdk.SourceLink.props" Condition="'$(_SourceLinkSdkSubDir)' == ''"/>

<PropertyGroup>
<EmbedUntrackedSources Condition="'$(EmbedUntrackedSources)' == ''">true</EmbedUntrackedSources>
</PropertyGroup>
Expand Down
136 changes: 83 additions & 53 deletions src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ private unsafe void ValidatePdb(string pdbPath, bool expectedEmbeddedSources)
}
}

private static TestAsset Multitarget(TestAsset testAsset, string targetFrameworks)
=> testAsset.WithProjectChanges(p =>
{
var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework");
tfmNode.Name = p.Root.Name.Namespace + "TargetFrameworks";
tfmNode.Value = targetFrameworks;
});

private static TestAsset WithProperties(TestAsset testAsset, params (string key, string value)[] properties)
=> testAsset.WithProjectChanges(p =>
{
var ns = p.Root.Name.Namespace;
var propertyGroup = p.Root.Descendants(ns + "PropertyGroup").First();
foreach (var (key, value) in properties)
{
propertyGroup.Add(new XElement(ns + key, value));
}
});

private static TestAsset WithItems(TestAsset testAsset, params (string key, XAttribute[] attributes)[] items)
=> testAsset.WithProjectChanges(p =>
{
var ns = p.Root.Name.Namespace;
var itemGroup = new XElement(ns + "ItemGroup");
p.Root.Add(itemGroup);
foreach (var (key, attributes) in items)
{
itemGroup.Add(new XElement(ns + key, (object[])attributes));
}
});

[Fact]
public void WithNoGitMetadata()
{
Expand Down Expand Up @@ -151,24 +185,16 @@ public void WithRemoteOrigin_UnknownDomain()
[InlineData("https://test.visualstudio.com/org/_git/repo", "https://test.visualstudio.com/org/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=1200000000000000000000000000000000000000&path=/*")]
public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, bool multitarget = false)
{
string targetFrameworks = null;
string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : "");

var testAsset = _testAssetsManager
.CopyTestAsset("SourceLinkTestApp", identifier: origin)
.WithSource()
.WithProjectChanges(p =>
{
var ns = p.Root.Name.Namespace;
var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework");
targetFrameworks = tfmNode.Value;
if (multitarget)
{
tfmNode.Name = ns + "TargetFrameworks";
targetFrameworks += ";netstandard2.0";
tfmNode.Value = targetFrameworks;
}
});
.WithSource();

if (multitarget)
{
testAsset = Multitarget(testAsset, targetFrameworks);
}

Assert.NotNull(targetFrameworks);

Expand Down Expand Up @@ -199,29 +225,18 @@ public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, boo
[InlineData(false)]
public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget)
{
string targetFrameworks = null;
string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : "");

var testAsset = _testAssetsManager
.CopyTestAsset("SourceLinkTestApp")
.WithSource()
.WithProjectChanges(p =>
{
var ns = p.Root.Name.Namespace;
var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework");
targetFrameworks = tfmNode.Value;
if (multitarget)
{
tfmNode.Name = ns + "TargetFrameworks";
targetFrameworks += ";netstandard2.0";
tfmNode.Value = targetFrameworks;
}
.WithSource();

var propertyGroup = new XElement(ns + "PropertyGroup");
p.Root.Add(propertyGroup);
testAsset = WithProperties(testAsset, ("SuppressImplicitGitSourceLink", "true"));

propertyGroup.Add(new XElement(ns + "SuppressImplicitGitSourceLink", "true"));
});
if (multitarget)
{
testAsset = Multitarget(testAsset, targetFrameworks);
}

Assert.NotNull(targetFrameworks);

Expand All @@ -246,31 +261,18 @@ public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget)
[InlineData(false)]
public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget)
{
string targetFrameworks = null;
string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : "");

var testAsset = _testAssetsManager
.CopyTestAsset("SourceLinkTestApp")
.WithSource()
.WithProjectChanges(p =>
{
var ns = p.Root.Name.Namespace;
var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework");
targetFrameworks = tfmNode.Value;
if (multitarget)
{
tfmNode.Name = ns + "TargetFrameworks";
targetFrameworks += ";netstandard2.0";
tfmNode.Value = targetFrameworks;
}
.WithSource();

var itemGroup = new XElement(ns + "ItemGroup");
p.Root.Add(itemGroup);
if (multitarget)
{
testAsset = Multitarget(testAsset, targetFrameworks);
}

itemGroup.Add(new XElement(ns + "PackageReference",
new XAttribute("Include", "Microsoft.SourceLink.GitHub"),
new XAttribute("Version", "1.0.0")));
});
testAsset = WithItems(testAsset, ("PackageReference", new[] { new XAttribute("Include", "Microsoft.SourceLink.GitHub"), new XAttribute("Version", "1.0.0") }));

Assert.NotNull(targetFrameworks);

Expand All @@ -292,5 +294,33 @@ public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget)
ValidatePdb(Path.Combine(intermediateDir.FullName, "SourceLinkTestApp.pdb"), expectedEmbeddedSources: false);
}
}

[FullMSBuildOnlyFact]
public void Cpp()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
.WithSource();

testAsset = WithProperties(testAsset, ("EnableManagedpackageReferenceSupport", "true"));

CreateGitFiles(testAsset.Path, "https://github.com/a/b");

var buildCommand = new BuildCommand(testAsset, "NETCoreCppCliTest")
{
WorkingDirectory = testAsset.Path
};

buildCommand.Execute("-p:Platform=x64").Should().Pass();

var outputDir = Path.Combine(testAsset.Path, "x64", "Debug");
var sourceLinkFilePath = Path.Combine(outputDir, "NETCoreCppCliTest.sourcelink.json");
var actualContent = File.ReadAllText(sourceLinkFilePath, Encoding.UTF8);
var expectedSourceLink = """{"documents":{"https://github.com/org/repo":"https://github.com/raw/org/repo/1200000000000000000000000000000000000000/*"}}""";
Assert.Equal(expectedSourceLink, actualContent);

var pdbText = File.ReadAllText(Path.Combine(outputDir, "NETCoreCppCliTest.pdb"), Encoding.UTF8);
Assert.Contains(expectedSourceLink, pdbText);
}
}
}

0 comments on commit 7d8bea7

Please sign in to comment.