Example #1
0
            public async Task CancellationToken_ExceptionThrown()
            {
                var contents = Enumerable.Repeat("This is a test line.", 300000).ToList();
                var path     = Path.Combine(moveTestFolder, nameof(CancellationToken_ExceptionThrown));

                Directory.CreateDirectory(moveTestFolder);
                File.WriteAllLines(path, contents);

                var movePath = Path.Combine(moveTestFolder, $"{nameof(CancellationToken_ExceptionThrown)}_Moved");

                File.Delete(movePath);

                var tokenSource = new CancellationTokenSource();

                Assert.ThrowsAsync <TaskCanceledException>(async() =>
                {
                    var task = AsyncFile.MoveAsync(path, movePath, tokenSource.Token);
                    tokenSource.Cancel();
                    await task;
                });

                var result = File.ReadAllLines(movePath);

                Assert.IsTrue(contents.Count > result.Length);
                Assert.IsTrue(File.Exists(path));
            }
Example #2
0
File: IO.cs Project: BENICHN/BenLib
        public static async Task MoveAsync(string sourceDirectoryName, string destDirectoryName, CancellationToken cancellationToken = default)
        {
            foreach (string dir in Directory.EnumerateDirectories(sourceDirectoryName))
            {
                string postDir = Path.Combine(destDirectoryName, dir.Substring(sourceDirectoryName.Length + 1));
                if (!Directory.Exists(postDir))
                {
                    Directory.CreateDirectory(postDir);
                }
                await MoveAsync(dir, postDir, cancellationToken);
            }

            foreach (string file in Directory.EnumerateFiles(sourceDirectoryName))
            {
                await AsyncFile.MoveAsync(file, Path.Combine(destDirectoryName, Path.GetFileName(file)), cancellationToken);
            }
        }
Example #3
0
            public async Task SamePath_FileMoved()
            {
                var contents = Enumerable.Repeat("This is a test line.", 150000).ToList();
                var path     = Path.Combine(moveTestFolder, nameof(SamePath_FileMoved));

                Directory.CreateDirectory(moveTestFolder);
                File.WriteAllLines(path, contents);

                var movePath = path;

                await AsyncFile.MoveAsync(path, movePath);

                var result = File.ReadAllLines(movePath);

                CollectionAssert.AreEqual(contents, result);
                Assert.IsTrue(File.Exists(path));
            }
Example #4
0
            public void Overwrite_ExceptionThrown()
            {
                var contents = Enumerable.Repeat("This is a test line.", 150).ToList();
                var path     = Path.Combine(moveTestFolder, nameof(Overwrite_ExceptionThrown));

                Directory.CreateDirectory(moveTestFolder);
                File.WriteAllLines(path, contents);

                var movePath = Path.Combine(moveTestFolder, $"{nameof(Overwrite_ExceptionThrown)}_Moved");

                if (!File.Exists(movePath))
                {
                    File.Create(movePath).Dispose();
                }

                Assert.ThrowsAsync <IOException>(async() => await AsyncFile.MoveAsync(path, movePath));
            }