public void MockFile_Delete_ShouldDeleteFile() { var fileSystem = new MockFileSystem(); var path = XFS.Path("C:\\test"); var directory = fileSystem.Path.GetDirectoryName(path); fileSystem.AddFile(path, new MockFileData("Bla")); var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length; fileSystem.File.Delete(path); var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length; Assert.AreEqual(1, fileCount1, "File should have existed"); Assert.AreEqual(0, fileCount2, "File should have been deleted"); }
public void MockFileSystem_AddFile_ShouldRepaceExistingFile() { const string path = @"c:\some\file.txt"; const string existingContent = "Existing content"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { path, new MockFileData(existingContent) } }); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(existingContent)); const string newContent = "New content"; fileSystem.AddFile(path, new MockFileData(newContent)); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent)); }
public void MockFile_Delete_ShouldDeleteFile() { MockFileSystem fileSystem = new MockFileSystem(); string path = XFS.Path("C:\\test"); string directory = fileSystem.Path.GetDirectoryName(path); fileSystem.AddFile(path, new MockFileData("Bla")); int fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length; fileSystem.File.Delete(path); int fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length; Assert.Equal(1, fileCount1); Assert.Equal(0, fileCount2); }
public void MockFileInfo_MoveTo_SameSourceAndTargetIsANoOp() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); // Act string destination = XFS.Path(XFS.Path(@"c:\temp\file.txt")); fileInfo.MoveTo(destination); Assert.AreEqual(fileInfo.FullName, destination); Assert.True(fileInfo.Exists); }
public void InstallCommandForPackageReferenceFileDoesNotThrowIfThereIsNoPackageToInstall() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(@"x:\test\packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> </packages>".AsStream()); var installCommand = new TestInstallCommand(GetFactory(), GetSourceProvider(), fileSystem, allowPackageRestore: false); installCommand.Arguments.Add(@"x:\test\packages.config"); // Act & Assert installCommand.ExecuteCommand(); }
public async Task UploadModel_ShouldSaveFile() { // Arrange var mockFileSystem = new MockFileSystem(); var folderPath = mockFileSystem.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".mlops"); mockFileSystem.AddFile("model.zip", new MockFileData("test")); var sut = new LocalFileModelRepository(mockFileSystem); var expectedFilePath = mockFileSystem.Path.Combine(folderPath, $"{new Guid()}.zip"); // Act await sut.UploadModelAsync(new Guid(), "model.zip"); // Assert mockFileSystem.FileExists(expectedFilePath).Should().Be(true); }
public void MockFile_WriteAllText_ShouldThrowAnUnauthorizedAccessExceptionIfTheFileIsReadOnly() { // Arrange var fileSystem = new MockFileSystem(); string filePath = XFS.Path(@"c:\something\demo.txt"); var mockFileData = new MockFileData(new byte[0]); mockFileData.Attributes = FileAttributes.ReadOnly; fileSystem.AddFile(filePath, mockFileData); // Act TestDelegate action = () => fileSystem.File.WriteAllText(filePath, null); // Assert Assert.Throws <UnauthorizedAccessException>(action); }
public void MockFileInfo_OpenText_ShouldReturnStringContentOfFile() { var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); string result; using (var streamReader = fileInfo.OpenText()) { result = streamReader.ReadToEnd(); } Assert.AreEqual(@"line 1\r\nline 2", result); }
public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() { var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); string destinationFolder = XFS.Path(@"c:\temp2"); string destination = XFS.Path(destinationFolder + @"\file.txt"); fileSystem.AddDirectory(destination); fileInfo.MoveTo(destination); Assert.AreEqual(fileInfo.DirectoryName, destinationFolder); Assert.AreEqual(fileInfo.FullName, destination); }
public void OutputProcessedOrderCsvData() { const string inputDir = @"c:\root\in"; const string inputFileName = "myfile.csv"; var inputFilePath = Path.Combine(inputDir, inputFileName); const string outputDir = @"c:\root\out"; const string outputFileName = "myfileout.csv"; var outputFilePath = Path.Combine(outputDir, outputFileName); var csvLines = new StringBuilder(); csvLines.AppendLine("OrderNumber,CustomerNumber,Description,Quantity"); csvLines.AppendLine("42, 100001, Shirt, II"); csvLines.AppendLine("43, 200002, Shorts, I"); csvLines.AppendLine("@ This is a comment"); csvLines.AppendLine(""); csvLines.Append("44, 300003, Cap, V"); var mockInputFile = new MockFileData(csvLines.ToString()); var mockFileSystem = new MockFileSystem(); mockFileSystem.AddFile(inputFilePath, mockInputFile); mockFileSystem.AddDirectory(outputDir); var sut = new CsvFileProcessor(inputFilePath, outputFilePath, mockFileSystem); sut.Process(); Assert.True(mockFileSystem.FileExists(outputFilePath)); var processedFile = mockFileSystem.GetFile(outputFilePath); var lines = processedFile.TextContents.SplitLines(); //Assert.Equal("OrderNumber,Customer,Amount", lines[0]); //Assert.Equal("42,100001,2", lines[1]); //Assert.Equal("43,200002,1", lines[2]); //Assert.Equal("44,300003,5", lines[3]); Approvals.Verify(processedFile.TextContents); }
public void AssemblyReferencesIsFilteredCorrectlyAccordingToTargetFramework() { // Arrange var files = new IPackageFile[] { CreatePackageFile(@"lib\net40\one.dll"), CreatePackageFile(@"lib\net40\two.dll"), CreatePackageFile(@"lib\sl30\one.dll"), CreatePackageFile(@"lib\sl30\two.dll"), CreatePackageFile(@"lib\net45\foo.dll"), CreatePackageFile(@"lib\net45\bar.dll") }; var references = new PackageReferenceSet[] { new PackageReferenceSet( new FrameworkName("Silverlight, Version=2.0"), new [] { "two.dll" }), new PackageReferenceSet( new FrameworkName(".NET, Version=4.0"), new string[0]), new PackageReferenceSet( new FrameworkName(".NET, Version=4.5"), new string[] { "foo.dll", "bar.dll" }), }; var ms = GetPackageStream(files, references); var fileSystem = new MockFileSystem("x:\\"); fileSystem.AddFile("pam.nupkg", ms); var expandedFileSystem = new MockFileSystem("y:\\"); var ozp = new TestableOptimizedZipPackage(fileSystem, "pam.nupkg", expandedFileSystem); // Act var assemblies = ozp.AssemblyReferences.OrderBy(p => p.Path).ToList(); // Assert Assert.Equal(3, assemblies.Count); Assert.Equal(@"lib\net45\bar.dll", assemblies[0].Path); Assert.Equal(@"lib\net45\foo.dll", assemblies[1].Path); Assert.Equal(@"lib\sl30\two.dll", assemblies[2].Path); }
public void Return_no_configuration_if_secrets_folder_path_does_not_exist() { var fileSystemMock = new MockFileSystem(); var secretsProvider = new DockerSecretsConfigurationProvider( "/run/secrets_two", "__", new List <string>(), fileSystemMock ); fileSystemMock.AddFile("/run/secrets/secret_one", new MockFileData("secret one content")); secretsProvider.Load(); secretsProvider.TryGet("secret_one", out _).Should() .BeFalse("specified folder path differs from path where the secret is stored"); }
public void MockDirectory_EnumerateDirectories_WithTopDirectories_ShouldOnlyReturnTopDirectories() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act IEnumerable <string> actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo"); // Assert Assert.Equal(actualResult, new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\") }); }
public void MockDirectory_GetDirectories_WithAllDirectories_ShouldReturnsAllMatchingSubFolders() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act string[] actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories); // Assert Assert.Equal(actualResult, new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\"), XFS.Path(@"C:\Folder\.foo\.foo\") }); }
public void MockFileSystem_AddFile_ShouldRepaceExistingFile() { const string PATH = @"c:\some\file.txt"; const string EXISTING_CONTENT = "Existing content"; MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { PATH, new MockFileData(EXISTING_CONTENT) } }); Assert.Equal(fileSystem.GetFile(PATH).TextContents, EXISTING_CONTENT); const string NEW_CONTENT = "New content"; fileSystem.AddFile(PATH, new MockFileData(NEW_CONTENT)); Assert.Equal(fileSystem.GetFile(PATH).TextContents, NEW_CONTENT); }
public void MockFileSystem_AddFile_ShouldRepaceExistingFile() { var path = XFS.Path(@"c:\some\file.txt"); const string existingContent = "Existing content"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { path, new MockFileData(existingContent) } }); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(existingContent)); const string newContent = "New content"; fileSystem.AddFile(path, new MockFileData(newContent)); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent)); }
public async Task Read_PastEndOfFile_InvalidOperationExceptionThrown() { var fs = new MockFileSystem(); fs.AddFile("test.dat", new MockFileData(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5 })); // 0 + 10 == past end of file var data = new TestDataType(0); await Assert.ThrowsAsync <InvalidOperationException>(async() => { using (var file = fs.File.OpenRead("test.dat")) { await data.ReadAsync(file, 10, new NefsProgress()); } }); }
public async Task RemoveSourceAssemblyPathAsync_Otherwise_SourceAssemblyPathsDoesNotContainSourceAssemblyPath(string sourceAssemblyPath) { var fileSystem = new MockFileSystem(null, "DummyDirectory"); fileSystem.AddFile(sourceAssemblyPath, sourceAssemblyPath); var discoveryManager = Substitute.For <ITestCaseDiscoveryManager>(); var testObjectFactory = FakeTestObjectFactory.Default; var uut = new TestResultManager(fileSystem, discoveryManager, testObjectFactory); await uut.AddSourceAssemblyPathAsync(sourceAssemblyPath); await uut.RemoveSourceAssemblyPathAsync(sourceAssemblyPath); var expected = fileSystem.Path.GetFullPath(sourceAssemblyPath); CollectionAssert.DoesNotContain(uut.SourceAssemblyPaths, expected); }
public void ExistingFileOverrideTest() { var fs = new MockFileSystem(); fs.AddFile("C:\\site\\page1.html", new MockFileData("xyz")); var publisher = new LocalFileSystemPublisher(fs, new Mock <IPublisherExtension>().Object, new Mock <ILogger>().Object, new Mock <ITargetDirectoryCleaner>().Object); var files = new IFile[] { new FileMock(Location.FromPath("C:\\site\\page1.html"), "abc") }; Assert.ThrowsAsync <FilePublishOverwriteForbiddenException>(() => publisher.Write(Location.FromPath("C:\\site"), files.ToAsyncEnumerable())); }
public void MockFileInfo_MoveTo_NonExistentDestination_ShouldUpdateFileInfoDirectoryAndFullName() { var fileSystem = new MockFileSystem(); var sourcePath = XFS.Path(@"c:\temp\file.txt"); var destinationFolder = XFS.Path(@"c:\temp2"); var destinationPath = XFS.Path(destinationFolder + @"\file.txt"); fileSystem.AddFile(sourcePath, new MockFileData("1")); var fileInfo = fileSystem.FileInfo.FromFileName(sourcePath); fileSystem.AddDirectory(destinationFolder); fileInfo.MoveTo(destinationPath); Assert.AreEqual(fileInfo.DirectoryName, destinationFolder); Assert.AreEqual(fileInfo.FullName, destinationPath); }
public void UserSetting_CallingGetValuesWithNonExistantSectionReturnsEmpty() { // Arrange var mockFileSystem = new MockFileSystem(); var nugetConfigPath = "NuGet.Config"; string config = @"<configuration></configuration>"; mockFileSystem.AddFile(nugetConfigPath, config); Settings settings = new Settings(mockFileSystem); // Act var result = settings.GetValues("DoesNotExisit"); // Assert Assert.Empty(result); }
public void MockFileInfo_OpenRead_ShouldReturnByteContentOfFile() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(new byte[] { 1, 2 })); var fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); // Act byte[] result = new byte[2]; using (var stream = fileInfo.OpenRead()) { stream.Read(result, 0, 2); } Assert.AreEqual(new byte[] { 1, 2 }, result); }
public void CheckFirstStringUpperTest() { var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("line1\nline2\nline3"); mockFileSystem.AddFile(@"C:\temp\in.txt", mockInputFile); var sut = new CopyClassStream(mockFileSystem); sut.ConvertFirstLineToUpper(@"C:\temp\in.txt"); MockFileData mockOutputFile = mockFileSystem.GetFile(@"C:\temp\in.out.txt"); var bytes = mockOutputFile.Contents; string[] outputLines = mockOutputFile.TextContents.SplitLines(); Assert.AreEqual("LINE1", outputLines[0]); Assert.AreEqual("line2", outputLines[1]); Assert.AreEqual("line3", outputLines[2]); }
public void ExistChecksForPresenceOfManifestFileUnderDirectory(string id, string version, string path) { // Arrange var fileSystem = new MockFileSystem("x:\root"); fileSystem.CreateDirectory(path); fileSystem.AddFile(path + "\\" + path + ".nuspec"); var configFileSystem = new MockFileSystem(); var repository = new SharedPackageRepository(new DefaultPackagePathResolver(fileSystem), fileSystem, configFileSystem); // Act bool exists = repository.Exists(id, new SemanticVersion(version)); // Assert Assert.True(exists); }
public void Should_Create_Empty_CheckRun_With_Configuration() { var annotations = new Annotation[0]; var configurationFile = Faker.System.FilePath(); var mockFileSystem = new MockFileSystem(); mockFileSystem.AddFile(configurationFile, new MockFileData("{rules: [{code: 'CS1234', reportAs: 'Ignore'}]}")); var checkRun = GetCheckRun(CreateMockBinaryLogProcessor(annotations, Faker.Lorem.Paragraph()), owner: Arg.Any <string>(), repo: Arg.Any <string>(), hash: Arg.Any <string>(), configurationFile: configurationFile, mockFileSystem: mockFileSystem); checkRun.Conclusion.Should().Be(CheckConclusion.Success); checkRun.Name.Should().Be("MSBuild Log"); checkRun.Title.Should().Be("0 errors - 0 warnings"); checkRun.Summary.Should().NotBeNullOrWhiteSpace(); checkRun.Annotations.Should().AllBeEquivalentTo(annotations); }
public void renders_nested_object(string ext, string fileContent) { fileSystem.AddFile(Path.Combine(dataDirectory, $"person.{ext}"), new MockFileData(fileContent)); var template = Template.Parse(@"{{ data.person.name }}"); var hash = Hash.FromAnonymousObject(new { Data = data }); var result = template.Render(hash); Assert.Equal("Eric Mill", result.Trim()); }
public async Task Read_NegativeAbsoluteOffset_InvalidOperationExceptionThrown() { var fs = new MockFileSystem(); fs.AddFile("test.dat", new MockFileData(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5 })); // -4 + 0 == negative absolute offset var data = new TestDataType(-4); await Assert.ThrowsAsync <InvalidOperationException>(async() => { using (var file = fs.File.OpenRead("test.dat")) { await data.ReadAsync(file, 0, new NefsProgress()); } }); }
public async Task AddSourceAssemblyPathAsync_DiscoveryManagerIsDiscoveryInProgressIsTrue_InvokesDiscoveryManagerWaitForDiscoveryCompleteUntilDiscoveryManagerIsDiscoveryInProgressIsFalse(string sourceAssemblyPath) { var fileSystem = new MockFileSystem(null, "DummyDirectory"); fileSystem.AddFile(sourceAssemblyPath, sourceAssemblyPath); var discoveryManager = Substitute.For <ITestCaseDiscoveryManager>(); discoveryManager.IsDiscoveryInProgress.Returns(true, true, true, false, true); discoveryManager.WaitForDiscoveryCompleteAsync().Returns(Task.CompletedTask); var testObjectFactory = FakeTestObjectFactory.Default; var uut = new TestResultManager(fileSystem, discoveryManager, testObjectFactory); await uut.AddSourceAssemblyPathAsync(sourceAssemblyPath); await discoveryManager.Received(3).WaitForDiscoveryCompleteAsync(); }
public void MockDirectory_EnumerateDirectories_WithAllDirectories_ShouldReturnsAllMatchingSubFolders() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories); // Assert Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\"), XFS.Path(@"C:\Folder\.foo\.foo\") })); }
public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopDirectories() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo"); // Assert Assert.That(actualResult, Is.EquivalentTo(new [] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\") })); }
public void ParseChangedFiles_FileWithOneFileRemoved_Parsed() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile("changes.txt", new MockFileData(@"VCE Drivers/Cisco NxOS/Debug.tsdrv:REMOVED:136346")); var changedFilesParser = new ChangedFilesParser(fileSystem); // Act var changedFiles = changedFilesParser.ParseChangedFiles("changes.txt"); // Assert var changedFile = changedFiles.Single(); changedFile.RelativeFilePath.Should().Be(@"VCE Drivers\Cisco NxOS\Debug.tsdrv"); changedFile.ChangeStatus.Should().Be(FileChangeStatus.Removed); changedFile.ChangesetNumber.Should().Be("136346"); }
public void MockFileStream_Dispose_ShouldNotResurrectFile() { var fileSystem = new MockFileSystem(); var path = XFS.Path("C:\\test"); var directory = fileSystem.Path.GetDirectoryName(path); fileSystem.AddFile(path, new MockFileData("Bla")); var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete); var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length; fileSystem.File.Delete(path); var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length; stream.Dispose(); var fileCount3 = fileSystem.Directory.GetFiles(directory, "*").Length; Assert.AreEqual(1, fileCount1, "File should have existed"); Assert.AreEqual(0, fileCount2, "File should have been deleted"); Assert.AreEqual(0, fileCount3, "Disposing stream should not have resurrected the file"); }
public void Move_DirectoryExistsWithDifferentCase_DirectorySuccessfullyMoved() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(@"C:\OLD_LOCATION\Data"); fileSystem.AddFile(@"C:\old_location\Data\someFile.txt", new MockFileData("abc")); // Act fileSystem.Directory.Move(@"C:\old_location", @"C:\NewLocation\"); // Assert Assert.IsTrue(fileSystem.File.Exists(@"C:\NewLocation\Data\someFile.txt")); }
public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopDirectories() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo"); // Assert Assert.That(actualResult, Is.EquivalentTo(new []{XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\")})); }
public void MockFileInfo_OpenText_ShouldReturnStringContentOfFile() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(@"c:\temp\file.txt", new MockFileData(@"line 1\r\nline 2")); var fileInfo = fileSystem.FileInfo.FromFileName(@"c:\temp\file.txt"); // Act string result; using (var streamReader = fileInfo.OpenText()) { result = streamReader.ReadToEnd(); } Assert.AreEqual(@"line 1\r\nline 2", result); }
public void MockFile_WriteAllText_ShouldThrowAnUnauthorizedAccessExceptionIfTheFileIsReadOnly() { // Arrange var fileSystem = new MockFileSystem(); string filePath = XFS.Path(@"c:\something\demo.txt"); var mockFileData = new MockFileData(new byte[0]); mockFileData.Attributes = FileAttributes.ReadOnly; fileSystem.AddFile(filePath, mockFileData); // Act TestDelegate action = () => fileSystem.File.WriteAllText(filePath, null); // Assert Assert.Throws<UnauthorizedAccessException>(action); }
public void MockDirectory_Exists_ShouldReturnTrueForFolderContainingFileAddedToMockFileSystem() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\foo\bar.txt"), new MockFileData("Demo text content")); // Act var result = fileSystem.Directory.Exists(XFS.Path(@"c:\foo\")); // Assert Assert.IsTrue(result); }
public void MockFileData_AcceptsFuncAndReturnsContents() { // create a file in a source file system var sourceFileSystem = new MockFileSystem(); sourceFileSystem.File.WriteAllBytes(sourceFileName, fileContent); sourceFileSystem.File.SetAttributes(sourceFileName, attributes); sourceFileSystem.File.SetCreationTime(sourceFileName, dateCreated); sourceFileSystem.File.SetLastWriteTime(sourceFileName, dateLastWritten); sourceFileSystem.File.SetLastAccessTime(sourceFileName, dateLastAccessed); var sourceFile = sourceFileSystem.DirectoryInfo.FromDirectoryName(Path.GetDirectoryName(sourceFileName)).GetFiles().Single(); // create a new destination file system and copy the source file there var dfs = new MockFileSystem(); dfs.AddFile(destinationFileName, new MockFileData(sourceFileSystem, sourceFile)); destinationFileInfo = dfs.DirectoryInfo.FromDirectoryName(Path.GetDirectoryName(destinationFileName)).GetFiles().Single(); destinationFileSystem = dfs; }
public void MockFileSystem_AddDirectory_ShouldThrowExceptionIfDirectoryIsReadOnly() { // Arrange const string baseDirectory = @"C:\Test"; var fileSystem = new MockFileSystem(); fileSystem.AddFile(baseDirectory, new MockFileData(string.Empty)); fileSystem.File.SetAttributes(baseDirectory, FileAttributes.ReadOnly); // Act TestDelegate act = () => fileSystem.AddDirectory(baseDirectory); // Assert Assert.Throws<UnauthorizedAccessException>(act); }
public void MockDirectory_Exists_ShouldReturnFalseForFiles() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\foo\bar.txt"), new MockFileData("Demo text content")); // Act var result = fileSystem.Directory.Exists(XFS.Path(@"c:\foo\bar.txt")); // Assert Assert.IsFalse(result); }
public void MockFileInfo_OpenRead_ShouldReturnByteContentOfFile() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddFile(@"c:\temp\file.txt", new MockFileData(new byte[] { 1, 2 })); var fileInfo = fileSystem.FileInfo.FromFileName(@"c:\temp\file.txt"); // Act byte[] result = new byte[2]; using (var stream = fileInfo.OpenRead()) { stream.Read(result, 0, 2); } Assert.AreEqual(new byte[] { 1, 2 }, result); }
public void MockFile_OpenText_ShouldRetainCreationTime() { // Arrange var fs = new MockFileSystem(); string filepath = XFS.Path(@"C:\TestData\test.txt"); var file = new MockFileData(@"I'm here"); var creationTime = new DateTime(2012, 03, 21); file.CreationTime = creationTime; fs.AddFile(filepath, file); // Act using (var reader = fs.File.OpenText(filepath)) { reader.ReadLine(); } // Assert Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime); }