public void TestExtractInvalidData()
        {
            if (!WindowsUtils.IsWindows) Assert.Ignore("7z extraction relies on a Win32 DLL and therefore will not work on non-Windows platforms");

            using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
                Assert.Throws<IOException>(() => Extractor.FromStream(new MemoryStream(_garbageData), sandbox, Archive.MimeType7Z).Run());
        }
예제 #2
0
파일: Utils.cs 프로젝트: whuthj/VisualRust
 // We assume that every file has structure: name.ext
 // this is obviously not true in general case, but good enough for tests
 public static TemporaryDirectory LoadResourceDirectory(string path)
 {
     string pathStart = Path.Combine(@"VisualRust\Test", path).Replace('\\', '.');
     TemporaryDirectory dir = new TemporaryDirectory();
     int lastSlash = pathStart.LastIndexOf('.');
     TemporaryDirectory actualRoot = dir.SubDir(pathStart.Substring(lastSlash + 1, pathStart.Length - lastSlash - 1));
     foreach(string resName in resourceNames.Where(p => p.StartsWith(pathStart)))
     {
         string relPath = resName.Substring(pathStart.Length, resName.Length - pathStart.Length);
         int extDot = relPath.LastIndexOf('.');
         int fileDot = relPath.LastIndexOf('.', extDot - 1);
         string subDir = relPath.Substring(1, fileDot);
         string currDir = actualRoot.DirPath;
         if(subDir.Length > 0)
         {
             currDir = Path.Combine(actualRoot.DirPath, subDir);
             Directory.CreateDirectory(currDir);
         }
         using(FileStream fileStream = File.Create(Path.Combine(currDir, relPath.Substring(fileDot + 1, relPath.Length - fileDot - 1))))
         {
             using(var resStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resName))
             {
                 resStream.CopyTo(fileStream);
             }
         }
     }
     return actualRoot;
 }
예제 #3
0
        public void TestGetFilesAbsolute()
        {
            using (var tempDir = new TemporaryDirectory("unit-tests"))
            {
                File.WriteAllText(Path.Combine(tempDir, "a.txt"), @"a");
                File.WriteAllText(Path.Combine(tempDir, "b.txt"), @"b");
                File.WriteAllText(Path.Combine(tempDir, "c.inf"), @"c");
                File.WriteAllText(Path.Combine(tempDir, "d.nfo"), @"d");

                string subdirPath = Path.Combine(tempDir, "dir");
                Directory.CreateDirectory(subdirPath);
                File.WriteAllText(Path.Combine(subdirPath, "1.txt"), @"1");
                File.WriteAllText(Path.Combine(subdirPath, "2.inf"), @"a");

                var result = ArgumentUtils.GetFiles(new[]
                {
                    Path.Combine(tempDir, "*.txt"), // Wildcard
                    Path.Combine(tempDir, "d.nfo"), // Specifc file
                    subdirPath // Directory with implict default wildcard
                }, "*.txt");
                result[0].FullName.Should().Be(Path.Combine(tempDir, "a.txt"));
                result[1].FullName.Should().Be(Path.Combine(tempDir, "b.txt"));
                result[2].FullName.Should().Be(Path.Combine(tempDir, "d.nfo"));
                result[3].FullName.Should().Be(Path.Combine(subdirPath, "1.txt"));
            }
        }
예제 #4
0
        public string GetFile( string fileRepositoryPath, long version, TemporaryDirectory tempDir )
        {
            try
            {
                // Find the file by path in our current tree cache.
                VaultClientFile foundFile;
                if( !CanGetFile( fileRepositoryPath, version, out foundFile ) )
                {
                    return String.Empty;
                }

                // This is important: do not modify the VaultClientFile reference returned by FindFileRecursive,
                // as you will modify your tree cache (which will be saved to disk).  Make a copy of the
                // object and modify that one.
                VaultClientFile copy = new VaultClientFile( foundFile );

                // Modify the copy to represent the requested file version.
                // Files added in this feature will have an initial version of 0, which doesn't actually exist in the
                // Vault repository.  When we are asked to retrieve such a file, we bump the version number up to 1.
                copy.Version = Math.Max(1, version);

                // Get the file.
                Client.GetByDisplayVersionToNonWorkingFolder( copy, MakeWritableType.MakeAllFilesWritable, SetFileTimeType.Current, copy.Parent.FullPath, tempDir.DirectoryPath, null );

                return Path.Combine( tempDir.DirectoryPath, copy.Name );
            }
            catch( Exception e )
            {
                // Intentionally squashed.
                Trace.WriteLine( e );
                return String.Empty;
            }
        }
        public void ServerPackageRepositoryRemovePackage()
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                // Arrange
                var serverRepository = CreateServerPackageRepository(temporaryDirectory.Path, repository =>
                {
                    repository.AddPackage(CreatePackage("test", "1.11"));
                    repository.AddPackage(CreatePackage("test", "1.9"));
                    repository.AddPackage(CreatePackage("test", "2.0-alpha"));
                });

                // Act
                serverRepository.RemovePackage(CreateMockPackage("test", "1.11"));
                serverRepository.RemovePackage(CreateMockPackage("test", "2.0-alpha"));
                var packages = serverRepository.GetPackages();

                // Assert
                Assert.Equal(1, packages.Count());
                Assert.Equal(1, packages.Count(p => p.IsLatestVersion));
                Assert.Equal("1.9", packages.First(p => p.IsLatestVersion).Version.ToString());

                Assert.Equal(1, packages.Count(p => p.IsAbsoluteLatestVersion));
                Assert.Equal("1.9", packages.First(p => p.IsAbsoluteLatestVersion).Version.ToString());
            }
        }
예제 #6
0
        public void ShouldHandleSha1()
        {
            using (var package = new TemporaryDirectory("0install-unit-tests"))
            {
                string innerPath = Path.Combine(package, "inner");
                Directory.CreateDirectory(innerPath);

                string manifestPath = Path.Combine(package, Manifest.ManifestFile);
                string innerExePath = Path.Combine(innerPath, "inner.exe");
                File.WriteAllText(innerExePath, @"xxxxxxx");
                if (WindowsUtils.IsWindows)
                {
                    string flagPath = Path.Combine(package, FlagUtils.XbitFile);
                    File.WriteAllText(flagPath, @"/inner/inner.exe");
                }
                else FileUtils.SetExecutable(innerExePath, true);
                CreateDotFile(package, ManifestFormat.Sha1, new MockTaskHandler());
                using (var manifestFile = File.OpenText(manifestPath))
                {
                    string currentLine = manifestFile.ReadLine();
                    Assert.True(Regex.IsMatch(currentLine, @"^D \w+ /inner$"), "Manifest didn't match expected format:\n" + currentLine);
                    currentLine = manifestFile.ReadLine();
                    Assert.True(Regex.IsMatch(currentLine, @"^X \w+ \w+ \d+ inner.exe$"), "Manifest didn't match expected format:\n" + currentLine);
                }
            }
        }
예제 #7
0
        public void EnumerateWithSymLinkToDirectory()
        {
            using (var containingFolder = new TemporaryDirectory())
            {
                // Test a symlink to a directory that does and then doesn't exist
                using (var targetDir = new TemporaryDirectory())
                {
                    // Create a symlink to a folder that exists
                    string linkPath = Path.Combine(containingFolder.Path, Path.GetRandomFileName());
                    Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetDir.Path, isDirectory: true));

                    Assert.True(Directory.Exists(linkPath));
                    Assert.Equal(1, GetEntries(containingFolder.Path).Count());
                }

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Assert.Equal(1, GetEntries(containingFolder.Path).Count());
                    Assert.Equal(0, Directory.GetFiles(containingFolder.Path).Count());
                }
                else
                {
                    // The target file is gone and the symlink still exists; since it can't be resolved,
                    // on Unix it's treated as a file rather than as a directory.
                    Assert.Equal(0, GetEntries(containingFolder.Path).Count());
                    Assert.Equal(1, Directory.GetFiles(containingFolder.Path).Count());
                }
            }
        }
예제 #8
0
        public void TestExtractInvalidData()
        {
            if (!WindowsUtils.IsWindows) Assert.Ignore("MSI extraction relies on a Win32 API and therefore will not work on non-Windows platforms");

            using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
            using (var tempFile = Deploy(new MemoryStream(_garbageData)))
                Assert.Throws<IOException>(() => Extractor.FromFile(tempFile, sandbox, Archive.MimeTypeMsi));
        }
예제 #9
0
 public void Build( DataSet changeHistory, string fileName )
 {
     using( TemporaryDirectory revisionsDirectory = new TemporaryDirectory( ) )
     {
         RetrieveFiles( changeHistory, revisionsDirectory );
         MakeArchive( fileName, revisionsDirectory );
     }
 }
예제 #10
0
        public override void SetUp()
        {
            base.SetUp();

            _destination = new TemporaryDirectory("0install-unit-test");
            var selections = SelectionsTest.CreateTestSelections();
            _target = new Exporter(selections, new Architecture(), _destination);
        }
예제 #11
0
        public override void SetUp()
        {
            base.SetUp();

            _destinationDirectory = new TemporaryDirectory("0install-unit-tests");
            _destinationManifestPath = Path.Combine(_destinationDirectory, Manifest.ManifestFile);
            _destinationFile1Path = Path.Combine(_destinationDirectory, "file1");
            _destinationSubdirPath = Path.Combine(_destinationDirectory, "subdir");
            _destinationFile2Path = Path.Combine(_destinationSubdirPath, "file2");
        }
