Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
Fixes incorrect dependency in non-lib packages (#284)
Browse files Browse the repository at this point in the history
* Stopped NETStandard.Library reference from being added to target project

* Set up integration tests on targets packages

* Removed local NuGet feed copy as requested

* Fixed generated integration test projects failing due to not being in a repository
  • Loading branch information
jnm2 authored and ctaggart committed Nov 16, 2017
1 parent 64a4f4d commit 62c3efa
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
1 change: 1 addition & 0 deletions SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
1 change: 1 addition & 0 deletions SourceLink.Create.GitLab/SourceLink.Create.GitLab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>
<ItemGroup Label="dotnet pack instructions">
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
<Content Include="SourceLink.Embed.AllSourceFiles.targets">
<Pack>true</Pack>
<PackagePath>build</PackagePath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>
<ItemGroup Label="dotnet pack instructions">
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
<Content Include="SourceLink.Embed.PaketFiles.targets">
<Pack>true</Pack>
<PackagePath>build</PackagePath>
Expand Down
1 change: 1 addition & 0 deletions SourceLink.Test/SourceLink.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>

<!-- https://docs.microsoft.com/en-us/dotnet/articles/core/preview3/tools/extensibility -->
Expand Down
93 changes: 93 additions & 0 deletions Tests/Helpers/CsprojFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Tests.Helpers
{
public sealed class CsprojFixture : IDisposable
{
private readonly TempDirectory tempDir;
private const string CsprojName = "test.csproj";
private const string PackagesCache = "packages";

public CsprojFixture(string[] csprojLines)
{
tempDir = new TempDirectory();

File.WriteAllLines(Path.Combine(tempDir, CsprojName), csprojLines);
}

public void Dispose()
{
tempDir.Dispose();
}

public void AddFile(string path, string[] lines)
{
File.WriteAllLines(Path.Combine(tempDir, path), lines);
}

public void DotnetRestore(string packageSource)
{
RunProcess(tempDir, "dotnet", $"restore --no-cache --packages \"{PackagesCache}\" --source \"{Path.GetFullPath(packageSource)}\"");
}

public void DotnetMSBuild()
{
RunProcess(tempDir, "dotnet", "msbuild");
}

private static void RunProcess(string workingDirectory, string fileName, string arguments)
{
var result = ProcessUtils.Run(new ProcessStartInfo
{
WorkingDirectory = workingDirectory,
FileName = "dotnet",
Arguments = arguments
});

var hasErrorOutput = result.StandardStreamData.Any(_ => _.IsError);
if (hasErrorOutput || result.ExitCode != 0)
{
var message = new StringBuilder(fileName).Append(' ').Append(arguments).Append(" exited with code ").Append(result.ExitCode);

if (hasErrorOutput) message.Append(" and wrote to stderr");

if (result.StandardStreamData.Length == 0)
{
message.Append(" and no output.");
}
else
{
message.Append(':');
foreach (var data in result.StandardStreamData)
message.AppendLine().Append(data);
}

throw new Exception(message.ToString());
}
}

public string[] GetFiles(string relativePath, SearchOption searchOption)
{
return GetFiles(relativePath, null, searchOption);
}

public string[] GetFiles(string relativePath, string searchPattern, SearchOption searchOption)
{
if (relativePath != null && Path.IsPathRooted(relativePath))
throw new ArgumentException("Path must be relative.", nameof(relativePath));

var files = Directory.GetFiles(Path.Combine(tempDir, relativePath), searchPattern ?? "*", searchOption);

var tempDirPathLength = tempDir.Path.Length + 1;

for (var i = 0; i < files.Length; i++)
files[i] = files[i].Substring(tempDirPathLength);

return files;
}
}
}
116 changes: 116 additions & 0 deletions Tests/Helpers/ProcessUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Tests.Helpers
{
public static class ProcessUtils
{
public static ProcessResult Run(ProcessStartInfo startInfo)
{
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;

using (var process = new Process { StartInfo = startInfo })
{
var standardStreamData = new List<StandardStreamData>();
var currentData = new StringBuilder();
var currentDataIsError = false;

process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData.Clear();
currentDataIsError = false;
}
currentData.AppendLine(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null) return;
if (!currentDataIsError)
{
if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));
currentData.Clear();
currentDataIsError = true;
}
currentData.AppendLine(e.Data);
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

if (currentData.Length != 0)
standardStreamData.Add(new StandardStreamData(currentDataIsError, currentData.ToString()));

return new ProcessResult(process.ExitCode, standardStreamData.ToArray());
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct ProcessResult
{
public ProcessResult(int exitCode, StandardStreamData[] standardStreamData)
{
ExitCode = exitCode;
StandardStreamData = standardStreamData;
}

public int ExitCode { get; }
public StandardStreamData[] StandardStreamData { get; }

public override string ToString() => ToString(true);

/// <param name="showStreamSource">If true, appends "[stdout] " or "[stderr] " to the beginning of each line.</param>
public string ToString(bool showStreamSource)
{
var r = new StringBuilder("Exit code ").Append(ExitCode);

if (StandardStreamData.Length != 0) r.AppendLine();

foreach (var data in StandardStreamData)
{
if (showStreamSource)
{
var lines = data.Data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

// StandardStreamData.Data always ends with a blank line, so skip that
for (var i = 0; i < lines.Length - 1; i++)
r.Append(data.IsError ? "[stderr] " : "[stdout] ").AppendLine(lines[i]);
}
else
{
r.Append(data.Data);
}
}

return r.ToString();
}
}

[DebuggerDisplay("{ToString(),nq}")]
public struct StandardStreamData
{
public StandardStreamData(bool isError, string data)
{
IsError = isError;
Data = data;
}

public bool IsError { get; }
public string Data { get; }

public override string ToString() => (IsError ? "[stderr] " : "[stdout] ") + Data;
}
}
}
34 changes: 34 additions & 0 deletions Tests/Helpers/TempDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Tests.Helpers
{
[DebuggerDisplay("{ToString(),nq}")]
public sealed class TempDirectory : IDisposable
{
public TempDirectory() : this(System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()))
{
}

public TempDirectory(string path)
{
Directory.CreateDirectory(path);
this.path = path;
}

private string path;
public string Path => path;

public static implicit operator string(TempDirectory tempFile) => tempFile.path;

public override string ToString() => path;

public void Dispose()
{
var path = Interlocked.Exchange(ref this.path, null);
if (path != null) Directory.Delete(path, recursive: true);
}
}
}
54 changes: 54 additions & 0 deletions Tests/Integration/When_package_without_lib_is_installed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using Tests.Helpers;
using Xunit;

