Exemplo n.º 1
0
        public void CopyTo_CanCreateNewOnwership()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var srcDir  = driver.CreateOrGetDirectory(@"c:\src");
                var srcFile = driver.CreateOrGetFile(@"c:\src\file.txt");
                driver.CreateOrGetFile(@"c:\src\file_2.txt");

                var targetDir = driver.CreateOrGetDirectory(@"c:\targ");

                // act
                srcFile.CopyTo(@"c:\targ\file.txt", true);

                // assert
                var targetFiles = targetDir.GetFiles();
                var srcFiles    = srcDir.GetFiles();
                Assert.AreEqual(2, srcFiles.Length);
                Assert.AreEqual(1, srcFiles.Where(i => i.FullName == @"c:\src\file.txt").Count());
                Assert.AreEqual(1, srcFiles.Where(i => i.FullName == @"c:\src\file_2.txt").Count());

                Assert.AreEqual(1, targetFiles.Length);
                Assert.AreEqual(@"c:\targ\file.txt", targetFiles[0].FullName);
            }
        }
Exemplo n.º 2
0
        public void Move_TargetDirAlreadyExists_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var from = driver.CreateOrGetDirectory(@"c:\dir1");
                driver.CreateOrGetDirectory(@"c:\dir2");

                // act/assert
                Assert.ThrowsException <IOException>(() => from.MoveTo(@"c:\dir2"));
            }
        }
Exemplo n.º 3
0
        public void GetDirectories_Recurse_Wilchars_CanFind()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_2");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder1"); // to check if they are excluded from result
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder1"); // to check if they are excluded from result


                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_3");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_4");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder3"); // to check if they are excluded from result
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder4"); // to check if they are excluded from result

                var root = driver.GetDirectory(@"c:\root");

                // act
                var result = root.GetDirectories(@"subfolder_*", SearchOption.AllDirectories);

                // assert
                Assert.AreEqual(4, result.Length);
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_1").Count());
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_2").Count());
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_2\subfolder_3").Count());
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_2\subfolder_4").Count());
            }
        }
Exemplo n.º 4
0
        public void MoveTo_TargetIsNewFile_CanMoveAndChangeName()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var src = driver.CreateOrGetFile(@"c:\src\file.txt");
                driver.CreateOrGetDirectory(@"c:\targ");
                using (var t = src.CreateText())
                {
                    t.Write("CONTENT");
                }

                // act
                src.MoveTo(@"c:\targ\moved.txt");
                src.Refresh();

                // assert
                var target = driver.GetFile(@"c:\targ\moved.txt");
                Assert.AreEqual(@"c:\targ\moved.txt", src.FullName);
                Assert.IsFalse(driver.GetFile(@"c:\src\file.txt").Exists);
                Assert.IsTrue(target.Exists);
                using (var t = target.OpenText())
                {
                    Assert.AreEqual("CONTENT", t.ReadToEnd());
                }
            }
        }
Exemplo n.º 5
0
        public void Timestamp_DeleteDirectory_GrandparentDirectoryIsntUpdated()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var beforeCreate = DateTime.Now;

                var f      = driver.CreateOrGetDirectory(@"c:\parent\subfolder\folder");
                var parent = driver.GetDirectory(@"c:\parent");

                var expected = beforeCreate.AddMinutes(-5);
                parent.LastAccessTime = parent.LastWriteTime = parent.CreationTime = expected;

                Assert.IsTrue(parent.LastAccessTime == expected);
                Assert.IsTrue(parent.LastWriteTime == expected);
                Assert.IsTrue(parent.CreationTime == expected);

                // act
                var folder = driver.GetDirectory(@"c:\parent\subfolder\folder");
                folder.Delete();

                // Assert
                Assert.IsTrue(parent.LastAccessTime == expected);
                Assert.IsTrue(parent.LastWriteTime == expected);
                Assert.IsTrue(parent.CreationTime == expected);
            }
        }
