forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAssetExtensions.cs
More file actions
48 lines (43 loc) · 2 KB
/
TestAssetExtensions.cs
File metadata and controls
48 lines (43 loc) · 2 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System.Reflection;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
static class TestAssetExtensions
{
public static TestAsset WithVersionVariables(this TestAsset testAssetInstance)
{
var assemblyMetadata = typeof(TestAssetExtensions).Assembly
.GetCustomAttributes(typeof(AssemblyMetadataAttribute))
.Cast<AssemblyMetadataAttribute>()
.ToDictionary(a => a.Key, a => a.Value);
return testAssetInstance.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
foreach (var valueToReplace in new[] { "MSTestVersion", "MicrosoftNETTestSdkPackageVersion" })
{
var packageReferencesToUpdate =
project.Root.Descendants(ns + "PackageReference")
.Where(pr => pr.Attribute("Version").Value.Equals($"$({valueToReplace})",
StringComparison.OrdinalIgnoreCase));
foreach (var packageReference in packageReferencesToUpdate)
{
packageReference.Attribute("Version").Value = assemblyMetadata[valueToReplace];
}
}
});
}
// For tests which want the global packages folder isolated in the repo, but
// can share it with other tests
public static TestAsset WithRepoGlobalPackages(this TestAsset testAsset)
{
return testAsset.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
project.Root.Element(ns + "PropertyGroup")
.Add(new XElement(ns + "RestorePackagesPath", TestContext.Current.TestGlobalPackagesFolder));
});
}
}
}