This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PackageResolverTest.cs
96 lines (78 loc) · 3.69 KB
/
PackageResolverTest.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
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.Extensions.DependencyModel.Resolution;
using Xunit;
using F = Microsoft.Extensions.DependencyModel.Tests.TestLibraryFactory;
namespace Microsoft.Extensions.DependencyModel.Tests
{
public class PackageResolverTest
{
private static string PackagesPath = Path.Combine("package", "directory", "location");
[Fact]
public void ShouldUseEnvironmentVariableToGetDefaultLocation()
{
var environment = EnvironmentMockBuilder.Create()
.AddVariable("NUGET_PACKAGES", PackagesPath)
.Build();
var result = PackageCompilationAssemblyResolver.GetDefaultPackageDirectory(Platform.Unknown, environment);
result.Should().Be(PackagesPath);
}
[Fact]
public void ShouldUseNuGetUnderUserProfileOnWindows()
{
var environment = EnvironmentMockBuilder.Create()
.AddVariable("USERPROFILE", "User Profile")
.Build();
var result = PackageCompilationAssemblyResolver.GetDefaultPackageDirectory(Platform.Windows, environment);
result.Should().Be(Path.Combine("User Profile", ".nuget", "packages"));
}
[Fact]
public void ShouldUseNuGetUnderHomeOnNonWindows()
{
var environment = EnvironmentMockBuilder.Create()
.AddVariable("HOME", "User Home")
.Build();
var result = PackageCompilationAssemblyResolver.GetDefaultPackageDirectory(Platform.Linux, environment);
result.Should().Be(Path.Combine("User Home", ".nuget", "packages"));
}
[Fact]
public void ResolvesAllAssemblies()
{
var packagePath = GetPackagesPath(F.DefaultPackageName, F.DefaultVersion);
var fileSystem = FileSystemMockBuilder.Create()
.AddFiles(packagePath, F.TwoAssemblies)
.Build();
var library = F.Create(assemblies: F.TwoAssemblies);
var resolver = new PackageCompilationAssemblyResolver(fileSystem, PackagesPath);
var assemblies = new List<string>();
var result = resolver.TryResolveAssemblyPaths(library, assemblies);
assemblies.Should().HaveCount(2);
assemblies.Should().Contain(Path.Combine(packagePath, F.DefaultAssemblyPath));
assemblies.Should().Contain(Path.Combine(packagePath, F.SecondAssemblyPath));
}
[Fact]
public void FailsWhenOneOfAssembliesNotFound()
{
var packagePath = GetPackagesPath(F.DefaultPackageName, F.DefaultVersion);
var fileSystem = FileSystemMockBuilder.Create()
.AddFiles(packagePath, F.DefaultAssemblyPath)
.Build();
var library = F.Create(assemblies: F.TwoAssemblies);
var resolver = new PackageCompilationAssemblyResolver(fileSystem, PackagesPath);
var assemblies = new List<string>();
var exception = Assert.Throws<InvalidOperationException>(() => resolver.TryResolveAssemblyPaths(library, assemblies));
exception.Message.Should()
.Contain(F.SecondAssemblyPath)
.And.Contain(library.Name);
}
private static string GetPackagesPath(string id, string version)
{
return Path.Combine(PackagesPath, id.ToLowerInvariant(), version.ToLowerInvariant());
}
}
}