Exemplo n.º 6
0
        public void Timestamps_DeleteDirectory_UpdateTimestamps()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var subdir = driver.CreateOrGetDirectory(@"c:\folder\subdir");

                var folder = driver.GetDirectory(@"c:\folder");

                var beforeDelete = DateTime.Now;
                var old          = beforeDelete.AddMinutes(-5);
                folder.LastAccessTime = folder.LastWriteTime = folder.CreationTime = old;

                Assert.IsTrue(folder.LastAccessTime == old);
                Assert.IsTrue(folder.LastWriteTime == old);
                Assert.IsTrue(folder.CreationTime == old);

                // act
                subdir.Delete();

                folder.Refresh();

                // Assert
                Assert.IsTrue(folder.LastAccessTime >= beforeDelete);
                Assert.IsTrue(folder.LastWriteTime >= beforeDelete);
                Assert.IsTrue(folder.CreationTime == old);
            }
        }
Exemplo n.º 7
0
        public void Timestamps_CreateOrGetFile_CanUpdatestamps()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var beforeCreate = DateTime.Now;

                var f = driver.CreateOrGetDirectory(@"c:\folder");

                var old = beforeCreate.AddMinutes(-5);
                f.LastAccessTime = f.LastWriteTime = f.CreationTime = old;

                Assert.IsTrue(f.LastAccessTime == old);
                Assert.IsTrue(f.LastWriteTime == old);
                Assert.IsTrue(f.CreationTime == old);

                // act
                var file = driver.CreateOrGetFile(@"c:\folder\file.txt");

                f.Refresh();

                // Assert
                Assert.IsTrue(f.LastAccessTime >= beforeCreate);
                Assert.IsTrue(f.LastWriteTime >= beforeCreate);
                Assert.IsTrue(f.CreationTime == old);
            }
        }
Exemplo n.º 8
0
        public void GetDirectories_ExplicitSearchPath_IllegalWildchar_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_2");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_3");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_4");

                var root = driver.GetDirectory(@"c:\root");

                // act/assert
                Assert.ThrowsException <ArgumentException>(() => root.GetDirectories(@"folder_*\subfolder_*", SearchOption.AllDirectories));
            }
        }
Exemplo n.º 9
0
        public void OpenFile_CreateMode_FileExists_CanOverwriteFile()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\temp");
                var file = driver.GetFile(@"c:\temp\file.txt");
                using (var writer = new StreamWriter(file.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)))
                {
                    writer.Write("1");
                }

                // act
                using (var writer = new StreamWriter(file.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)))
                {
                    writer.Write("2");
                }

                // assert
                using (var reader = new StreamReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                {
                    Assert.AreEqual("2", reader.ReadToEnd());
                }
            }
        }
Exemplo n.º 10
0
        public void Move_CanUpdateStubs()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_1\file_1.txt");
                driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_1\file_2.txt");
                driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_2\file_1.txt");
                driver.CreateOrGetFile(@"c:\src\folder_1\subfolder_2\file_2.txt");
                driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_1\file_1.txt");
                driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_1\file_2.txt");
                driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_2\file_1.txt");
                driver.CreateOrGetFile(@"c:\src\folder_2\subfolder_2\file_2.txt");

                driver.CreateOrGetDirectory(@"c:\targ");

                var folder_1 = driver.GetDirectory(@"c:\src\folder_1");

                // act
                folder_1.MoveTo(@"c:\targ\folder_1");

                // assert
                var fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_1\file_1.txt");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_1\file_1.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_1\file_2.txt");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_1\file_2.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_2\file_1.txt");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_2\file_1.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\targ\folder_1\subfolder_2\file_2.txt");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_2\file_2.txt", fileStub.FullName);

                fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_1\file_1.txt");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_1\file_1.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_1\file_2.txt");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_1\file_2.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_2\file_1.txt");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_2\file_1.txt", fileStub.FullName);
                fileStub = driver.GetFile(@"c:\src\folder_2\subfolder_2\file_2.txt");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_2\file_2.txt", fileStub.FullName);

                var dirStub = driver.GetDirectory(@"c:\targ");
                Assert.AreEqual(@"c:\targ", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\targ\folder_1");
                Assert.AreEqual(@"c:\targ\folder_1", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\targ\folder_1\subfolder_1");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_1", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\targ\folder_1\subfolder_2");
                Assert.AreEqual(@"c:\targ\folder_1\subfolder_2", dirStub.FullName);

                dirStub = driver.GetDirectory(@"c:\src");
                Assert.AreEqual(@"c:\src", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\src\folder_2");
                Assert.AreEqual(@"c:\src\folder_2", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\src\folder_2\subfolder_1");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_1", dirStub.FullName);
                dirStub = driver.GetDirectory(@"c:\src\folder_2\subfolder_2");
                Assert.AreEqual(@"c:\src\folder_2\subfolder_2", dirStub.FullName);
            }
        }
Exemplo n.º 11
0
        public void CreateSubdirectory_PathIsAbsolute_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var folder = driver.CreateOrGetDirectory(@"c:\folder");

                // act/assert
                Assert.ThrowsException <ArgumentException>(() => folder.CreateSubdirectory(@"c:\folder\subfolder"));
            }
        }
