Permalink
Browse files

Changes to ~/nuget.exe per code review

  • Loading branch information...
1 parent cde6862 commit 0e6a9e83b77189930f5ab01bfd36e49b1c012137 @pranavkm pranavkm committed with pranavkm May 30, 2012
@@ -1069,6 +1069,100 @@ public void WillCurateThePackage()
fakeAutoCuratePackageCmd.Verify(fake => fake.Execute(fakePackage, fakeNuGetPackage.Object));
}
+
+ [Fact]
+ public void WillExtractNuGetExe()
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = true };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ nugetExeDownloader.Setup(d => d.UpdateExecutable(It.IsAny<IPackage>())).Verifiable();
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify();
+ }
+
+ [Fact]
+ public void WillNotExtractNuGetExeIfIsNotLatestStable()
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = false };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
+ }
+
+ [Theory]
+ [InlineData("nuget-commandline")]
+ [InlineData("nuget..commandline")]
+ [InlineData("nuget.command")]
+ public void WillNotExtractNuGetExeIfIsItDoesNotMatchId(string id)
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = id }, Version = "2.0.0", IsLatestStable = true };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
+ }
}
public class TheCancelVerifyPackageAction
@@ -1124,7 +1218,8 @@ public void RedirectsToUploadPageAfterDelete()
Mock<IPackage> fakeNuGetPackage = null,
Mock<ISearchService> searchService = null,
Exception readPackageException = null,
- Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null)
+ Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null,
+ Mock<INuGetExeDownloaderService> downloaderSvc = null)
{
packageSvc = packageSvc ?? new Mock<IPackageService>();
@@ -1133,14 +1228,16 @@ public void RedirectsToUploadPageAfterDelete()
messageSvc = messageSvc ?? new Mock<IMessageService>();
searchService = searchService ?? CreateSearchService();
autoCuratePackageCmd = autoCuratePackageCmd ?? new Mock<IAutomaticallyCuratePackageCommand>();
+ downloaderSvc = downloaderSvc ?? new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
var controller = new Mock<PackagesController>(
packageSvc.Object,
uploadFileSvc.Object,
userSvc.Object,
messageSvc.Object,
searchService.Object,
- autoCuratePackageCmd.Object);
+ autoCuratePackageCmd.Object,
+ downloaderSvc.Object);
controller.CallBase = true;
if (httpContext != null)
@@ -1,5 +1,4 @@
-using System;
-using System.IO;
+using System.IO;
using System.Linq;
using System.Web.Mvc;
using Moq;
@@ -10,34 +9,36 @@ namespace NuGetGallery.Services
{
public class NuGetExeDownloaderServiceFacts
{
- private static readonly string _exePath = @"x:\NuGetGallery\nuget.exe";
-
[Fact]
- public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExistsAndIsRecent()
+ public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExists()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true).Verifiable();
- fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
- .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(60)))
- .Verifiable();
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe"))
+ .Returns(true).Verifiable();
+ fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
+ .Returns(Mock.Of<ActionResult>())
+ .Verifiable();
// Act
- var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
+ var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
+ downloaderSvc.CreateNuGetExeDownloadActionResult();
// Assert
- fileSystem.Verify();
- AssertActionResult(result);
+ fileStorage.Verify();
}
[Fact]
public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(false);
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe")).Returns(false);
+ fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
+ .Verifiable();
+ fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
+ .Returns(Mock.Of<ActionResult>())
+ .Verifiable();
var package = new Package { Version = "2.0.0" };
var packageService = new Mock<IPackageService>(MockBehavior.Strict);
@@ -50,70 +51,31 @@ public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
.Verifiable();
// Act
- var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
+ var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileStorage);
+ downloaderSvc.CreateNuGetExeDownloadActionResult();
// Assert
packageFileSvc.Verify();
packageService.Verify();
- AssertActionResult(result);
}
[Fact]
- public void CreateNuGetExeDownloadExtractsFileIfItExistsButIsNotRecent()
+ public void UpdateExecutableExtractsExeToFileStorage()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true);
- fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
- .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromHours(32)));
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
-
- var package = new Package { Version = "2.0.0" };
- var packageService = new Mock<IPackageService>(MockBehavior.Strict);
- packageService.Setup(s => s.FindPackageByIdAndVersion("NuGet.CommandLine", null, false))
- .Returns(package)
- .Verifiable();
- var packageFileSvc = new Mock<IPackageFileService>(MockBehavior.Strict);
- packageFileSvc.Setup(s => s.DownloadPackageFile(package))
- .Returns(CreateCommandLinePackage)
- .Verifiable();
-
- // Act
- var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
-
- // Assert
- packageFileSvc.Verify();
- packageService.Verify();
- AssertActionResult(result);
- }
-
- [Fact]
- public void UpdateExecutableExtractsExeToDisk()
- {
- // Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
+ .Verifiable();
var nugetPackage = new Mock<IPackage>();
nugetPackage.Setup(s => s.GetFiles()).Returns(new[] { CreateExePackageFile() }.AsQueryable());
// Act
- var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
+ var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
downloaderSvc.UpdateExecutable(nugetPackage.Object);
// Assert
- fileSystem.Verify();
- }
-
- private static void AssertActionResult(ActionResult result)
- {
- Assert.IsType<FilePathResult>(result);
- var filePathResult = (FilePathResult)result;
- Assert.Equal(_exePath, filePathResult.FileName);
- Assert.Equal(@"application/octet-stream", filePathResult.ContentType);
- Assert.Equal(@"NuGet.exe", filePathResult.FileDownloadName);
+ fileStorage.Verify();
}
private static Stream CreateCommandLinePackage()
@@ -146,16 +108,13 @@ private static IPackageFile CreateExePackageFile()
private static NuGetExeDownloaderService GetDownloaderService(
Mock<IPackageService> packageSvc = null,
Mock<IPackageFileService> packageFileSvc = null,
- Mock<IFileSystemService> fileSystemSvc = null)
+ Mock<IFileStorageService> fileStorageSvc = null)
{
packageSvc = packageSvc ?? new Mock<IPackageService>(MockBehavior.Strict);
packageFileSvc = packageFileSvc ?? new Mock<IPackageFileService>(MockBehavior.Strict);
- fileSystemSvc = fileSystemSvc ?? new Mock<IFileSystemService>(MockBehavior.Strict);
+ fileStorageSvc = fileStorageSvc ?? new Mock<IFileStorageService>(MockBehavior.Strict);
- return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileSystemSvc.Object)
- {
- NuGetExePath = _exePath
- };
+ return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileStorageSvc.Object);
}
}
}
View
@@ -14,14 +14,22 @@ public static class Constants
public const string NuGetPackageFileExtension = ".nupkg";
public const string PackageFileDownloadUriTemplate = "packages/{0}/{1}/download";
public const string PackageFileSavePathTemplate = "{0}.{1}{2}";
+
public const string PackagesFolderName = "packages";
+ public const string DownloadsFolderName = "downloads";
+
public const string PopularitySortOrder = "package-download-count";
public const string RecentSortOrder = "package-created";
public const string RelevanceSortOrder = "relevance";
+
public const string Sha1HashAlgorithmId = "SHA1";
public const string Sha512HashAlgorithmId = "SHA512";
public const string PBKDF2HashAlgorithmId = "PBKDF2";
+
public const string UploadFileNameTemplate = "{0}{1}";
public const string UploadsFolderName = "uploads";
+
+ public const string NuGetCommandLinePackageId = "NuGet.CommandLine";
+
}
}
@@ -11,7 +11,6 @@ namespace NuGetGallery
{
public partial class ApiController : Controller
{
- private const string NuGetCommandLinePackage = "NuGet.CommandLine";
private readonly IPackageService packageSvc;
private readonly IUserService userSvc;
private readonly IPackageFileService packageFileSvc;
@@ -55,7 +54,7 @@ public virtual ActionResult GetPackage(string id, string version)
OutputCache(VaryByParam = "none", Location = OutputCacheLocation.ServerAndClient, Duration = 600)]
public virtual ActionResult GetNuGetExe()
{
- return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionnResult();
+ return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionResult();
}
[ActionName("VerifyPackageKeyApi"), HttpGet]
@@ -130,7 +129,7 @@ private ActionResult CreatePackageInternal(string apiKey)
}
var package = packageSvc.CreatePackage(packageToPush, user);
- if (packageToPush.Id.Equals(NuGetCommandLinePackage, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
+ if (packageToPush.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
{
// If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
nugetExeDownloaderSvc.UpdateExecutable(packageToPush);
@@ -24,21 +24,24 @@ public partial class PackagesController : Controller
private readonly IMessageService messageService;
private readonly ISearchService searchSvc;
private readonly IAutomaticallyCuratePackageCommand autoCuratedPackageCmd;
+ private readonly INuGetExeDownloaderService nugetExeDownloaderSvc;
public PackagesController(
IPackageService packageSvc,
IUploadFileService uploadFileSvc,
IUserService userSvc,
IMessageService messageService,
ISearchService searchSvc,
- IAutomaticallyCuratePackageCommand autoCuratedPackageCmd)
+ IAutomaticallyCuratePackageCommand autoCuratedPackageCmd,
+ INuGetExeDownloaderService nugetExeDownloaderSvc)
{
this.packageSvc = packageSvc;
this.uploadFileSvc = uploadFileSvc;
this.userSvc = userSvc;
this.messageService = messageService;
this.searchSvc = searchSvc;
this.autoCuratedPackageCmd = autoCuratedPackageCmd;
+ this.nugetExeDownloaderSvc = nugetExeDownloaderSvc;
}
[Authorize]
@@ -512,6 +515,12 @@ public virtual ActionResult VerifyPackage(bool? listed)
tx.Complete();
}
+ if (package.PackageRegistration.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
+ {
+ // If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
+ nugetExeDownloaderSvc.UpdateExecutable(nugetPackage);
+ }
+
TempData["Message"] = String.Format(CultureInfo.CurrentCulture, Strings.SuccessfullyUploadedPackage, package.PackageRegistration.Id, package.Version);
return RedirectToRoute(RouteName.DisplayPackage, new { package.PackageRegistration.Id, package.Version });
}
Oops, something went wrong.

0 comments on commit 0e6a9e8

Please sign in to comment.