public void ShouldFindPackageAlreadyUploaded() { using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion))) { var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateSemanticVersion(packageVersion), ".nupkg")); File.Copy(acmeWeb.FilePath, destinationFilePath); var result = FindPackages(packageId, packageVersion, acmeWeb.Hash); result.AssertSuccess(); result.AssertServiceMessage( ServiceMessageNames.CalamariFoundPackage.Name, Is.True, message: "Expected service message '{0}' to be True", args: ServiceMessageNames.CalamariFoundPackage.Name); result.AssertOutput("Package {0} {1} hash {2} has already been uploaded", packageId, packageVersion, acmeWeb.Hash); result.AssertServiceMessage(ServiceMessageNames.FoundPackage.Name, Is.True); var foundPackage = result.CapturedOutput.FoundPackage; Assert.AreEqual(VersionFactory.CreateSemanticVersion(packageVersion), foundPackage.Version); Assert.AreEqual(acmeWeb.Hash, foundPackage.Hash); Assert.AreEqual(destinationFilePath, foundPackage.RemotePath); Assert.AreEqual(".nupkg", foundPackage.FileExtension); Assert.AreEqual(packageId, foundPackage.PackageId); } }
public static void DownloadPackage(string packageId, IVersion version, Uri feedUri, string targetFilePath) { if (!Directory.Exists(feedUri.LocalPath)) { throw new Exception($"Path does not exist: '{feedUri}'"); } // Lookup files which start with the name "<Id>." and attempt to match it with all possible version string combinations (e.g. 1.2.0, 1.2.0.0) // before opening the package. To avoid creating file name strings, we attempt to specifically match everything after the last path separator // which would be the file name and extension. var package = (from path in GetPackageLookupPaths(packageId, version, feedUri) let pkg = new LocalNuGetPackage(path) where pkg.Metadata.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) && VersionFactory.CreateSemanticVersion(pkg.Metadata.Version.ToString()).Equals(version) select path ).FirstOrDefault(); if (package == null) { throw new Exception($"Could not find package {packageId} v{version} in feed: '{feedUri}'"); } Log.VerboseFormat("Found package {0} v{1}", packageId, version); Log.Verbose("Downloading to: " + targetFilePath); using (var targetFile = File.Open(targetFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { using (var fileStream = File.OpenRead(package)) { fileStream.CopyTo(targetFile); } } }
PackageIdentity CreatePackageIdentity(string packageId, string packageVersion) { var version = VersionFactory.CreateSemanticVersion(packageVersion); var path = new PackagePath($"C:\\{packageId}.{packageVersion}.zip"); return(new PackageIdentity(new PackageId(packageId), version, path)); }
public void ShouldFindTheCorrectPackageWhenSimilarPackageExist() { using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion))) using (var acmeWebTest = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId + ".Tests", packageVersion))) { var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateVersion(packageVersion, VersionFormat.Semver), ".nupkg")); File.Copy(acmeWeb.FilePath, destinationFilePath); var destinationFilePathTest = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId + ".Tests", VersionFactory.CreateVersion(packageVersion, VersionFormat.Semver), ".nupkg")); File.Copy(acmeWebTest.FilePath, destinationFilePathTest); using (var newAcmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, newpackageVersion))) { var result = FindPackages(packageId, newpackageVersion, newAcmeWeb.Hash); result.AssertSuccess(); result.AssertOutput("Package {0} version {1} hash {2} has not been uploaded.", packageId, newpackageVersion, newAcmeWeb.Hash); result.AssertOutput("Finding earlier packages that have been uploaded to this Tentacle"); result.AssertOutput("Found 1 earlier version of {0} on this Tentacle", packageId); result.AssertOutput(" - {0}: {1}", packageVersion, destinationFilePath); result.AssertServiceMessage(ServiceMessageNames.FoundPackage.Name, Is.True); var foundPackage = result.CapturedOutput.FoundPackage; Assert.AreEqual(VersionFactory.CreateSemanticVersion(packageVersion), foundPackage.Version); Assert.AreEqual(acmeWeb.Hash, foundPackage.Hash); Assert.AreEqual(destinationFilePath, foundPackage.RemotePath); Assert.AreEqual(".nupkg", foundPackage.FileExtension); Assert.AreEqual(packageId, foundPackage.PackageId); } } }
public int AttemptsTheRightNumberOfTimesOnError(int maxDownloadAttempts) { var packageId = "FakePackageId"; var version = VersionFactory.CreateSemanticVersion(1, 2, 3); var feedUri = new Uri("http://www.myget.org"); var feedCredentials = new CredentialCache(); var targetFilePath = "FakeTargetFilePath"; var filesystem = Substitute.For <ICalamariFileSystem>(); var variables = new CalamariVariables(); var calledCount = 0; Assert.Throws <Exception>(() => { var downloader = new InternalNuGetPackageDownloader(filesystem, variables); downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: maxDownloadAttempts, downloadAttemptBackoff: TimeSpan.Zero, action: (arg1, arg2, arg3, arg4, arg5) => { calledCount++; throw new Exception("Expected exception from test: simulate download failing"); }); }); return(calledCount); }
public void FromFile_OldSchoolFileType() { var details = PackageName.FromFile("blah/MyPackage.1.0.8-cat+jat.jar"); Assert.AreEqual("MyPackage", details.PackageId); Assert.AreEqual(VersionFactory.CreateSemanticVersion("1.0.8-cat+jat"), details.Version); Assert.AreEqual(".jar", details.Extension); }
public void ToCachedFileName_Semver() { var filename = PackageName.ToCachedFileName("My/Package", VersionFactory.CreateSemanticVersion("12.32.1-meta+data"), ".zip"); var scrubbedFilename = Regex.Replace(filename, "[0-9A-F]{32}", "<CACHE-GUID>"); Assert.AreEqual("My%[email protected]+data@<CACHE-GUID>.zip", scrubbedFilename); }
static IVersion DecodeVersion(string input) { switch (input[0]) { case 'S': return(VersionFactory.CreateSemanticVersion(Decode(input.Substring(1)), true)); case 'M': return(VersionFactory.CreateMavenVersion(Decode(input.Substring(1)))); } throw new Exception($"Unrecognised Version format `{input}`"); }
void RunNugetV3TimeoutTest(string timeoutInVariables, TimeSpan serverResponseTime, TimeSpan estimatedTimeout) { using (var server = new TestHttpServer(9001, serverResponseTime)) { var packageId = "FakePackageId"; var version = VersionFactory.CreateSemanticVersion(1, 2, 3); var feedCredentials = new CredentialCache(); var targetFilePath = "FakeTargetFilePath"; var filesystem = Substitute.For <ICalamariFileSystem>(); var v3NugetUri = new Uri(server.BaseUrl + "/index.json"); var variables = new CalamariVariables(); if (timeoutInVariables != null) { variables[KnownVariables.NugetHttpTimeout] = timeoutInVariables; } var downloader = new InternalNuGetPackageDownloader(filesystem, variables); var stopwatch = new Stopwatch(); Action invocation = () => { stopwatch.Start(); try { downloader.DownloadPackage( packageId, version, v3NugetUri, feedCredentials, targetFilePath, maxDownloadAttempts: 1, downloadAttemptBackoff: TimeSpan.Zero ); } finally { stopwatch.Stop(); } }; invocation.Should() .ThrowExactly <Exception>(); stopwatch.Elapsed .Should() .BeCloseTo(estimatedTimeout, TimeSpan.FromSeconds(0.5)); } }
public void AttemptsOnlyOnceIfSuccessful() { var packageId = "FakePackageId"; var version = VersionFactory.CreateSemanticVersion(1, 2, 3); var feedUri = new Uri("http://www.myget.org"); var feedCredentials = new CredentialCache(); var targetFilePath = "FakeTargetFilePath"; var filesystem = Substitute.For <ICalamariFileSystem>(); var calledCount = 0; var downloader = new InternalNuGetPackageDownloader(filesystem); downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: 5, downloadAttemptBackoff: TimeSpan.Zero, action: (arg1, arg2, arg3, arg4, arg5) => { calledCount++; }); Assert.That(calledCount, Is.EqualTo(1)); }
public void ShouldNotFindEarlierPackageVersionWhenExactMatchRequested() { using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion))) { var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateSemanticVersion(packageVersion), ".nupkg")); File.Copy(acmeWeb.FilePath, destinationFilePath); using (var newAcmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, newpackageVersion))) { var result = FindPackagesExact(packageId, newpackageVersion, newAcmeWeb.Hash, true); result.AssertSuccess(); result.AssertOutput("Package {0} version {1} hash {2} has not been uploaded.", packageId, newpackageVersion, newAcmeWeb.Hash); result.AssertNoOutput("Finding earlier packages that have been uploaded to this Tentacle"); } } }