Debian Codegraph @

external/nuget-buildtasks/src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs

raw symbols SCIP
1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Linq;
8 using Microsoft.Build.Framework;
9 using Microsoft.Build.Utilities;
10 using Newtonsoft.Json.Linq;
11 using Xunit;
12 using Microsoft.NuGet.Build.Tasks.Tests.Helpers;
13
14 namespace Microsoft.NuGet.Build.Tasks.Tests
15 {
16 internal static class NuGetTestHelpers
17 {
18 public static ResolvePackagesResult ResolvePackagesWithJsonFileContents(
19 string projectLockJsonFileContents,
20 string targetMoniker,
21 string runtimeIdentifier,
22 bool isLockFileProjectJsonBased = true,
23 string projectLanguage = null,
24 bool allowFallbackOnTargetSelection = false,
25 TryGetRuntimeVersion tryGetRuntimeVersion = null,
26 bool includeFrameworkReferences = true,
27 string projectJsonFileContents = null,
28 bool createTemporaryFolderForPackages = true)
29 {
30 var rootDirectory = new TempRoot();
31 using (rootDirectory)
32 {
33 var projectDirectory = rootDirectory.CreateDirectory();
34
35 var projectLockJsonFile = projectDirectory.CreateFile(isLockFileProjectJsonBased ? "project.lock.json" : "project.assets.json");
36 projectLockJsonFile.WriteAllText(projectLockJsonFileContents);
37
38 if (projectJsonFileContents != null)
39 {
40 var projectJsonFile = projectDirectory.CreateFile("project.json");
41 projectJsonFile.WriteAllText(projectJsonFileContents);
42 }
43
44 var filesInPackages = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
45 DisposableDirectory packagesDirectory = null;
46
47 if (createTemporaryFolderForPackages)
48 {
49 packagesDirectory = rootDirectory.CreateDirectory();
50
51 foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, packagesDirectory.Path))
52 {
53 filesInPackages.Add(fileInPackage);
54 }
55 }
56 else
57 {
58 // We will assume there is a location in the lock file we're using
59 var lockFile = JObject.Parse(projectLockJsonFileContents);
60 var firstLocation = ((JObject)lockFile["packageFolders"]).Properties().First().Name;
61
62 foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, firstLocation))
63 {
64 filesInPackages.Add(fileInPackage);
65 }
66 }
67
68 // Don't require the packages be restored on the machine
69 ResolveNuGetPackageAssets task = null;
70 FileExists fileExists = path => filesInPackages.Contains(path) || File.Exists(path);
71
72 task = new ResolveNuGetPackageAssets(fileExists, tryGetRuntimeVersion);
73 var sw = new StringWriter();
74 task.BuildEngine = new MockBuildEngine(sw);
75
76 task.AllowFallbackOnTargetSelection = allowFallbackOnTargetSelection;
77 task.IncludeFrameworkReferences = includeFrameworkReferences;
78 task.NuGetPackagesDirectory = packagesDirectory?.Path;
79 task.RuntimeIdentifier = runtimeIdentifier;
80 task.ProjectLockFile = projectLockJsonFile.Path;
81 task.ProjectLanguage = projectLanguage;
82 task.TargetMonikers = new ITaskItem[] { new TaskItem(targetMoniker) };
83
84 // When we create the task for unit-testing purposes, the constructor sets an internal bit which should always
85 // cause task.Execute to throw.
86 Assert.True(task.Execute());
87
88 var analyzers = task.ResolvedAnalyzers;
89 var copyLocalItems = task.ResolvedCopyLocalItems;
90 var references = task.ResolvedReferences;
91 var referencedPackages = task.ReferencedPackages;
92
93 return new ResolvePackagesResult(analyzers, copyLocalItems, references, referencedPackages, rootDirectory.Root);
94 }
95 }
96
97 /// <summary>
98 /// Given the contents of a project.lock.json, figures out all the "fake" full paths that are in those packages as if
99 /// those packages are restored on disk.
100 /// </summary>
101 private static IEnumerable<string> GetFakeFileNamesFromPackages(string projectJsonContents, string packagesDirectory)
102 {
103 var lockFile = JObject.Parse(projectJsonContents);
104
105 foreach (JProperty library in lockFile["libraries"])
106 {
107 var files = library.Value["files"] as JArray;
108 if (files != null)
109 {
110 foreach (string file in files)
111 {
112 yield return Path.Combine(packagesDirectory, library.Name, file).Replace('/', '\\');
113 }
114 }
115
116 // Some earlier versions of NuGet didn't include the hash file in the file list, so fake that
117 // in here.
118 yield return Path.Combine(packagesDirectory, library.Name.Replace('/', '\\'), library.Name.Replace('/', '.') + ".nupkg.sha512");
119 }
120 }
121 }
122 }