Exemplo n.º 1
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    Write(data, 0, data.Length);
                    Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }
        }
        public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
        {
            if (!directoryPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                directoryPath += Path.DirectorySeparatorChar;

            this.mockFileDataAccessor = mockFileDataAccessor;
            this.directoryPath = directoryPath;
        }
        internal PathVerifier(IMockFileDataAccessor mockFileDataAccessor)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            _mockFileDataAccessor = mockFileDataAccessor;
        }
        public MockFileInfoFactory(IMockFileDataAccessor mockFileSystem)
        {
            if (mockFileSystem == null)
            {
                throw new ArgumentNullException("mockFileSystem");
            }

            this.mockFileSystem = mockFileSystem;
        }
        public MockPath(IMockFileDataAccessor mockFileDataAccessor)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;
        }
        public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
        {
            if (mockFileSystem == null)
            {
                throw new ArgumentNullException("mockFileSystem");
            }

            this.mockFileSystem = mockFileSystem;
            this.path           = path;
        }
        public MockFile(IMockFileDataAccessor mockFileDataAccessor)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;
            mockPath = new MockPath(mockFileDataAccessor);
        }
        public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
        {
            if (mockFileSystem == null)
            {
                throw new ArgumentNullException("mockFileSystem");
            }

            this.mockFileSystem = mockFileSystem;
            this.path = path;
        }
        public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.currentDirectory     = currentDirectory;
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.fileBase             = fileBase;
        }
        public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.currentDirectory = currentDirectory;
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.fileBase = fileBase;
        }
        public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
        {
            if (mockFileSystem == null)
            {
                throw new ArgumentNullException("mockFileSystem");
            }
            //Workaround - this forces MSCORLIB's Path to validate the path.
            mockFileSystem.Path.GetFullPath(path);

            this.mockFileSystem = mockFileSystem;
            this.path           = path;
        }
Exemplo n.º 12
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            FileMode mode,
            FileAccess access   = FileAccess.ReadWrite,
            FileOptions options = FileOptions.None)

        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (mode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, access);

                var existingContents     = fileData.Contents;
                var keepExistingContents =
                    existingContents?.Length > 0 &&
                    mode != FileMode.Truncate && mode != FileMode.Create;
                if (keepExistingContents)
                {
                    Write(existingContents, 0, existingContents.Length);
                    Seek(0, mode == FileMode.Append
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                var directoryPath = mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(directoryPath) && !mockFileDataAccessor.Directory.Exists(directoryPath))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (mode.Equals(FileMode.Open) || mode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            this.access = access;
        }
        public void MockFile_WriteAllLinesAsyncGeneric_ShouldWriteTheCorrectContent(IMockFileDataAccessor fileSystem, Action action, string expectedContent)
        {
            // Arrange
            // is done in the test case source

            // Act
            action();

            // Assert
            var actualContent = fileSystem.GetFile(TestDataForWriteAllLines.Path).TextContents;

            Assert.That(actualContent, Is.EqualTo(expectedContent));
        }
Exemplo n.º 14
0
        public void MockFile_WriteAllLinesGeneric_ShouldWriteTheCorrectContent(IMockFileDataAccessor fileSystem, Action action, string expectedContent)
        {
            // Arrange
            // is done in the test case source

            // Act
            action();

            // Assert
            string actualContent = fileSystem.GetFile(Path).TextContents;

            Assert.Equal(actualContent, expectedContent);
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                base.Write(data, 0, data.Length);
                base.Seek(0, SeekOrigin.Begin);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
        /// </summary>
        /// <param name="mockFileDataAccessor">The mock file data accessor.</param>
        /// <param name="directoryPath">The directory path.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="mockFileDataAccessor"/> or <paramref name="directoryPath"/> is <see langref="null"/>.</exception>
        public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;

            directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);

            this.directoryPath = EnsurePathEndsWithDirectorySeparator(directoryPath);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
        /// </summary>
        /// <param name="mockFileDataAccessor">The mock file data accessor.</param>
        /// <param name="directoryPath">The directory path.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="mockFileDataAccessor"/> or <paramref name="directoryPath"/> is <see langref="null"/>.</exception>
        public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;

            directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);

            this.directoryPath = EnsurePathEndsWithDirectorySeparator(directoryPath);
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                base.Write(data, 0, data.Length);
                base.Seek(0, SeekOrigin.Begin);
            }
        }
        public void MockFile_WriteAllLinesGeneric_ShouldWriteTheCorrectContent(IMockFileDataAccessor fileSystem, Action action, string expectedContent, string message)
        {
            // Arrange
            // is done in the test case source

            // Act
            action();

            // Assert
            var actualContent = fileSystem.GetFile(TestDataForWriteAllLines.Path).TextContents;

            actualContent.Should().Be(actualContent, message);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
        /// </summary>
        /// <param name="mockFileDataAccessor">The mock file data accessor.</param>
        /// <param name="directoryPath">The directory path.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="mockFileDataAccessor"/> or <paramref name="directoryPath"/> is <see langref="null"/>.</exception>
        public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));

            originalPath  = directoryPath;
            directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);

            directoryPath = directoryPath.TrimSlashes();
            if (XFS.IsWindowsPlatform())
            {
                directoryPath = directoryPath.TrimEnd(' ');
            }
            this.directoryPath = directoryPath;
        }
