diff --git a/build/Microsoft.DotNet.Cli.Prepare.targets b/build/Microsoft.DotNet.Cli.Prepare.targets index 0a47f2622..c241433d7 100644 --- a/build/Microsoft.DotNet.Cli.Prepare.targets +++ b/build/Microsoft.DotNet.Cli.Prepare.targets @@ -281,7 +281,8 @@ + ProjectPath=""%(RestoreSrcPackagesInput.FullPath)"" + ConfigFile="$(RepoRoot)\NuGet.Config" /> @@ -306,7 +307,8 @@ + ProjectPath=""%(RestoreToolsPackagesInput.FullPath)"" + ConfigFile="$(RepoRoot)\NuGet.Config" /> diff --git a/src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs b/src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs index 246d0f462..d4718c002 100644 --- a/src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs +++ b/src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs @@ -85,6 +85,16 @@ namespace Microsoft.DotNet.Configurer using (var temporaryDotnetNewDirectory = _directory.CreateTemporaryDirectory()) { var workingDirectory = temporaryDotnetNewDirectory.DirectoryPath; + var nugetConfigPath = Path.Combine(workingDirectory, "NuGet.Config"); + + _file.WriteAllText( + nugetConfigPath, + $@" + + + + +"); File.WriteAllText( Path.Combine(workingDirectory, "global.json"), @@ -98,7 +108,7 @@ namespace Microsoft.DotNet.Configurer if (succeeded) { - succeeded &= RestoreTemporaryProject(extractedPackagesArchiveDirectory, workingDirectory); + succeeded &= RestoreTemporaryProject(nugetConfigPath, workingDirectory); } } } @@ -118,11 +128,11 @@ namespace Microsoft.DotNet.Configurer workingDirectory); } - private bool RestoreTemporaryProject(string extractedPackagesArchiveDirectory, string workingDirectory) + private bool RestoreTemporaryProject(string nugetConfigPath, string workingDirectory) { return RunCommand( "restore", - new[] { "-s", extractedPackagesArchiveDirectory }, + new[] { "--configfile", nugetConfigPath }, workingDirectory); } diff --git a/src/Microsoft.DotNet.InternalAbstractions/FileWrapper.cs b/src/Microsoft.DotNet.InternalAbstractions/FileWrapper.cs index 9a71ba5d2..47de50767 100644 --- a/src/Microsoft.DotNet.InternalAbstractions/FileWrapper.cs +++ b/src/Microsoft.DotNet.InternalAbstractions/FileWrapper.cs @@ -40,5 +40,10 @@ namespace Microsoft.Extensions.EnvironmentAbstractions { } } + + public void WriteAllText(string path, string content) + { + File.WriteAllText(path, content); + } } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.InternalAbstractions/IFile.cs b/src/Microsoft.DotNet.InternalAbstractions/IFile.cs index 3e19c63f0..d29a6bc91 100644 --- a/src/Microsoft.DotNet.InternalAbstractions/IFile.cs +++ b/src/Microsoft.DotNet.InternalAbstractions/IFile.cs @@ -22,5 +22,7 @@ namespace Microsoft.Extensions.EnvironmentAbstractions FileOptions fileOptions); void CreateEmptyFile(string path); + + void WriteAllText(string path, string content); } } \ No newline at end of file diff --git a/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs index 3268bfed2..79769e8db 100644 --- a/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs +++ b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; +using System.IO; using FluentAssertions; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.Test.Utilities.Mock; @@ -150,12 +151,26 @@ namespace Microsoft.DotNet.Configurer.UnitTests } [Fact] - public void It_uses_the_packages_archive_with_dotnet_restore() + public void It_writes_a_config_file_with_the_extracted_archive_as_a_package_source() { + var nugetConfigPath = Path.Combine(_temporaryDirectoryMock.DirectoryPath, "NuGet.Config"); + _fileSystemMock.File.ReadAllText(nugetConfigPath).Should().Be( + $@" + + + + +"); + } + + [Fact] + public void It_uses_a_config_file_with_dotnet_restore() + { + var nugetConfigPath = Path.Combine(_temporaryDirectoryMock.DirectoryPath, "NuGet.Config"); _commandFactoryMock.Verify( c => c.Create( "restore", - new[] { "-s", $"{PACKAGES_ARCHIVE_PATH}" }, + new[] { "--configfile", nugetConfigPath }, null, Constants.DefaultConfiguration), Times.Exactly(2)); diff --git a/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs index 3971462db..992679f60 100644 --- a/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs +++ b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs @@ -175,6 +175,11 @@ namespace Microsoft.DotNet.Configurer.UnitTests { throw new NotImplementedException(); } + + public void WriteAllText(string path, string content) + { + throw new NotImplementedException(); + } } private class MockStream : MemoryStream diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs index 9176cc2d8..46db578c1 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Mock/FileSystemMockBuilder.cs @@ -60,6 +60,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests private class FileMock : IFile { private Dictionary _files; + public FileMock(Dictionary files) { _files = files; @@ -100,6 +101,11 @@ namespace Microsoft.Extensions.DependencyModel.Tests { _files.Add(path, string.Empty); } + + public void WriteAllText(string path, string content) + { + _files[path] = content; + } } private class DirectoryMock : IDirectory diff --git a/test/dotnet-restore.Tests/GivenThatIWantToRestoreApp.cs b/test/dotnet-restore.Tests/GivenThatIWantToRestoreApp.cs index cbe226760..416b8908a 100644 --- a/test/dotnet-restore.Tests/GivenThatIWantToRestoreApp.cs +++ b/test/dotnet-restore.Tests/GivenThatIWantToRestoreApp.cs @@ -14,6 +14,8 @@ namespace Microsoft.DotNet.Restore.Tests { public class GivenThatIWantToRestoreApp : TestBase { + private static string RepoRootNuGetConfig = Path.Combine(RepoDirectoriesProvider.RepoRoot, "NuGet.Config"); + [Fact] public void ItRestoresAppToSpecificDirectory() { @@ -29,7 +31,7 @@ namespace Microsoft.DotNet.Restore.Tests .Should() .Pass(); - string args = $"--packages \"{dir}\""; + string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\""; new RestoreCommand() .WithWorkingDirectory(rootPath) .ExecuteWithCapturedOutput(args) @@ -56,7 +58,7 @@ namespace Microsoft.DotNet.Restore.Tests .Should() .Pass(); - string args = $"--packages \"{dir}\""; + string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\""; new RestoreCommand() .WithWorkingDirectory(rootPath) .ExecuteWithCapturedOutput(args) @@ -76,7 +78,7 @@ namespace Microsoft.DotNet.Restore.Tests string dir = "pkgs"; string fullPath = Path.GetFullPath(Path.Combine(rootPath, dir)); - string args = $"--packages \"{dir}\""; + string args = $"--configfile {RepoRootNuGetConfig} --packages \"{dir}\""; new RestoreCommand() .WithWorkingDirectory(rootPath) .ExecuteWithCapturedOutput(args)