forked from microsoft/vstest
/
InferHelper.cs
188 lines (171 loc) · 7.38 KB
/
InferHelper.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CommandLineUtilities
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using Microsoft.VisualStudio.TestPlatform.CommandLine;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors;
using Microsoft.VisualStudio.TestPlatform.CommandLine.Processors.Utilities;
using Microsoft.VisualStudio.TestPlatform.Common.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
internal class InferHelper
{
private IAssemblyMetadataProvider assemblyMetadataProvider;
internal InferHelper(IAssemblyMetadataProvider assemblyMetadataProvider)
{
this.assemblyMetadataProvider = assemblyMetadataProvider;
}
/// <summary>
/// Determines Architecture from sources.
/// </summary>
public Architecture AutoDetectArchitecture(List<string> sources, IDictionary<string, Architecture> sourcePlatforms, Architecture defaultArchitecture)
{
var architecture = defaultArchitecture;
try
{
if (sources != null && sources.Count > 0)
{
Architecture? finalArch = null;
foreach (string source in sources)
{
Architecture arch;
if (IsDotNETAssembly(source))
{
arch = assemblyMetadataProvider.GetArchitecture(source);
}
else
{
// Set AnyCPU for non dotnet test sources (js, py and other). Otherwise warning will
// show up if there is mismatch with user provided platform.
arch = Architecture.AnyCPU;
}
sourcePlatforms[source]=(Architecture)arch;
if (Architecture.AnyCPU.Equals(arch))
{
// If arch is AnyCPU ignore it.
continue;
}
if (finalArch == null)
{
finalArch = arch;
continue;
}
if (!finalArch.Equals(arch))
{
finalArch = defaultArchitecture;
EqtTrace.Info("Conflict in platform architecture, using default platform:{0}", finalArch);
}
}
if (finalArch != null)
{
architecture = (Architecture)finalArch;
}
}
}
catch (Exception ex)
{
EqtTrace.Error("Failed to determine platform: {0}, using default: {1}", ex, architecture);
}
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Determined platform for all sources: {0}", architecture);
}
return architecture;
}
/// <summary>
/// Determines Framework from sources.
/// </summary>
public Framework AutoDetectFramework(List<string> sources, IDictionary<string, Framework> sourceFrameworkVersions)
{
Framework framework = Framework.DefaultFramework;
try
{
if (sources != null && sources.Count > 0)
{
var finalFx = DetermineFrameworkName(sources, sourceFrameworkVersions, out var conflictInFxIdentifier);
framework = Framework.FromString(finalFx.FullName);
if (conflictInFxIdentifier && EqtTrace.IsInfoEnabled)
{
// TODO Log to console and client.
EqtTrace.Info(
"conflicts in Framework indentifier of provided sources(test assemblies), using default framework:{0}",
framework);
}
}
}
catch (Exception ex)
{
EqtTrace.Error("Failed to determine framework:{0}, using defaulf: {1}", ex, framework);
}
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Determined framework for all sources: {0}", framework);
}
return framework;
}
private FrameworkName DetermineFrameworkName(IEnumerable<string> sources, IDictionary<string, Framework> sourceFrameworkVersions, out bool conflictInFxIdentifier)
{
FrameworkName finalFx = null;
conflictInFxIdentifier = false;
foreach (string source in sources)
{
FrameworkName fx;
if (IsDotNETAssembly(source))
{
fx = assemblyMetadataProvider.GetFrameWork(source);
}
else
{
// TODO What else to do with appx, js and other?
var extension = Path.GetExtension(source);
if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase))
{
// Currently to run tests for .NET Core, assembly need dependency to Microsoft.NET.Test.Sdk. Which is not
// possible for js files. So using default .NET Full framework version.
fx = new FrameworkName(Constants.DotNetFramework40);
}
else if (extension.Equals(".appx", StringComparison.OrdinalIgnoreCase)
|| extension.Equals(".appxrecipe", StringComparison.OrdinalIgnoreCase))
{
fx = new FrameworkName(Constants.DotNetFrameworkUap10);
}
else
{
fx = new FrameworkName(Framework.DefaultFramework.Name);
}
}
sourceFrameworkVersions[source] = Framework.FromString(fx.FullName);
if (finalFx == null)
{
finalFx = fx;
continue;
}
if (finalFx.Identifier.Equals(fx.Identifier))
{
// Use latest version.
if (finalFx.Version < fx.Version)
{
finalFx = fx;
}
}
else
{
conflictInFxIdentifier = true;
finalFx = new FrameworkName(Framework.DefaultFramework.Name);
}
}
return finalFx;
}
private bool IsDotNETAssembly(string filePath)
{
var extType = Path.GetExtension(filePath);
return extType != null && (extType.Equals(".dll", StringComparison.OrdinalIgnoreCase) ||
extType.Equals(".exe", StringComparison.OrdinalIgnoreCase));
}
}
}