Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NuGetTestHelpers.cs « Microsoft.NuGet.Build.Tasks.Tests « src - github.com/mono/NuGet.BuildTasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f31e9d0a76a872fb7e9e2b60a0b1d7b31d43da5b (plain)
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
// Copyright (c) .NET Foundation. All rights reserved. 
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.NuGet.Build.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
using Microsoft.NuGet.Build.Tasks.Tests.Helpers;

namespace Microsoft.NuGet.Build.Tasks.Tests
{
    internal static class NuGetTestHelpers
    {
        public static ResolvePackagesResult ResolvePackagesWithJsonFileContents(
            string projectLockJsonFileContents,
            string targetMoniker,
            string runtimeIdentifier,
            string projectLanguage = null,
            bool allowFallbackOnTargetSelection = false,
            TryGetRuntimeVersion tryGetRuntimeVersion = null,
            bool includeFrameworkReferences = true,
            string projectJsonFileContents = null,
            bool createTemporaryFolderForPackages = true)
        {
            var rootDirectory = new TempRoot();
            using (rootDirectory)
            {
                var projectDirectory = rootDirectory.CreateDirectory();

                var projectLockJsonFile = projectDirectory.CreateFile("project.lock.json");
                projectLockJsonFile.WriteAllText(projectLockJsonFileContents);

                if (projectJsonFileContents != null)
                {
                    var projectJsonFile = projectDirectory.CreateFile("project.json");
                    projectJsonFile.WriteAllText(projectJsonFileContents);
                }

                var filesInPackages = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                DisposableDirectory packagesDirectory = null;

                if (createTemporaryFolderForPackages)
                {
                    packagesDirectory = rootDirectory.CreateDirectory();

                    foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, packagesDirectory.Path))
                    {
                        filesInPackages.Add(fileInPackage);
                    }
                }
                else
                {
                    // We will assume there is a location in the lock file we're using
                    var lockFile = JObject.Parse(projectLockJsonFileContents);
                    var firstLocation = ((JObject)lockFile["packageFolders"]).Properties().First().Name;

                    foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, firstLocation))
                    {
                        filesInPackages.Add(fileInPackage);
                    }
                }

                // Don't require the packages be restored on the machine
                ResolveNuGetPackageAssets task = null;
                FileExists fileExists = path => filesInPackages.Contains(path) || File.Exists(path);

                task = new ResolveNuGetPackageAssets(fileExists, tryGetRuntimeVersion);
                var sw = new StringWriter();
                task.BuildEngine = new MockBuildEngine(sw);

                task.AllowFallbackOnTargetSelection = allowFallbackOnTargetSelection;
                task.IncludeFrameworkReferences = includeFrameworkReferences;
                task.NuGetPackagesDirectory = packagesDirectory?.Path;
                task.RuntimeIdentifier = runtimeIdentifier;
                task.ProjectLockFile = projectLockJsonFile.Path;
                task.ProjectLanguage = projectLanguage;
                task.TargetMonikers = new ITaskItem[] { new TaskItem(targetMoniker) };

                // When we create the task for unit-testing purposes, the constructor sets an internal bit which should always
                // cause task.Execute to throw.
                Assert.True(task.Execute());

                var analyzers = task.ResolvedAnalyzers;
                var copyLocalItems = task.ResolvedCopyLocalItems;
                var references = task.ResolvedReferences;
                var referencedPackages = task.ReferencedPackages;

                return new ResolvePackagesResult(analyzers, copyLocalItems, references, referencedPackages, rootDirectory.Root);
            }
        }

        /// <summary>
        /// Given the contents of a project.lock.json, figures out all the "fake" full paths that are in those packages as if
        /// those packages are restored on disk.
        /// </summary>
        private static IEnumerable<string> GetFakeFileNamesFromPackages(string projectJsonContents, string packagesDirectory)
        {
            var lockFile = JObject.Parse(projectJsonContents);

            foreach (JProperty library in lockFile["libraries"])
            {
                var files = library.Value["files"] as JArray;
                if (files != null)
                {
                    foreach (string file in files)
                    {
                        yield return Path.Combine(packagesDirectory, library.Name, file).Replace('/', '\\');
                    }
                }

                // Some earlier versions of NuGet didn't include the hash file in the file list, so fake that
                // in here.
                yield return Path.Combine(packagesDirectory, library.Name.Replace('/', '\\'), library.Name.Replace('/', '.') + ".nupkg.sha512");
            }
        }
    }
}