Exemplo n.º 21
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            StreamType streamType,
            FileOptions options,
            FileMode fileMode = FileMode.Append)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (fileMode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, streamType != StreamType.READ ? FileAccess.Write : FileAccess.Read);

                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = fileData.Contents;
                if (data != null && data.Length > 0 && streamType != StreamType.TRUNCATE)
                {
                    Write(data, 0, data.Length);
                    Seek(0, StreamType.APPEND.Equals(streamType)
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                if (!mockFileDataAccessor.Directory.Exists(mockFileDataAccessor.Path.GetDirectoryName(path)))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (StreamType.READ.Equals(streamType) ||
                    fileMode.Equals(FileMode.Open) ||
                    fileMode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            canWrite = streamType != StreamType.READ;
        }
Exemplo n.º 22
0
        public async Task ThenAnEmptyListIsReturnedWhenReadingTheFileFails(
            AccountsRepository sut,
            IWorkingCopy workingCopy,
            IMockFileDataAccessor mockFileDataAccessor,
            Wrapper <string> existingSettingPath)
        {
            //Arrange
            mockFileDataAccessor.AddFile(existingSettingPath, new MockFileData("{ invalid json }"));
            Mock.Get(workingCopy).Setup(w => w.SettingsPath).Returns(existingSettingPath);

            //Act
            var accounts = await sut.GetList();

            // Assert
            accounts.Should().BeEmpty();
        }
Exemplo n.º 23
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    base.Write(data, 0, data.Length);
                    base.Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
        }
Exemplo n.º 24
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    base.Write(data, 0, data.Length);
                    base.Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
        }
Exemplo n.º 25
0
        public async Task ThenStoredAccountsAreReturned(
            AccountsRepository sut,
            IWorkingCopy workingCopy,
            IMockFileDataAccessor mockFileDataAccessor,
            Wrapper <string> existingSettingPath,
            List <AccountEntity> existingEntities)
        {
            //Arrange
            var rawEntities = JsonConvert.SerializeObject(existingEntities);

            mockFileDataAccessor.AddFile(existingSettingPath, new MockFileData(rawEntities));
            Mock.Get(workingCopy).Setup(w => w.SettingsPath).Returns(existingSettingPath);

            //Act
            var accounts = await sut.GetList();

            // Assert
            accounts.Count.Should().Be(existingEntities.Count);
        }
        public MockDriveInfo(IMockFileDataAccessor mockFileDataAccessor, string name)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException(nameof(mockFileDataAccessor));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            const string DRIVE_SEPARATOR = @":\";

            if (name.Length == 1)
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else if (name.Length == 2 && name[1] == ':')
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else if (name.Length == 3 && name.EndsWith(DRIVE_SEPARATOR, StringComparison.Ordinal))
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else
            {
                MockPath.CheckInvalidPathChars(name);
                name = mockFileDataAccessor.Path.GetPathRoot(name);

                if (string.IsNullOrEmpty(name) || name.StartsWith(@"\\", StringComparison.Ordinal))
                {
                    throw new ArgumentException(
                              @"Object must be a root directory (""C:\"") or a drive letter (""C"").");
                }
            }

            this._mockFileDataAccessor = mockFileDataAccessor;

            Name    = name;
            IsReady = true;
        }
        public MockDriveInfo(IMockFileDataAccessor mockFileDataAccessor, string name)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            const string DRIVE_SEPARATOR = @":\";
            if (name.Length == 1)
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else if (name.Length == 2 && name[1] == ':')
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else if (name.Length == 3 && name.EndsWith(DRIVE_SEPARATOR, StringComparison.Ordinal))
            {
                name = char.ToUpperInvariant(name[0]) + DRIVE_SEPARATOR;
            }
            else
            {
                MockPath.CheckInvalidPathChars(name);
                name = mockFileDataAccessor.Path.GetPathRoot(name);

                if (string.IsNullOrEmpty(name) || name.StartsWith(@"\\", StringComparison.Ordinal))
                {
                    throw new ArgumentException(
                        @"Object must be a root directory (""C:\"") or a drive letter (""C"").");
                }
            }

            this.mockFileDataAccessor = mockFileDataAccessor;

            Name = name;
            IsReady = true;
        }
 public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory) 
 {
     this.currentDirectory = currentDirectory;
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.fileBase = fileBase;
 }