namespace Tests.Integration
{
public static class When_package_without_lib_is_installed
{
[Theory]
[InlineData("SourceLink.Create.BitBucket")]
[InlineData("SourceLink.Create.BitBucketServer")]
[InlineData("SourceLink.Create.CommandLine")]
[InlineData("SourceLink.Create.GitHub")]
[InlineData("SourceLink.Create.GitLab")]
[InlineData("SourceLink.Embed.AllSourceFiles")]
[InlineData("SourceLink.Embed.PaketFiles")]
[InlineData("SourceLink.Test")]
public static void Should_not_reference_additional_libraries(string packageName)
{
var packageSource = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Integration");

var packageFile = Assert.Single(Directory.GetFiles(packageSource, $"{packageName}.*.nupkg"));
var packageVersion = Path.GetFileNameWithoutExtension(packageFile).Substring(packageName.Length + 1);

const string targetFramework = "net462";
using (var fixture = new CsprojFixture(new[]
{
"<Project Sdk=\"Microsoft.Net.Sdk\">",
" <PropertyGroup>",
$" <TargetFramework>{targetFramework}</TargetFramework>",
" </PropertyGroup>",
" <ItemGroup>",
$" <PackageReference Include=\"{packageName}\" Version=\"{packageVersion}\" ExcludeAssets=\"build\" PrivateAssets=\"all\" />",
" </ItemGroup>",
"</Project>"
}))
{
fixture.AddFile("test.cs", Array.Empty<string>());

fixture.DotnetRestore(packageSource);
fixture.DotnetMSBuild();

Assert.Equal(
new[]
{
$@"bin\Debug\{targetFramework}\test.dll",
$@"bin\Debug\{targetFramework}\test.pdb"
},
fixture.GetFiles($@"bin\Debug\{targetFramework}", SearchOption.AllDirectories));
}
}
}
}
19 changes: 19 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
<ProjectReference Include="..\SourceLink.Create.GitLab\SourceLink.Create.GitLab.csproj" />
</ItemGroup>

<Target Name="CopyPackagesForIntegrationTesting" AfterTargets="AfterBuild">
<ItemGroup>
<ProjectForPackageTesting Include="..\SourceLink.Create.BitBucket\SourceLink.Create.BitBucket.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Create.BitBucketServer\SourceLink.Create.BitBucketServer.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Create.CommandLine\SourceLink.Create.CommandLine.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Create.GitHub\SourceLink.Create.GitHub.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Create.GitLab\SourceLink.Create.GitLab.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Embed.AllSourceFiles\SourceLink.Embed.AllSourceFiles.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Embed.PaketFiles\SourceLink.Embed.PaketFiles.csproj" />
<ProjectForPackageTesting Include="..\SourceLink.Test\SourceLink.Test.csproj" />
</ItemGroup>

<PropertyGroup>
<IntegrationTestPackageDir>$(OutputPath)Integration\</IntegrationTestPackageDir>
</PropertyGroup>
<RemoveDir Directories="$(IntegrationTestPackageDir)" />
<MSBuild Projects="@(ProjectForPackageTesting)" Targets="Pack" RemoveProperties="TargetFramework" Properties="PackageOutputPath=$([System.IO.Path]::GetFullPath($(IntegrationTestPackageDir)))" />
</Target>

<ItemGroup>
<!-- tagged by Test Explorer http://stackoverflow.com/a/22917902/23059 -->
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Expand Down
5 changes: 0 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,3 @@ foreach($nupkg in $nupkgs){
}

Set-Location $psscriptroot

# testing on local nuget feed
if (-not $env:appveyor){
Copy-Item .\bin\*$version$versionSuffix.nupkg C:\dotnet\nupkg\
}

0 comments on commit 62c3efa

Please sign in to comment.