예제 #12
0
        public void ShouldAllowToAddFolder()
        {
            using (var packageDir = new TemporaryDirectory("0install-unit-tests"))
            {
                var digest = new ManifestDigest(ManifestTest.CreateDotFile(packageDir, ManifestFormat.Sha256, _handler));
                _store.AddDirectory(packageDir, digest, _handler);

                Assert.IsTrue(_store.Contains(digest), "After adding, Store must contain the added package");
                CollectionAssert.AreEqual(new[] {digest}, _store.ListAll(), "After adding, Store must show the added package in the complete list");
            }
        }
예제 #13
0
        public void SetUp()
        {
            // Create a temporary cache
            _tempDir = new TemporaryDirectory("0install-unit-tests");
            _cache = new DiskIconCache(_tempDir);

            // Add some dummy icons to the cache
            File.WriteAllText(Path.Combine(_tempDir, new FeedUri("http://0install.de/feeds/images/test1.png").Escape()), "");
            File.WriteAllText(Path.Combine(_tempDir, new FeedUri("http://0install.de/feeds/images/test2.png").Escape()), "");
            File.WriteAllText(Path.Combine(_tempDir, "http_invalid"), "");
        }
예제 #14
0
        private void DoDiff( string fileRepositoryPath, long firstVersion, long secondVersion )
        {
            // We need two temp folders because we'll retrieve two files, which will probably have the
            // same name, and we don't want to overwrite the other.
            using( TemporaryDirectory tempDir1 = new TemporaryDirectory(),
                tempDir2 = new TemporaryDirectory() )
            {
                try
                {
                    string firstVersionFilePath = fileRetriever.GetFile( fileRepositoryPath, firstVersion, tempDir1 );
                    string secondVersionFilePath = fileRetriever.GetFile( fileRepositoryPath, secondVersion, tempDir2 );

                    if (firstVersionFilePath == String.Empty || secondVersionFilePath == String.Empty)
                    {
                        return;
                    }

                    #region Diff the files

                    string diffApplication = "sgdm.exe";
                    string diffArgsFormatString = Client.UserOptions.GetString( VaultOptions.CustomDiff_Args );

                    // Process the arg format string to insert the correct paths and captions.
                    string processedArgString = Client.ReplaceDiffArgs(
                        diffArgsFormatString,
                        firstVersionFilePath,
                        Path.GetFileName( firstVersionFilePath ) + " " + firstVersion.ToString( ),
                        secondVersionFilePath,
                        Path.GetFileName( secondVersionFilePath ) + " " + secondVersion.ToString( ) );

                    Console.WriteLine( "Launching diff application." );

                    Process diffProc = ExternalToolsHelper.GetDiffProgram(
                        firstVersionFilePath,
                        secondVersionFilePath,
                        diffApplication,
                        processedArgString,
                        externalToolsSettings);

                    diffProc.Start();

                    // Do we want to wait?  Probably not
                    diffProc.WaitForExit();

                    #endregion
                }
                catch( Exception e )
                {
                    // Intentionally squashed.
                    System.Diagnostics.Trace.WriteLine( e );
                }
            }
        }
        public void SetUp()
        {
            var packageBuilder = new PackageBuilder()
                .AddFolder("someFolder")
                .AddFolder("someOtherFolder")
                .AddFile("nestedFile1", "abc")
                .AddFile("nestedFile2", "123");

            _sandbox = new TemporaryDirectory("0install-unit-tests");
            packageBuilder.WritePackageInto(_sandbox);

            _someGenerator = new ManifestGenerator(_sandbox, ManifestFormat.Sha256);
        }
예제 #16
0
        public void TestIsSymlinkNoMatch()
        {
            using (var tempDir = new TemporaryDirectory("unit-tests"))
            {
                string normalFile = Path.Combine(tempDir, "normal");
                FileUtils.Touch(normalFile);

                CygwinUtils.IsSymlink(normalFile).Should().BeFalse();

                string target;
                CygwinUtils.IsSymlink(normalFile, out target).Should().BeFalse();
            }
        }
예제 #17
0
        public void TestCreateSymlink()
        {
            if (!WindowsUtils.IsWindows) Assert.Ignore("The 'system' file attribute can only be set on the Windows platform.");

            using (var tempDir = new TemporaryDirectory("unit-tests"))
            {
                string symlinkFile = Path.Combine(tempDir, "symlink");
                CygwinUtils.CreateSymlink(symlinkFile, "target");

                File.Exists(symlinkFile).Should().BeTrue();
                CollectionAssert.AreEqual(expected: _symlinkBytes, actual: File.ReadAllBytes(symlinkFile));
            }
        }
예제 #18
0
        public void TestDeployPublicKey()
        {
            using (var tempDir = new TemporaryDirectory("0install-unit-tests"))
            {
                const string publicKey = "public";
                var openPgpMock = MockRepository.Create<IOpenPgp>();

                openPgpMock.Setup(x => x.ExportKey(_secretKey)).Returns(publicKey);
                FeedUtils.DeployPublicKey(tempDir.Path, _secretKey, openPgpMock.Object);

                Assert.AreEqual(publicKey, File.ReadAllText(tempDir + Path.DirectorySeparatorChar + _secretKey.KeyID + ".gpg"),
                    "Public key should be written to parallel file in directory");
            }
        }
예제 #19
0
        public void TestIsUnixFS()
        {
            using (var tempDir = new TemporaryDirectory("0install-unit-tests"))
            {
                if (UnixUtils.IsUnix)
                {
                    Assert.IsTrue(FlagUtils.IsUnixFS(tempDir));

                    FlagUtils.MarkAsNoUnixFS(tempDir);
                    Assert.IsFalse(FlagUtils.IsUnixFS(tempDir));
                }
                else Assert.IsFalse(FlagUtils.IsUnixFS(tempDir));
            }
        }
예제 #20
0
        public void TestIsUnixFS()
        {
            using (var tempDir = new TemporaryDirectory("0install-unit-tests"))
            {
                if (UnixUtils.IsUnix)
                {
                    FlagUtils.IsUnixFS(tempDir).Should().BeTrue();

                    FlagUtils.MarkAsNoUnixFS(tempDir);
                    FlagUtils.IsUnixFS(tempDir).Should().BeFalse();
                }
                else FlagUtils.IsUnixFS(tempDir).Should().BeFalse();
            }
        }
예제 #21
0
        public void TestIsSymlinkMatch()
        {
            using (var tempDir = new TemporaryDirectory("unit-tests"))
            {
                string symlinkFile = Path.Combine(tempDir, "symlink");
                File.WriteAllBytes(symlinkFile, _symlinkBytes);
                File.SetAttributes(symlinkFile, FileAttributes.System);

                CygwinUtils.IsSymlink(symlinkFile).Should().BeTrue();

                string target;
                CygwinUtils.IsSymlink(symlinkFile, out target).Should().BeTrue();
                target.Should().Be("target");
            }
        }
예제 #22
0
        public void TestDeployPublicKey()
        {
            using (var tempDir = new TemporaryDirectory("0install-unit-tests"))
            {
                const string publicKey = "public";
                var secretKey = new OpenPgpSecretKey(keyID: 123, fingerprint: new byte[] { 1, 2, 3 }, userID: "user");

                var openPgpMock = CreateMock<IOpenPgp>();
                openPgpMock.Setup(x => x.ExportKey(secretKey)).Returns(publicKey);
                openPgpMock.Object.DeployPublicKey(secretKey, tempDir.Path);

                File.ReadAllText(tempDir + Path.DirectorySeparatorChar + secretKey.FormatKeyID() + ".gpg")
                    .Should().Be(publicKey, because: "Public key should be written to parallel file in directory");
            }
        }
예제 #23
0
        public void TestExtractSubDir()
        {
            if (!WindowsUtils.IsWindows) Assert.Ignore("CAB extraction relies on a Win32 API and therefore will not work on non-Windows platforms");

            using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
            using (var extractor = Extractor.FromStream(this.GetEmbedded("testArchive.cab"), sandbox, Archive.MimeTypeCab))
            {
                extractor.SubDir = "folder1";
                extractor.Run();

                string filePath = Path.Combine(sandbox, "file");
                Assert.IsTrue(File.Exists(filePath), "Should extract file 'dir/file'");
                Assert.AreEqual(new DateTime(2000, 1, 1, 13, 0, 0), File.GetLastWriteTimeUtc(filePath), "Correct last write time should be set");
                Assert.AreEqual("def", File.ReadAllText(filePath));
            }
        }
예제 #24
0
        public void TestGetFiles()
        {
            using (var flagDir = new TemporaryDirectory("0install-unit-tests"))
            {
                File.WriteAllText(Path.Combine(flagDir, FlagUtils.XbitFile), "/dir1/file1\n/dir2/file2\n");

                var expectedResult = new[]
                {
                    new[] {flagDir, "dir1", "file1"}.Aggregate(Path.Combine),
                    new[] {flagDir, "dir2", "file2"}.Aggregate(Path.Combine)
                };

                CollectionAssert.AreEquivalent(expectedResult, FlagUtils.GetFiles(FlagUtils.XbitFile, flagDir), "Should find .xbit file in same directory");
                CollectionAssert.AreEquivalent(expectedResult, FlagUtils.GetFiles(FlagUtils.XbitFile, Path.Combine(flagDir, "subdir")), "Should find .xbit file in parent directory");
            }
        }
