Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit f67a72d

Browse files
committed
Making restore use a config file so that it does not use fallback folders that may exist in the machine.
1 parent fd95373 commit f67a72d

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

build/Microsoft.DotNet.Cli.Prepare.targets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@
281281
<CallTarget Targets="CleanSrcLockFiles" />
282282

283283
<DotNetRestore ToolPath="$(DotNetPath)"
284-
ProjectPath="&quot;%(RestoreSrcPackagesInput.FullPath)&quot;" />
284+
ProjectPath="&quot;%(RestoreSrcPackagesInput.FullPath)&quot;"
285+
ConfigFile="$(RepoRoot)\NuGet.Config" />
285286

286287
</Target>
287288

@@ -306,7 +307,8 @@
306307
<CallTarget Targets="CleanToolsLockFiles" />
307308

308309
<DotNetRestore ToolPath="$(DotNetPath)"
309-
ProjectPath="&quot;%(RestoreToolsPackagesInput.FullPath)&quot;" />
310+
ProjectPath="&quot;%(RestoreToolsPackagesInput.FullPath)&quot;"
311+
ConfigFile="$(RepoRoot)\NuGet.Config" />
310312

311313
</Target>
312314

src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ private void PrimeCacheUsingArchive(string extractedPackagesArchiveDirectory)
8585
using (var temporaryDotnetNewDirectory = _directory.CreateTemporaryDirectory())
8686
{
8787
var workingDirectory = temporaryDotnetNewDirectory.DirectoryPath;
88+
var nugetConfigPath = Path.Combine(workingDirectory, "NuGet.Config");
89+
90+
_file.WriteAllText(
91+
nugetConfigPath,
92+
$@"<?xml version=""1.0"" encoding=""utf-8""?>
93+
<configuration>
94+
<packageSources>
95+
<add key=""extractedArchive"" value=""{extractedPackagesArchiveDirectory}"" />
96+
</packageSources>
97+
</configuration>");
8898

8999
File.WriteAllText(
90100
Path.Combine(workingDirectory, "global.json"),
@@ -98,7 +108,7 @@ private void PrimeCacheUsingArchive(string extractedPackagesArchiveDirectory)
98108

99109
if (succeeded)
100110
{
101-
succeeded &= RestoreTemporaryProject(extractedPackagesArchiveDirectory, workingDirectory);
111+
succeeded &= RestoreTemporaryProject(nugetConfigPath, workingDirectory);
102112
}
103113
}
104114
}
@@ -118,11 +128,11 @@ private bool CreateTemporaryProject(string workingDirectory, IReadOnlyList<strin
118128
workingDirectory);
119129
}
120130

121-
private bool RestoreTemporaryProject(string extractedPackagesArchiveDirectory, string workingDirectory)
131+
private bool RestoreTemporaryProject(string nugetConfigPath, string workingDirectory)
122132
{
123133
return RunCommand(
124134
"restore",
125-
new[] { "-s", extractedPackagesArchiveDirectory },
135+
new[] { "--configfile", nugetConfigPath },
126136
workingDirectory);
127137
}
128138

src/Microsoft.DotNet.InternalAbstractions/FileWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@ public void CreateEmptyFile(string path)
4040
{
4141
}
4242
}
43+
44+
public void WriteAllText(string path, string content)
45+
{
46+
File.WriteAllText(path, content);
47+
}
4348
}
4449
}

src/Microsoft.DotNet.InternalAbstractions/IFile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ Stream OpenFile(
2222
FileOptions fileOptions);
2323

2424
void CreateEmptyFile(string path);
25+
26+
void WriteAllText(string path, string content);
2527
}
2628
}

test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.IO;
67
using FluentAssertions;
78
using Microsoft.DotNet.Cli.Utils;
89
using Microsoft.DotNet.Tools.Test.Utilities.Mock;
@@ -150,12 +151,26 @@ public void It_actually_runs_dotnet_new()
150151
}
151152

