Skip to content

Commit

Permalink
Merge pull request #7 from eXpl0it3r/feature/zip-tests
Browse files Browse the repository at this point in the history
Add tests for NetZipArchiveFileSystem
  • Loading branch information
b3b00 committed Feb 13, 2023
2 parents dc2899d + 3bdbe5a commit b46b9bd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 30 deletions.
4 changes: 2 additions & 2 deletions SharpCoreFileSystem/FileSystems/NetZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static NetZipArchiveFileSystem Open(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Update, true));
}

public static NetZipArchiveFileSystem OpenReadOnly(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Read, true));
Expand Down Expand Up @@ -85,7 +85,7 @@ public Stream CreateFile(FileSystemPath path)

public Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (access != FileAccess.Read)
if (ZipArchive.Mode == ZipArchiveMode.Read && access != FileAccess.Read)
throw new InvalidOperationException("This is a read-only filesystem.");

var zae = ZipArchive.GetEntry(ToEntryPath(path));
Expand Down
93 changes: 72 additions & 21 deletions SharpFileSystem.Tests/FileSystems/NetZipArchiveFileSystemTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using SharpFileSystem.IO;
Expand All @@ -8,32 +9,19 @@

namespace SharpFileSystem.Tests.FileSystems
{
public class NetZipArchiveFileSystemTest
public class NetZipArchiveFileSystemTest : IDisposable
{
private Stream zipStream;
private NetZipArchiveFileSystem fileSystem;
private NetZipArchiveFileSystem fileSystemReadOnly;
private string fileContentString = "this is a file";

//setup
public NetZipArchiveFileSystemTest()
{
var memoryStream = new MemoryStream();
zipStream = memoryStream;
var zipOutput = new ZipOutputStream(zipStream);


var fileContentBytes = Encoding.ASCII.GetBytes(fileContentString);
zipOutput.PutNextEntry(new ZipEntry("textfileA.txt")
{
Size = fileContentBytes.Length
});
zipOutput.Write(fileContentBytes);
zipOutput.PutNextEntry(new ZipEntry("directory/fileInDirectory.txt"));
zipOutput.PutNextEntry(new ZipEntry("scratchdirectory/scratch"));
zipOutput.Finish();

memoryStream.Position = 0;
zipStream = new FileStream("filesystem.zip", FileMode.Open);
fileSystem = NetZipArchiveFileSystem.Open(zipStream);
fileSystemReadOnly = NetZipArchiveFileSystem.OpenReadOnly(zipStream);
}

//teardown
Expand All @@ -45,6 +33,7 @@ public void Dispose()

private readonly FileSystemPath directoryPath = FileSystemPath.Parse("/directory/");
private readonly FileSystemPath textfileAPath = FileSystemPath.Parse("/textfileA.txt");
private readonly FileSystemPath textfileBPath = FileSystemPath.Parse("/textfileB.txt");
private readonly FileSystemPath fileInDirectoryPath = FileSystemPath.Parse("/directory/fileInDirectory.txt");
private readonly FileSystemPath scratchDirectoryPath = FileSystemPath.Parse("/scratchdirectory/");

Expand All @@ -53,9 +42,10 @@ public void GetEntitiesOfRootTest()
{
Assert.Equal(new[]
{
textfileAPath,
directoryPath,
scratchDirectoryPath
scratchDirectoryPath,
textfileAPath,
textfileBPath
}, fileSystem.GetEntities(FileSystemPath.Root).ToArray());
}

Expand Down Expand Up @@ -91,13 +81,13 @@ public void CanReadFile()
[Fact]
public void CanWriteFile()
{
var file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var file = fileSystem.OpenFile(textfileBPath, FileAccess.ReadWrite);
var textBytes = Encoding.ASCII.GetBytes(fileContentString + " and a new string");
file.Write(textBytes);
file.Close();


file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
file = fileSystem.OpenFile(textfileBPath, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.True(string.Equals(text, fileContentString + " and a new string"));
}
Expand Down Expand Up @@ -126,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));
}
}
}
10 changes: 3 additions & 7 deletions SharpFileSystem.Tests/SharpFileSystem.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
</PackageReference>
<PackageReference Include="AssertNet.Xunit" Version="1.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<!-- <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19554-01">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference> -->
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
Expand All @@ -65,8 +61,8 @@
<EmbeddedResource Include="resDir\deep\deeper\deeper.txt" />
<None Remove="resDir\deep\deep.txt" />
<EmbeddedResource Include="resDir\deep\deep.txt" />
</ItemGroup>
<ItemGroup>
<Compile Remove="FileSystems\NetZipArchiveFileSystemTest.cs" />
<None Update="filesystem.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file added SharpFileSystem.Tests/filesystem.zip
Binary file not shown.

0 comments on commit b46b9bd

Please sign in to comment.