예제 #25
0
        private void RetrieveAndPlaceRevisionOfFile( object objectId, long version, TemporaryDirectory tempDir, string displayedPath )
        {
            if( currentScmSystem.FileVersionExists( objectId, version ) )
            {
                DirectoryInfo newDirectory = Directory.CreateDirectory( tempDir.DirectoryPath
                    + Path.DirectorySeparatorChar
                    + Path.GetDirectoryName( currentScmSystem.TransformSourceControlPathToFilesystemPath( displayedPath ) ) );

                string pathToFile = currentScmSystem.GetFile( objectId, version, tempDir );

                if( pathToFile != String.Empty )
                {
                    File.Move( pathToFile,
                        newDirectory.FullName + Path.DirectorySeparatorChar + Path.GetFileName( pathToFile ) + "." + version );
                }
            }
        }
예제 #26
0
        public void SaveLoad_Data_AreEqual()
        {
            using (var dir = new TemporaryDirectory())
            {
                var resourceFile = new XResourceFile(new List<XResourceFileData>()
                {
                    new XResourceFileData("key0", "Some value 0"),
                    new XResourceFileData("key1", "1.1", "comment 1.1"),
                });

                var file = dir.CreateFile("SaveLoad_Data_AreEqual.resx");
                resourceFile.Save(file);

                var act = XResourceFile.Load(file);
                Assert.AreEqual(2, act.Data.Count);
            }
        }
        public virtual void SetUp()
        {
            TempDir = new TemporaryDirectory("0install-unit-tests");

            File1Path = Path.Combine(TempDir, "file1");
            FileUtils.Touch(File1Path);

            SubdirPath = Path.Combine(TempDir, "subdir");
            Directory.CreateDirectory(SubdirPath);

            File2Path = Path.Combine(SubdirPath, "file2");
            FileUtils.Touch(File2Path);

            var generator = new ManifestGenerator(TempDir, ManifestFormat.Sha256New);
            generator.Run();
            Manifest = generator.Manifest;
        }
예제 #28
0
        public static TemporaryDirectory Apply([NotNull] this Recipe recipe, [NotNull, ItemNotNull] IEnumerable<TemporaryFile> downloadedFiles, [NotNull] ITaskHandler handler, [CanBeNull] object tag = null)
        {
            #region Sanity checks
            if (recipe == null) throw new ArgumentNullException("recipe");
            if (downloadedFiles == null) throw new ArgumentNullException("downloadedFiles");
            if (handler == null) throw new ArgumentNullException("handler");
            #endregion

            if (recipe.UnknownElements != null && recipe.UnknownElements.Length != 0)
                throw new NotSupportedException(string.Format(Resources.UnknownRecipeStepType, recipe.UnknownElements[0].Name));

            var workingDir = new TemporaryDirectory("0install-recipe");

            try
            {
                IEnumerator<TemporaryFile> downloadedEnum = downloadedFiles.GetEnumerator();
                // ReSharper disable AccessToDisposedClosure
                new PerTypeDispatcher<IRecipeStep>(ignoreMissing: false)
                {
                    (Archive step) =>
                    {
                        downloadedEnum.MoveNext();
                        if (downloadedEnum.Current == null) throw new ArgumentException(Resources.RecipeFileNotDownloaded, "downloadedFiles");
                        step.Apply(downloadedEnum.Current, workingDir, handler, tag);
                    },
                    (SingleFile step) =>
                    {
                        downloadedEnum.MoveNext();
                        if (downloadedEnum.Current == null) throw new ArgumentException(Resources.RecipeFileNotDownloaded, "downloadedFiles");
                        step.Apply(downloadedEnum.Current, workingDir, handler, tag);
                    },
                    (RemoveStep step) => step.Apply(workingDir),
                    (RenameStep step) => step.Apply(workingDir)
                }.Dispatch(recipe.Steps);
                // ReSharper restore AccessToDisposedClosure
                return workingDir;
            }
                #region Error handling
            catch
            {
                workingDir.Dispose();
                throw;
            }
            #endregion
        }
예제 #29
0
        public void TestGetFiles()
        {
            using (var flagDir = new TemporaryDirectory("0install-unit-tests"))
            {
                File.WriteAllText(Path.Combine(flagDir, FlagUtils.XbitFile), "/dir1/file1\n/dir2/file2\n");

                var expectedResult = new[]
                {
                    Path.Combine(flagDir, "dir1", "file1"),
                    Path.Combine(flagDir, "dir2", "file2")
                };

                FlagUtils.GetFiles(FlagUtils.XbitFile, flagDir)
                    .Should().BeEquivalentTo(expectedResult, because: "Should find .xbit file in same directory");
                FlagUtils.GetFiles(FlagUtils.XbitFile, Path.Combine(flagDir, "subdir"))
                    .Should().BeEquivalentTo(expectedResult, because: "Should find .xbit file in parent directory");
            }
        }
예제 #30
0
        public override void SetUp()
        {
            base.SetUp();

            // Create a temporary cache
            _tempDir = new TemporaryDirectory("0install-unit-tests");
            _cache = new DiskFeedCache(_tempDir, new Mock<IOpenPgp>().Object);

            // Add some dummy feeds to the cache
            _feed1 = FeedTest.CreateTestFeed();
            _feed1.Uri = FeedTest.Test1Uri;
            _feed1.SaveXml(Path.Combine(_tempDir, _feed1.Uri.Escape()));

            var feed2 = FeedTest.CreateTestFeed();
            feed2.Uri = FeedTest.Test2Uri;
            feed2.SaveXml(Path.Combine(_tempDir, feed2.Uri.Escape()));
            File.WriteAllText(Path.Combine(_tempDir, "http_invalid"), "");
        }
예제 #31
0
        public async Task ServerPackageRepositorySemVer2IsLatest(bool enableDelisting)
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                // Arrange
                var getSetting       = enableDelisting ? EnableDelisting : (Func <string, object, object>)null;
                var serverRepository = await CreateServerPackageRepositoryAsync(temporaryDirectory.Path, repository =>
                {
                    repository.AddPackage(CreatePackage("test", "1.11"));
                    repository.AddPackage(CreatePackage("test", "2.0"));
                    repository.AddPackage(CreatePackage("test", "1.9"));
                    repository.AddPackage(CreatePackage("test", "2.0-alpha"));
                    repository.AddPackage(CreatePackage("test1", "1.0.0"));
                    repository.AddPackage(CreatePackage("test1", "1.2.0+taggedOnly"));
                    repository.AddPackage(CreatePackage("test1", "2.0.0-alpha"));
                }, getSetting);

                await serverRepository.RemovePackageAsync(
                    "test",
                    new SemanticVersion("2.0"),
                    CancellationToken.None);

                // Act
                var packages = await serverRepository.GetPackagesAsync(ClientCompatibility.Max, Token);

                var latestVersions = packages.Where(p => p.SemVer2IsLatest).Select(p => p.Version.ToString()).ToList();

                // Assert
                Assert.Equal(2, packages.Count(p => p.SemVer2IsLatest));
                Assert.Equal("1.11", packages
                             .OrderBy(p => p.Id)
                             .First(p => p.SemVer2IsLatest)
                             .Version
                             .ToString());
            }
        }
예제 #32
0
        public async Task ServerPackageRepositoryRemovePackage()
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                // Arrange
                var serverRepository = await CreateServerPackageRepositoryAsync(temporaryDirectory.Path, repository =>
                {
                    repository.AddPackage(CreatePackage("test", "1.11"));
                    repository.AddPackage(CreatePackage("test", "1.9"));
                    repository.AddPackage(CreatePackage("test", "2.0-alpha"));
                    repository.AddPackage(CreatePackage("test", "2.0.0"));
                    repository.AddPackage(CreatePackage("test", "2.0.0-0test"));
                    repository.AddPackage(CreatePackage("test", "2.0.0-test+tag"));
                    repository.AddPackage(CreatePackage("test", "2.0.1+taggedOnly"));
                });

                // Act
                await serverRepository.RemovePackageAsync("test", new SemanticVersion("1.11"), Token);

                await serverRepository.RemovePackageAsync("test", new SemanticVersion("2.0-alpha"), Token);

                await serverRepository.RemovePackageAsync("test", new SemanticVersion("2.0.1"), Token);

                await serverRepository.RemovePackageAsync("test", new SemanticVersion("2.0.0-0test"), Token);

                var packages = await serverRepository.GetPackagesAsync(ClientCompatibility.Max, Token);

                // Assert
                Assert.Equal(3, packages.Count());
                Assert.Equal(1, packages.Count(p => p.SemVer2IsLatest));
                Assert.Equal("2.0.0", packages.First(p => p.SemVer2IsLatest).Version.ToString());

                Assert.Equal(1, packages.Count(p => p.SemVer2IsAbsoluteLatest));
                Assert.Equal("2.0.0", packages.First(p => p.SemVer2IsAbsoluteLatest).Version.ToString());
            }
        }
