public void Mockfile_Create_OverwritesExistingFile()
        {
            string path = XFS.Path(@"c:\some\file.txt");
            var fileSystem = new MockFileSystem();

            var mockFile = new MockFile(fileSystem);

            fileSystem.Directory.CreateDirectory(Path.GetDirectoryName(path));

            // Create a file
            using (var stream = mockFile.Create(path))
            {
                var contents = new UTF8Encoding(false).GetBytes("Test 1");
                stream.Write(contents, 0, contents.Length);
            }

            // Create new file that should overwrite existing file
            var expectedContents = new UTF8Encoding(false).GetBytes("Test 2");
            using (var stream = mockFile.Create(path))
            {
                stream.Write(expectedContents, 0, expectedContents.Length);
            }

            var actualContents = fileSystem.GetFile(path).Contents;

            Assert.That(actualContents, Is.EqualTo(expectedContents));
        }
        public void Mockfile_Create_OverwritesExistingFile()
        {
            string         path       = XFS.Path(@"c:\some\file.txt");
            MockFileSystem fileSystem = new MockFileSystem();

            MockFile mockFile = new MockFile(fileSystem);

            // Create a file
            using (Stream stream = mockFile.Create(path))
            {
                byte[] contents = new UTF8Encoding(false).GetBytes("Test 1");
                stream.Write(contents, 0, contents.Length);
            }

            // Create new file that should overwrite existing file
            byte[] expectedContents = new UTF8Encoding(false).GetBytes("Test 2");
            using (Stream stream = mockFile.Create(path))
            {
                stream.Write(expectedContents, 0, expectedContents.Length);
            }

            byte[] actualContents = fileSystem.GetFile(path).Contents;

            Assert.Equal(actualContents, expectedContents);
        }
        public void Mockfile_Create_OverwritesExistingFile()
        {
            string path       = XFS.Path(@"c:\some\file.txt");
            var    fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"c:\some"));

            var mockFile = new MockFile(fileSystem);

            // Create a file
            using (var stream = mockFile.Create(path))
            {
                var contents = new UTF8Encoding(false).GetBytes("Test 1");
                stream.Write(contents, 0, contents.Length);
            }

            // Create new file that should overwrite existing file
            var expectedContents = new UTF8Encoding(false).GetBytes("Test 2");

            using (var stream = mockFile.Create(path))
            {
                stream.Write(expectedContents, 0, expectedContents.Length);
            }

            var actualContents = fileSystem.GetFile(path).Contents;

            Assert.That(actualContents, Is.EqualTo(expectedContents));
        }
        private IFile MockSecretFile(string secretKey, string text)
        {
            var file = new MockFile("c:\\sss\\secret.txt", null);

            file.Create(text);
            ((MockDirectory)pubProfiles).AddRawFile(
                $@"C:\Profile\Microsoft\UserSecrets\{secretKey}\secrets.json",
                file);
            return(file);
        }
 public void Mockfile_Create_ThrowsWhenPathIsReadOnly()
 {
     string path = XFS.Path(@"c:\something\read-only.txt");
     var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { path, new MockFileData("Content") } });
     var mockFile = new MockFile(fileSystem);
     
     mockFile.SetAttributes(path, FileAttributes.ReadOnly);
  
     var exception =  Assert.Throws<UnauthorizedAccessException>(() => mockFile.Create(path).Close());
     Assert.That(exception.Message, Is.EqualTo(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path)));
 }
        public void Mockfile_CreateOverload_ShouldCreateNewStream()
        {
            string fullPath   = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"c:\something"));
            FileOptions fileOption = FileOptions.WriteThrough;
            var         mockFile   = new MockFile(fileSystem);

            Assert.That(fileSystem.FileExists(fullPath), Is.False);
            mockFile.Create(fullPath, 20, fileOption).Dispose();
            Assert.That(fileSystem.FileExists(fullPath), Is.True);
        }
        public void Mockfile_Create_ShouldCreateNewStream()
        {
            string fullPath   = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem();

            var sut = new MockFile(fileSystem);

            Assert.False(fileSystem.FileExists(fullPath));

            sut.Create(fullPath).Dispose();

            Assert.True(fileSystem.FileExists(fullPath));
        }
        public void Mockfile_Create_ShouldCreateNewStream()
        {
            string fullPath = XFS.Path(@"c:\something\demo.txt");
            var fileSystem = new MockFileSystem();

            var sut = new MockFile(fileSystem);

            Assert.That(fileSystem.FileExists(fullPath), Is.False);

            sut.Create(fullPath).Close();

            Assert.That(fileSystem.FileExists(fullPath), Is.True);
        }
        public void Mockfile_Create_ShouldCreateNewStream()
        {
            const string fullPath   = @"c:\something\demo.txt";
            var          fileSystem = new MockFileSystem();

            var sut = new MockFile(fileSystem);

            Assert.That(fileSystem.FileExists(fullPath), Is.False);

            sut.Create(fullPath).Close();

            Assert.That(fileSystem.FileExists(fullPath), Is.True);
        }
        public void Mockfile_Create_ThrowsWhenPathIsReadOnly()
        {
            const string path       = @"c:\something\read-only.txt";
            var          fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> {
                { path, new MockFileData("Content") }
            });
            var mockFile = new MockFile(fileSystem);

            mockFile.SetAttributes(path, FileAttributes.ReadOnly);

            var exception = Assert.Throws <UnauthorizedAccessException>(() => mockFile.Create(path).Close());

            Assert.That(exception.Message, Is.EqualTo(string.Format("Access to the path '{0}' is denied.", path)));
        }
        public void Mockfile_Create_ShouldThrowUnauthorizedAccessExceptionIfPathIsReadOnly()
        {
            // Arrange
            string path       = XFS.Path(@"c:\something\read-only.txt");
            var    fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> {
                { path, new MockFileData("Content") }
            });
            var mockFile = new MockFile(fileSystem);

            // Act
            mockFile.SetAttributes(path, FileAttributes.ReadOnly);

            // Assert
            var exception = Assert.Throws <UnauthorizedAccessException>(() => mockFile.Create(path).Dispose());

            Assert.That(exception.Message, Is.EqualTo(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path)));
        }
        public void Mockfile_Create_CanWriteToNewStream()
        {
            string fullPath = XFS.Path(@"c:\something\demo.txt");
            var fileSystem = new MockFileSystem();
            var data = new UTF8Encoding(false).GetBytes("Test string");

            var sut = new MockFile(fileSystem);
            using (var stream = sut.Create(fullPath))
            {
                stream.Write(data, 0, data.Length);
            }

            var mockFileData = fileSystem.GetFile(fullPath);
            var fileData = mockFileData.Contents;

            Assert.That(fileData, Is.EqualTo(data));
        }
        public void Mockfile_Create_CanWriteToNewStream()
        {
            const string fullPath   = @"c:\something\demo.txt";
            var          fileSystem = new MockFileSystem();
            var          data       = new UTF8Encoding(false).GetBytes("Test string");

            var sut = new MockFile(fileSystem);

            using (var stream = sut.Create(fullPath))
            {
                stream.Write(data, 0, data.Length);
            }

            var mockFileData = fileSystem.GetFile(fullPath);
            var fileData     = mockFileData.Contents;

            Assert.That(fileData, Is.EqualTo(data));
        }
        public void Mockfile_Create_CanWriteToNewStream()
        {
            string         fullPath   = XFS.Path(@"c:\something\demo.txt");
            MockFileSystem fileSystem = new MockFileSystem();

            byte[] data = new UTF8Encoding(false).GetBytes("Test string");

            MockFile sut = new MockFile(fileSystem);

            using (Stream stream = sut.Create(fullPath))
            {
                stream.Write(data, 0, data.Length);
            }

            MockFileData mockFileData = fileSystem.GetFile(fullPath);

            byte[] fileData = mockFileData.Contents;

            Assert.Equal(fileData, data);
        }
        public void Mockfile_CreateOverload_CanWriteToNewStream()
        {
            string fullPath   = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"c:\something"));
            var data = new UTF8Encoding(false).GetBytes("Test string");

            var         sut        = new MockFile(fileSystem);
            FileOptions fileOption = FileOptions.WriteThrough;

            using (var stream = sut.Create(fullPath, data.Length, fileOption))
            {
                stream.Write(data, 0, data.Length);
            }

            var mockFileData = fileSystem.GetFile(fullPath);
            var fileData     = mockFileData.Contents;

            Assert.That(fileData, Is.EqualTo(data));
        }
        public void Mockfile_Create_ShouldCreateNewStream()
        {
            const string fullPath = @"c:\something\demo.txt";
            var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());

            var sut = new MockFile(fileSystem);

            Assert.That(fileSystem.FileExists(fullPath), Is.False);

            sut.Create(fullPath).Close();

            Assert.That(fileSystem.FileExists(fullPath), Is.True);
        }
        public void Mockfile_Create_ShouldThrowUnauthorizedAccessExceptionIfPathIsReadOnly()
        {
            // Arrange
            string         path       = XFS.Path(@"c:\something\read-only.txt");
            MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> {
                { path, new MockFileData("Content") }
            });
            MockFile mockFile = new MockFile(fileSystem);

            // Act
            mockFile.SetAttributes(path, FileAttributes.ReadOnly);

            // Assert
            UnauthorizedAccessException exception = Assert.Throws <UnauthorizedAccessException>(() => mockFile.Create(path).Dispose());

            Assert.Equal(exception.Message, $"Access to the path '{path}' is denied.");
        }