forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RARPrecomputedCache_Tests.cs
142 lines (131 loc) · 7.41 KB
/
RARPrecomputedCache_Tests.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.UnitTests;
using Microsoft.Build.Utilities;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;
namespace Microsoft.Build.Tasks.UnitTests
{
public class RARPrecomputedCache_Tests
{
[Fact]
public void TestPrecomputedCacheOutput()
{
using (TestEnvironment env = TestEnvironment.Create())
{
TransientTestFile standardCache = env.CreateFile(".cache");
ResolveAssemblyReference t = new ResolveAssemblyReference()
{
_cache = new SystemState()
};
t._cache.instanceLocalFileStateCache = new Dictionary<string, SystemState.FileState>() {
{ Path.Combine(standardCache.Path, "assembly1"), new SystemState.FileState(DateTime.Now) },
{ Path.Combine(standardCache.Path, "assembly2"), new SystemState.FileState(DateTime.Now) { Assembly = new Shared.AssemblyNameExtension("hi") } } };
t._cache.IsDirty = true;
t.StateFile = standardCache.Path;
t.WriteStateFile();
int standardLen = File.ReadAllText(standardCache.Path).Length;
File.Delete(standardCache.Path);
standardLen.ShouldBeGreaterThan(0);
string precomputedPath = standardCache.Path + ".cache";
t._cache.IsDirty = true;
t.AssemblyInformationCacheOutputPath = precomputedPath;
t.WriteStateFile();
File.Exists(standardCache.Path).ShouldBeFalse();
int preLen = File.ReadAllText(precomputedPath).Length;
preLen.ShouldBeGreaterThan(0);
preLen.ShouldNotBe(standardLen);
}
}
[Fact]
public void StandardCacheTakesPrecedence()
{
using (TestEnvironment env = TestEnvironment.Create())
{
TransientTestFile standardCache = env.CreateFile(".cache");
ResolveAssemblyReference rarWriterTask = new ResolveAssemblyReference()
{
_cache = new SystemState()
};
rarWriterTask._cache.instanceLocalFileStateCache = new Dictionary<string, SystemState.FileState>();
rarWriterTask.StateFile = standardCache.Path;
rarWriterTask._cache.IsDirty = true;
// Write standard cache
rarWriterTask.WriteStateFile();
string dllName = Path.Combine(Path.GetDirectoryName(standardCache.Path), "randomFolder", "dll.dll");
rarWriterTask._cache.instanceLocalFileStateCache.Add(dllName,
new SystemState.FileState(DateTime.Now)
{
Assembly = null,
RuntimeVersion = "v4.0.30319",
FrameworkNameAttribute = new System.Runtime.Versioning.FrameworkName(".NETFramework", Version.Parse("4.7.2"), "Profile"),
scatterFiles = new string[] { "first", "second" }
});
string precomputedCachePath = standardCache.Path + ".cache";
rarWriterTask.AssemblyInformationCacheOutputPath = precomputedCachePath;
rarWriterTask._cache.IsDirty = true;
// Write precomputed cache
rarWriterTask.WriteStateFile();
ResolveAssemblyReference rarReaderTask = new ResolveAssemblyReference();
rarReaderTask.StateFile = standardCache.Path;
rarReaderTask.AssemblyInformationCachePaths = new ITaskItem[]
{
new TaskItem(precomputedCachePath)
};
// At this point, we should have created two cache files: one "normal" one and one "precomputed" one.
// When we read the state file, it should read from the caches produced in a normal build. In this case,
// the normal cache does not have dll.dll, whereas the precomputed cache does, so it should not be
// present when we read it.
rarReaderTask.ReadStateFile(p => true);
rarReaderTask._cache.instanceLocalFileStateCache.ShouldNotContainKey(dllName);
}
}
[Fact]
public void TestPreComputedCacheInputMatchesOutput()
{
using (TestEnvironment env = TestEnvironment.Create())
{
TransientTestFile precomputedCache = env.CreateFile(".cache");
ResolveAssemblyReference rarWriterTask = new ResolveAssemblyReference()
{
_cache = new SystemState()
};
string dllName = Path.Combine(Path.GetDirectoryName(precomputedCache.Path), "randomFolder", "dll.dll");
rarWriterTask._cache.instanceLocalFileStateCache = new Dictionary<string, SystemState.FileState>() {
{ Path.Combine(precomputedCache.Path, "..", "assembly1", "assembly1"), new SystemState.FileState(DateTime.Now) },
{ Path.Combine(precomputedCache.Path, "assembly2"), new SystemState.FileState(DateTime.Now) { Assembly = new Shared.AssemblyNameExtension("hi") } },
{ dllName, new SystemState.FileState(DateTime.Now) {
Assembly = null,
RuntimeVersion = "v4.0.30319",
FrameworkNameAttribute = new System.Runtime.Versioning.FrameworkName(".NETFramework", Version.Parse("4.7.2"), "Profile"),
scatterFiles = new string[] { "first", "second" } } } };
rarWriterTask.AssemblyInformationCacheOutputPath = precomputedCache.Path;
rarWriterTask._cache.IsDirty = true;
// Throws an exception because precomputedCache.Path already exists.
Should.Throw<InvalidOperationException>(() => rarWriterTask.WriteStateFile());
File.Delete(precomputedCache.Path);
rarWriterTask.WriteStateFile();
ResolveAssemblyReference rarReaderTask = new ResolveAssemblyReference();
rarReaderTask.StateFile = precomputedCache.Path.Substring(0, precomputedCache.Path.Length - 6); // Not a real path; should not be used.
rarReaderTask.AssemblyInformationCachePaths = new ITaskItem[]
{
new TaskItem(precomputedCache.Path)
};
// At this point, the standard cache does not exist, so it defaults to reading the "precomputed" cache.
// Then we verify that the information contained in that cache matches what we'd expect.
rarReaderTask.ReadStateFile(p => true);
rarReaderTask._cache.instanceLocalFileStateCache.ShouldContainKey(dllName);
SystemState.FileState assembly3 = rarReaderTask._cache.instanceLocalFileStateCache[dllName];
assembly3.Assembly.ShouldBeNull();
assembly3.RuntimeVersion.ShouldBe("v4.0.30319");
assembly3.FrameworkNameAttribute.Version.ShouldBe(Version.Parse("4.7.2"));
assembly3.scatterFiles.Length.ShouldBe(2);
assembly3.scatterFiles[1].ShouldBe("second");
}
}
}
}