152153
[Fact]
153-
public void It_uses_the_packages_archive_with_dotnet_restore()
154+
public void It_writes_a_config_file_with_the_extracted_archive_as_a_package_source()
154155
{
156+
var nugetConfigPath = Path.Combine(_temporaryDirectoryMock.DirectoryPath, "NuGet.Config");
157+
_fileSystemMock.File.ReadAllText(nugetConfigPath).Should().Be(
158+
$@"<?xml version=""1.0"" encoding=""utf-8""?>
159+
<configuration>
160+
<packageSources>
161+
<add key=""extractedArchive"" value=""{PACKAGES_ARCHIVE_PATH}"" />
162+
</packageSources>
163+
</configuration>");
164+
}
165+
166+
[Fact]
167+
public void It_uses_a_config_file_with_dotnet_restore()
168+
{
169+
var nugetConfigPath = Path.Combine(_temporaryDirectoryMock.DirectoryPath, "NuGet.Config");
155170
_commandFactoryMock.Verify(
156171
c => c.Create(
157172
"restore",
158-
new[] { "-s", $"{PACKAGES_ARCHIVE_PATH}" },
173+
new[] { "--configfile", nugetConfigPath },
159174
null,
160175
Constants.DefaultConfiguration),
161176
Times.Exactly(2));

test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public void CreateEmptyFile(string path)
175175
{
176176
throw new NotImplementedException();
177177
}
178+
179+
public void WriteAllText(string path, string content)
180+
{
181+
throw new NotImplementedException();
182+
}
178183
}
179184

180185
private class MockStream : MemoryStream

test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public FileSystemMock(Dictionary<string, string> files, string temporaryFolder)
6060
private class FileMock : IFile
6161
{
6262
private Dictionary<string, string> _files;
63+
6364
public FileMock(Dictionary<string, string> files)
6465
{
6566
_files = files;
@@ -100,6 +101,11 @@ public void CreateEmptyFile(string path)
100101
{
101102
_files.Add(path, string.Empty);
102103
}
104+
105+
public void WriteAllText(string path, string content)
106+
{
107+
_files[path] = content;
108+
}
103109
}
104110

105111
private class DirectoryMock : IDirectory

test/dotnet-restore.Tests/GivenThatIWantToRestoreApp.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Microsoft.DotNet.Restore.Tests
1414
{
1515
public class GivenThatIWantToRestoreApp : TestBase
1616
{
17+
private static string RepoRootNuGetConfig = Path.Combine(RepoDirectoriesProvider.RepoRoot, "NuGet.Config");
18+
1719
[Fact]
1820
public void ItRestoresAppToSpecificDirectory()
1921
{
@@ -29,7 +31,7 @@ public void ItRestoresAppToSpecificDirectory()
2931
.Should()
3032
.Pass();
3133

32-
string args = $"--packages \"{dir}\"";
34+
string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\"";
3335
new RestoreCommand()
3436
.WithWorkingDirectory(rootPath)
3537
.ExecuteWithCapturedOutput(args)
@@ -56,7 +58,7 @@ public void ItRestoresLibToSpecificDirectory()
5658
.Should()
5759
.Pass();
5860

59-
string args = $"--packages \"{dir}\"";
61+
string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\"";
6062
new RestoreCommand()
6163
.WithWorkingDirectory(rootPath)
6264
.ExecuteWithCapturedOutput(args)
@@ -76,7 +78,7 @@ public void ItRestoresTestAppToSpecificDirectory()
7678
string dir = "pkgs";
7779
string fullPath = Path.GetFullPath(Path.Combine(rootPath, dir));
7880

79-
string args = $"--packages \"{dir}\"";
81+
string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\"";
8082
new RestoreCommand()
8183
.WithWorkingDirectory(rootPath)
8284
.ExecuteWithCapturedOutput(args)

0 commit comments

Comments
 (0)