private void When_moving_file_to_other_drive_it_must_succeed()
        {
            // Arrange
            const string sourcePath = @"C:\source.txt";
            const string targetPath = @"D:\target.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(sourcePath, BufferFactory.Create(768))
                                     .IncludingVolume("D:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(16384)
                                                      .WithFreeSpace(6144))
                                     .Build();

            // Act
            fileSystem.File.Move(sourcePath, targetPath);

            // Assert
            IDriveInfo driveInfoC = fileSystem.ConstructDriveInfo("C:");

            driveInfoC.AvailableFreeSpace.Should().Be(4096);

            IDriveInfo driveInfoD = fileSystem.ConstructDriveInfo("D:");

            driveInfoD.AvailableFreeSpace.Should().Be(5376);
        }
Exemplo n.º 2
0
        private void When_constructing_drive_info_for_existing_drive_it_must_succeed([NotNull] string driveName)
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo(driveName);

            // Assert
            driveInfo.Name.Should().Be(@"c:\");
            driveInfo.IsReady.Should().BeTrue();
            driveInfo.AvailableFreeSpace.Should().Be(OneGigabyte);
            driveInfo.TotalFreeSpace.Should().Be(OneGigabyte);
            driveInfo.TotalSize.Should().Be(OneGigabyte);
            driveInfo.DriveType.Should().Be(DriveType.Fixed);
            driveInfo.DriveFormat.Should().Be("NTFS");
            driveInfo.VolumeLabel.Should().BeEmpty();
            driveInfo.ToString().Should().Be(@"c:\");

            IDirectoryInfo directoryInfo = driveInfo.RootDirectory.ShouldNotBeNull();

            directoryInfo.FullName.Should().Be(@"c:\");
            directoryInfo.Exists.Should().BeTrue();
        }
Exemplo n.º 3
0
        private void When_setting_volume_label_it_must_store_value()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("x:", new FakeVolumeInfoBuilder()
                                                      .Labeled("StartName"))
                                     .Build();

            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("x:");

            // Act
            driveInfo.VolumeLabel = "NextName";

            // Assert
            fileSystem.ConstructDriveInfo("x:").VolumeLabel.Should().Be("NextName");
        }
Exemplo n.º 4
0
        private void When_constructing_drive_info_for_missing_drive_it_must_succeed([NotNull] string driveName)
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo(driveName);

            // Assert
            driveInfo.Name.Should().Be(@"X:\");
            driveInfo.IsReady.Should().BeFalse();
            ActionFactory.IgnoreReturnValue(() => driveInfo.AvailableFreeSpace).Should().ThrowExactly <DriveNotFoundException>()
            .WithMessage(@"Could not find the drive 'X:\'. The drive might not be ready or might not be mapped.");
            ActionFactory.IgnoreReturnValue(() => driveInfo.TotalFreeSpace).Should().ThrowExactly <DriveNotFoundException>()
            .WithMessage(@"Could not find the drive 'X:\'. The drive might not be ready or might not be mapped.");
            ActionFactory.IgnoreReturnValue(() => driveInfo.TotalSize).Should().ThrowExactly <DriveNotFoundException>()
            .WithMessage(@"Could not find the drive 'X:\'. The drive might not be ready or might not be mapped.");
            driveInfo.DriveType.Should().Be(DriveType.NoRootDirectory);
            ActionFactory.IgnoreReturnValue(() => driveInfo.DriveFormat).Should().ThrowExactly <DriveNotFoundException>()
            .WithMessage(@"Could not find the drive 'X:\'. The drive might not be ready or might not be mapped.");
            ActionFactory.IgnoreReturnValue(() => driveInfo.VolumeLabel).Should().ThrowExactly <DriveNotFoundException>()
            .WithMessage(@"Could not find the drive 'X:\'. The drive might not be ready or might not be mapped.");
            driveInfo.ToString().Should().Be(@"X:\");

            IDirectoryInfo directoryInfo = driveInfo.RootDirectory.ShouldNotBeNull();

            directoryInfo.FullName.Should().Be(@"X:\");
            directoryInfo.Exists.Should().BeFalse();
        }
Exemplo n.º 5
0
        private void When_constructing_drive_info_it_must_return_custom_values()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("P:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8 * OneGigabyte)
                                                      .WithFreeSpace(3 * OneGigabyte)
                                                      .OfType(DriveType.Network)
                                                      .InFormat("FAT32")
                                                      .Labeled("CompanyShare"))
                                     .Build();

            // Act
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("p");

            // Assert
            driveInfo.Name.Should().Be(@"p:\");
            driveInfo.IsReady.Should().BeTrue();
            driveInfo.AvailableFreeSpace.Should().Be(3 * OneGigabyte);
            driveInfo.TotalFreeSpace.Should().Be(3 * OneGigabyte);
            driveInfo.TotalSize.Should().Be(8 * OneGigabyte);
            driveInfo.DriveType.Should().Be(DriveType.Network);
            driveInfo.DriveFormat.Should().Be("FAT32");
            driveInfo.VolumeLabel.Should().Be("CompanyShare");
            driveInfo.ToString().Should().Be(@"p:\");

            IDirectoryInfo directoryInfo = driveInfo.RootDirectory.ShouldNotBeNull();

            directoryInfo.FullName.Should().Be(@"p:\");
            directoryInfo.Exists.Should().BeTrue();
        }
Exemplo n.º 6
0
        private void When_setting_volume_label_to_null_it_must_store_empty_string()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("x:", new FakeVolumeInfoBuilder()
                                                      .Labeled("StartName"))
                                     .Build();

            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("x:");

            // Act
            // ReSharper disable once AssignNullToNotNullAttribute
            driveInfo.VolumeLabel = null;

            // Assert
            fileSystem.ConstructDriveInfo("x:").VolumeLabel.Should().BeEmpty();
        }
Exemplo n.º 7
0
        private void When_constructing_drive_info_for_empty_string_it_must_fail()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            Action action = () => fileSystem.ConstructDriveInfo(string.Empty);

            // Assert
            action.Should().ThrowExactly <ArgumentException>().WithMessage("The path is not of a legal form.*");
        }
Exemplo n.º 8
0
        private void When_constructing_drive_info_for_null_it_must_fail()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            // ReSharper disable once AssignNullToNotNullAttribute
            Action action = () => fileSystem.ConstructDriveInfo(null);

            // Assert
            action.Should().ThrowExactly <ArgumentNullException>();
        }
Exemplo n.º 9
0
        private void When_constructing_drive_info_for_invalid_name_it_must_fail([NotNull] string driveName)
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            Action action = () => fileSystem.ConstructDriveInfo(driveName);

            // Assert
            action.Should().ThrowExactly <ArgumentException>().WithMessage(
                @"Drive name must be a root directory ('C:\\') or a drive letter ('C').*");
        }
        private void When_writing_to_new_file_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .Build();

            // Act
            fileSystem.File.WriteAllBytes(@"C:\file.txt", BufferFactory.Create(1024));

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(3072);
        }
        private void When_creating_temporary_file_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .WithTempDirectory(@"c:\")
                                     .Build();

            // Act
            fileSystem.Path.GetTempFileName();

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(4096);
        }
        private void When_deleting_directory_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingDirectory(@"C:\some\folder\subtree")
                                     .Build();

            // Act
            fileSystem.Directory.Delete(@"C:\some", true);

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(4096);
        }
        private void When_decreasing_file_size_using_overwrite_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(path, BufferFactory.Create(1024))
                                     .Build();

            // Act
            fileSystem.File.WriteAllBytes(path, BufferFactory.Create(256));

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(3840);
        }
        private void When_encrypting_file_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(path, BufferFactory.Create(1024))
                                     .Build();

            // Act
            fileSystem.File.Encrypt(path);

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(3072);
        }
        private void When_renaming_directory_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(@"c:\source\src.txt", BufferFactory.Create(64))
                                     .IncludingBinaryFile(@"c:\source\nested\src.txt", BufferFactory.Create(256))
                                     .IncludingDirectory(@"c:\source\subfolder")
                                     .Build();

            // Act
            fileSystem.Directory.Move(@"C:\source", @"C:\target");

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(3776);
        }
        private void When_copying_file_to_same_drive_it_must_succeed()
        {
            // Arrange
            const string sourcePath = @"C:\source.txt";
            const string targetPath = @"C:\target.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(sourcePath, BufferFactory.Create(768))
                                     .Build();

            // Act
            fileSystem.File.Copy(sourcePath, targetPath);

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(2560);
        }
        private void When_writing_to_new_file_with_DeleteOnClose_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .Build();

            // Act
            using (IFileStream stream = fileSystem.File.Create(@"C:\file.txt", options: FileOptions.DeleteOnClose))
            {
                byte[] buffer = BufferFactory.Create(1024);
                stream.Write(buffer, 0, buffer.Length);
            }

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(4096);
        }
        private void When_moving_position_past_end_of_file_it_must_not_allocate_disk_space()
        {
            // Arrange
            const string path = @"C:\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(512))
                                     .IncludingBinaryFile(path, BufferFactory.Create(32))
                                     .Build();

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open))
            {
                // Act
                stream.Position = 1024;

                // Assert
                IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");
                driveInfo.AvailableFreeSpace.Should().Be(480);
            }
        }
        private void When_replacing_file_without_backup_it_must_succeed()
        {
            // Arrange
            const string sourcePath = @"C:\source.txt";
            const string targetPath = @"C:\target.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(2112))
                                     .IncludingBinaryFile(sourcePath, BufferFactory.Create(768))
                                     .IncludingBinaryFile(targetPath, BufferFactory.Create(1280))
                                     .Build();

            // Act
            fileSystem.File.Replace(sourcePath, targetPath, null);

            // Assert
            IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");

            driveInfo.AvailableFreeSpace.Should().Be(1344);
        }
        private void When_increasing_file_size_using_SetLength_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingVolume("C:", new FakeVolumeInfoBuilder()
                                                      .OfCapacity(8192)
                                                      .WithFreeSpace(4096))
                                     .IncludingBinaryFile(path, BufferFactory.Create(1024))
                                     .Build();

            using (IFileStream stream = fileSystem.File.OpenWrite(path))
            {
                // Act
                stream.SetLength(1280);

                // Assert
                IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");
                driveInfo.AvailableFreeSpace.Should().Be(2816);
            }
        }