예제 #33
0
        public void EnumerateWithSymLinkToFile()
        {
            using (var containingFolder = new TemporaryDirectory())
            {
                string linkPath;

                // Test a symlink to a file that does and then doesn't exist
                using (var targetFile = new TemporaryFile())
                {
                    linkPath = Path.Combine(containingFolder.Path, Path.GetRandomFileName());
                    Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetFile.Path, isDirectory: false));

                    Assert.True(File.Exists(linkPath));
                    Assert.Equal(1, GetEntries(containingFolder.Path).Count());
                }

                // The symlink still exists even though the target file is gone.
                Assert.Equal(1, GetEntries(containingFolder.Path).Count());

                // The symlink is gone
                File.Delete(linkPath);
                Assert.Equal(0, GetEntries(containingFolder.Path).Count());
            }
        }
예제 #34
0
        public void TestSignaturesV2_3_X()
        {
            SparkContext sc = SparkContext.GetOrCreate(new SparkConf());

            _ = sc.GetConf();
            _ = sc.DefaultParallelism;

            sc.SetJobDescription("job description");

            sc.SetJobGroup("group id", "description");
            sc.SetJobGroup("group id", "description", true);

            sc.ClearJobGroup();

            string filePath = $"{TestEnvironment.ResourceDirectory}people.txt";

            sc.AddFile(filePath);
            sc.AddFile(filePath, true);

            using (var tempDir = new TemporaryDirectory())
            {
                sc.SetCheckpointDir(TestEnvironment.ResourceDirectory);
            }
        }
예제 #35
0
        public async Task AssertDeployFailedScriptIsRunOnFailure(bool deleteScripts)
        {
            using (var tempPath = TemporaryDirectory.Create())
            {
                File.WriteAllText(Path.Combine(tempPath.DirectoryPath, $"{DeploymentStages.DeployFailed}.ps1"), $"echo 'Hello from {DeploymentStages.DeployFailed}'");

                await CommandTestBuilder.CreateAsync <MyBadCommand, MyProgram>()
                .WithArrange(context =>
                {
                    context.Variables.Add(KnownVariables.Package.RunPackageScripts, bool.TrueString);
                    context.Variables.Add(KnownVariables.DeleteScriptsOnCleanup, deleteScripts.ToString());
                    context.WithFilesToCopy(tempPath.DirectoryPath);
                })
                .WithAssert(result =>
                {
                    result.WasSuccessful.Should().BeFalse();

                    result.FullLog.Should().Contain($"Hello from {DeploymentStages.DeployFailed}");

                    File.Exists(Path.Combine(tempPath.DirectoryPath, $"{DeploymentStages.DeployFailed}.ps1")).Should().Be(!deleteScripts);
                })
                .Execute(false);
            }
        }
예제 #36
0
        public void ReusesExtractedCopy()
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                using (var zip = new ZipArchive(File.OpenWrite(temporaryDirectory.GetPath("test.zip")), ZipArchiveMode.Create))
                {
                    zip.CreateEntry("fileA");
                    zip.CreateEntry("fileB");
                    zip.CreateEntry("fileC");
                }

                var zipFile = new FileInfo(temporaryDirectory.GetPath("test.zip"));

                using (var buffer = new TemporaryDirectory())
                {
                    var cache = new ZipFileCache(buffer, caseSensitive: false);

                    var extracted1 = cache.ExtractToCache(zipFile);
                    var extracted2 = cache.ExtractToCache(zipFile);

                    Assert.That(extracted1, Is.EqualTo(extracted2));
                }
            }
        }
예제 #37
0
        public void TestStreamingScenario()
        {
            using var tempDirectory = new TemporaryDirectory();
            // Write [0, 1, 2, 3, 4] to a Delta table.
            string sourcePath = Path.Combine(tempDirectory.Path, "source-delta-table");

            _spark.Range(0, 5).Write().Format("delta").Save(sourcePath);

            // Create a stream from the source DeltaTable to the sink DeltaTable.
            // To make the test synchronous and deterministic, we will use a series of
            // "one-time micro-batch" triggers.
            string           sinkPath         = Path.Combine(tempDirectory.Path, "sink-delta-table");
            DataStreamWriter dataStreamWriter = _spark
                                                .ReadStream()
                                                .Format("delta")
                                                .Load(sourcePath)
                                                .WriteStream()
                                                .Format("delta")
                                                .OutputMode("append")
                                                .Option("checkpointLocation", Path.Combine(tempDirectory.Path, "checkpoints"));

            // Trigger the first stream batch
            dataStreamWriter.Trigger(Trigger.Once()).Start(sinkPath).AwaitTermination();

            // Now read the sink DeltaTable and validate its content.
            DeltaTable sink = DeltaTable.ForPath(sinkPath);

            ValidateRangeDataFrame(Enumerable.Range(0, 5), sink.ToDF());

            // Write [5,6,7,8,9] to the source and trigger another stream batch.
            _spark.Range(5, 10).Write().Format("delta").Mode("append").Save(sourcePath);
            dataStreamWriter.Trigger(Trigger.Once()).Start(sinkPath).AwaitTermination();

            // Finally, validate that the new data made its way to the sink.
            ValidateRangeDataFrame(Enumerable.Range(0, 10), sink.ToDF());
        }
예제 #38
0
    public void GetUniquePath_ReturnsUniquePaths()
    {
        using (var temporaryData = new TemporaryDirectory(_dirPath))
        {
            temporaryData.PrepareForWriting();

            for (int i = 0; i < 100; i++)
            {
                string path = temporaryData.GetUniquePath();

                Assert.IsFalse(File.Exists(path));
                Assert.IsFalse(Directory.Exists(path));

                if (i % 2 == 0)
                {
                    File.WriteAllText(temporaryData.GetUniquePath(), "a");
                }
                else
                {
                    Directory.CreateDirectory(path);
                }
            }
        }
    }
        public async Task Remove_NoOpsWhenPackageDoesNotExist()
        {
            // Arrange
            using (var directory = new TemporaryDirectory())
            {
                var fileSystem = new PhysicalFileSystem(directory);
                var repository = new ExpandedPackageRepository(fileSystem);
                var logger     = new Infrastructure.NullLogger();

                repository.AddPackage(CreatePackage(PackageId, PackageVersion));

                var target = new ServerPackageStore(fileSystem, repository, logger);

                // Act
                target.Remove("Foo", PackageVersion, enableDelisting: false);

                // Assert
                var package = (await target.GetAllAsync(enableDelisting: false, token: Token)).FirstOrDefault();
                Assert.NotNull(package);
                Assert.Equal(PackageId, package.Id);
                Assert.Equal(PackageVersion, package.Version);
                Assert.True(package.Listed);
            }
        }
예제 #40
0
        public void ServerPackageRepositoryMultipleIds()
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                // Arrange
                var serverRepository = CreateServerPackageRepository(temporaryDirectory.Path, repository =>
                {
                    repository.AddPackage(CreatePackage("test", "0.9"));
                    repository.AddPackage(CreatePackage("test", "1.0"));
                    repository.AddPackage(CreatePackage("test2", "1.0"));
                    repository.AddPackage(CreatePackage("test3", "1.0-alpha"));
                    repository.AddPackage(CreatePackage("test4", "2.0"));
                });

                // Act
                var packages = serverRepository.GetPackages();

                // Assert
                Assert.Equal(4, packages.Count(p => p.IsAbsoluteLatestVersion));
                Assert.Equal(3, packages.Count(p => p.IsLatestVersion));
                Assert.Equal(1, packages.Count(p => !p.IsAbsoluteLatestVersion));
                Assert.Equal(2, packages.Count(p => !p.IsLatestVersion));
            }
        }
        public void ServerPackageRepositoryFindPackageById()
        {
            using (var temporaryDirectory = new TemporaryDirectory())
            {
                // Arrange
                var serverRepository = CreateServerPackageRepository(temporaryDirectory.Path, repository =>
                {
                    repository.AddPackage(CreatePackage("test", "1.0"));
                    repository.AddPackage(CreatePackage("test2", "1.0"));
                    repository.AddPackage(CreatePackage("test3", "1.0-alpha"));
                    repository.AddPackage(CreatePackage("test4", "2.0"));
                    repository.AddPackage(CreatePackage("test4", "3.0.0+tagged"));
                    repository.AddPackage(CreatePackage("Not5", "4.0"));
                });

                // Act
                var valid   = serverRepository.FindPackagesById("test");
                var invalid = serverRepository.FindPackagesById("bad");

                // Assert
                Assert.Equal("test", valid.First().Id);
                Assert.Equal(0, invalid.Count());
            }
        }
예제 #42
0
        public void TestExtract()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("CAB extraction relies on a Win32 API and therefore will not work on non-Windows platforms");
            }

            using (var stream = typeof(ExtractorTest).GetEmbeddedStream("testArchive.cab"))
                using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
                    using (var extractor = Extractor.FromStream(stream, sandbox, Archive.MimeTypeCab))
                    {
                        extractor.Run();

                        string filePath = Path.Combine(sandbox, "file");
                        Assert.IsTrue(File.Exists(filePath), "Should extract file 'file'");
                        Assert.AreEqual(new DateTime(2000, 1, 1, 13, 0, 0), File.GetLastWriteTimeUtc(filePath), "Correct last write time should be set");
                        Assert.AreEqual("abc", File.ReadAllText(filePath));

                        filePath = Path.Combine(sandbox, Path.Combine("folder1", "file"));
                        Assert.IsTrue(File.Exists(filePath), "Should extract file 'dir/file'");
                        Assert.AreEqual(new DateTime(2000, 1, 1, 13, 0, 0), File.GetLastWriteTimeUtc(filePath), "Correct last write time should be set");
                        Assert.AreEqual("def", File.ReadAllText(filePath));
                    }
        }
