-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestResourceFinder.cs
190 lines (169 loc) · 6.32 KB
/
TestResourceFinder.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
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PubComp.Building.NuGetPack.UnitTests
{
public static class TestResourceFinder
{
private const string BuildTemplateSourcesDir = @"src";
private const string BuildTemplateBinariesDir = @"bin";
public static void FindResources(
TestContext testContext,
string rootDirName,
string testDirName,
bool isAnyCpu,
out string rootPath,
out string testSrcDir,
out string testBinDir,
out string testRunDir,
out bool isLocal)
{
FindResourcesInternal(testContext,
rootDirName,
testDirName,
isAnyCpu,
false,
out rootPath,
out testSrcDir,
out testBinDir,
out testRunDir,
out isLocal);
}
public static void FindResourcesForWebApplication(
TestContext testContext,
string rootDirName,
string testDirName,
bool isAnyCpu,
out string rootPath,
out string testSrcDir,
out string testBinDir,
out string testRunDir,
out bool isLocal)
{
FindResourcesInternal(testContext,
rootDirName,
testDirName,
isAnyCpu,
true,
out rootPath,
out testSrcDir,
out testBinDir,
out testRunDir,
out isLocal);
}
private static void FindResourcesInternal(
TestContext testContext,
string rootDirName,
string testDirName,
bool isAnyCpu,
bool isWebApplication,
out string rootPath,
out string testSrcDir,
out string testBinDir,
out string testRunDir,
out bool isLocal)
{
testRunDir = testContext.DeploymentDirectory;
isLocal = false;
if (GetLocalRunRootPath(testContext, rootDirName, testDirName, isAnyCpu, isWebApplication, out rootPath,
out testSrcDir, out testBinDir))
{
isLocal = true;
return;
}
if (GetBuildServerRootPath(testContext, rootDirName, testDirName, out rootPath, out testSrcDir, out testBinDir))
{
return;
}
testSrcDir = null;
testBinDir = null;
testRunDir = null;
Assert.Fail("Could not find test resources directory. Search started at: " + testContext.DeploymentDirectory);
}
public static void CopyResources(
string sourceDir, string destDir, string filter = "*.*")
{
foreach (var file in Directory.EnumerateFiles(sourceDir, filter))
{
var src = file;
var dest = destDir + @"\" + Path.GetFileName(file);
if (!File.Exists(dest))
{
File.Copy(src, dest, false);
File.SetAttributes(dest, FileAttributes.Normal);
}
}
foreach (var dir in Directory.EnumerateDirectories(sourceDir))
{
var src = dir;
var dest = destDir + @"\" + Path.GetFileName(dir);
CopyResources(src, dest, filter);
}
}
private static bool GetLocalRunRootPath(
TestContext testContext, String rootDirName, String testDirName, bool isAnyCpu,
bool isWebApplication, out String rootPath, out String testSrcDir, out String testBinDir)
{
var current = Path.GetDirectoryName(testContext.DeploymentDirectory);
while (!current.EndsWith(@"\" + rootDirName))
{
current = Path.GetFullPath(current + @"\..");
if (current == Path.GetPathRoot(current))
{
rootPath = null;
testSrcDir = null;
testBinDir = null;
return false;
}
}
rootPath = current;
testSrcDir = rootPath + @"\" + testDirName;
testBinDir = rootPath + @"\" + testDirName + GetLocalBinOffset(isAnyCpu, isWebApplication);
return true;
}
private static bool GetBuildServerRootPath(
TestContext testContext, String rootDirName, String testDirName,
out String rootPath, out String testSrcDir, out String testBinDir)
{
var current = Path.GetDirectoryName(testContext.DeploymentDirectory);
while (current != Path.GetPathRoot(current))
{
var sourcesPath = Path.GetFullPath(current + @"\" + BuildTemplateSourcesDir);
var binariesPath = Path.GetFullPath(current + @"\" + BuildTemplateBinariesDir);
var root = sourcesPath + @"\" + rootDirName;
if (Directory.Exists(sourcesPath) && Directory.Exists(binariesPath) && Directory.Exists(root))
{
rootPath = root;
testSrcDir = root + @"\" + testDirName;
testBinDir = binariesPath;
return true;
}
current = Path.GetFullPath(current + @"\..");
}
rootPath = null;
testSrcDir = null;
testBinDir = null;
return false;
}
private static string GetLocalBinOffset(bool isAnyCpu, bool isWebApplication)
{
var binOffSet = @"\bin";
if (!isAnyCpu)
{
if (IntPtr.Size == 8)
binOffSet += @"\x64";
else
binOffSet += @"\x86";
}
if (!isWebApplication)
{
#if DEBUG
binOffSet += @"\Debug";
#else
binOffSet += @"\Release";
#endif
}
return binOffSet;
}
}
}