Skip to content

Commit

Permalink
Add tests for ReadOnly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
eXpl0it3r committed Feb 13, 2023
1 parent c774bd1 commit 3bdbe5a
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions SharpFileSystem.Tests/FileSystems/NetZipArchiveFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public class NetZipArchiveFileSystemTest : IDisposable
{
private Stream zipStream;
private NetZipArchiveFileSystem fileSystem;
private NetZipArchiveFileSystem fileSystemReadOnly;
private string fileContentString = "this is a file";

//setup
public NetZipArchiveFileSystemTest()
{
zipStream = new FileStream("filesystem.zip", FileMode.Open);
fileSystem = NetZipArchiveFileSystem.Open(zipStream);
fileSystemReadOnly = NetZipArchiveFileSystem.OpenReadOnly(zipStream);
}

//teardown
Expand Down Expand Up @@ -114,5 +116,66 @@ public void CanAddDirectory()

Assert.True(fileSystem.Exists(fsp));
}

[Fact]
public void ReadOnlyGetEntitiesOfRootTest()
{
Assert.Equal(new[]
{
directoryPath,
scratchDirectoryPath,
textfileAPath,
textfileBPath
}, fileSystemReadOnly.GetEntities(FileSystemPath.Root).ToArray());
}

[Fact]
public void ReadOnlyGetEntitiesOfDirectoryTest()
{
Assert.Equal(new[]
{
fileInDirectoryPath
}, fileSystemReadOnly.GetEntities(directoryPath).ToArray());
}

[Fact]
public void ReadOnlyExistsTest()
{
Assert.True(fileSystemReadOnly.Exists(FileSystemPath.Root));
Assert.True(fileSystemReadOnly.Exists(textfileAPath));
Assert.True(fileSystemReadOnly.Exists(directoryPath));
Assert.True(fileSystemReadOnly.Exists(fileInDirectoryPath));
Assert.False(fileSystemReadOnly.Exists(FileSystemPath.Parse("/nonExistingFile")));
Assert.False(fileSystemReadOnly.Exists(FileSystemPath.Parse("/nonExistingDirectory/")));
Assert.False(fileSystemReadOnly.Exists(FileSystemPath.Parse("/directory/nonExistingFileInDirectory")));
}

[Fact]
public void ReadOnlyCanReadFile()
{
var file = fileSystemReadOnly.OpenFile(textfileAPath, FileAccess.Read);
var text = file.ReadAllText();
Assert.True(string.Equals(text, fileContentString));
}

[Fact]
public void ReadOnlyCanNotWriteFile()
{
Assert.Throws<InvalidOperationException>(() => fileSystemReadOnly.OpenFile(textfileBPath, FileAccess.ReadWrite));
}

[Fact]
public void ReadOnlyCanNotAddFile()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/recentlyadded.txt");
Assert.Throws<InvalidOperationException>(() => fileSystemReadOnly.CreateFile(fsp));
}

[Fact]
public void ReadOnlyCanNotAddDirectory()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/dir/");
Assert.Throws<InvalidOperationException>(() => fileSystemReadOnly.CreateDirectory(fsp));
}
}
}

0 comments on commit 3bdbe5a

Please sign in to comment.