Exemplo n.º 12
0
        public void Root_ReturnsRoot()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var f = driver.CreateOrGetDirectory(@"c:\folder\subfolder");

                // assert
                Assert.AreEqual(@"c:\", f.Root.FullName);
            }
        }
Exemplo n.º 13
0
        public void Parent_DirectoryIsRoot_ReturnsNull()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var c = driver.CreateOrGetDirectory(@"c:\");

                // assert
                Assert.IsNull(c.Parent);
            }
        }
Exemplo n.º 14
0
        public void Delete_ForDirectory_RemoveRoot_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var c = driver.CreateOrGetDirectory(@"c:\");

                // act/assert
                Assert.ThrowsException <InvalidOperationException>(() => c.Delete(true));
            }
        }
Exemplo n.º 15
0
        public void Move_PartOfTargetDoesntExists_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var dir = driver.CreateOrGetDirectory(@"c:\dir");

                // act/assert
                Assert.ThrowsException <DirectoryNotFoundException>(() => dir.MoveTo(@"c:\temp\dir"));
            }
        }
Exemplo n.º 16
0
        public void GetDirectories_TopDirOnly_NoWildchars_CanFind()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\folder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\folder_2");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\folder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\folder_2");

                var root = driver.GetDirectory(@"c:\root");

                // act
                var result = root.GetDirectories(@"folder_2", SearchOption.TopDirectoryOnly);

                // assert
                Assert.AreEqual(1, result.Length);
                Assert.AreEqual(@"c:\root\folder_2", result[0].FullName);
            }
        }
Exemplo n.º 17
0
        public void GetDirectories_Recurse_NoWilchars_CanFind()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_2");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_3");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_4");

                var root = driver.GetDirectory(@"c:\root");

                // act
                var result = root.GetDirectories(@"subfolder_4", SearchOption.AllDirectories);

                // assert
                Assert.AreEqual(1, result.Length);
                Assert.AreEqual(@"c:\root\folder_2\subfolder_4", result[0].FullName);
            }
        }
Exemplo n.º 18
0
        public void MoveTo_TargetIsDirectory_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var src = driver.CreateOrGetFile(@"c:\src\file.txt");
                driver.CreateOrGetDirectory(@"c:\targ");

                // act/assert
                Assert.ThrowsException <IOException>(() => src.MoveTo(@"c:\targ"));
            }
        }
Exemplo n.º 19
0
        public void MoveTo_SourceFileDoesntExist_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\src");
                var src = driver.GetFile(@"c:\src\file.txt");

                // act/assert
                Assert.ThrowsException <FileNotFoundException>(() => src.MoveTo(@"c:\targ"));
            }
        }
Exemplo n.º 20
0
        public void GetDirectories_ExplicitSearchPath_CanFind()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1");
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_2");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_3");
                driver.CreateOrGetDirectory(@"c:\root\folder_2\subfolder_4");

                var root = driver.GetDirectory(@"c:\root");

                // act
                var result = root.GetDirectories(@"folder_1\subfolder_*", SearchOption.AllDirectories);

                // assert
                Assert.AreEqual(2, result.Length);
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_1").Count());
                Assert.AreEqual(1, result.Where(i => i.FullName == @"c:\root\folder_1\subfolder_2").Count());
            }
        }
Exemplo n.º 21
0
        public void Delete_ForDirectory_Flatten_DirectoryNotEmpty_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var folder_1 = driver.CreateOrGetDirectory(@"c:\folder_1");
                driver.CreateOrGetFile(@"c:\folder_1\file.txt");

                // act
                Assert.ThrowsException <IOException>(() => folder_1.Delete(false));
            }
        }