예제 #43
0
        public void TestIDFModel()
        {
            string expectedInputCol     = "rawFeatures";
            string expectedOutputCol    = "features";
            int    expectedDocFrequency = 100;

            IDF idf = new IDF()
                      .SetInputCol(expectedInputCol)
                      .SetOutputCol(expectedOutputCol)
                      .SetMinDocFreq(expectedDocFrequency);

            Assert.Equal(expectedInputCol, idf.GetInputCol());
            Assert.Equal(expectedOutputCol, idf.GetOutputCol());
            Assert.Equal(expectedDocFrequency, idf.GetMinDocFreq());

            using (var tempDirectory = new TemporaryDirectory())
            {
                string savePath = Path.Join(tempDirectory.Path, "IDF");
                idf.Save(savePath);

                IDF loadedIdf = IDF.Load(savePath);
                Assert.Equal(idf.Uid(), loadedIdf.Uid());
            }
        }
예제 #44
0
        public void ShowDiff(object repositoryId, long firstVersion, long lastVersion)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    using (TemporaryDirectory tempDir1 = new TemporaryDirectory( ), tempDir2 = new TemporaryDirectory( ))
                    {
                        string tempFile1 = GetFile(repositoryId, firstVersion, tempDir1);
                        string tempFile2 = GetFile(repositoryId, lastVersion, tempDir2);

                        Process viewer = ExternalToolsHelper.GetDiffProgram(
                            tempFile1,
                            tempFile2,
                            String.Empty,
                            String.Empty,
                            ExternalToolsSettings);

                        // If no suitable default tool exists, use the ClearCase diff tool.
                        if (viewer.StartInfo.FileName == String.Empty)
                        {
                            ClientHelper.CmdExec("diff -g \"{0}@@/main/{1}\" \"{0}@@/main/{2}\"", repositoryId, firstVersion, lastVersion);
                        }
                        else
                        {
                            viewer.Start( );
                            viewer.WaitForExit( );
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            });
        }
        public async Task Deploy_WebApp_With_PhysicalPath()
        {
            var webAppName = SdkContext.RandomResourceName(nameof(DeployAzureWebCommandFixture), 60);

            var webApp = await CreateWebApp(webAppName);

            using var tempPath = TemporaryDirectory.Create();
            var actualText = "Hello World";

            File.WriteAllText(Path.Combine(tempPath.DirectoryPath, "index.html"), actualText);
            const string rootPath = "Hello";

            await CommandTestBuilder.CreateAsync <DeployAzureWebCommand, Program>()
            .WithArrange(context =>
            {
                AddDefaults(context, webAppName);
                context.Variables.Add(SpecialVariables.Action.Azure.PhysicalPath, rootPath);

                context.WithFilesToCopy(tempPath.DirectoryPath);
            })
            .Execute();

            await AssertContent(webApp.DefaultHostName, actualText, rootPath);
        }
예제 #46
0
        public async Task TestUnchanged()
        {
            using (var oldDir = new TemporaryDirectory())
                using (var newDir = new TemporaryDirectory())
                    using (var patchDir = new TemporaryDirectory())
                        using (var builder = new DirectoryPatchBuilder(new XdeltaPatchBuilder(XdeltaPatchSystemFactory.Preferred)))
                        {
                            File.WriteAllText(Path.Combine(oldDir.Path, "a"), DummyDataA);
                            File.WriteAllText(Path.Combine(newDir.Path, "a"), DummyDataA);

                            await builder.CreatePatchAsync(oldDir.Path, newDir.Path, patchDir.Path);

                            string instructionsString = File.ReadAllText(patchDir.Path + Path.DirectorySeparatorChar + "instructions.json");
                            var    instructions       = JsonConvert.DeserializeObject <List <FilePatchInstruction> >(instructionsString);

                            Assert.AreEqual(1, instructions.Count);
                            Assert.AreEqual("a", instructions[0].Path);
                            Assert.AreEqual(GetStringHash(DummyDataA), instructions[0].OldHash);
                            Assert.AreEqual(GetStringHash(DummyDataA), instructions[0].NewHash);
                            Assert.AreEqual(false, instructions[0].HasDelta);
                            Assert.IsTrue(File.Exists(patchDir.Path + Path.DirectorySeparatorChar + "full" + Path.DirectorySeparatorChar + instructions[0].NewHash));
                            Assert.IsFalse(File.Exists(patchDir.Path + Path.DirectorySeparatorChar + "delta" + Path.DirectorySeparatorChar + instructions[0].NewHash + "_from_" + instructions[0].OldHash));
                        }
        }
예제 #47
0
 public async Task ConfiguredScriptDoesNotExecute()
 {
     using (var tempPath = TemporaryDirectory.Create())
     {
         await CommandTestBuilder.CreateAsync <NoConfiguredScriptCommand, MyProgram>()
         .WithArrange(context =>
         {
             context.Variables.Add(KnownVariables.Features.CustomScripts, bool.TrueString);
             foreach (var stage in defaultScriptStages)
             {
                 context.Variables.Add(KnownVariables.Action.CustomScripts.GetCustomScriptStage(stage, ScriptSyntax.PowerShell), $"echo 'Hello from {stage}");
             }
         })
         .WithAssert(result =>
         {
             result.WasSuccessful.Should().BeTrue();
             foreach (var stage in defaultScriptStages)
             {
                 result.FullLog.Should().NotContain($"Hello from {stage}");
             }
         })
         .Execute(true);
     }
 }
예제 #48
0
        public void TestCalculateDigest()
        {
            using var testDir = new TemporaryDirectory("0install-unit-tests");
            new TestRoot
            {
                new TestDirectory("subdir")
                {
                    new TestFile("file")
                }
            }.Build(testDir);

            GenerateManifest(testDir, ManifestFormat.Sha1New, new MockTaskHandler())
            .CalculateDigest()
            .Should().Be(CreateDotFile(testDir, ManifestFormat.Sha1New, new MockTaskHandler()),
                         because: "sha1new dot file and digest should match");
            GenerateManifest(testDir, ManifestFormat.Sha256, new MockTaskHandler())
            .CalculateDigest()
            .Should().Be(CreateDotFile(testDir, ManifestFormat.Sha256, new MockTaskHandler()),
                         because: "sha256 dot file and digest should match");
            GenerateManifest(testDir, ManifestFormat.Sha256New, new MockTaskHandler())
            .CalculateDigest()
            .Should().Be(CreateDotFile(testDir, ManifestFormat.Sha256New, new MockTaskHandler()),
                         because: "sha256new dot file and digest should match");
        }
예제 #49
0
        public override void Execute(CancellationToken cancellationToken)
        {
            base.Execute(cancellationToken);

            foreach (var entry in _entries)
            {
                var tempDirName = _packagePath + string.Format("{0}_{1}_{2}", entry.Name, entry.Offset, entry.Size);
                TemporaryDirectory.ExecuteIn(tempDirName, (tempDir) =>
                {
                    _logger.LogDebug(string.Format("Repairing the file {0}", entry.Name));
                    string packagePath   = Path.Combine(tempDir.Path, ".pack" + Path.GetRandomFileName());
                    string unarchivePath = Path.Combine(tempDir.Path, Path.GetRandomFileName());

                    if (!Directory.Exists(unarchivePath))
                    {
                        DirectoryOperations.CreateDirectory(unarchivePath, cancellationToken);
                    }

                    var downloader = new ChunkedHttpDownloader(packagePath, _resource.ResourceUrls, _resource.ChunksData, _resource.Size);

                    long start = entry.Offset.GetValueOrDefault();
                    long end   = (start + entry.Size.GetValueOrDefault()) - 1; // Offset by 1 to denote a byte index

                    var range = new BytesRange(start, end);

                    downloader.SetRange(range);
                    var effectiveRange = downloader.CalculateContainingChunksRange(range);

                    long totalData = effectiveRange.End == -1 ? _resource.Size - effectiveRange.Start : effectiveRange.End - effectiveRange.Start;

                    var downloadStatus = _entryStatus[entry].DownloadStatus;
                    var repairStatus   = _entryStatus[entry].RepairStatus;

                    downloadStatus.IsActive.Value    = true;
                    downloadStatus.TotalBytes.Value  = totalData;
                    downloadStatus.Description.Value = "Downloading broken file...";
                    downloadStatus.Bytes.Value       = 0;

                    downloader.DownloadProgressChanged += downloadedBytes =>
                    {
                        downloadStatus.Bytes.Value = downloadedBytes;
                    };

                    _logger.LogDebug(string.Format("Downloading the partial package with range {0}-{1}", start, end));
                    downloader.Download(cancellationToken);

                    downloadStatus.IsActive.Value = false;

                    repairStatus.IsActive.Value    = true;
                    repairStatus.Description.Value = "Reparing broken file...";
                    repairStatus.Progress.Value    = 0.0;

                    _logger.LogDebug("Unarchiving the package.");
                    var unarchiver = new Pack1Unarchiver(packagePath, _meta, unarchivePath, _packagePassword, _unpackingSuffix, effectiveRange);
                    unarchiver.UnarchiveProgressChanged += (name, isFile, unarchiveEntry, amount, entryProgress) =>
                    {
                        repairStatus.Progress.Value = entryProgress;
                    };

                    unarchiver.UnarchiveSingleFile(entry, cancellationToken);

                    EmplaceFile(Path.Combine(unarchivePath, entry.Name + _unpackingSuffix), Path.Combine(_localData.Path, entry.Name), cancellationToken);

                    repairStatus.IsActive.Value = false;
                });
            }
        }
