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
5 changes: 0 additions & 5 deletions build/vsts_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -862,11 +862,6 @@ jobs:
artifactName: "$(VsixPublishDir)"
downloadPath: "$(Build.Repository.LocalPath)/artifacts"

- task: NuGetToolInstaller@0
displayName: "Use NuGet 4.5.0"
Comment thread
nkolev92 marked this conversation as resolved.
Resolved
inputs:
versionSpec: "4.5.0"

- task: MSBuild@1
displayName: "Bootstrap NuGet packages"
inputs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class AssemblyMetadataExtractor
{
private static T CreateInstance<T>(this AppDomain domain)
{
return (T)domain.CreateInstanceAndUnwrap(typeof(T).Assembly.FullName, typeof(T).FullName);
return (T)domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(T).FullName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments why choose the CreateInstanceFromAndUnwrap instead of CreateInstanceAndUnwrap.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it works when an assembly is renamed? :)

There are tests covering this scenario, and I don't see this as a complex change that requires additional explanation.

My thought is adding a comment is too much, but can add it if we think it's necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given nuget.exe is ILMerged, it probably doesn't matter, but would keeping the typeof(T) be a bit safer, rather than Assembly.GetExecutingAssembly()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yeah, if anyone ever moves these types around it would be safer, but there's a test to catch this.

If you care I can change it but I'd reallly hate to try to fight with CI because of a change like that :D

lmk, this merge is not time critical so we can go through the CI churn if necessary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zivkan Do you care about this or should I merge?

}

public static AssemblyMetadata GetMetadata(string assemblyPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6362,6 +6362,243 @@ public class Class1
r.AllOutput.Should().NotContain(NuGetLogCode.NU5105.ToString());
}
}

[Fact]
public void PackCommand_WhenNuspecReplacementTokensAreUsed_AssemblyMetadataIsExtracted()
{
var nugetexe = Util.GetNuGetExePath();

using (var workingDirectory = TestDirectory.Create())
{
var projectDirectory = Path.Combine(workingDirectory, "proj");

// create project 1
Util.CreateFile(
projectDirectory,
"proj.csproj",
@"<Project ToolsVersion='14.0' DefaultTargets='Build'
xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<OutputType>Library</OutputType>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include='System'/>
<Reference Include='System.Core'/>
<Reference Include='System.Xml.Linq'/>
<Reference Include='System.Data.DataSetExtensions'/>
<Reference Include='Microsoft.CSharp'/>
<Reference Include='System.Data'/>
<Reference Include='System.Net.Http'/>
<Reference Include='System.Xml'/>
</ItemGroup>
<ItemGroup>
<Compile Include='proj_file1.cs' />
<Compile Include='AssemblyInfo.cs' />
</ItemGroup>
<ItemGroup>
<Content Include='proj_file2.txt' />
</ItemGroup>
<Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
</Project>");
Util.CreateFile(
projectDirectory,
"proj_file1.cs",
@"using System;

namespace Proj
{
public class Class1
{
public int A { get; set; }
}
}");

Util.CreateFile(
projectDirectory,
"AssemblyInfo.cs",
@"using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle(""MetadataExtractor"")]
[assembly: AssemblyDescription(""MetadataExtractor"")]
[assembly: AssemblyConfiguration("""")]
[assembly: AssemblyCompany(""Company"")]
[assembly: AssemblyProduct(""MetadataExtractor"")]
[assembly: AssemblyCopyright(""Copyright © 2050"")]
[assembly: AssemblyTrademark("""")]
[assembly: AssemblyCulture("""")]
[assembly: AssemblyVersion(""1.0.0"")]
[assembly: AssemblyFileVersion(""1.0.0"")]
");

Util.CreateFile(
workingDirectory,
"proj.nuspec",
@"<package xmlns='http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd'>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>$copyright$</copyright>
</metadata>
</package>");

// Act
var r = CommandRunner.Run(
nugetexe,
projectDirectory,
"pack -build",
waitForExit: true);
Assert.True(0 == r.Item1, r.Item2 + " " + r.Item3);

// Assert
var nupkgPath = Path.Combine(projectDirectory, "proj.1.0.0.nupkg");

Assert.True(File.Exists(nupkgPath), $"The {nupkgPath} does not exist.");
using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

Assert.Equal("proj", nuspecReader.GetIdentity().Id);
Assert.Equal("1.0.0.0", nuspecReader.GetVersion().Version.ToString());
Assert.Equal("Company", nuspecReader.GetAuthors());
Assert.Equal("Company", nuspecReader.GetOwners());
Assert.Equal("MetadataExtractor", nuspecReader.GetDescription());
Assert.Equal("MetadataExtractor", nuspecReader.GetTitle());
Assert.Equal("Copyright © 2050", nuspecReader.GetCopyright());
}
}
}

[Fact]
public void PackCommand_WhenNuGetExeIsRenamed_AssemblyMetadataIsStillExtracted()
{
using (var workingDirectory = TestDirectory.Create())
{
var projectDirectory = Path.Combine(workingDirectory, "proj");

// create project 1
Util.CreateFile(
projectDirectory,
"proj.csproj",
@"<Project ToolsVersion='14.0' DefaultTargets='Build'
xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<OutputType>Library</OutputType>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include='System'/>
<Reference Include='System.Core'/>
<Reference Include='System.Xml.Linq'/>
<Reference Include='System.Data.DataSetExtensions'/>
<Reference Include='Microsoft.CSharp'/>
<Reference Include='System.Data'/>
<Reference Include='System.Net.Http'/>
<Reference Include='System.Xml'/>
</ItemGroup>
<ItemGroup>
<Compile Include='proj_file1.cs' />
<Compile Include='AssemblyInfo.cs' />
</ItemGroup>
<ItemGroup>
<Content Include='proj_file2.txt' />
</ItemGroup>
<Import Project='$(MSBuildToolsPath)\Microsoft.CSharp.targets' />
</Project>");
Util.CreateFile(
projectDirectory,
"proj_file1.cs",
@"using System;

namespace Proj
{
public class Class1
{
public int A { get; set; }
}
}");

Util.CreateFile(
projectDirectory,
"AssemblyInfo.cs",
@"using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle(""MetadataExtractor"")]
[assembly: AssemblyDescription(""MetadataExtractor"")]
[assembly: AssemblyConfiguration("""")]
[assembly: AssemblyCompany(""Company"")]
[assembly: AssemblyProduct(""MetadataExtractor"")]
[assembly: AssemblyCopyright(""Copyright © 2050"")]
[assembly: AssemblyTrademark("""")]
[assembly: AssemblyCulture("""")]
[assembly: AssemblyVersion(""1.0.0"")]
[assembly: AssemblyFileVersion(""1.0.0"")]
");

Util.CreateFile(
workingDirectory,
"proj.nuspec",
@"<package xmlns='http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd'>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>$copyright$</copyright>
</metadata>
</package>");

// Finally: Copy & rename NuGet.exe
var nuGetDir = Directory.CreateDirectory(Path.Combine(workingDirectory, "nuget"));
var renamedNuGetExe = Path.Combine(nuGetDir.FullName, "NuGet-A.exe");
File.Copy(Util.GetNuGetExePath(), renamedNuGetExe, overwrite: true);

// Act
var r = CommandRunner.Run(
renamedNuGetExe,
projectDirectory,
"pack -build",
waitForExit: true);
Assert.True(0 == r.Item1, r.Item2 + " " + r.Item3);

// Assert
var nupkgPath = Path.Combine(projectDirectory, "proj.1.0.0.nupkg");

Assert.True(File.Exists(nupkgPath), $"The {nupkgPath} does not exist.");
using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

Assert.Equal("proj", nuspecReader.GetIdentity().Id);
Assert.Equal("1.0.0.0", nuspecReader.GetVersion().Version.ToString());
Assert.Equal("Company", nuspecReader.GetAuthors());
Assert.Equal("Company", nuspecReader.GetOwners());
Assert.Equal("MetadataExtractor", nuspecReader.GetDescription());
Assert.Equal("MetadataExtractor", nuspecReader.GetTitle());
Assert.Equal("Copyright © 2050", nuspecReader.GetCopyright());
}
}
}
}

internal static class PackageArchiveReaderTestExtensions
Expand Down