-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GivenAnAssetsFileResolver.cs
204 lines (186 loc) · 9.43 KB
/
GivenAnAssetsFileResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using FluentAssertions;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Versioning;
using Xunit;
namespace Microsoft.NET.Build.Tasks.UnitTests
{
/// <summary>
/// Tests that AssetsFileResolver resolves files correctly.
/// </summary>
public class GivenAnAssetsFileResolver
{
[Theory]
[MemberData(nameof(ProjectData))]
public void ItResolvesAssembliesFromProjectLockFiles(string projectName, string runtime, object[] expectedResolvedFiles)
{
LockFile lockFile = TestLockFiles.GetLockFile(projectName);
ProjectContext projectContext = lockFile.CreateProjectContext(
FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
runtime,
Constants.DefaultPlatformLibrary,
runtimeFrameworks: null,
isSelfContained: false);
IEnumerable<ResolvedFile> resolvedFiles = new AssetsFileResolver(new MockPackageResolver())
.Resolve(projectContext);
resolvedFiles
.Should()
.BeEquivalentTo(expectedResolvedFiles);
}
[Theory]
[MemberData(nameof(ProjectData1))]
public void ItResolvesAssembliesFromProjectLockFilesWithStoreLayout(string projectName, string runtime, object[] expectedResolvedFiles)
{
LockFile lockFile = TestLockFiles.GetLockFile(projectName);
ProjectContext projectContext = lockFile.CreateProjectContext(
FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
runtime,
Constants.DefaultPlatformLibrary,
runtimeFrameworks: null,
isSelfContained: false);
IEnumerable<ResolvedFile> resolvedFiles = new AssetsFileResolver(new MockPackageResolver())
.WithPreserveStoreLayout(true)
.Resolve(projectContext);
resolvedFiles
.Should()
.BeEquivalentTo(expectedResolvedFiles);
}
public static IEnumerable<object[]> ProjectData
{
get
{
return new[]
{
new object[] {
"dotnet.new",
null,
new object[] { }
},
new object[] {
"simple.dependencies",
null,
new object[] {
CreateResolvedFileForTFM("Newtonsoft.Json", "9.0.1", "netstandard1.0"),
CreateResolvedFileForTFM("System.Runtime.Serialization.Primitives", "4.1.1", "netstandard1.3"),
CreateResolvedFileForTFM("System.Collections.NonGeneric", "4.0.1", "netstandard1.3"),
}
},
new object[] {
"all.asset.types",
null,
new object[] {
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/osx/native/libuv.dylib"),
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/win7-x64/native/libuv.dll"),
CreateResolvedFileForTFM("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa"),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "de"),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "zh-Hant"),
}
},
new object[] {
"all.asset.types",
"osx.10.11-x64",
new object[] {
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/osx/native/libuv.dylib", destinationSubDirectory: null, preserveStoreLayout: false),
CreateResolvedFileForTFM("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa"),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "de"),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "zh-Hant"),
}
},
};
}
}
public static IEnumerable<object[]> ProjectData1
{
get
{
return new[]
{
new object[] {
"dotnet.new",
null,
new object[] { }
},
new object[] {
"simple.dependencies",
null,
new object[] {
CreateResolvedFileForTFM("Newtonsoft.Json", "9.0.1", "netstandard1.0", true),
CreateResolvedFileForTFM("System.Runtime.Serialization.Primitives", "4.1.1", "netstandard1.3", true),
CreateResolvedFileForTFM("System.Collections.NonGeneric", "4.0.1", "netstandard1.3", true),
}
},
new object[] {
"all.asset.types",
null,
new object[] {
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/osx/native/libuv.dylib", true),
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/win7-x64/native/libuv.dll", true),
CreateResolvedFileForTFM("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", true),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "de", true),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "zh-Hant", true),
}
},
new object[] {
"all.asset.types",
"osx.10.11-x64",
new object[] {
CreateNativeResolvedFile("Libuv", "1.9.0", "runtimes/osx/native/libuv.dylib", destinationSubDirectory: null, preserveStoreLayout: true),
CreateResolvedFileForTFM("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", true),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "de", true),
CreateResourceResolvedFile("System.Spatial", "5.7.0", "portable-net45+wp8+win8+wpa", "zh-Hant", true),
}
},
};
}
}
private static ResolvedFile CreateResolvedFileForTFM(string packageId, string version, string tfm, bool preserveStoreLayout = false)
{
return CreateResolvedFile(packageId, version, $"lib/{tfm}/{packageId}.dll", null, preserveStoreLayout, AssetType.Runtime);
}
private static ResolvedFile CreateResourceResolvedFile(string packageId, string version, string tfm, string locale, bool preserveStoreLayout = false)
{
return CreateResolvedFile(packageId, version, $"lib/{tfm}/{locale}/{packageId}.resources.dll", locale, preserveStoreLayout, AssetType.Resources);
}
private static ResolvedFile CreateNativeResolvedFile(string packageId, string version, string filePath, bool preserveStoreLayout = false)
{
return CreateNativeResolvedFile(packageId, version, filePath, Path.GetDirectoryName(filePath), preserveStoreLayout);
}
private static ResolvedFile CreateNativeResolvedFile(
string packageId,
string version,
string filePath,
string destinationSubDirectory,
bool preserveStoreLayout)
{
return CreateResolvedFile(packageId, version, filePath, destinationSubDirectory, preserveStoreLayout, AssetType.Native);
}
private static ResolvedFile CreateResolvedFile(
string packageId,
string version,
string filePath,
string destinationSubDirectory,
bool preserveStoreLayout,
AssetType assetType)
{
string packageRoot;
string packageDirectory = new MockPackageResolver()
.GetPackageDirectory(packageId, NuGetVersion.Parse(version), out packageRoot);
assetType.Should().NotBe(AssetType.None);
string sourcepath = Path.Combine(packageDirectory, filePath);
string sourcedir = Path.GetDirectoryName(sourcepath);
string destinationSubDirPath = preserveStoreLayout ? sourcedir.Substring(packageRoot.Length) : destinationSubDirectory;
if (!String.IsNullOrEmpty(destinationSubDirPath) && !destinationSubDirPath.EndsWith(Path.DirectorySeparatorChar))
{
destinationSubDirPath += Path.DirectorySeparatorChar;
}
return new ResolvedFile(
sourcepath,
destinationSubDirPath,
new PackageIdentity(packageId, NuGetVersion.Parse(version)),
assetType);
}
}
}