Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
</Target>

<Target Name="GenerateNuspec" DependsOnTargets="$(GenerateNuspecDependsOn);_CalculateInputsOutputsForPack;_GetProjectReferenceVersions" Condition="$(IsPackable) == 'true'"
<Target Name="GenerateNuspec" DependsOnTargets="$(GenerateNuspecDependsOn);_CalculateInputsOutputsForPack;_GetProjectReferenceVersions;_InitializeNuspecRepositoryInformationProperties" Condition="$(IsPackable) == 'true'"
Inputs="@(NuGetPackInput)" Outputs="@(NuGetPackOutput)">
<!-- Call Pack -->
<PackTask PackItem="$(PackProjectInputFile)"
Expand Down Expand Up @@ -246,6 +246,19 @@ Copyright (c) .NET Foundation. All rights reserved.
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"/>
</Target>

<!--
Initialize Repository* properties from properties set by a source control package, if available in the project.
-->
<Target Name="_InitializeNuspecRepositoryInformationProperties"
DependsOnTargets="InitializeSourceControlInformation"
Condition="'$(SourceControlInformationFeatureSupported)' == 'true'">
<PropertyGroup>
<!-- The project must specify PublishRepositoryUrl=true in order to publish the URL, in order to prevent inadvertent leak of internal URL. -->
<RepositoryUrl Condition="'$(RepositoryUrl)' == '' and '$(PublishRepositoryUrl)' == 'true'">$(PrivateRepositoryUrl)</RepositoryUrl>
<RepositoryCommit Condition="'$(RepositoryCommit)' == ''">$(SourceRevisionId)</RepositoryCommit>
</PropertyGroup>
</Target>

<!--
============================================================
_LoadPackGraphEntryPoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2314,5 +2314,233 @@ public void PackCommand_ManualAddPackage_DevelopmentDependency()
}
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackWithSourceControlInformation_Unsupported_VerifyNuspec()
{
using (var testDirectory = TestDirectory.Create())
{
var projectName = "ClassLibrary1";
var workingDirectory = Path.Combine(testDirectory, projectName);
var projectFile = Path.Combine(workingDirectory, $"{projectName}.csproj");

// Act
msbuildFixture.CreateDotnetNewProject(testDirectory.Path, projectName, " classlib");

using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);
var ns = xml.Root.Name.Namespace;

ProjectFileUtils.AddProperty(xml, "RepositoryType", "git");

// mock implementation of InitializeSourceControlInformation common targets:
xml.Root.Add(
new XElement(ns + "Target",
new XAttribute("Name", "InitializeSourceControlInformation"),
new XElement(ns + "PropertyGroup",
new XElement("SourceRevisionId", "e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1"),
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client.git"))));

xml.Root.Add(
new XElement(ns + "PropertyGroup",
new XElement("SourceControlInformationFeatureSupported", "false")));

ProjectFileUtils.WriteXmlToFile(xml, stream);
}

msbuildFixture.RestoreProject(workingDirectory, projectName, string.Empty);
msbuildFixture.PackProject(workingDirectory, projectName, $"-o {workingDirectory}");

var nupkgPath = Path.Combine(workingDirectory, $"{projectName}.1.0.0.nupkg");
var nuspecPath = Path.Combine(workingDirectory, "obj", $"{projectName}.1.0.0.nuspec");
Assert.True(File.Exists(nupkgPath), "The output .nupkg is not in the expected place");
Assert.True(File.Exists(nuspecPath), "The intermediate nuspec file is not in the expected place");

using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

// Validate the output .nuspec.
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Commit.Should().Be("");
}
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackWithSourceControlInformation_PrivateUrl_VerifyNuspec()
{
using (var testDirectory = TestDirectory.Create())
{
var projectName = "ClassLibrary1";
var workingDirectory = Path.Combine(testDirectory, projectName);
var projectFile = Path.Combine(workingDirectory, $"{projectName}.csproj");

// Act
msbuildFixture.CreateDotnetNewProject(testDirectory.Path, projectName, " classlib");

using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);
var ns = xml.Root.Name.Namespace;

ProjectFileUtils.AddProperty(xml, "RepositoryType", "git");

// mock implementation of InitializeSourceControlInformation common targets:
xml.Root.Add(
new XElement(ns + "Target",
new XAttribute("Name", "InitializeSourceControlInformation"),
new XElement(ns + "PropertyGroup",
new XElement("SourceRevisionId", "e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1"),
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client.git"))));

xml.Root.Add(
new XElement(ns + "PropertyGroup",
new XElement("SourceControlInformationFeatureSupported", "true")));

ProjectFileUtils.WriteXmlToFile(xml, stream);
}

msbuildFixture.RestoreProject(workingDirectory, projectName, string.Empty);
msbuildFixture.PackProject(workingDirectory, projectName, $"-o {workingDirectory}");

var nupkgPath = Path.Combine(workingDirectory, $"{projectName}.1.0.0.nupkg");
var nuspecPath = Path.Combine(workingDirectory, "obj", $"{projectName}.1.0.0.nuspec");
Assert.True(File.Exists(nupkgPath), "The output .nupkg is not in the expected place");
Assert.True(File.Exists(nuspecPath), "The intermediate nuspec file is not in the expected place");

using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

// Validate the output .nuspec.
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Commit.Should().Be("e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1");
}
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackWithSourceControlInformation_PublishedUrl_VerifyNuspec()
{
using (var testDirectory = TestDirectory.Create())
{
var projectName = "ClassLibrary1";
var workingDirectory = Path.Combine(testDirectory, projectName);
var projectFile = Path.Combine(workingDirectory, $"{projectName}.csproj");

// Act
msbuildFixture.CreateDotnetNewProject(testDirectory.Path, projectName, " classlib");

using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);
var ns = xml.Root.Name.Namespace;

ProjectFileUtils.AddProperty(xml, "RepositoryType", "git");
ProjectFileUtils.AddProperty(xml, "PublishRepositoryUrl", "true");

// mock implementation of InitializeSourceControlInformation common targets:
xml.Root.Add(
new XElement(ns + "Target",
new XAttribute("Name", "InitializeSourceControlInformation"),
new XElement(ns + "PropertyGroup",
new XElement("SourceRevisionId", "e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1"),
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client.git"))));

xml.Root.Add(
new XElement(ns + "PropertyGroup",
new XElement("SourceControlInformationFeatureSupported", "true")));

ProjectFileUtils.WriteXmlToFile(xml, stream);
}

msbuildFixture.RestoreProject(workingDirectory, projectName, string.Empty);
msbuildFixture.PackProject(workingDirectory, projectName, $"-o {workingDirectory}");

var nupkgPath = Path.Combine(workingDirectory, $"{projectName}.1.0.0.nupkg");
var nuspecPath = Path.Combine(workingDirectory, "obj", $"{projectName}.1.0.0.nuspec");
Assert.True(File.Exists(nupkgPath), "The output .nupkg is not in the expected place");
Assert.True(File.Exists(nuspecPath), "The intermediate nuspec file is not in the expected place");

using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

// Validate the output .nuspec.
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("https://github.com/NuGet/NuGet.Client.git");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Commit.Should().Be("e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1");
}
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackWithSourceControlInformation_ProjectOverride_VerifyNuspec()
{
using (var testDirectory = TestDirectory.Create())
{
var projectName = "ClassLibrary1";
var workingDirectory = Path.Combine(testDirectory, projectName);
var projectFile = Path.Combine(workingDirectory, $"{projectName}.csproj");

// Act
msbuildFixture.CreateDotnetNewProject(testDirectory.Path, projectName, " classlib");

using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);
var ns = xml.Root.Name.Namespace;

ProjectFileUtils.AddProperty(xml, "RepositoryType", "git");
ProjectFileUtils.AddProperty(xml, "PublishRepositoryUrl", "true");
ProjectFileUtils.AddProperty(xml, "RepositoryCommit", "1111111111111111111111111111111111111111");
ProjectFileUtils.AddProperty(xml, "RepositoryUrl", "https://github.com/Overridden");

// mock implementation of InitializeSourceControlInformation common targets:
xml.Root.Add(
new XElement(ns + "Target",
new XAttribute("Name", "InitializeSourceControlInformation"),
new XElement(ns + "PropertyGroup",
new XElement("SourceRevisionId", "e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1"),
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client"))));

xml.Root.Add(
new XElement(ns + "PropertyGroup",
new XElement("SourceControlInformationFeatureSupported", "true")));

ProjectFileUtils.WriteXmlToFile(xml, stream);
}

msbuildFixture.RestoreProject(workingDirectory, projectName, string.Empty);
msbuildFixture.PackProject(workingDirectory, projectName, $"-o {workingDirectory}");

var nupkgPath = Path.Combine(workingDirectory, $"{projectName}.1.0.0.nupkg");
var nuspecPath = Path.Combine(workingDirectory, "obj", $"{projectName}.1.0.0.nuspec");
Assert.True(File.Exists(nupkgPath), "The output .nupkg is not in the expected place");
Assert.True(File.Exists(nuspecPath), "The intermediate nuspec file is not in the expected place");

using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

// Validate the output .nuspec.
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("https://github.com/Overridden");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Commit.Should().Be("1111111111111111111111111111111111111111");
}
}
}
}
}