2015-12-15 01:39:29 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
using System.Collections.Generic;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
using System.IO;
|
2016-01-11 21:40:47 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
2015-12-19 00:39:43 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2016-01-11 21:40:47 +00:00
|
|
|
|
using Xunit;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Publish.Tests
|
|
|
|
|
{
|
|
|
|
|
public class PublishTests : TestBase
|
|
|
|
|
{
|
|
|
|
|
private string _testProjectsRoot = @"TestProjects";
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<object[]> PublishOptions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
new object[] { "", "", "", "" },
|
|
|
|
|
new object[] { "dnxcore50", "", "", "" },
|
2015-12-19 00:39:43 +00:00
|
|
|
|
new object[] { "", PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier(), "", "" },
|
2015-12-15 01:39:29 +00:00
|
|
|
|
new object[] { "", "", "Release", "" },
|
|
|
|
|
new object[] { "", "", "", "some/dir"},
|
2015-12-16 21:04:29 +00:00
|
|
|
|
//new object[] { "", "", "", "\"some/dir/with spaces\"" }, // issue - https://github.com/dotnet/cli/issues/525
|
2015-12-19 00:39:43 +00:00
|
|
|
|
new object[] { "dnxcore50", PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier(), "Debug", "some/dir" },
|
2015-12-15 01:39:29 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[MemberData("PublishOptions")]
|
|
|
|
|
public void PublishOptionsTest(string framework, string runtime, string config, string outputDir)
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var root = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
root.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
var testAppDir = root.CreateDirectory("TestApp");
|
|
|
|
|
var testLibDir = root.CreateDirectory("TestLibrary");
|
|
|
|
|
|
|
|
|
|
//copy projects to the temp dir
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestApp"), testAppDir);
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestLibrary"), testLibDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testAppDir.Path);
|
|
|
|
|
RunRestore(testLibDir.Path);
|
|
|
|
|
|
|
|
|
|
// run publish
|
|
|
|
|
outputDir = string.IsNullOrEmpty(outputDir) ? "" : Path.Combine(root.Path, outputDir);
|
|
|
|
|
var testProject = GetProjectPath(testAppDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject, output: outputDir);
|
|
|
|
|
publishCommand.Execute().Should().Pass();
|
|
|
|
|
|
|
|
|
|
// verify the output executable generated
|
|
|
|
|
var publishedDir = publishCommand.GetOutputDirectory();
|
|
|
|
|
var outputExe = publishCommand.GetOutputExecutable();
|
|
|
|
|
var outputPdb = Path.ChangeExtension(outputExe, "pdb");
|
|
|
|
|
|
|
|
|
|
// lets make sure that the output exe is runnable
|
|
|
|
|
var outputExePath = Path.Combine(publishedDir.FullName, publishCommand.GetOutputExecutable());
|
|
|
|
|
var command = new TestCommand(outputExePath);
|
|
|
|
|
command.Execute("").Should().ExitWith(100);
|
|
|
|
|
|
|
|
|
|
// the pdb should also be published
|
|
|
|
|
publishedDir.Should().HaveFile(outputPdb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
[ActiveIssue(491)]
|
|
|
|
|
public void ProjectWithContentsTest()
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var testDir = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
testDir.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
var testAppDir = Path.Combine(_testProjectsRoot, "TestAppWithContents");
|
|
|
|
|
|
|
|
|
|
// copy projects to the temp dir
|
|
|
|
|
CopyProjectToTempDir(testAppDir, testDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testDir.Path);
|
|
|
|
|
|
|
|
|
|
// run publish
|
|
|
|
|
var testProject = GetProjectPath(testDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
publishCommand.Execute().Should().Pass();
|
|
|
|
|
|
|
|
|
|
// make sure that the output dir has the content files
|
2016-01-11 21:40:47 +00:00
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("testcontentfile.txt");
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BeforeRestoreTest()
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var root = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
root.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
var testAppDir = root.CreateDirectory("TestApp");
|
|
|
|
|
var testLibDir = root.CreateDirectory("TestLibrary");
|
|
|
|
|
|
|
|
|
|
// copy projects to the temp dir
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestApp"), testAppDir);
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestLibrary"), testLibDir);
|
|
|
|
|
|
|
|
|
|
var testProject = GetProjectPath(testAppDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
publishCommand.Execute().Should().Fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void LibraryPublishTest()
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var root = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
root.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
var testLibDir = root.CreateDirectory("TestLibrary");
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
//copy projects to the temp dir
|
2015-12-15 01:39:29 +00:00
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestLibrary"), testLibDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testLibDir.Path);
|
|
|
|
|
|
|
|
|
|
var testProject = GetProjectPath(testLibDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
publishCommand.Execute().Should().Pass();
|
|
|
|
|
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().NotHaveFile("TestLibrary.exe");
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibrary.dll");
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibrary.pdb");
|
|
|
|
|
// dependencies should also be copied
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("System.Runtime.dll");
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 23:02:26 +00:00
|
|
|
|
[WindowsOnlyFact]
|
|
|
|
|
public void TestLibraryPublishTest()
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var root = Temp.CreateDirectory();
|
|
|
|
|
var testLibDir = root.CreateDirectory("TestLibraryWithRunner");
|
|
|
|
|
|
|
|
|
|
//copy projects to the temp dir
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestLibraryWithRunner"), testLibDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testLibDir.Path);
|
|
|
|
|
|
|
|
|
|
var testProject = GetProjectPath(testLibDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
publishCommand.Execute().Should().Pass();
|
|
|
|
|
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibraryWithRunner.dll");
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibraryWithRunner.pdb");
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibraryWithRunner.deps");
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("TestLibraryWithRunner.dll.config");
|
|
|
|
|
// dependencies should also be copied
|
|
|
|
|
publishCommand.GetOutputDirectory().Should().HaveFile("Newtonsoft.Json.dll");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void CompilationFailedTest()
|
|
|
|
|
{
|
|
|
|
|
var testDir = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
testDir.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
var compileFailDir = Path.Combine(_testProjectsRoot, "CompileFail");
|
|
|
|
|
|
|
|
|
|
CopyProjectToTempDir(compileFailDir, testDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testDir.Path);
|
|
|
|
|
|
|
|
|
|
var testProject = GetProjectPath(testDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
|
|
|
|
|
publishCommand.Execute().Should().Fail();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void PublishScriptsRun()
|
|
|
|
|
{
|
|
|
|
|
// create unique directories in the 'temp' folder
|
|
|
|
|
var root = Temp.CreateDirectory();
|
2016-01-13 00:36:31 +00:00
|
|
|
|
root.CopyFile(Path.Combine(_testProjectsRoot, "global.json"));
|
2016-01-11 21:40:47 +00:00
|
|
|
|
var testAppDir = root.CreateDirectory("TestApp");
|
|
|
|
|
var testLibDir = root.CreateDirectory("TestLibrary");
|
|
|
|
|
|
|
|
|
|
//copy projects to the temp dir
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestApp"), testAppDir);
|
|
|
|
|
CopyProjectToTempDir(Path.Combine(_testProjectsRoot, "TestLibrary"), testLibDir);
|
|
|
|
|
|
|
|
|
|
RunRestore(testAppDir.Path);
|
|
|
|
|
RunRestore(testLibDir.Path);
|
|
|
|
|
|
|
|
|
|
// run publish
|
|
|
|
|
var testProject = GetProjectPath(testAppDir);
|
|
|
|
|
var publishCommand = new PublishCommand(testProject);
|
|
|
|
|
|
|
|
|
|
var result = publishCommand.ExecuteWithCapturedOutput();
|
|
|
|
|
|
|
|
|
|
result.Should().StdOutMatchPattern("\nprepublish_output( \\?[^%]+\\?){5}.+\npostpublish_output( \\?[^%]+\\?){5}", RegexOptions.Singleline);
|
|
|
|
|
result.Should().Pass();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir)
|
|
|
|
|
{
|
|
|
|
|
// copy all the files to temp dir
|
|
|
|
|
foreach (var file in Directory.EnumerateFiles(projectDir))
|
|
|
|
|
{
|
|
|
|
|
// never copy project.lock.json. All the tests are expected to call 'dotnet restore'
|
|
|
|
|
if (file.ToLower().EndsWith("project.lock.json"))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tempDir.CopyFile(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetProjectPath(TempDirectory projectDir)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(projectDir.Path, "project.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RunRestore(string args)
|
|
|
|
|
{
|
|
|
|
|
var restoreCommand = new RestoreCommand();
|
|
|
|
|
restoreCommand.Execute($"--quiet {args}").Should().Pass();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-11 21:40:47 +00:00
|
|
|
|
}
|