Example #1
0
        private void When_reading_from_existing_file_it_must_update_file_timings()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

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

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

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

            clock.UtcNow = () => changeTimeUtc;

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

            // Assert
            fileSystem.File.GetCreationTimeUtc(path).Should().Be(creationTimeUtc);
            fileSystem.File.GetLastWriteTimeUtc(path).Should().Be(creationTimeUtc);
            fileSystem.File.GetLastAccessTimeUtc(path).Should().Be(changeTimeUtc);
        }
        private void When_reading_from_existing_file_it_must_update_properties_only_after_closing()
        {
            // Arrange
            const string path = @"C:\file.txt";

            DateTime createTimeUtc = 4.January(2017).At(22, 14);
            var      clock         = new SystemClock {
                UtcNow = () => createTimeUtc
            };

            IFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                     .IncludingTextFile(path, "X")
                                     .Build();

            DateTime accessTimeUtc = 5.January(2017).At(23, 11);

            clock.UtcNow = () => accessTimeUtc;

            // Act
            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.Read))
            {
                stream.ReadByte();

                // Assert
                fileSystem.File.GetCreationTimeUtc(path).Should().Be(createTimeUtc);
                fileSystem.File.GetLastWriteTimeUtc(path).Should().Be(createTimeUtc);
                fileSystem.File.GetLastAccessTimeUtc(path).Should().Be(createTimeUtc);
            }

            fileSystem.File.GetCreationTimeUtc(path).Should().Be(createTimeUtc);
            fileSystem.File.GetLastWriteTimeUtc(path).Should().Be(createTimeUtc);
            fileSystem.File.GetLastAccessTimeUtc(path).Should().Be(accessTimeUtc);
        }
Example #3
0
        private void When_reading_from_writeonly_stream_it_must_fail()
        {
            // Arrange
            const string path = @"C:\file.txt";

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

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.Write))
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.ReadByte();

                // Assert
                action.ShouldThrow <NotSupportedException>().WithMessage("Stream does not support reading.");
            }
        }
Example #4
0
        private void When_opening_existing_local_file_in_OpenOrCreate_mode_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

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

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

                // Assert
                stream.Length.Should().Be(DefaultContents.Length);
            }

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            info.Exists.Should().BeTrue();
        }
        private void When_opening_existing_local_file_in_OpenOrCreate_mode_it_must_succeed()
        {
            // Arrange
            const string path = @"C:\some\file.txt";

            DateTime creationTimeUtc = 9.September(2016);

            var clock = new SystemClock
            {
                UtcNow = () => creationTimeUtc
            };

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

            DateTime lastAccessTimeUtc = 12.September(2016);

            clock.UtcNow = () => lastAccessTimeUtc;

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

                // Assert
                stream.Length.Should().Be(DefaultContents.Length);
            }

            IFileInfo info = fileSystem.ConstructFileInfo(path);

            info.Exists.Should().BeTrue();

            info.CreationTimeUtc.Should().Be(creationTimeUtc);
            info.LastAccessTimeUtc.Should().Be(lastAccessTimeUtc);
            info.LastWriteTimeUtc.Should().Be(creationTimeUtc);
        }