Пример #1
0
        public async Task TestPushPackage_Local_Directory_PackageSource(NuGetProtocolVersion protocolVersion)
        {
            await using var testPackageSrcDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageIdentity = new PackageIdentity("test", NuGetVersion.Parse("1.0.0"));

            var packageFilenameAbsolute = _snapFilesystem.PathCombine(testPackageSrcDirectory,
                                                                      $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(packageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(packageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            await using var publishDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageSource = new PackageSource(publishDirectory, "test", true)
            {
                ProtocolVersion = (int)protocolVersion
            };

            var nuGetPackageSources = new NuGetInMemoryPackageSources(publishDirectory, packageSource);

            await _nugetService.PushAsync(packageFilenameAbsolute, nuGetPackageSources, packageSource, default);

            var dstFilename = _snapFilesystem.PathCombine(publishDirectory, _snapFilesystem.PathGetFileName(packageFilenameAbsolute));

            using var packageArchiveRead = new PackageArchiveReader(dstFilename);
            Assert.Equal(packageArchiveRead.GetIdentity(), packageIdentity);
            Assert.Equal(new Uri(publishDirectory), packageSource.SourceUri);
        }
Пример #2
0
        public async Task TestDeletePackage_Local_Directory_PackageSource(NuGetProtocolVersion protocolVersion)
        {
            await using var deletePackageSrcDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageIdentity = new PackageIdentity("test", NuGetVersion.Parse("1.0.0"));

            var packageFilenameAbsolute = _snapFilesystem.PathCombine(deletePackageSrcDirectory,
                                                                      $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(packageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(packageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            var packageSource = new PackageSource(deletePackageSrcDirectory, "test", true)
            {
                ProtocolVersion = (int)protocolVersion
            };

            var packageSources = new NuGetInMemoryPackageSources(deletePackageSrcDirectory, packageSource);

            await _nugetService.DeleteAsync(packageIdentity, packageSources, packageSource);

            Assert.False(_snapFilesystem.FileExists(packageFilenameAbsolute));
            Assert.Equal(new Uri(deletePackageSrcDirectory), packageSource.SourceUri);
        }
Пример #3
0
        public async Task TestBuildNugetSources_Source_Uri_Is_Null(NuGetProtocolVersion protocolVersion)
        {
            await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem);

            var snapNugetFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                ProtocolVersion = protocolVersion,
                Source          = null
            };

            var snapChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = snapNugetFeed,
                UpdateFeed = snapNugetFeed
            };

            var snapApp = new SnapApp
            {
                Channels = new List <SnapChannel> {
                    snapChannel
                }
            };

            var nugetPackageSources = snapApp.BuildNugetSources(nugetTempDirectory);

            Assert.Empty(nugetPackageSources);
        }
Пример #4
0
        public async Task TestDownloadLatestAsync_Local_Directory_PackageSource(NuGetProtocolVersion protocolVersion)
        {
            await using var testPackageDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageSource = new PackageSource(testPackageDirectory, "test", true)
            {
                ProtocolVersion = (int)protocolVersion
            };

            var initialPackageIdentity = new PackageIdentity("test", NuGetVersion.Parse("1.0.0"));

            var initialPackageFilenameAbsolute = _snapFilesystem.PathCombine(testPackageDirectory,
                                                                             $"{initialPackageIdentity.Id}.{initialPackageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(initialPackageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(initialPackageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            var secondPackageIdentity = new PackageIdentity("test", NuGetVersion.Parse("2.0.0"));

            var secondPackageFilenameAbsolute = _snapFilesystem.PathCombine(testPackageDirectory,
                                                                            $"{secondPackageIdentity.Id}.{secondPackageIdentity.Version.ToNormalizedString()}.nupkg");

            int packageFileSize;

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(secondPackageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(secondPackageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);

                packageFileSize = (int)nupkgStream.Length;
            }

            var downloadContext = new DownloadContext
            {
                PackageIdentity = secondPackageIdentity,
                PackageFileSize = packageFileSize
            };

            using var downloadResourceResult = await _nugetService.DownloadLatestAsync(secondPackageIdentity.Id, packageSource, true, true);

            Assert.NotNull(downloadResourceResult);
            Assert.Equal(downloadContext.PackageFileSize, downloadResourceResult.PackageStream.Length);
            Assert.Equal(0, downloadResourceResult.PackageStream.Position);

            using var package = new PackageArchiveReader(downloadResourceResult.PackageStream);
            Assert.Equal(package.GetIdentity(), secondPackageIdentity);
        }
Пример #5
0
        public async Task TestGetMetadatasAsync_Local_Directory_PackageSource(NuGetProtocolVersion protocolVersion)
        {
            await using var packagesDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageSource = new PackageSource(packagesDirectory, "test", true)
            {
                ProtocolVersion = (int)protocolVersion
            };

            var initialPackageIdentity = new PackageIdentity("test", NuGetVersion.Parse("1.0.0"));

            var initialPackageFilenameAbsolute = _snapFilesystem.PathCombine(packagesDirectory,
                                                                             $"{initialPackageIdentity.Id}.{initialPackageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(initialPackageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(initialPackageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            var secondPackageIdentity = new PackageIdentity("test", NuGetVersion.Parse("2.0.0"));

            var secondPackageFilenameAbsolute = _snapFilesystem.PathCombine(packagesDirectory,
                                                                            $"{secondPackageIdentity.Id}.{secondPackageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(secondPackageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(secondPackageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            var differentPackageIdentity = new PackageIdentity("test2", NuGetVersion.Parse("2.0.0"));

            var differentPackageFilenameAbsolute = _snapFilesystem.PathCombine(packagesDirectory,
                                                                               $"{differentPackageIdentity.Id}.{differentPackageIdentity.Version.ToNormalizedString()}.nupkg");

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(differentPackageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(differentPackageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);
            }

            var packageSources = new NuGetInMemoryPackageSources(packagesDirectory, packageSource);

            var packages = await _nugetService
                           .GetMetadatasAsync("test", packageSources, false, cancellationToken : CancellationToken.None);

            Assert.Equal(2, packages.Count);
        }
Пример #6
0
        public async Task TestDirectDownloadWithProgressAsync_Local_Directory_PackageSource(NuGetProtocolVersion protocolVersion)
        {
            await using var testPackageDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);

            var packageSource = new PackageSource(testPackageDirectory, "test", true)
            {
                ProtocolVersion = (int)protocolVersion
            };

            var packageIdentity = new PackageIdentity("test", NuGetVersion.Parse("1.0.0"));

            var packageFilenameAbsolute = _snapFilesystem.PathCombine(testPackageDirectory,
                                                                      $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg");

            int packageFileSize;

            await using (var packageOutputStream = _snapFilesystem.FileReadWrite(packageFilenameAbsolute))
            {
                await using var nupkgStream = BuildNupkg(packageIdentity);
                await nupkgStream.CopyToAsync(packageOutputStream);

                packageFileSize = (int)nupkgStream.Length;
            }

            var percentages = new List <int>();

            var progressSourceMock = new Mock <INugetServiceProgressSource>();

            progressSourceMock.Setup(x => x.Raise(
                                         It.IsAny <int>(),
                                         It.IsAny <long>(),
                                         It.IsAny <long>(),
                                         It.IsAny <long>()))
            .Callback((int percentage, long bytesRead, long totalBytesSoFar, long totalBytesDownloaded) =>
            {
                percentages.Add(percentage);
            });

            var downloadContext = new DownloadContext
            {
                PackageIdentity = packageIdentity,
                PackageFileSize = packageFileSize
            };

            using var downloadResourceResult = await _nugetService.DownloadAsyncWithProgressAsync(packageSource, downloadContext, progressSourceMock.Object, default);

            Assert.NotNull(downloadResourceResult);
            Assert.Equal(downloadContext.PackageFileSize, downloadResourceResult.PackageStream.Length);
            Assert.Equal(0, downloadResourceResult.PackageStream.Position);
            Assert.IsType <MemoryStream>(downloadResourceResult.PackageStream);

            using var package = new PackageArchiveReader(downloadResourceResult.PackageStream);
            Assert.Equal(package.GetIdentity(), packageIdentity);

            progressSourceMock.Verify(x => x.Raise(
                                          It.Is <int>(v => v == 0),
                                          It.Is <long>(v => v == 0),
                                          It.Is <long>(v => v == 0),
                                          It.Is <long>(v => v == downloadContext.PackageFileSize)), Times.Once);

            progressSourceMock.Verify(x => x.Raise(
                                          It.Is <int>(v => v == 100),
                                          It.IsAny <long>(),
                                          It.Is <long>(v => v == downloadContext.PackageFileSize),
                                          It.Is <long>(v => v == downloadContext.PackageFileSize)), Times.Once);

            Assert.Equal(progressSourceMock.Invocations.Count, percentages.Count);
        }
Пример #7
0
        public async Task TestBuildSnapFeedsFromNugetPackageSources(NuGetProtocolVersion protocolVersion)
        {
            await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem);

            var feedUrl = protocolVersion switch
            {
                NuGetProtocolVersion.V2 => NuGetConstants.V2FeedUrl,
                NuGetProtocolVersion.V3 => NuGetConstants.V3FeedUrl,
                _ => throw new NotSupportedException(protocolVersion.ToString())
            };

            var snapNugetFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                ProtocolVersion = protocolVersion,
                Source          = new Uri(feedUrl),
                Username        = "******",
                Password        = "******",
                ApiKey          = "myapikey"
            };

            var snapChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = snapNugetFeed,
                UpdateFeed = snapNugetFeed
            };

            var snapApp = new SnapApp
            {
                Channels = new List <SnapChannel> {
                    snapChannel
                }
            };

            var nugetPackageSources = snapApp.BuildNugetSources(nugetTempDirectory);

            Assert.NotNull(nugetPackageSources.Settings);
            Assert.Single(nugetPackageSources.Items);

            var snapFeeds = snapApp.BuildNugetSources(nugetTempDirectory);

            Assert.NotNull(snapFeeds.Settings);
            Assert.Single(snapFeeds.Items);

            var snapNugetFeedAfter = snapFeeds.Items.Single();

            Assert.Equal(snapNugetFeed.Name, snapNugetFeedAfter.Name);
            Assert.Equal((int)snapNugetFeed.ProtocolVersion, snapNugetFeedAfter.ProtocolVersion);
            Assert.Equal(snapNugetFeed.Source, snapNugetFeedAfter.SourceUri);
            Assert.Equal(snapNugetFeed.Username, snapNugetFeedAfter.Credentials.Username);
            var credential = snapNugetFeedAfter.Credentials;

            if (nugetPackageSources.IsPasswordEncryptionSupported())
            {
                Assert.False(credential.IsPasswordClearText);
                Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password);
                Assert.Equal(snapNugetFeed.Password, credential.PasswordText);
            }
            else
            {
                Assert.True(credential.IsPasswordClearText);
                Assert.Equal(snapNugetFeed.Password, credential.Password);
            }
            Assert.Equal(snapNugetFeed.ApiKey, snapNugetFeedAfter.GetDecryptedValue(nugetPackageSources, ConfigurationConstants.ApiKeys));
        }
Пример #8
0
        public void TestBuildNugetSourcesFromSnapApp(NuGetProtocolVersion protocolVersion)
        {
            string feedUrl;

            switch (protocolVersion)
            {
            case NuGetProtocolVersion.V2:
                feedUrl = NuGetConstants.V2FeedUrl;
                break;

            case NuGetProtocolVersion.V3:
                feedUrl = NuGetConstants.V3FeedUrl;
                break;

            default:
                throw new NotSupportedException(protocolVersion.ToString());
            }

            var snapNugetFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                ProtocolVersion = protocolVersion,
                Source          = new Uri(feedUrl),
                Username        = "******",
                Password        = "******",
                ApiKey          = "myapikey"
            };

            var snapChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = snapNugetFeed,
                UpdateFeed = snapNugetFeed
            };

            var snapApp = new SnapApp
            {
                Channels = new List <SnapChannel> {
                    snapChannel
                }
            };

            var nuGetPackageSources = snapApp.BuildNugetSources(_baseFixture.NugetTempDirectory);

            Assert.Single(nuGetPackageSources.Items);

            var packageSource = nuGetPackageSources.Items.Single();

            Assert.True(packageSource.IsEnabled);
            Assert.True(packageSource.IsOfficial);
            Assert.False(packageSource.IsPersistable);
            Assert.False(packageSource.IsMachineWide);

            Assert.Equal(snapNugetFeed.Name, packageSource.Name);
            Assert.Equal(snapNugetFeed.Source.ToString(), packageSource.TrySourceAsUri.ToString());
            Assert.Equal((int)snapNugetFeed.ProtocolVersion, packageSource.ProtocolVersion);
            Assert.NotNull(packageSource.Credentials);

            var credential = packageSource.Credentials;

            Assert.Equal(snapNugetFeed.Username, credential.Username);
            if (nuGetPackageSources.IsPasswordEncryptionSupported())
            {
                Assert.False(credential.IsPasswordClearText);
                Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password);
                Assert.Equal(snapNugetFeed.Password, credential.PasswordText);
            }
            else
            {
                Assert.True(credential.IsPasswordClearText);
                Assert.Equal(snapNugetFeed.Password, credential.Password);
            }
            Assert.Equal(snapNugetFeed.Name, credential.Source);

            Assert.Equal(snapNugetFeed.ApiKey, packageSource.GetDecryptedValue(nuGetPackageSources, ConfigurationConstants.ApiKeys));
        }