예제 #50
0
        public static void Apply([NotNull] this SingleFile step, [NotNull] TemporaryFile downloadedFile, [NotNull] TemporaryDirectory workingDir)
        {
            #region Sanity checks
            if (step == null)
            {
                throw new ArgumentNullException(nameof(step));
            }
            if (downloadedFile == null)
            {
                throw new ArgumentNullException(nameof(downloadedFile));
            }
            if (workingDir == null)
            {
                throw new ArgumentNullException(nameof(workingDir));
            }
            #endregion

            #region Path validation
            if (string.IsNullOrEmpty(step.Destination))
            {
                throw new IOException(Resources.FileMissingDest);
            }
            string destination = FileUtils.UnifySlashes(step.Destination);
            if (FileUtils.IsBreakoutPath(destination))
            {
                throw new IOException(string.Format(Resources.RecipeInvalidPath, destination));
            }
            #endregion

            string destinationPath = Path.Combine(workingDir, destination);
            string parentDir       = Path.GetDirectoryName(destinationPath);
            if (!string.IsNullOrEmpty(parentDir) && !Directory.Exists(parentDir))
            {
                Directory.CreateDirectory(parentDir);
            }
            FileUtils.Replace(downloadedFile, destinationPath);
            File.SetLastWriteTimeUtc(destinationPath, FileUtils.FromUnixTime(0));

            // Update in flag files as well
            FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.XbitFile), destination);
            FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.SymlinkFile), destination);
        }
