Beispiel #1
0
        private void When_seeking_from_current_to_past_end_it_must_add_extra_zero_bytes()
        {
            // Arrange
            const string path = @"C:\file.txt";
            const string data = "ABC";

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

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite))
            {
                stream.Seek(1, SeekOrigin.Begin);

                // Act
                stream.Seek(2 + 10, SeekOrigin.Current);

                // Assert
                stream.Length.Should().Be(data.Length + 10);
            }

            string contents = fileSystem.File.ReadAllText(path);

            contents.Substring(data.Length).Should().Be(new string('\0', 10));
        }
        private void When_increasing_file_size_using_Seek_followed_by_write_it_must_fail()
        {
            // 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.OpenWrite(path))
            {
                byte[] buffer = BufferFactory.Create(64);
                stream.Write(buffer, 0, buffer.Length);

                stream.Seek(1280, SeekOrigin.Begin);

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.WriteByte(0x33);

                // Assert
                action.Should().ThrowExactly <IOException>().WithMessage("There is not enough space on the disk.");

                AssertFreeSpaceOnDrive(fileSystem, "C:", 448);
            }

            AssertFileSize(fileSystem, path, 64);
        }
Beispiel #3
0
        public (string line, ReadLine sizeExceeded) ReadLineFromCurrentPositionToEnd(long maxStringSize)
        {
            var result = InternalReadLineFromCurrentPositionToEnd(maxStringSize);

            if (String.IsNullOrEmpty(result.line))
            {
                _stream?.Seek(_currentPosition, SeekOrigin.Begin);
            }
            return(result);
        }
Beispiel #4
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        bool SetPositionRelative(IFileStream stream, long offset)
        {
            var current = stream.Position;

            var newPos = stream.Seek(offset, SeekOrigin.Current);


            Debug.Assert((newPos - current) == offset);
            return((newPos - current) == offset); // We assume that we won't position more than Int32.Max
        }
Beispiel #5
0
        private void When_seeking_from_current_to_before_start_it_must_fail()
        {
            // Arrange
            const string path = @"C:\file.txt";

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

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.Read))
            {
                stream.Seek(2, SeekOrigin.Begin);

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.Seek(-3, SeekOrigin.Current);

                // Assert
                action.ShouldThrow <ArgumentOutOfRangeException>();
            }
        }
Beispiel #6
0
        // Will be used for unit testing only - since Moq cannot handle Span<byte>
        public string FOR_UNIT_TEST_SLOW_FUNCTION_ReadLineCharByCharTillCRLF()
        {
            if (_stream == null)
            {
                return(String.Empty);
            }

            string result = String.Empty;

            byte[] buf   = new byte[1];
            int    index = 0;
            var    pos   = _stream.Position;

            for (; ;)
            {
                if (_stream.Read(buf) != 1)
                {
                    return(String.Empty);
                }
                var c = buf[0];

                ++index;
                if (c == '\n')
                {
                    break;
                }
            }

            buf = new byte[index];
            _stream.Seek(pos, SeekOrigin.Begin);
            if (_stream.Read(buf) != index)
            {
                return(String.Empty);
            }
            string complete = System.Text.Encoding.Default.GetString(buf);

            return(complete);
        }
Beispiel #7
0
        private bool InitializeNewStream()
        {
            _stream = new FileStreamWrapper(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
            _stream.Seek(0, SeekOrigin.End);
            _currentPosition = _stream.Position;

            // Seek back to the last line - if we don't do that we will miss the first written line

            var foundLine = _stream.SeekLastLineFromCurrentAndPositionOnStartOfIt();

            if (!foundLine)
            {
                return(false); // There is no line feed - that is by definition wrong - so let's what has been written
            }
            _currentPosition = _stream.Position;
            return(true);
        }
Beispiel #8
0
        private void When_seeking_from_end_to_past_end_of_readonly_stream_it_must_fail()
        {
            // Arrange
            const string path = @"C:\file.txt";

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

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.Read))
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.Seek(10, SeekOrigin.End);

                // Assert
                action.ShouldThrow <NotSupportedException>();
            }
        }
Beispiel #9
0
        private void When_seeking_in_closed_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.Read))
            {
                stream.Dispose();

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.Seek(0, SeekOrigin.Begin);

                // Assert
                action.ShouldThrow <ObjectDisposedException>().WithMessage("Cannot access a closed file.");
            }
        }
Beispiel #10
0
        private void When_seeking_to_before_end_in_stream_opened_in_Append_mode_it_must_fail()
        {
            // Arrange
            const string path = @"C:\file.txt";

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

            using (IFileStream stream = fileSystem.File.Open(path, FileMode.Append))
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => stream.Seek(-1, SeekOrigin.Current);

                // Assert
                action.ShouldThrow <IOException>()
                .WithMessage(
                    "Unable seek backward to overwrite data that previously existed in a file opened in Append mode.");
            }
        }
        private void When_async_appending_to_stream_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string containerDirectoryName = "Container";
            const string fileNameToWrite        = "file.txt";

            string pathToFileToWrite = Path.Combine(directoryToWatch, containerDirectoryName, fileNameToWrite);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingBinaryFile(pathToFileToWrite, BufferFactory.Create(1024))
                                        .Build();

            var buffer = new byte[512];

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    using (IFileStream stream = fileSystem.File.Open(pathToFileToWrite, FileMode.Open, FileAccess.ReadWrite))
                    {
                        stream.Seek(0, SeekOrigin.End);

                        // Act
                        Task task = stream.WriteAsync(buffer, 0, buffer.Length);
                        task.Wait();
                    }

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
        private void When_seeking_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.Seek(1024, SeekOrigin.Begin);

                // Assert
                IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");
                driveInfo.AvailableFreeSpace.Should().Be(480);
            }
        }
        private void When_increasing_file_size_using_Seek_followed_by_write_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.Seek(1280, SeekOrigin.Begin);
                stream.WriteByte(0x33);

                // Assert
                IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:");
                driveInfo.AvailableFreeSpace.Should().Be(2815);
            }
        }
Beispiel #14
0
 void SetPosition(IFileStream stream, long position)
 {
     stream.Seek(position, SeekOrigin.Begin);
 }