Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
for NuGet targets
  • Loading branch information
Nirmal4G committed Jul 10, 2020
1 parent a9f3d78 commit b71efdb
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/Tests/EndToEnd.Tests/GivenDotNetUsesMSBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.NET.TestFramework.Commands;
using Xunit;
using Xunit.Abstractions;
Expand Down
3 changes: 2 additions & 1 deletion src/Tests/Microsoft.NET.Build.Tests/COMReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -81,7 +82,7 @@ class MediaPlayerClass { }
buildCommand.Execute().Should().Pass();

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);
var runCommand = new RunExeCommand(Log, outputDirectory.File("UseMediaPlayer.exe").FullName);
var runCommand = new RunExeCommand(Log, outputDirectory.GetFile("UseMediaPlayer.exe").FullName);
runCommand.Execute().Should().Pass();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -45,7 +46,7 @@ public void It_has_target_path_and_final_outputput_path_metadata(string targetFr
command.Execute().Should().Pass();

var outputDirectory = command.GetOutputDirectory(targetFramework);
var runtimeConfigFile = outputDirectory.File("HelloWorld.runtimeconfig.json");
var runtimeConfigFile = outputDirectory.GetFile("HelloWorld.runtimeconfig.json");
var (_, metadata) = command.GetValuesWithMetadata().Single(i => i.value == runtimeConfigFile.FullName);

metadata.Count.Should().Be(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Xunit.Abstractions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.NET.TestFramework.Commands;
using System.IO;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyModel;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System;
using System.IO;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.Build.Utilities;

namespace Microsoft.NET.Build.Tests
Expand Down Expand Up @@ -97,7 +98,7 @@ End Module

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);
var actualVBRuntime = GetVBRuntime(buildCommand.GetValues().FirstOrDefault());
File.Delete(outputDirectory.File("VBRuntimeValues.txt").FullName);
File.Delete(outputDirectory.GetFile("VBRuntimeValues.txt").FullName);

outputDirectory.Should().OnlyHaveFiles(expectedOutputFiles);
actualVBRuntime.Should().Be(expectedVBRuntime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -205,7 +206,7 @@ public void It_chooses_lowest_netfx_in_default_atf()
var buildCommand = new BuildCommand(testProjectTestAsset);
buildCommand.Execute().Should().Pass();

var referencedDll = buildCommand.GetOutputDirectory("netcoreapp3.0").File("net462_net472_pkg.dll").FullName;
var referencedDll = buildCommand.GetOutputDirectory("netcoreapp3.0").GetFile("net462_net472_pkg.dll").FullName;
var referencedTargetFramework = AssemblyInfo.Get(referencedDll)["TargetFrameworkAttribute"];
referencedTargetFramework.Should().Be(".NETFramework,Version=v4.6.2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -58,7 +59,7 @@ public void NonCopyLocalProjectReferenceDoesNotGoToDeps()

var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

using var stream = File.OpenRead(outputDirectory.File("MainProject.deps.json").FullName);
using var stream = File.OpenRead(outputDirectory.GetFile("MainProject.deps.json").FullName);
using var reader = new DependencyContextJsonReader();

reader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Microsoft.NET.TestFramework.Commands;
using FluentAssertions;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -610,7 +611,7 @@ static void Main() {}

var publishDir = publishCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: rid);
publishDir.Should().HaveFile("System.IO.Compression.ZipFile.dll");
GivenThatWeWantToPublishReadyToRun.DoesImageHaveR2RInfo(publishDir.File("TestWeb.Views.dll").FullName);
GivenThatWeWantToPublishReadyToRun.DoesImageHaveR2RInfo(publishDir.GetFile("TestWeb.Views.dll").FullName);
}

private static bool DoesImageHaveMethod(string path, string methodNameToCheck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using Microsoft.NET.TestFramework.Utilities;

using NuGet.Packaging.Core;
using NuGet.Versioning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,5 @@ public static DirectoryInfoAssertions Should(this DirectoryInfo dir)
{
return new DirectoryInfoAssertions(dir);
}

public static DirectoryInfo Sub(this DirectoryInfo dir, string name)
{
return new DirectoryInfo(Path.Combine(dir.FullName, name));
}

public static FileInfo File(this DirectoryInfo dir, string name)
{
return new FileInfo(Path.Combine(dir.FullName, name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ public static IDisposable NuGetLock(this FileInfo subject)
{
return new FileInfoNuGetLock(subject);
}

public static string ReadAllText(this FileInfo subject)
{
return File.ReadAllText(subject.FullName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#if NETCOREAPP

using Microsoft.DotNet.Cli.Utils;
using System.IO;
using System.Runtime.InteropServices;
using Xunit.Abstractions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework.Utilities;

namespace Microsoft.NET.TestFramework.Commands
{
Expand All @@ -20,24 +21,23 @@ public ComposeStoreCommand(ITestOutputHelper log, string projectPath, string rel
}

public override DirectoryInfo GetOutputDirectory(string targetFramework = "netcoreapp1.0", string configuration = "Debug", string runtimeIdentifier = "")
{
string output = Path.Combine(ProjectRootPath, "bin", BuildRelativeOutputPath(targetFramework, configuration, runtimeIdentifier));
return new DirectoryInfo(output);
}

public string GetPublishedAppPath(string appName)
{
return Path.Combine(GetOutputDirectory().FullName, $"{appName}.dll");
}

private string BuildRelativeOutputPath(string targetFramework, string configuration, string runtimeIdentifier)
{
if (runtimeIdentifier.Length == 0)
{
runtimeIdentifier = RuntimeInformation.RuntimeIdentifier;
}
string arch = runtimeIdentifier.Substring(runtimeIdentifier.LastIndexOf("-") + 1);
return Path.Combine(configuration, arch, targetFramework, PublishSubfolderName);
string relativeToBaseOutputPath = Path.Combine(configuration, arch, targetFramework, runtimeIdentifier, PublishSubfolderName);
return base.GetBaseOutputDirectory().Sub(relativeToBaseOutputPath);

// TODO: SDK-Style projects must not contain arch in the platform. Replace the above logic with the following once it's fixed.
// DirectoryInfo baseDirectory = base.GetBaseOutputDirectory(targetFramework, configuration, runtimeIdentifier);
// return baseDirectory.Sub(PublishSubfolderName);
}

public string GetPublishedAppPath(string appName)
{
return Path.Combine(GetOutputDirectory().FullName, $"{appName}.dll");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.IO;
using FluentAssertions;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Utilities;
using Xunit.Abstractions;

namespace Microsoft.NET.TestFramework.Commands
Expand Down Expand Up @@ -68,10 +68,9 @@ protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
newArgs.AddRange(args);

// Override build target to write out DefineConstants value to a file in the output directory
Directory.CreateDirectory(GetBaseIntermediateDirectory().FullName);
string injectTargetPath = Path.Combine(
GetBaseIntermediateDirectory().FullName,
Path.GetFileName(ProjectFile) + ".WriteValuesToFile.g.targets");
DirectoryInfo objDir = GetBaseIntermediateDirectory();
if (!objDir.Exists) objDir.Create();
FileInfo injectTargetsFile = objDir.GetFile($"{Path.GetFileName(ProjectFile)}.WriteValuesToFile.g.targets");

string linesAttribute;
if (_valueType == ValueType.Property)
Expand Down Expand Up @@ -115,7 +114,7 @@ protected override SdkCommandSpec CreateCommand(IEnumerable<string> args)
</Project>";
injectTargetContents = injectTargetContents.Replace('`', '"');

File.WriteAllText(injectTargetPath, injectTargetContents);
File.WriteAllText(injectTargetsFile.FullName, injectTargetContents);

var outputDirectory = GetOutputDirectory(_targetFramework);
outputDirectory.Create();
Expand Down
49 changes: 32 additions & 17 deletions src/Tests/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Microsoft.NET.TestFramework.Commands
{
public class MSBuildCommand : TestCommand
{
{
public string Target { get; }

private readonly string _projectRootPath;
Expand Down Expand Up @@ -72,38 +72,53 @@ internal static string FindProjectFile(ref string projectRootPath, string relati
return buildProjectFiles[0];
}

public virtual DirectoryInfo GetOutputDirectory(string targetFramework, string configuration = "Debug", string runtimeIdentifier = "")
private DirectoryInfo GetDirectoryRelativeToProjectRoot(params string[] dirs)
{
string relativePath = Path.Combine(dirs);
string output = Path.Combine(ProjectRootPath, relativePath);
return new DirectoryInfo(output);
}

public virtual DirectoryInfo GetBaseOutputDirectory()
{
return GetDirectoryRelativeToProjectRoot("bin");
}

public virtual DirectoryInfo GetBaseIntermediateDirectory()
{
return GetDirectoryRelativeToProjectRoot("obj");
}

public virtual DirectoryInfo GetNonSDKOutputDirectory(string configuration = "Debug")
{
targetFramework = targetFramework ?? string.Empty;
configuration = configuration ?? string.Empty;
runtimeIdentifier = runtimeIdentifier ?? string.Empty;

string output = Path.Combine(ProjectRootPath, "bin", configuration, targetFramework, runtimeIdentifier);
return new DirectoryInfo(output);
return GetDirectoryRelativeToProjectRoot("bin", configuration);
}

public virtual DirectoryInfo GetIntermediateDirectory(string targetFramework, string configuration = "Debug", string runtimeIdentifier = "")
public virtual DirectoryInfo GetNonSDKIntermediateDirectory(string configuration = "Debug")
{
targetFramework = targetFramework ?? string.Empty;
configuration = configuration ?? string.Empty;
runtimeIdentifier = runtimeIdentifier ?? string.Empty;

string output = Path.Combine(ProjectRootPath, "obj", configuration, targetFramework, runtimeIdentifier);
return new DirectoryInfo(output);
return GetDirectoryRelativeToProjectRoot("obj", configuration);
}

public virtual DirectoryInfo GetNonSDKOutputDirectory(string configuration = "Debug")
public virtual DirectoryInfo GetOutputDirectory(string targetFramework, string configuration = "Debug", string runtimeIdentifier = "")
{
targetFramework = targetFramework ?? string.Empty;
configuration = configuration ?? string.Empty;
runtimeIdentifier = runtimeIdentifier ?? string.Empty;

string output = Path.Combine(ProjectRootPath, "bin", configuration);
return new DirectoryInfo(output);
return GetDirectoryRelativeToProjectRoot("bin", configuration, targetFramework, runtimeIdentifier);
}

public DirectoryInfo GetBaseIntermediateDirectory()
public virtual DirectoryInfo GetIntermediateDirectory(string targetFramework, string configuration = "Debug", string runtimeIdentifier = "")
{
string output = Path.Combine(ProjectRootPath, "obj");
return new DirectoryInfo(output);
targetFramework = targetFramework ?? string.Empty;
configuration = configuration ?? string.Empty;
runtimeIdentifier = runtimeIdentifier ?? string.Empty;

return GetDirectoryRelativeToProjectRoot("obj", configuration, targetFramework, runtimeIdentifier);
}

protected virtual bool ExecuteWithRestoreByDefault => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public string GetNuGetPackage(string packageId = null, string configuration = "D
packageId = Path.GetFileNameWithoutExtension(ProjectFile);
}

return Path.Combine(GetOutputDirectory(null, configuration).FullName, $"{packageId}.{packageVersion}.nupkg");
return Path.Combine(GetBaseOutputDirectory().FullName, configuration, $"{packageId}.{packageVersion}.nupkg");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Microsoft.NET.TestFramework.Utilities;
using Xunit.Abstractions;

namespace Microsoft.NET.TestFramework.Commands
Expand All @@ -17,8 +18,8 @@ public PublishCommand(ITestOutputHelper log, string projectPath)

public override DirectoryInfo GetOutputDirectory(string targetFramework = "netcoreapp1.1", string configuration = "Debug", string runtimeIdentifier = "")
{
DirectoryInfo baseDirectory = base.GetOutputDirectory(targetFramework, configuration, runtimeIdentifier);
return new DirectoryInfo(Path.Combine(baseDirectory.FullName, PublishSubfolderName));
DirectoryInfo baseDirectory = base.GetOutputDirectory(targetFramework, configuration, runtimeIdentifier);
return baseDirectory.Sub(PublishSubfolderName);
}

public string GetPublishedAppPath(string appName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.NET.TestFramework.Utilities
{
public static class CommonExtensions
{
public static DirectoryInfo Sub(this DirectoryInfo dir, string name)
{
return new DirectoryInfo(Path.Combine(dir.FullName, name));
}

public static FileInfo GetFile(this DirectoryInfo dir, string name)
{
return new FileInfo(Path.Combine(dir.FullName, name));
}

public static string ReadAllText(this FileInfo subject)
{
return File.ReadAllText(subject.FullName);
}
}
}
Loading

0 comments on commit b71efdb

Please sign in to comment.