예제 #51
0
        public static TemporaryDirectory Apply([NotNull] this Recipe recipe, [NotNull, ItemNotNull] IEnumerable <TemporaryFile> downloadedFiles, [NotNull] ITaskHandler handler, [CanBeNull] object tag = null)
        {
            #region Sanity checks
            if (recipe == null)
            {
                throw new ArgumentNullException(nameof(recipe));
            }
            if (downloadedFiles == null)
            {
                throw new ArgumentNullException(nameof(downloadedFiles));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            if (recipe.UnknownElements != null && recipe.UnknownElements.Length != 0)
            {
                throw new NotSupportedException(string.Format(Resources.UnknownRecipeStepType, recipe.UnknownElements[0].Name));
            }

            var workingDir = new TemporaryDirectory("0install-recipe");

            try
            {
                IEnumerator <TemporaryFile> downloadedEnum = downloadedFiles.GetEnumerator();
                // ReSharper disable AccessToDisposedClosure
                new PerTypeDispatcher <IRecipeStep>(ignoreMissing: false)
                {
                    (Archive step) =>
                    {
                        downloadedEnum.MoveNext();
                        if (downloadedEnum.Current == null)
                        {
                            throw new ArgumentException(Resources.RecipeFileNotDownloaded, nameof(downloadedFiles));
                        }
                        step.Apply(downloadedEnum.Current, workingDir, handler, tag);
                    },
                    (SingleFile step) =>
                    {
                        downloadedEnum.MoveNext();
                        if (downloadedEnum.Current == null)
                        {
                            throw new ArgumentException(Resources.RecipeFileNotDownloaded, nameof(downloadedFiles));
                        }
                        step.Apply(downloadedEnum.Current, workingDir);
                    },
                    (RemoveStep step) => step.Apply(workingDir),
                    (RenameStep step) => step.Apply(workingDir)
                }.Dispatch(recipe.Steps);
                // ReSharper restore AccessToDisposedClosure
                return(workingDir);
            }
            #region Error handling
            catch
            {
                workingDir.Dispose();
                throw;
            }
            #endregion
        }
예제 #52
0
        /// <summary>
        /// Applies a <see cref="Archive"/> to a <see cref="TemporaryDirectory"/>.
        /// </summary>
        /// <param name="step">The <see cref="Archive"/> to apply.</param>
        /// <param name="localPath">The local path of the archive.</param>
        /// <param name="workingDir">The <see cref="TemporaryDirectory"/> to apply the changes to.</param>
        /// <param name="handler">A callback object used when the the user needs to be informed about progress.</param>
        /// <param name="tag">The <see cref="ITaskHandler"/> tag used by <paramref name="handler"/>; can be <c>null</c>.</param>
        /// <exception cref="IOException">A path specified in <paramref name="step"/> is illegal.</exception>
        public static void Apply([NotNull] this Archive step, [NotNull] string localPath, [NotNull] TemporaryDirectory workingDir, [NotNull] ITaskHandler handler, [CanBeNull] object tag = null)
        {
            #region Sanity checks
            if (step == null)
            {
                throw new ArgumentNullException(nameof(step));
            }
            if (string.IsNullOrEmpty(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (workingDir == null)
            {
                throw new ArgumentNullException(nameof(workingDir));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            #region Path validation
            if (!string.IsNullOrEmpty(step.Destination))
            {
                string destination = FileUtils.UnifySlashes(step.Destination);
                if (FileUtils.IsBreakoutPath(destination))
                {
                    throw new IOException(string.Format(Resources.RecipeInvalidPath, destination));
                }
            }
            #endregion

            if (string.IsNullOrEmpty(step.MimeType))
            {
                throw new IOException(Resources.UnknownArchiveType);
            }

            using (var extractor = ArchiveExtractor.Create(localPath, workingDir, step.MimeType))
            {
                extractor.SubDir      = step.Extract;
                extractor.Destination = FileUtils.UnifySlashes(step.Destination);
                extractor.Tag         = tag;
                handler.RunTask(extractor);
            }
        }
예제 #53
0
        /// <summary>
        /// Applies a <see cref="SingleFile"/> to a <see cref="TemporaryDirectory"/>.
        /// </summary>
        /// <param name="step">The <see cref="Archive"/> to apply.</param>
        /// <param name="localPath">The local path of the file.</param>
        /// <param name="workingDir">The <see cref="TemporaryDirectory"/> to apply the changes to.</param>
        /// <param name="handler">A callback object used when the the user needs to be informed about progress.</param>
        /// <exception cref="IOException">A path specified in <paramref name="step"/> is illegal.</exception>
        public static void Apply([NotNull] this SingleFile step, [NotNull] string localPath, [NotNull] TemporaryDirectory workingDir, [NotNull] ITaskHandler handler)
        {
            #region Sanity checks
            if (step == null)
            {
                throw new ArgumentNullException(nameof(step));
            }
            if (string.IsNullOrEmpty(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (workingDir == null)
            {
                throw new ArgumentNullException(nameof(workingDir));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            // Use a copy of the original file because the source file is moved
            using (var tempFile = new TemporaryFile("0install"))
            {
                // ReSharper disable once AccessToDisposedClosure
                handler.RunTask(new SimpleTask(Resources.CopyFiles, () => File.Copy(localPath, tempFile, overwrite: true)));
                step.Apply(tempFile, workingDir);
            }
        }
예제 #54
0
 public void SetUp()
 {
     _handler = new MockTaskHandler();
     _tempDir = new TemporaryDirectory("0install-unit-tests");
     _store   = new DirectoryStore(_tempDir);
 }
예제 #55
0
 public void DisposedDeletedDirectory()
 {
     using var dir = TemporaryDirectory.Create();
     Directory.Delete(dir.FullPath);
 }
예제 #56
0
        private void ShareMinimal(ZipFileShare zip)
        {
            TemporaryDirectory tempDir = null;

            try
            {
                var docOriginal = Document;
                if (Document.Settings.HasBackgroundProteome)
                {
                    // Remove any background proteome reference
                    Document = Document.ChangeSettings(Document.Settings.ChangePeptideSettings(
                                                           set => set.ChangeBackgroundProteome(BackgroundProteome.NONE)));
                }
                if (Document.Settings.HasRTCalcPersisted)
                {
                    // Minimize any persistable retention time calculator
                    tempDir = new TemporaryDirectory();
                    string tempDbPath = Document.Settings.PeptideSettings.Prediction.RetentionTime
                                        .Calculator.PersistMinimized(tempDir.DirPath, Document);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath);
                    }
                }
                if (Document.Settings.HasOptimizationLibraryPersisted)
                {
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    string tempDbPath = Document.Settings.TransitionSettings.Prediction.OptimizedLibrary.PersistMinimized(
                        tempDir.DirPath, Document);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath);
                    }
                }
                if (Document.Settings.HasIonMobilityLibraryPersisted)
                {
                    // Minimize any persistable ion mobility predictor
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    string tempDbPath = Document.Settings.TransitionSettings.IonMobilityFiltering.IonMobilityLibrary
                                        .PersistMinimized(tempDir.DirPath, Document, null, out _);
                    if (tempDbPath != null)
                    {
                        zip.AddFile(tempDbPath);
                    }
                }
                if (Document.Settings.HasLibraries)
                {
                    // Minimize all libraries in a temporary directory, and add them
                    if (tempDir == null)
                    {
                        tempDir = new TemporaryDirectory();
                    }
                    Document = BlibDb.MinimizeLibraries(Document, tempDir.DirPath,
                                                        Path.GetFileNameWithoutExtension(DocumentPath),
                                                        null,
                                                        ProgressMonitor);
                    if (ProgressMonitor != null && ProgressMonitor.IsCanceled)
                    {
                        return;
                    }

                    foreach (var librarySpec in Document.Settings.PeptideSettings.Libraries.LibrarySpecs)
                    {
                        var tempLibPath = Path.Combine(tempDir.DirPath, Path.GetFileName(librarySpec.FilePath) ?? string.Empty);
                        zip.AddFile(tempLibPath);

                        // If there is a .redundant.blib file that corresponds to a .blib file
                        // in the temp temporary directory, add that as well
                        IncludeRedundantBlib(librarySpec, zip, tempLibPath);
                    }
                }

                var auditLogDocPath = DocumentPath;
                tempDir = ShareDataAndView(zip, tempDir);
                if (ReferenceEquals(docOriginal, Document) && null == ShareType.SkylineVersion)
                {
                    zip.AddFile(DocumentPath);
                }
                else
                {
                    tempDir = ShareDocument(zip, tempDir, out auditLogDocPath);
                }

                SafeAuditLog(zip, auditLogDocPath);

                Save(zip);
            }
            finally
            {
                DeleteTempDir(tempDir);
            }
        }
예제 #57
0
        private void ShareComplete(ZipFileShare zip)
        {
            TemporaryDirectory tempDir = null;

            try
            {
                // If complete sharing, just zip up existing files
                var pepSettings        = Document.Settings.PeptideSettings;
                var transitionSettings = Document.Settings.TransitionSettings;
                if (Document.Settings.HasBackgroundProteome)
                {
                    zip.AddFile(pepSettings.BackgroundProteome.BackgroundProteomeSpec.DatabasePath);
                }
                if (Document.Settings.HasRTCalcPersisted)
                {
                    zip.AddFile(pepSettings.Prediction.RetentionTime.Calculator.PersistencePath);
                }
                if (Document.Settings.HasOptimizationLibraryPersisted)
                {
                    zip.AddFile(transitionSettings.Prediction.OptimizedLibrary.PersistencePath);
                }
                if (Document.Settings.HasIonMobilityLibraryPersisted)
                {
                    zip.AddFile(transitionSettings.IonMobilityFiltering.IonMobilityLibrary.FilePath);
                }

                var libfiles = new HashSet <string>();
                foreach (var librarySpec in pepSettings.Libraries.LibrarySpecs)
                {
                    if (libfiles.Add(librarySpec.FilePath))
                    // Sometimes the same .blib file is referred to by different library specs
                    {
                        zip.AddFile(librarySpec.FilePath);

                        if (Document.Settings.TransitionSettings.FullScan.IsEnabledMs)
                        {
                            // If there is a .redundant.blib file that corresponds
                            // to a .blib file, add that as well
                            IncludeRedundantBlib(librarySpec, zip, librarySpec.FilePath);
                        }
                    }
                }

                var auditLogDocPath = DocumentPath;
                // ReSharper disable ExpressionIsAlwaysNull
                tempDir = ShareDataAndView(zip, tempDir);
                // ReSharper restore ExpressionIsAlwaysNull
                if (null == ShareType.SkylineVersion)
                {
                    zip.AddFile(DocumentPath); // CONSIDER(bpratt) there's no check to see if this is a current representation of the document - a dirty check would be good
                }
                else
                {
                    tempDir = ShareDocument(zip, tempDir, out auditLogDocPath);
                }

                SafeAuditLog(zip, auditLogDocPath);

                Save(zip);
            }
            finally
            {
                DeleteTempDir(tempDir);
            }
        }
예제 #58
0
        private void PatchFile(
            string fileName, string packageDirPath, string suffix,
            TemporaryDirectory tempDiffDir, CancellationToken cancellationToken)
        {
            _logger.LogDebug(string.Format("Processing patch file entry {0}", fileName));

            var filePath = _localData.Path.PathCombine(fileName);

            _logger.LogTrace("filePath = " + filePath);

            if (!File.Exists(filePath))
            {
                throw new MissingLocalDataFileException(
                          string.Format("Couldn't patch file {0} because it doesn't exists in local data.", fileName));
            }

            var fileVersion = _localMetaData.GetEntryVersionId(fileName);

            _logger.LogTrace("fileVersion = " + fileVersion);

            if (fileVersion != _versionId - 1)
            {
                throw new InvalidLocalDataFileVersionException(string.Format(
                                                                   "Couldn't patch file {0} because expected file version to be ({1}) but it's {2}.",
                                                                   fileName, _versionId - 1, fileVersion));
            }

            _logger.LogDebug("Checking whether patching file content is necessary...");
            if (IsPatchingFileContentNecessary(fileName))
            {
                _logger.LogDebug("Patching is necessary. Generating new file with patched content...");

                var sourceDeltaFilePath = Path.Combine(packageDirPath, fileName + suffix);
                _logger.LogTrace("sourceDeltaFilePath = " + sourceDeltaFilePath);

                if (!File.Exists(sourceDeltaFilePath))
                {
                    throw new MissingFileFromPackageException(string.Format("Cannot find delta file {0} in diff package.",
                                                                            fileName));
                }

                var newFilePath = tempDiffDir.GetUniquePath();
                _logger.LogTrace("newFilePath = " + newFilePath);

                var filePatcher = new FilePatcher(filePath, sourceDeltaFilePath, newFilePath);
                filePatcher.Patch();

                _logger.LogDebug("New file generated. Deleting old file in local data...");
                FileOperations.Delete(filePath, cancellationToken);

                _logger.LogDebug("Old file deleted. Moving new file to local data...");
                FileOperations.Move(newFilePath, filePath, cancellationToken);

                _logger.LogDebug("New file moved.");
            }
            else
            {
                _logger.LogDebug("Patching is not necessary. File content is the same as in previous version.");
            }

            _localMetaData.RegisterEntry(fileName, _versionId);

            _logger.LogDebug("Patch file entry processed.");
        }
예제 #59
0
        public void TestSignaturesV2_3_X()
        {
            Assert.IsType <Column>(_df["name"]);
            Assert.IsType <Column>(_df["age"]);

            Assert.IsType <DataFrame>(_df.ToDF());
            Assert.IsType <DataFrame>(_df.ToDF("name2", "age2"));

            StructType schema = _df.Schema();

            Assert.NotNull(schema);

            _df.PrintSchema();

            _df.Explain();
            _df.Explain(true);
            _df.Explain(false);

            Assert.Equal(2, _df.Columns().ToArray().Length);

            var expected = new List <Tuple <string, string> >
            {
                new Tuple <string, string>("age", "integer"),
                new Tuple <string, string>("name", "string")
            };

            Assert.Equal(expected, _df.DTypes());

            Assert.IsType <bool>(_df.IsLocal());

            Assert.IsType <bool>(_df.IsStreaming());

            using (var tempDir = new TemporaryDirectory())
            {
                // The following is required for *CheckPoint().
                _spark.SparkContext.SetCheckpointDir(tempDir.Path);

                Assert.IsType <DataFrame>(_df.Checkpoint());
                Assert.IsType <DataFrame>(_df.Checkpoint(false));

                Assert.IsType <DataFrame>(_df.LocalCheckpoint());
                Assert.IsType <DataFrame>(_df.LocalCheckpoint(false));
            }

            Assert.IsType <DataFrame>(_df.WithWatermark("time", "10 minutes"));

            _df.Show();
            _df.Show(10);
            _df.Show(10, 10);
            _df.Show(10, 10, true);

            Assert.IsType <DataFrame>(_df.Join(_df));
            Assert.IsType <DataFrame>(_df.Join(_df, "name"));
            Assert.IsType <DataFrame>(_df.Join(_df, new[] { "name" }));
            Assert.IsType <DataFrame>(_df.Join(_df, new[] { "name" }, "outer"));
            Assert.IsType <DataFrame>(_df.Join(_df, _df["age"] == _df["age"]));
            Assert.IsType <DataFrame>(_df.Join(_df, _df["age"] == _df["age"], "outer"));

            Assert.IsType <DataFrame>(_df.CrossJoin(_df));

            Assert.IsType <DataFrame>(_df.SortWithinPartitions("age"));
            Assert.IsType <DataFrame>(_df.SortWithinPartitions("age", "name"));
            Assert.IsType <DataFrame>(_df.SortWithinPartitions());
            Assert.IsType <DataFrame>(_df.SortWithinPartitions(_df["age"]));
            Assert.IsType <DataFrame>(_df.SortWithinPartitions(_df["age"], _df["name"]));

            Assert.IsType <DataFrame>(_df.Sort("age"));
            Assert.IsType <DataFrame>(_df.Sort("age", "name"));
            Assert.IsType <DataFrame>(_df.Sort());
            Assert.IsType <DataFrame>(_df.Sort(_df["age"]));
            Assert.IsType <DataFrame>(_df.Sort(_df["age"], _df["name"]));

            Assert.IsType <DataFrame>(_df.OrderBy("age"));
            Assert.IsType <DataFrame>(_df.OrderBy("age", "name"));
            Assert.IsType <DataFrame>(_df.OrderBy());
            Assert.IsType <DataFrame>(_df.OrderBy(_df["age"]));
            Assert.IsType <DataFrame>(_df.OrderBy(_df["age"], _df["name"]));

            Assert.IsType <DataFrame>(_df.Hint("broadcast"));
            Assert.IsType <DataFrame>(_df.Hint("broadcast", new[] { "hello", "world" }));

            Assert.IsType <Column>(_df.Col("age"));

            Assert.IsType <Column>(_df.ColRegex("age"));

            Assert.IsType <DataFrame>(_df.As("alias"));

            Assert.IsType <DataFrame>(_df.Alias("alias"));

            Assert.IsType <DataFrame>(_df.Select("age"));
            Assert.IsType <DataFrame>(_df.Select("age", "name"));
            Assert.IsType <DataFrame>(_df.Select());
            Assert.IsType <DataFrame>(_df.Select(_df["age"]));
            Assert.IsType <DataFrame>(_df.Select(_df["age"], _df["name"]));

            Assert.IsType <DataFrame>(_df.SelectExpr());
            Assert.IsType <DataFrame>(_df.SelectExpr("age * 2"));
            Assert.IsType <DataFrame>(_df.SelectExpr("age * 2", "abs(age)"));

            Assert.IsType <DataFrame>(_df.Filter(_df["age"] > 21));
            Assert.IsType <DataFrame>(_df.Filter("age > 21"));

            Assert.IsType <DataFrame>(_df.Where(_df["age"] > 21));
            Assert.IsType <DataFrame>(_df.Where("age > 21"));

            Assert.IsType <RelationalGroupedDataset>(_df.GroupBy("age"));
            Assert.IsType <RelationalGroupedDataset>(_df.GroupBy("age", "name"));
            Assert.IsType <RelationalGroupedDataset>(_df.GroupBy());
            Assert.IsType <RelationalGroupedDataset>(_df.GroupBy(_df["age"]));
            Assert.IsType <RelationalGroupedDataset>(_df.GroupBy(_df["age"], _df["name"]));

            {
                RelationalGroupedDataset df =
                    _df.WithColumn("tempAge", _df["age"]).GroupBy("name");

                Assert.IsType <DataFrame>(df.Mean("age"));
                Assert.IsType <DataFrame>(df.Mean("age", "tempAge"));

                Assert.IsType <DataFrame>(df.Max("age"));
                Assert.IsType <DataFrame>(df.Max("age", "tempAge"));

                Assert.IsType <DataFrame>(df.Avg("age"));
                Assert.IsType <DataFrame>(df.Avg("age", "tempAge"));

                Assert.IsType <DataFrame>(df.Min("age"));
                Assert.IsType <DataFrame>(df.Min("age", "tempAge"));

                Assert.IsType <DataFrame>(df.Sum("age"));
                Assert.IsType <DataFrame>(df.Sum("age", "tempAge"));
            }

            Assert.IsType <RelationalGroupedDataset>(_df.Rollup("age"));
            Assert.IsType <RelationalGroupedDataset>(_df.Rollup("age", "name"));
            Assert.IsType <RelationalGroupedDataset>(_df.Rollup());
            Assert.IsType <RelationalGroupedDataset>(_df.Rollup(_df["age"]));
            Assert.IsType <RelationalGroupedDataset>(_df.Rollup(_df["age"], _df["name"]));

            Assert.IsType <RelationalGroupedDataset>(_df.Cube("age"));
            Assert.IsType <RelationalGroupedDataset>(_df.Cube("age", "name"));
            Assert.IsType <RelationalGroupedDataset>(_df.Cube());
            Assert.IsType <RelationalGroupedDataset>(_df.Cube(_df["age"]));
            Assert.IsType <RelationalGroupedDataset>(_df.Cube(_df["age"], _df["name"]));

            Assert.IsType <DataFrame>(_df.Agg(Avg(_df["age"])));
            Assert.IsType <DataFrame>(_df.Agg(Avg(_df["age"]), Avg(_df["name"])));

            Assert.IsType <DataFrame>(_df.Limit(10));

            Assert.IsType <DataFrame>(_df.Union(_df));

            Assert.IsType <DataFrame>(_df.UnionByName(_df));

            Assert.IsType <DataFrame>(_df.Intersect(_df));

            Assert.IsType <DataFrame>(_df.Except(_df));

            Assert.IsType <DataFrame>(_df.Sample(0.5));
            Assert.IsType <DataFrame>(_df.Sample(0.5, true));
            Assert.IsType <DataFrame>(_df.Sample(0.5, false, 12345));

            Assert.IsType <DataFrame[]>(_df.RandomSplit(new[] { 0.2, 0.8 }));
            Assert.IsType <DataFrame[]>(_df.RandomSplit(new[] { 0.2, 0.8 }, 12345));

            Assert.IsType <DataFrame>(_df.WithColumn("age2", _df["age"]));

            Assert.IsType <DataFrame>(_df.WithColumnRenamed("age", "age2"));

            Assert.IsType <DataFrame>(_df.Drop());
            Assert.IsType <DataFrame>(_df.Drop("age"));
            Assert.IsType <DataFrame>(_df.Drop("age", "name"));

            Assert.IsType <DataFrame>(_df.Drop(_df["age"]));

            Assert.IsType <DataFrame>(_df.DropDuplicates());
            Assert.IsType <DataFrame>(_df.DropDuplicates("age"));
            Assert.IsType <DataFrame>(_df.DropDuplicates("age", "name"));

            Assert.IsType <DataFrame>(_df.Describe());
            Assert.IsType <DataFrame>(_df.Describe("age"));
            Assert.IsType <DataFrame>(_df.Describe("age", "name"));

            Assert.IsType <DataFrame>(_df.Summary());
            Assert.IsType <DataFrame>(_df.Summary("count"));
            Assert.IsType <DataFrame>(_df.Summary("count", "mean"));

            Assert.IsType <Row[]>(_df.Head(2).ToArray());
            Assert.IsType <Row>(_df.Head());

            Assert.IsType <Row>(_df.First());

            Assert.IsType <DataFrame>(_df.Transform(df => df.Drop("age")));

            Assert.IsType <Row[]>(_df.Take(3).ToArray());

            Assert.IsType <Row[]>(_df.Collect().ToArray());

            Assert.IsType <Row[]>(_df.ToLocalIterator().ToArray());

            Assert.IsType <long>(_df.Count());

            Assert.IsType <DataFrame>(_df.Repartition(2));
            Assert.IsType <DataFrame>(_df.Repartition(2, _df["age"]));
            Assert.IsType <DataFrame>(_df.Repartition(_df["age"]));
            Assert.IsType <DataFrame>(_df.Repartition());

            Assert.IsType <DataFrame>(_df.RepartitionByRange(2, _df["age"]));
            Assert.IsType <DataFrame>(_df.RepartitionByRange(_df["age"]));

            Assert.IsType <DataFrame>(_df.Coalesce(1));

            Assert.IsType <DataFrame>(_df.Distinct());

            Assert.IsType <DataFrame>(_df.Persist());

            Assert.IsType <DataFrame>(_df.Persist(StorageLevel.DISK_ONLY));

            Assert.IsType <DataFrame>(_df.Cache());

            Assert.IsType <StorageLevel>(_df.StorageLevel());

            Assert.IsType <DataFrame>(_df.Unpersist());

            _df.CreateTempView("view");
            _df.CreateOrReplaceTempView("view");

            _df.CreateGlobalTempView("global_view");
            _df.CreateOrReplaceGlobalTempView("global_view");
        }
예제 #60
0
        public static TemporaryDirectory LocalApply([NotNull] this DownloadRetrievalMethod retrievalMethod, string localPath, [NotNull] ITaskHandler handler, [CanBeNull] ICommandExecutor executor = null)
        {
            #region Sanity checks
            if (retrievalMethod == null)
            {
                throw new ArgumentNullException("retrievalMethod");
            }
            if (string.IsNullOrEmpty(localPath))
            {
                throw new ArgumentNullException("localPath");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            #endregion

            if (executor == null)
            {
                executor = new SimpleCommandExecutor();
            }

            // Set local file size
            long newSize = new FileInfo(localPath).Length;
            if (retrievalMethod.Size != newSize)
            {
                executor.Execute(new SetValueCommand <long>(() => retrievalMethod.Size, value => retrievalMethod.Size = value, newSize));
            }

            var extractionDir = new TemporaryDirectory("0publish");
            try
            {
                new PerTypeDispatcher <DownloadRetrievalMethod>(ignoreMissing: true)
                {
                    // ReSharper disable AccessToDisposedClosure
                    (Archive archive) =>
                    {
                        // Guess MIME types now because the file ending is not known later
                        if (string.IsNullOrEmpty(archive.MimeType))
                        {
                            string mimeType = Archive.GuessMimeType(localPath);
                            executor.Execute(new SetValueCommand <string>(() => archive.MimeType, value => archive.MimeType = value, mimeType));
                        }

                        archive.Apply(localPath, extractionDir, handler);
                    },
                    (SingleFile file) =>
                    {
                        // Guess file name based on local path
                        if (string.IsNullOrEmpty(file.Destination))
                        {
                            string destination = Path.GetFileName(localPath);
                            executor.Execute(new SetValueCommand <string>(() => file.Destination, value => file.Destination = value, destination));
                        }

                        file.Apply(localPath, extractionDir, handler);
                    }
                    // ReSharper restore AccessToDisposedClosure
                }.Dispatch(retrievalMethod);
            }
            #region Error handling
            catch
            {
                extractionDir.Dispose();
                throw;
            }
            #endregion

            return(extractionDir);
        }