Exemplo n.º 22
0
        public void Delete_ForDirectory_Flatten_CanRemoveEmptyDir()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var folder_1 = driver.CreateOrGetDirectory(@"c:\folder_1");
                driver.CreateOrGetDirectory(@"c:\folder_2");

                // act
                folder_1.Delete(false);

                // assert
                folder_1.Refresh();
                Assert.IsFalse(folder_1.Exists);

                var root    = driver.GetDirectory(@"c:\");
                var folders = root.GetDirectories();
                Assert.AreEqual(1, folders.Length);
                Assert.AreEqual(@"c:\folder_2", folders[0].FullName);
            }
        }
Exemplo n.º 23
0
        public void GetLength_FileDoesntExist_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\temp");
                var file = driver.GetFile(@"c:\temp\file.txt");

                // act/assert
                Assert.ThrowsException <FileNotFoundException>(() => file.Length);
            }
        }
Exemplo n.º 24
0
        public void SetAttributes_FileDoesntExist_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\temp");
                var file = driver.GetFile(@"c:\temp\file.txt");

                // act/assert
                Assert.ThrowsException <FileNotFoundException>(() => file.Attributes = FileAttributes.Hidden);
            }
        }
Exemplo n.º 25
0
        public void OpenFile_DirectoryWithTheSameNameAlreadyExists_Throws(FileMode mode)
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\file");
                var f = driver.GetFile(@"c:\file");

                // act/assert
                Assert.ThrowsException <UnauthorizedAccessException>(() => f.Open(mode));
            }
        }
Exemplo n.º 26
0
        public void GetDirectories_ExplicitSearchPath_PartOfPathNotExists_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\root\folder_1\subfolder_1");

                var root = driver.GetDirectory(@"c:\root");

                // act/assert
                Assert.ThrowsException <DirectoryNotFoundException>(() => root.GetDirectories(@"folder_2\subfolder_*", SearchOption.AllDirectories));
            }
        }
Exemplo n.º 27
0
        public void TimeStamps_CreateOrGetDirectory_ReturnsCorrectTimestamps()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var beforeCreate = DateTime.Now;
                var folder       = driver.CreateOrGetDirectory(@"c:\folder");

                var old = beforeCreate.AddMinutes(-5);
                folder.CreationTime = folder.LastAccessTime = folder.LastWriteTime = old;

                Assert.IsTrue(folder.LastAccessTime == old);
                Assert.IsTrue(folder.LastWriteTime == old);
                Assert.IsTrue(folder.CreationTime == old);

                var subfolder = driver.CreateOrGetDirectory(@"c:\folder\subfolder");

                // Assert
                Assert.IsTrue(folder.LastAccessTime >= beforeCreate);
                Assert.IsTrue(folder.LastWriteTime >= beforeCreate);
                Assert.IsTrue(folder.CreationTime >= beforeCreate);
            }
        }
Exemplo n.º 28
0
        public void GetDirectories_EmptyDirectory_ReturnsEmpy()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var root = driver.CreateOrGetDirectory(@"c:\root");

                // act
                var result = root.GetDirectories(@"*", SearchOption.AllDirectories);

                // assert
                Assert.IsFalse(result.Any());
            }
        }
Exemplo n.º 29
0
        public void CreateSubdirectory_PathContainsDots_CanCraeate()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                var folder = driver.CreateOrGetDirectory(@"c:\folder");

                // act
                var subfolder = folder.CreateSubdirectory(@".\..\folder\.\subfolder");

                // assert
                Assert.IsTrue(subfolder.Exists);
                Assert.AreEqual(@"c:\folder\subfolder", subfolder.FullName);
            }
        }
Exemplo n.º 30
0
        public void OpenFile_TruncateMode_FileDeosntExist_Throws()
        {
            // arrange
            using (var driver = new DiskDriver())
            {
                driver.CreateOrGetDirectory(@"c:\temp");
                var file = driver.GetFile(@"c:\temp\file.txt");

                // act
                Assert.ThrowsException <FileNotFoundException>(() =>
                {
                    file.Open(FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
                });
            }
        }