Replace TestHelper with TestAssetsManager
This commit is contained in:
parent
f7e4714dc5
commit
13f97b0cb8
3 changed files with 41 additions and 137 deletions
|
@ -4,8 +4,10 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.ProjectModel.Server.Tests.Helpers;
|
||||
using Microsoft.DotNet.ProjectModel.Graph;
|
||||
using Microsoft.DotNet.TestFramework;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
@ -13,22 +15,40 @@ using Xunit;
|
|||
|
||||
namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
||||
{
|
||||
public class DthTests : IClassFixture<TestHelper>
|
||||
public class DthTests : TestBase
|
||||
{
|
||||
private readonly TestHelper _testHelper;
|
||||
private readonly TestAssetsManager _testAssetsManager;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
public DthTests(TestHelper helper)
|
||||
public DthTests()
|
||||
{
|
||||
_testHelper = helper;
|
||||
_loggerFactory = new LoggerFactory();
|
||||
|
||||
var testVerbose = Environment.GetEnvironmentVariable("DOTNET_TEST_VERBOSE");
|
||||
if (testVerbose == "2")
|
||||
{
|
||||
_loggerFactory.AddConsole(LogLevel.Trace);
|
||||
}
|
||||
else if (testVerbose == "1")
|
||||
{
|
||||
_loggerFactory.AddConsole(LogLevel.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
_loggerFactory.AddConsole(LogLevel.Warning);
|
||||
}
|
||||
|
||||
_testAssetsManager = new TestAssetsManager(
|
||||
Path.Combine(RepoRoot, "TestAssets", "ProjectModelServer", "DthTestProjects", "src"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DthStartup_GetProjectInformation()
|
||||
{
|
||||
var projectPath = _testHelper.FindSampleProject("EmptyConsoleApp");
|
||||
var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, "EmptyConsoleApp");
|
||||
Assert.NotNull(projectPath);
|
||||
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
client.Initialize(projectPath);
|
||||
|
@ -58,7 +78,7 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
[InlineData(3, 3)]
|
||||
public void DthStartup_ProtocolNegotiation(int requestVersion, int expectVersion)
|
||||
{
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
client.SetProtocolVersion(requestVersion);
|
||||
|
@ -73,7 +93,7 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
[Fact]
|
||||
public void DthStartup_ProtocolNegotiation_ZeroIsNoAllowed()
|
||||
{
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
client.SetProtocolVersion(0);
|
||||
|
@ -100,10 +120,10 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
return;
|
||||
}
|
||||
|
||||
var projectPath = _testHelper.FindSampleProject(testProjectName);
|
||||
var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, testProjectName);
|
||||
Assert.NotNull(projectPath);
|
||||
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
client.Initialize(projectPath);
|
||||
|
@ -157,12 +177,14 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
[Fact]
|
||||
public void DthNegative_BrokenProjectPathInLockFile()
|
||||
{
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
// After restore the project is copied to another place so that
|
||||
// the relative path in project lock file is invalid.
|
||||
var movedProjectPath = _testHelper.BuildProjectCopy("BrokenProjectPathSample");
|
||||
var movedProjectPath = _testAssetsManager.CreateTestInstance("BrokenProjectPathSample")
|
||||
.WithLockFiles()
|
||||
.TestRoot;
|
||||
|
||||
client.Initialize(movedProjectPath);
|
||||
|
||||
|
@ -185,10 +207,11 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
[Fact(Skip = "Require dotnet restore integration test")]
|
||||
public void DthDependencies_UpdateGlobalJson_RefreshDependencies()
|
||||
{
|
||||
var projectPath = _testHelper.CreateSampleProject("DthUpdateSearchPathSample");
|
||||
var assets = new TestAssetsManager(Path.Combine(AppContext.BaseDirectory, "TestAssets", "ProjectModelServer"));
|
||||
var projectPath = assets.CreateTestInstance("DthUpdateSearchPathSample").WithLockFiles().TestRoot;
|
||||
Assert.True(Directory.Exists(projectPath));
|
||||
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
var testProject = Path.Combine(projectPath, "home", "src", "MainProject");
|
||||
|
@ -243,10 +266,9 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
|
|||
[Fact]
|
||||
public void DthStartup_OpenProjectBeforeRestore()
|
||||
{
|
||||
var projectPath = _testHelper.BuildProjectCopy("EmptyConsoleApp");
|
||||
_testHelper.DeleteLockFile(projectPath);
|
||||
var projectPath = _testAssetsManager.CreateTestInstance("EmptyConsoleApp").TestRoot;
|
||||
|
||||
using (var server = new DthTestServer(_testHelper.LoggerFactory))
|
||||
using (var server = new DthTestServer(_loggerFactory))
|
||||
using (var client = new DthTestClient(server))
|
||||
{
|
||||
client.Initialize(projectPath);
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.DotNet.ProjectModel.Graph;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Server.Tests.Helpers
|
||||
{
|
||||
public class TestHelper
|
||||
{
|
||||
private readonly string _tempPath;
|
||||
private readonly string _testProjectsDir;
|
||||
|
||||
public TestHelper()
|
||||
{
|
||||
LoggerFactory = new LoggerFactory();
|
||||
|
||||
var testVerbose = Environment.GetEnvironmentVariable("DOTNET_TEST_VERBOSE");
|
||||
if (testVerbose == "2")
|
||||
{
|
||||
LoggerFactory.AddConsole(LogLevel.Trace);
|
||||
}
|
||||
else if (testVerbose == "1")
|
||||
{
|
||||
LoggerFactory.AddConsole(LogLevel.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
LoggerFactory.AddConsole(LogLevel.Warning);
|
||||
}
|
||||
|
||||
_tempPath = CreateTempFolder();
|
||||
_testProjectsDir = Path.Combine(AppContext.BaseDirectory, "TestAssets", "ProjectModelServer");
|
||||
CopyFiles(_testProjectsDir, _tempPath);
|
||||
|
||||
var logger = LoggerFactory.CreateLogger<TestHelper>();
|
||||
logger.LogInformation($"Test projects are copied to {_tempPath}");
|
||||
}
|
||||
|
||||
public ILoggerFactory LoggerFactory { get; }
|
||||
|
||||
public string FindSampleProject(string name)
|
||||
{
|
||||
var result = Path.Combine(_tempPath, "DthTestProjects", "src", name);
|
||||
if (Directory.Exists(result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string CreateSampleProject(string name)
|
||||
{
|
||||
var source = Path.Combine(AppContext.BaseDirectory, "TestAssets", "ProjectModelServer");
|
||||
if (!Directory.Exists(source))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var target = Path.Combine(CreateTempFolder(), name);
|
||||
CopyFiles(source, target);
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
public string BuildProjectCopy(string projectName)
|
||||
{
|
||||
var projectPath = FindSampleProject(projectName);
|
||||
var movedProjectPath = Path.Combine(CreateTempFolder(), projectName);
|
||||
CopyFiles(projectPath, movedProjectPath);
|
||||
|
||||
return movedProjectPath;
|
||||
}
|
||||
|
||||
public void DeleteLockFile(string folder)
|
||||
{
|
||||
var lockFilePath = Path.Combine(folder, LockFile.FileName);
|
||||
if (File.Exists(lockFilePath))
|
||||
{
|
||||
File.Delete(lockFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
private static string CreateTempFolder()
|
||||
{
|
||||
var result = Path.GetTempFileName();
|
||||
File.Delete(result);
|
||||
Directory.CreateDirectory(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void CopyFiles(string sourceFolder, string targetFolder)
|
||||
{
|
||||
if (!Directory.Exists(targetFolder))
|
||||
{
|
||||
Directory.CreateDirectory(targetFolder);
|
||||
}
|
||||
|
||||
foreach (var filePath in Directory.EnumerateFiles(sourceFolder))
|
||||
{
|
||||
var filename = Path.GetFileName(filePath);
|
||||
File.Copy(filePath, Path.Combine(targetFolder, filename));
|
||||
}
|
||||
|
||||
foreach (var folderPath in Directory.EnumerateDirectories(sourceFolder))
|
||||
{
|
||||
var folderName = new DirectoryInfo(folderPath).Name;
|
||||
CopyFiles(folderPath, Path.Combine(targetFolder, folderName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,8 +11,5 @@
|
|||
"imports": "portable-net45+win8"
|
||||
}
|
||||
},
|
||||
"content": [
|
||||
"../../TestAssets/ProjectModelServer/**"
|
||||
],
|
||||
"testRunner": "xunit"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue