Exemplo n.º 1
0
        private void When_using_parent_references_it_must_succeed()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(@"C:\docs\..\games");

            // Assert
            info.FullName.Should().Be(@"C:\games");
        }
Exemplo n.º 2
0
        private void When_directory_has_invalid_name_it_must_fail()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            Action action = () => fileSystem.ConstructFileInfo(@"c:\games\try?me");

            // Assert
            action.ShouldThrow <ArgumentException>().WithMessage(@"Illegal characters in path.*");
        }
Exemplo n.º 3
0
        private void When_network_share_has_invalid_name_it_must_fail()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            Action action = () => fileSystem.ConstructFileInfo(@"\\team*server");

            // Assert
            action.ShouldThrow <ArgumentException>().WithMessage(@"The UNC path should be of the form \\server\share.*");
        }
Exemplo n.º 4
0
        private void When_constructing_file_info_for_existing_relative_local_file_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            DateTime creationTimeUtc = 17.March(2006).At(14, 03, 53).AsUtc();
            var      clock           = new SystemClock {
                UtcNow = () => creationTimeUtc
            };

            IFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                     .IncludingEmptyFile(path)
                                     .Build();

            DateTime lastWriteTimeUtc = 18.March(2006).At(14, 03, 53).AsUtc();

            clock.UtcNow = () => lastWriteTimeUtc;

            fileSystem.File.WriteAllText(path, DefaultContents);

            DateTime lastAccessTimeUtc = 19.March(2006).At(14, 03, 53).AsUtc();

            clock.UtcNow = () => lastAccessTimeUtc;

            fileSystem.File.ReadAllText(path);

            fileSystem.Directory.SetCurrentDirectory(@"C:\some");

            // Act
            IFileInfo fileInfo = fileSystem.ConstructFileInfo(@"file.txt");

            // Assert
            fileInfo.Name.Should().Be("file.txt");
            fileInfo.Extension.Should().Be(".txt");
            fileInfo.FullName.Should().Be(@"C:\some\file.txt");
            fileInfo.DirectoryName.Should().Be(@"C:\some");
            fileInfo.Exists.Should().BeTrue();
            fileInfo.Length.Should().Be(DefaultContents.Length);
            fileInfo.IsReadOnly.Should().BeFalse();
            fileInfo.Attributes.Should().Be(FileAttributes.Archive);

            fileInfo.CreationTime.Should().Be(creationTimeUtc.ToLocalTime());
            fileInfo.CreationTimeUtc.Should().Be(creationTimeUtc);
            fileInfo.LastAccessTime.Should().Be(lastAccessTimeUtc.ToLocalTime());
            fileInfo.LastAccessTimeUtc.Should().Be(lastAccessTimeUtc);
            fileInfo.LastWriteTime.Should().Be(lastWriteTimeUtc.ToLocalTime());
            fileInfo.LastWriteTimeUtc.Should().Be(lastWriteTimeUtc);

            IDirectoryInfo directoryInfo = fileInfo.Directory.ShouldNotBeNull();

            directoryInfo.FullName.Should().Be(@"C:\some");
        }
Exemplo n.º 5
0
        private void When_constructing_file_info_for_null_it_must_fail()
        {
            // Arrange
            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

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

            // Assert
            action.ShouldThrow <ArgumentNullException>();
        }
        private void When_creating_drive_letter_it_must_succeed()
        {
            // Arrange
            const string path = @"E:\";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Assert
            info.FullName.Should().Be(path);
        }
        private void When_creating_extended_path_starting_with_network_share_it_must_succeed()
        {
            // Arrange
            const string path = @"\\?\UNC\teamserver\management\reports";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Assert
            info.FullName.Should().Be(path);
        }
        private void When_using_trailing_thin_space_it_must_preserve()
        {
            // Arrange
            const char thinSpace = (char)0x2009;

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(@"c:\some\a" + thinSpace);

            // Assert
            info.FullName.Should().Be(@"c:\some\a" + thinSpace);
        }
        private void When_creating_extended_path_starting_with_drive_letter_it_must_succeed()
        {
            // Arrange
            const string path = @"\\?\E:\Documents";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Assert
            info.FullName.Should().Be(path);
        }
        private void When_creating_network_share_with_IPv4_address_it_must_succeed()
        {
            // Arrange
            const string path = @"\\192.168.0.1\management";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Assert
            info.FullName.Should().Be(path);
        }
Exemplo n.º 11
0
        private void When_deleting_file_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingEmptyFile(path)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Act
            fileInfo.Delete();

            // Assert
            fileSystem.File.Exists(path).Should().BeFalse();
        }
Exemplo n.º 12
0
        private void When_encrypting_file_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingEmptyFile(path)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Act
            fileInfo.Encrypt();

            // Assert
            fileInfo.Attributes.Should().HaveFlag(FileAttributes.Encrypted);
            fileSystem.File.GetAttributes(path).Should().HaveFlag(FileAttributes.Encrypted);
        }
        private void When_setting_file_to_non_readonly_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingEmptyFile(path, FileAttributes.Hidden | FileAttributes.ReadOnly)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Act
            fileInfo.IsReadOnly = false;

            // Assert
            fileInfo.IsReadOnly.Should().Be(false);
            fileInfo.Attributes.Should().Be(FileAttributes.Hidden);
        }
        private void When_getting_file_attributes_it_must_lazy_load()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingEmptyFile(path, FileAttributes.Hidden)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            fileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

            // Act
            FileAttributes attributes = fileInfo.Attributes;

            // Assert
            attributes.Should().Be(FileAttributes.ReadOnly);
        }
        private void When_getting_file_readonly_state_it_must_lazy_load()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingEmptyFile(path)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            fileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

            // Act
            bool isReadOnly = fileInfo.IsReadOnly;

            // Assert
            isReadOnly.Should().BeTrue();
        }
Exemplo n.º 16
0
        private void When_getting_file_length_it_must_lazy_load()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(path, DefaultContents)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            fileSystem.File.WriteAllText(path, AlternateContents);

            // Act
            long length = fileInfo.Length;

            // Assert
            length.Should().Be(AlternateContents.Length);
        }
Exemplo n.º 17
0
        private void When_creating_file_as_text_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(@"c:\some")
                                     .Build();

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Act
            using (StreamWriter writer = info.CreateText())
            {
                // Assert
                writer.BaseStream.CanRead.Should().BeFalse();
                writer.BaseStream.CanWrite.Should().BeTrue();
            }
        }
Exemplo n.º 18
0
        private void When_appending_to_file_as_text_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(path, DefaultContents)
                                     .Build();

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Act
            using (StreamWriter writer = info.AppendText())
            {
                // Assert
                writer.BaseStream.CanRead.Should().BeFalse();
                writer.BaseStream.CanWrite.Should().BeTrue();
            }
        }
        private void When_creating_network_share_with_escaped_IPv6_address_it_must_succeed()
        {
            // https://msdn.microsoft.com/en-us/library/aa385353.aspx
            // The IPv6 literal format must be used so that the IPv6 address is parsed correctly. An IPv6 literal address is of the form:
            // ipv6-address with the ':' characters replaced by '-' characters, followed by the ".ipv6-literal.net" string.

            // Arrange
            string address = "fe80::ddad:d5e:41a2:43e7%5".Replace(":", "-") + ".ipv6-literal.net";
            string path    = @"\\" + address + @"\management";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Assert
            info.FullName.Should().Be(path);
        }
Exemplo n.º 20
0
        private void When_opening_file_for_writing_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(path, DefaultContents)
                                     .Build();

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            // Act
            using (IFileStream stream = info.OpenWrite())
            {
                // Assert
                stream.CanRead.Should().BeFalse();
                stream.CanWrite.Should().BeTrue();
            }
        }
Exemplo n.º 21
0
        private void When_getting_file_existence_it_must_lazy_load()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(@"c:\some")
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            fileSystem.File.WriteAllText(path, string.Empty);

            // Act
            bool found = fileInfo.Exists;

            // Assert
            found.Should().BeTrue();
        }
Exemplo n.º 22
0
        private void When_creating_file_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(@"c:\some")
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Act
            using (fileInfo.Create())
            {
            }

            // Assert
            fileSystem.File.Exists(path).Should().BeTrue();
            fileSystem.File.ReadAllText(path).Should().Be(string.Empty);
        }
        private void When_constructing_file_info_for_existing_local_directory_it_must_succeed()
        {
            // Arrange
            const string path = @"c:\some\folder";

            DateTime creationTimeUtc = 17.March(2006).At(14, 03, 53).AsUtc();
            var      clock           = new SystemClock(() => creationTimeUtc);

            IFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                     .IncludingDirectory(path)
                                     .Build();

            // Act
            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Assert
            fileInfo.Name.Should().Be("folder");
            fileInfo.Extension.Should().Be(string.Empty);
            fileInfo.FullName.Should().Be(path);
            fileInfo.DirectoryName.Should().Be(@"c:\some");
            fileInfo.Exists.Should().BeFalse();
            ActionFactory.IgnoreReturnValue(() => fileInfo.Length).Should().ThrowExactly <FileNotFoundException>()
            .WithMessage(@"Could not find file 'c:\some\folder'.");
            fileInfo.IsReadOnly.Should().BeFalse();
            fileInfo.Attributes.Should().Be(FileAttributes.Directory);

            fileInfo.CreationTime.Should().Be(creationTimeUtc.ToLocalTime());
            fileInfo.CreationTimeUtc.Should().Be(creationTimeUtc);
            fileInfo.LastAccessTime.Should().Be(creationTimeUtc.ToLocalTime());
            fileInfo.LastAccessTimeUtc.Should().Be(creationTimeUtc);
            fileInfo.LastWriteTime.Should().Be(creationTimeUtc.ToLocalTime());
            fileInfo.LastWriteTimeUtc.Should().Be(creationTimeUtc);

            fileInfo.ToString().Should().Be(path);

            IDirectoryInfo directoryInfo = fileInfo.Directory.ShouldNotBeNull();

            directoryInfo.Name.Should().Be("some");
            directoryInfo.FullName.Should().Be(@"c:\some");
            directoryInfo.ToString().Should().Be(@"c:\some");
        }
Exemplo n.º 24
0
        private void When_constructing_file_info_for_missing_network_share_it_must_fail()
        {
            // Arrange
            const string path = @"\\server\share\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .Build();

            // Act
            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            // Assert
            fileInfo.Name.Should().Be("file.txt");
            fileInfo.Extension.Should().Be(".txt");
            fileInfo.FullName.Should().Be(@"\\server\share\file.txt");
            fileInfo.DirectoryName.Should().Be(@"\\server\share");
            fileInfo.Exists.Should().BeFalse();
            ActionFactory.IgnoreReturnValue(() => fileInfo.Length)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.IsReadOnly)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.Attributes)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");

            ActionFactory.IgnoreReturnValue(() => fileInfo.CreationTime)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.CreationTimeUtc)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.LastAccessTime)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.LastAccessTimeUtc)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.LastWriteTime)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");
            ActionFactory.IgnoreReturnValue(() => fileInfo.LastWriteTimeUtc)
            .ShouldThrow <IOException>().WithMessage("The network path was not found");

            IDirectoryInfo directoryInfo = fileInfo.Directory.ShouldNotBeNull();

            directoryInfo.FullName.Should().Be(@"\\server\share");
        }
Exemplo n.º 25
0
        private void When_opening_missing_local_file_in_Append_mode_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(@"c:\some")
                                     .Build();

            // Act
            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Append))
            {
                // Assert
                stream.Length.Should().Be(0);
            }

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            info.Exists.Should().BeTrue();
            info.Length.Should().Be(0);
        }
Exemplo n.º 26
0
        private void When_creating_existing_local_file_it_must_overwrite()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(path, "existing data")
                                     .Build();

            // Act
            using (IFileStream stream = fileSystem.File.Create(path))
            {
                // Assert
                stream.Length.Should().Be(0);
            }

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            info.Exists.Should().BeTrue();
            info.Attributes.Should().Be(FileAttributes.Archive);
        }
Exemplo n.º 27
0
        private void When_creating_local_file_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingDirectory(@"C:\some")
                                     .Build();

            // Act
            using (IFileStream stream = fileSystem.File.Create(path))
            {
                stream.WriteByte(0xFF);
            }

            // Assert
            IFileInfo info = fileSystem.ConstructFileInfo(path);

            info.Exists.Should().BeTrue();
            info.Attributes.Should().Be(FileAttributes.Archive);
        }
Exemplo n.º 28
0
        private void When_replacing_file_without_backup_it_must_succeed()
        {
            // Arrange
            const string sourcePath = @"C:\some\source.txt";
            const string targetPath = @"C:\some\target.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(sourcePath, "SourceText")
                                     .IncludingTextFile(targetPath, "TargetText")
                                     .Build();

            IFileInfo sourceInfo = fileSystem.ConstructFileInfo(sourcePath);

            // Act
            IFileInfo targetInfo = sourceInfo.Replace(targetPath, null);

            // Assert
            sourceInfo.Exists.Should().BeFalse();
            targetInfo.Exists.Should().BeTrue();
            targetInfo.FullName.Should().Be(targetPath);
        }
Exemplo n.º 29
0
        private void When_getting_file_last_access_time_in_local_zone_it_must_lazy_load()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            var clock = new SystemClock(() => DefaultTimeUtc);

            IFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                     .IncludingEmptyFile(path)
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            fileSystem.File.SetLastAccessTime(path, AlternateTime);

            // Act
            DateTime time = fileInfo.LastAccessTime;

            // Assert
            time.Should().Be(AlternateTime);
        }
        private void When_moving_file_it_must_succeed()
        {
            // Arrange
            const string sourcePath      = @"c:\some\file.txt";
            const string destinationPath = @"c:\other\renamed.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(sourcePath, DefaultContents, attributes: FileAttributes.ReadOnly)
                                     .IncludingDirectory(@"c:\other")
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(sourcePath);

            // Act
            fileInfo.MoveTo(destinationPath);

            // Assert
            fileSystem.File.Exists(sourcePath).Should().BeFalse();
            fileSystem.File.ReadAllText(destinationPath).Should().Be(DefaultContents);
            fileSystem.File.GetAttributes(destinationPath).Should().Be(FileAttributes.ReadOnly);
        }