-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathBuildTasksUtilityTests.cs
More file actions
203 lines (175 loc) · 7.86 KB
/
BuildTasksUtilityTests.cs
File metadata and controls
203 lines (175 loc) · 7.86 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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 FluentAssertions;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Test.Utility;
using Test.Utility;
using Xunit;
namespace NuGet.Build.Tasks.Console.Test
{
public class BuildTasksUtilityTests
{
[Fact]
public void GetPackagesConfigFilePath_WithNonExistentPackagesConfig_ReturnsNull()
{
using (var testDirectory = TestDirectory.Create())
{
BuildTasksUtility.GetPackagesConfigFilePath(testDirectory, "Test")
.Should().BeNull();
}
}
[Fact]
public void GetPackagesConfigFilePath_WithNullProjectDirectory_ThrowsArgumentException()
{
Action action = () => { BuildTasksUtility.GetPackagesConfigFilePath(projectDirectory: null, projectName: "ProjectA"); };
action.Should().Throw<ArgumentException>();
}
[Fact]
public void GetPackagesConfigFilePath_WithNullProjectFullPath_ThrowsArgumentException()
{
Action action = () => { BuildTasksUtility.GetPackagesConfigFilePath(projectFullPath: null); };
action.Should().Throw<ArgumentException>();
}
[Fact]
public void GetPackagesConfigFilePath_WithNullProjectName_ThrowsArgumentException()
{
Action action = () => { BuildTasksUtility.GetPackagesConfigFilePath(projectDirectory: "SomePath", projectName: null); };
action.Should().Throw<ArgumentException>();
}
[Theory]
[InlineData("packages.config")]
[InlineData("packages.Test.config")]
public void GetPackagesConfigFilePath_WithProjectDirectoryAndProjectName_ReturnsPackagesConfig(string packagesConfigFilename)
{
using (var testDirectory = TestDirectory.Create())
{
string projectFullPath = Path.Combine(testDirectory, "Test.csproj");
string packagesConfigFilePath = Path.Combine(testDirectory, packagesConfigFilename);
File.WriteAllText(packagesConfigFilePath, string.Empty);
BuildTasksUtility.GetPackagesConfigFilePath(testDirectory, "Test")
.Should().Be(packagesConfigFilePath);
}
}
[Theory]
[InlineData("packages.config")]
[InlineData("packages.Test.config")]
public void GetPackagesConfigFilePath_WithProjectFullPath_ReturnsPackagesConfig(string packagesConfigFilename)
{
using (var testDirectory = TestDirectory.Create())
{
string projectFullPath = Path.Combine(testDirectory, "Test.csproj");
string packagesConfigFilePath = Path.Combine(testDirectory, packagesConfigFilename);
File.WriteAllText(packagesConfigFilePath, string.Empty);
BuildTasksUtility.GetPackagesConfigFilePath(projectFullPath)
.Should().Be(packagesConfigFilePath);
}
}
[Fact]
public void GetSources_WithRestoreSourcesProperty_ResolvesAgainstProjectDirectory()
{
using (var testDir = TestDirectory.CreateInTemp())
{
// Arrange
var startupDirectory = Path.Combine(testDir, "startup");
var projectDirectory = Path.Combine(testDir, "project");
var relativePath = "relativeSource";
// Act
var effectiveSources = BuildTasksUtility.GetSources(
startupDirectory: startupDirectory,
projectDirectory: projectDirectory,
sources: new string[] { relativePath },
sourcesOverride: null,
additionalProjectSources: Array.Empty<string>(),
settings: NullSettings.Instance
);
// Assert
effectiveSources.Should().BeEquivalentTo(new[] { Path.Combine(projectDirectory, relativePath) });
}
}
[Fact]
public void GetSources_WithRestoreSourcesGlobal_Property_ResolvesAgainstWorkingDirectory()
{
using (var testDir = TestDirectory.CreateInTemp())
{
// Arrange
var startupDirectory = Path.Combine(testDir, "startup");
var projectDirectory = Path.Combine(testDir, "project");
var relativePath = "relativeSource";
// Act
var effectiveSources = BuildTasksUtility.GetSources(
startupDirectory: startupDirectory,
projectDirectory: projectDirectory,
sources: new string[] { relativePath },
sourcesOverride: new string[] { relativePath },
additionalProjectSources: Array.Empty<string>(),
settings: NullSettings.Instance
);
// Assert
effectiveSources.Should().BeEquivalentTo(new[] { Path.Combine(startupDirectory, relativePath) });
}
}
[Fact]
public void GetSources_WithConfiguredSourceName_UsesConfiguredSourceValue()
{
using (var testDir = TestDirectory.CreateInTemp())
{
// Arrange
var startupDirectory = Path.Combine(testDir, "startup");
var projectDirectory = Path.Combine(testDir, "project");
var configuredSource = "https://configured-source/v3/index.json";
Directory.CreateDirectory(projectDirectory);
File.WriteAllText(
Path.Combine(projectDirectory, Settings.DefaultSettingsFileName),
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear />
<add key=""source_name"" value=""{configuredSource}"" />
</packageSources>
</configuration>");
var settings = Settings.LoadDefaultSettings(
root: projectDirectory,
configFileName: null,
machineWideSettings: null);
// Act
var effectiveSources = BuildTasksUtility.GetSources(
startupDirectory: startupDirectory,
projectDirectory: projectDirectory,
sources: new[] { "source_name" },
sourcesOverride: new[] { "source_name" },
additionalProjectSources: Array.Empty<string>(),
settings: settings
);
// Assert
effectiveSources.Should().BeEquivalentTo(new[] { configuredSource });
}
}
[Theory]
[InlineData("0", "false", 0)]
[InlineData("0", "true", 0)]
[InlineData("false", "false", 0)]
[InlineData("false", "true", 0)]
[InlineData(" 2 ", "false", 0)]
[InlineData(" 2 ", "true", 2)]
[InlineData("true", "false", 0)]
[InlineData("true", "true", 1)]
[InlineData(" 1 ", "false", 0)]
[InlineData(" 1 ", "true", 1)]
[InlineData("", "false", 0)]
[InlineData("", "true", 1)]
[InlineData(null, "false", 0)]
[InlineData(null, "true", 1)]
public void GetFilesToEmbedInBinlogValue_WithValue_ReturnsExpectedValue(string value, string binaryLoggerEnabled, int expected)
{
IEnvironmentVariableReader environmentVariableReader = new TestEnvironmentVariableReader(new Dictionary<string, string>
{
["MSBUILDBINARYLOGGERENABLED"] = binaryLoggerEnabled
});
BuildTasksUtility.GetFilesToEmbedInBinlogValue(value, environmentVariableReader).Should().Be(expected);
}
}
}