Exemplo n.º 29
0
 public PathVerifier(IMockFileDataAccessor mockFileDataAccessor)
 {
     _mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
 }
 public MockDriveInfoFactory(IMockFileDataAccessor mockFileSystem)
 {
     this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
 }
Exemplo n.º 31
0
 public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path) : base(mockFileSystem?.FileSystem)
 {
     this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
     this.path           = path ?? throw new ArgumentNullException(nameof(path));
 }
Exemplo n.º 32
0
 public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory) :
     this(mockFileDataAccessor, currentDirectory)
 {
 }
Exemplo n.º 33
0
 public MockFile(IMockFileDataAccessor mockFileDataAccessor)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
     mockPath = new MockPath(mockFileDataAccessor);
 }
Exemplo n.º 34
0
 public MockFileStreamFactory(IMockFileDataAccessor mockFileSystem)
 => this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
 public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.directoryPath = EnsurePathEndsWithDirectorySeparator(directoryPath);
 }
Exemplo n.º 36
0
 public MockFile(IMockFileDataAccessor mockFileDataAccessor)
 {
     this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
     mockPath = new MockPath(mockFileDataAccessor);
 }
Exemplo n.º 37
0
 public MockPath(IMockFileDataAccessor mockFileDataAccessor)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
 }
 public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.directoryPath        = EnsurePathEndsWithDirectorySeparator(directoryPath);
 }
Exemplo n.º 39
0
 public MockDirectoryInfoFactory(IMockFileDataAccessor mockFileSystem)
 {
     this.mockFileSystem = mockFileSystem;
 }
Exemplo n.º 40
0
 public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase, string currentDirectory)
 {
     this.currentDirectory     = currentDirectory;
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.fileBase             = fileBase;
 }
Exemplo n.º 41
0
 public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
 {
     this.mockFileSystem = mockFileSystem;
     this.path           = path;
 }
        public void MockFile_WriteAllLinesGeneric_ShouldWriteTheCorrectContent(IMockFileDataAccessor fileSystem, Action action, string expectedContent)
        {
            // Arrange
            // is done in the test case source

            // Act
            action();

            // Assert
            var actualContent = fileSystem.GetFile(TestDataForWriteAllLines.Path).TextContents;
            Assert.That(actualContent, Is.EqualTo(expectedContent));
        }
Exemplo n.º 43
0
 public FileSystemMockup(IMockFileDataAccessor data)
 {
     _file      = new MockFile(data);
     _path      = new MockPath(data);
     _directory = new MockDirectory(data, _file, data.Directory.GetCurrentDirectory());
 }
 public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.directoryPath = directoryPath;
 }
Exemplo n.º 45
0
 public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, FileBase fileBase)
 {
     this.mockFileDataAccessor = mockFileDataAccessor;
     this.fileBase = fileBase;
 }
 public DriveEqualityComparer(IMockFileDataAccessor mockFileSystem)
 {
     this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
 }
Exemplo n.º 47
0
 public MockDirectory(IMockFileDataAccessor mockFileDataAccessor, string currentDirectory) : base(mockFileDataAccessor?.FileSystem)
 {
     this.currentDirectory     = currentDirectory;
     this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
 }
 public MockDirectoryInfoFactory(IMockFileDataAccessor mockFileSystem)
 {
     this.mockFileSystem = mockFileSystem;
 }
 public MockFile(IMockFileDataAccessor mockFileDataAccessor) : base(mockFileDataAccessor?.FileSystem)
 {
     this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
 }
 public MockFileInfo(IMockFileDataAccessor mockFileSystem, string path)
 {
     this.mockFileSystem = mockFileSystem;
     this.path = path;
 }