Asset refactoring and content files
This commit is contained in:
parent
388bb7260c
commit
a71112ce8f
34 changed files with 1301 additions and 173 deletions
|
@ -0,0 +1,299 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.ProjectModel.Compilation;
|
||||
using Microsoft.DotNet.ProjectModel.Graph;
|
||||
using Microsoft.DotNet.ProjectModel.Resolution;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Tests
|
||||
{
|
||||
public class LibraryExporterPackageTests
|
||||
{
|
||||
private const string PackagePath = "PackagePath";
|
||||
|
||||
private LibraryExport ExportSingle(LibraryDescription description = null)
|
||||
{
|
||||
var rootProject = new Project()
|
||||
{
|
||||
Name = "RootProject",
|
||||
CompilerName = "csc"
|
||||
};
|
||||
|
||||
var rootProjectDescription = new ProjectDescription(
|
||||
new LibraryRange(),
|
||||
rootProject,
|
||||
new LibraryRange[] { },
|
||||
new TargetFrameworkInformation(),
|
||||
true);
|
||||
|
||||
if (description == null)
|
||||
{
|
||||
description = rootProjectDescription;
|
||||
}
|
||||
else
|
||||
{
|
||||
description.Parents.Add(rootProjectDescription);
|
||||
}
|
||||
|
||||
var libraryManager = new LibraryManager(new[] { description }, new DiagnosticMessage[] { }, "");
|
||||
var allExports = new LibraryExporter(rootProjectDescription, libraryManager, "config", "runtime", "basepath", "solutionroot").GetAllExports();
|
||||
var export = allExports.Single();
|
||||
return export;
|
||||
}
|
||||
|
||||
private PackageDescription CreateDescription(LockFileTargetLibrary target = null, LockFilePackageLibrary package = null)
|
||||
{
|
||||
return new PackageDescription(PackagePath,
|
||||
package ?? new LockFilePackageLibrary(),
|
||||
target ?? new LockFileTargetLibrary(),
|
||||
new List<LibraryRange>(), true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsPackageNativeLibraries()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
NativeLibraries = new List<LockFileItem>()
|
||||
{
|
||||
{ new LockFileItem() { Path = "lib/Native.so" } }
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.NativeLibraries.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.NativeLibraries.First();
|
||||
libraryAsset.Name.Should().Be("Native");
|
||||
libraryAsset.Transform.Should().BeNull();
|
||||
libraryAsset.RelativePath.Should().Be("lib/Native.so");
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "lib/Native.so"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsPackageCompilationAssebmlies()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
CompileTimeAssemblies = new List<LockFileItem>()
|
||||
{
|
||||
{ new LockFileItem() { Path = "ref/Native.dll" } }
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.CompilationAssemblies.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.CompilationAssemblies.First();
|
||||
libraryAsset.Name.Should().Be("Native");
|
||||
libraryAsset.Transform.Should().BeNull();
|
||||
libraryAsset.RelativePath.Should().Be("ref/Native.dll");
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "ref/Native.dll"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsPackageRuntimeAssebmlies()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
RuntimeAssemblies = new List<LockFileItem>()
|
||||
{
|
||||
{ new LockFileItem() { Path = "ref/Native.dll" } }
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.RuntimeAssemblies.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.RuntimeAssemblies.First();
|
||||
libraryAsset.Name.Should().Be("Native");
|
||||
libraryAsset.Transform.Should().BeNull();
|
||||
libraryAsset.RelativePath.Should().Be("ref/Native.dll");
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "ref/Native.dll"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsSources()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
package: new LockFilePackageLibrary()
|
||||
{
|
||||
Files = new List<string>()
|
||||
{
|
||||
Path.Combine("shared", "file.cs")
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.SourceReferences.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.SourceReferences.First();
|
||||
libraryAsset.Name.Should().Be("file");
|
||||
libraryAsset.Transform.Should().BeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("shared", "file.cs"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "shared", "file.cs"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsCopyToOutputContentFiles()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
ContentFiles = new List<LockFileContentFile>()
|
||||
{
|
||||
new LockFileContentFile()
|
||||
{
|
||||
CopyToOutput = true,
|
||||
Path = Path.Combine("content", "file.txt"),
|
||||
OutputPath = Path.Combine("Out","Path.txt"),
|
||||
PPOutputPath = "something"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.RuntimeAssets.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.RuntimeAssets.First();
|
||||
libraryAsset.Transform.Should().NotBeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("Out", "Path.txt"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.txt"));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
private void ExportsResourceContentFiles()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
ContentFiles = new List<LockFileContentFile>()
|
||||
{
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.EmbeddedResource,
|
||||
Path = Path.Combine("content", "file.txt"),
|
||||
PPOutputPath = "something"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.EmbeddedResources.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.EmbeddedResources.First();
|
||||
libraryAsset.Transform.Should().NotBeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("content", "file.txt"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.txt"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void ExportsCompileContentFiles()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
ContentFiles = new List<LockFileContentFile>()
|
||||
{
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.cs"),
|
||||
PPOutputPath = "something"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.SourceReferences.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.SourceReferences.First();
|
||||
libraryAsset.Transform.Should().NotBeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("content", "file.cs"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.cs"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Fact]
|
||||
private void SelectsContentFilesOfProjectCodeLanguage()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
ContentFiles = new List<LockFileContentFile>()
|
||||
{
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.cs"),
|
||||
PPOutputPath = "something",
|
||||
CodeLanguage = "cs"
|
||||
},
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.vb"),
|
||||
PPOutputPath = "something",
|
||||
CodeLanguage = "vb"
|
||||
},
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.any"),
|
||||
PPOutputPath = "something",
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.SourceReferences.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.SourceReferences.First();
|
||||
libraryAsset.Transform.Should().NotBeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("content", "file.cs"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.cs"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void SelectsContentFilesWithNoLanguageIfProjectLanguageNotMathed()
|
||||
{
|
||||
var description = CreateDescription(
|
||||
new LockFileTargetLibrary()
|
||||
{
|
||||
ContentFiles = new List<LockFileContentFile>()
|
||||
{
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.vb"),
|
||||
PPOutputPath = "something",
|
||||
CodeLanguage = "vb"
|
||||
},
|
||||
new LockFileContentFile()
|
||||
{
|
||||
BuildAction = BuildAction.Compile,
|
||||
Path = Path.Combine("content", "file.any"),
|
||||
PPOutputPath = "something",
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = ExportSingle(description);
|
||||
result.SourceReferences.Should().HaveCount(1);
|
||||
|
||||
var libraryAsset = result.SourceReferences.First();
|
||||
libraryAsset.Transform.Should().NotBeNull();
|
||||
libraryAsset.RelativePath.Should().Be(Path.Combine("content", "file.any"));
|
||||
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.any"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Microsoft.DotNet.Tools.Compiler;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Tests
|
||||
{
|
||||
public class PreprocessorTests
|
||||
{
|
||||
private string Preprocess(string text, IDictionary<string, string> parameters)
|
||||
{
|
||||
using (var input = new MemoryStream(Encoding.UTF8.GetBytes(text)))
|
||||
{
|
||||
using (var output = new MemoryStream())
|
||||
{
|
||||
PPFilePreprocessor.Preprocess(input, output, parameters);
|
||||
return Encoding.UTF8.GetString(output.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("$a$", "AValue")]
|
||||
[InlineData("$a", "$a")]
|
||||
[InlineData("a$", "a$")]
|
||||
[InlineData("$$a$", "$a$")]
|
||||
[InlineData("$a$$b$", "AValueBValue")]
|
||||
[InlineData("$$a$$$$b$$", "$a$$b$")]
|
||||
|
||||
public void ProcessesCorrectly(string input, string output)
|
||||
{
|
||||
var parameters = new Dictionary<string, string>()
|
||||
{
|
||||
{ "a", "AValue" },
|
||||
{ "b", "BValue" }
|
||||
};
|
||||
var result = Preprocess(input, parameters);
|
||||
result.Should().Be(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsOnParameterNotFound()
|
||||
{
|
||||
var ex = Assert.Throws<InvalidOperationException>(() => Preprocess("$a$", new Dictionary<string, string>()));
|
||||
ex.Message.Should().Contain("a");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,9 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Compiler.Tests
|
||||
|
@ -106,6 +108,44 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
|
|||
buildCommand.Execute().Should().Pass();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContentFilesAreCopied()
|
||||
{
|
||||
var testInstance = TestAssetsManager.CreateTestInstance("TestAppWithContentPackage")
|
||||
.WithLockFiles();
|
||||
|
||||
var root = testInstance.TestRoot;
|
||||
|
||||
// run compile
|
||||
var outputDir = Path.Combine(root, "bin");
|
||||
var testProject = ProjectUtils.GetProjectJson(root, "TestAppWithContentPackage");
|
||||
var buildCommand = new BuildCommand(testProject, output: outputDir, framework: DefaultFramework);
|
||||
var result = buildCommand.ExecuteWithCapturedOutput();
|
||||
result.Should().Pass();
|
||||
|
||||
result = Command.Create(Path.Combine(outputDir, buildCommand.GetOutputExecutableName()), new string [0])
|
||||
.CaptureStdErr()
|
||||
.CaptureStdOut()
|
||||
.Execute();
|
||||
result.Should().Pass();
|
||||
|
||||
// verify the output xml file
|
||||
new DirectoryInfo(outputDir).Sub("scripts").Should()
|
||||
.Exist()
|
||||
.And.HaveFile("run.cmd");
|
||||
new DirectoryInfo(outputDir).Should()
|
||||
.HaveFile("config.xml");
|
||||
// verify embedded resources
|
||||
result.StdOut.Should().Contain("TestAppWithContentPackage.dnf.png");
|
||||
result.StdOut.Should().Contain("TestAppWithContentPackage.ui.png");
|
||||
// verify 'all' language files not included
|
||||
result.StdOut.Should().NotContain("TestAppWithContentPackage.dnf_all.png");
|
||||
result.StdOut.Should().NotContain("TestAppWithContentPackage.ui_all.png");
|
||||
// verify classes
|
||||
result.StdOut.Should().Contain("TestAppWithContentPackage.Foo");
|
||||
result.StdOut.Should().Contain("MyNamespace.Util");
|
||||
}
|
||||
|
||||
private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir)
|
||||
{
|
||||
// copy all the files to temp dir
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue