示例#1
0
        public void TestWalk()
        {
            using (var tempDir = new TemporaryDirectory("unit-tests"))
            {
                string subDirPath = Path.Combine(tempDir, "subdir");
                Directory.CreateDirectory(subDirPath);
                string filePath = Path.Combine(subDirPath, "file");
                File.WriteAllText(filePath, "");
                string symlinkPath = Path.Combine(tempDir, "symlink");
                if (UnixUtils.IsUnix)
                {
                    UnixUtils.CreateSymlink(symlinkPath, ".");
                }
                else
                {
                    Directory.CreateDirectory(symlinkPath);
                }

                // Set up delegate mocks
                var dirCallbackMock = new Mock <IActionSimulator <string> >(MockBehavior.Strict);
                // ReSharper disable once AccessToDisposedClosure
                dirCallbackMock.Setup(x => x.Execute(tempDir)).Verifiable();
                dirCallbackMock.Setup(x => x.Execute(subDirPath)).Verifiable();
                dirCallbackMock.Setup(x => x.Execute(symlinkPath)).Verifiable();
                var fileCallbackMock = new Mock <IActionSimulator <string> >(MockBehavior.Strict);
                fileCallbackMock.Setup(x => x.Execute(filePath)).Verifiable();

                new DirectoryInfo(tempDir).Walk(
                    dir => dirCallbackMock.Object.Execute(dir.FullName),
                    file => fileCallbackMock.Object.Execute(file.FullName));

                dirCallbackMock.Verify();
                fileCallbackMock.Verify();
            }
        }
示例#2
0
        /// <inheritdoc/>
        protected override void OnStage()
        {
            if (!Directory.Exists(DestinationPath))
            {
                Directory.CreateDirectory(DestinationPath);
                _createdDirectories.Push(DestinationPath);
            }

            if (FileUtils.DetermineTimeAccuracy(DestinationPath) > 0)
            {
                throw new IOException(Resources.InsufficientFSTimeAccuracy);
            }

            string manifestPath     = System.IO.Path.Combine(DestinationPath, Manifest.ManifestFile);
            string tempManifestPath = Randomize(manifestPath);

            _pendingFileRenames.Push(new KeyValuePair <string, string>(tempManifestPath, manifestPath));
            Manifest.Save(tempManifestPath);

            Handler.RunTask(ForEachTask.Create(Resources.CopyFiles, ElementPaths, pair =>
            {
                string sourcePath      = System.IO.Path.Combine(Path, pair.Key);
                string destinationPath = System.IO.Path.Combine(DestinationPath, pair.Key);

                if (pair.Value is ManifestDirectory)
                {
                    if (!Directory.Exists(destinationPath))
                    {
                        Directory.CreateDirectory(destinationPath);
                        _createdDirectories.Push(destinationPath);
                    }
                }
                else
                {
                    string tempPath = Randomize(destinationPath);
                    _pendingFileRenames.Push(new KeyValuePair <string, string>(tempPath, destinationPath));

                    switch (pair.Value)
                    {
                    case ManifestFileBase file:
                        File.Copy(sourcePath, tempPath);
                        File.SetLastWriteTimeUtc(tempPath, file.ModifiedTime);

                        if (UnixUtils.IsUnix)
                        {
                            FileUtils.SetExecutable(tempPath, file is ManifestExecutableFile);
                        }
                        break;

                    case ManifestSymlink _:
                        if (UnixUtils.IsUnix)
                        {
                            if (UnixUtils.IsSymlink(sourcePath, out string?symlinkTarget))
                            {
                                UnixUtils.CreateSymlink(tempPath, symlinkTarget);
                            }
                        }
                        break;
                    }
                }
            }));
        }