-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GivenAGetDependsOnNETStandardTask.cs
152 lines (136 loc) · 5.62 KB
/
GivenAGetDependsOnNETStandardTask.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using FluentAssertions;
using Microsoft.Build.Framework;
using Microsoft.Extensions.DependencyModel;
using Xunit;
using Microsoft.NET.Build.Tasks.ConflictResolution;
using System.Linq;
using System.IO;
namespace Microsoft.NET.Build.Tasks.UnitTests
{
public class GivenAGetDependsOnNETStandardTask
{
[Fact]
public void CanCheckThisAssembly()
{
var thisAssemblyPath = typeof(GivenAGetDependsOnNETStandardTask).GetTypeInfo().Assembly.Location;
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[] { new MockTaskItem() { ItemSpec = thisAssemblyPath } }
};
task.Execute().Should().BeTrue();
// this test compiles against a sufficiently high System.Runtime
task.DependsOnNETStandard.Should().BeTrue();
}
[Fact]
public void CanCheckThisAssemblyByHintPath()
{
var thisAssemblyPath = typeof(GivenAGetDependsOnNETStandardTask).GetTypeInfo().Assembly.Location;
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[]
{
new MockTaskItem(
Path.GetFileNameWithoutExtension(thisAssemblyPath),
new Dictionary<string, string>
{
{"HintPath", thisAssemblyPath }
})
}
};
task.Execute().Should().BeTrue();
// this test compiles against a sufficiently high System.Runtime
task.DependsOnNETStandard.Should().BeTrue();
}
[Fact]
public void ReturnsFalseForNonPE()
{
string testFile = $"testFile.{nameof(GivenAGetDependsOnNETStandardTask)}.{nameof(ReturnsFalseForNonPE)}.txt";
try
{
File.WriteAllText(testFile, "test file");
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[] { new MockTaskItem() { ItemSpec = testFile } }
};
// ensure that false is returned and no exception is thrown
task.Execute().Should().BeTrue();
task.DependsOnNETStandard.Should().BeFalse();
// warn for a non-PE file
((MockBuildEngine)task.BuildEngine).Warnings.Count.Should().BeGreaterThan(0);
}
finally
{
File.Delete(testFile);
}
}
[Fact]
public void ReturnsFalseForNativeLibrary()
{
var corelibLocation = typeof(object).GetTypeInfo().Assembly.Location;
var corelibFolder = Path.GetDirectoryName(corelibLocation);
// do our best to try and find it, intentionally only use the windows name since linux will not be a PE.
var coreclrLocation = Directory.EnumerateFiles(corelibFolder, "coreclr.dll").FirstOrDefault();
// if we can't find it, skip the test
if (coreclrLocation != null)
{
// ensure that false is returned and no warning is logged for native library
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[] { new MockTaskItem() { ItemSpec = coreclrLocation } }
};
task.Execute().Should().BeTrue();
task.DependsOnNETStandard.Should().BeFalse();
// don't warn for PE with no metadata
((MockBuildEngine)task.BuildEngine).Warnings.Count.Should().Be(0);
}
}
[Fact]
public void SucceedsOnMissingFileReturnsFalse()
{
var missingFile = $"{nameof(SucceedsOnMissingFileReturnsFalse)}.shouldNotExist.dll";
File.Exists(missingFile).Should().BeFalse();
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[] { new MockTaskItem() { ItemSpec = missingFile } }
};
task.Execute().Should().BeTrue();
task.DependsOnNETStandard.Should().BeFalse();
((MockBuildEngine)task.BuildEngine).Warnings.Count.Should().Be(0);
}
[Fact]
public void SucceedsWithWarningOnLockedFile()
{
var lockedFile = $"{nameof(SucceedsWithWarningOnLockedFile)}.dll";
try
{
var task = new GetDependsOnNETStandard()
{
BuildEngine = new MockBuildEngine(),
References = new[] { new MockTaskItem() { ItemSpec = lockedFile } }
};
// create file with no sharing
using (var fileHandle = new FileStream(lockedFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
task.Execute().Should().BeTrue();
task.DependsOnNETStandard.Should().BeFalse();
((MockBuildEngine)task.BuildEngine).Warnings.Count.Should().BeGreaterThan(0);
}
}
finally
{
File.Delete(lockedFile);
}
}
}
}