Skip to content

Commit ccad777

Browse files
committed
Added unit tests, Bumped to v1.0.0
1 parent deb968b commit ccad777

11 files changed

Lines changed: 348 additions & 4 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testfile_1.txt Content No <Cr><Lf>!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Testfile_{2}.txt Content No <Cr><Lf>!
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp3.1;Net5.0</TargetFrameworks>
6+
<SignAssembly>true</SignAssembly>
7+
<AssemblyOriginatorKeyFile>..\Axuno.VirtualFileSystem\Axuno.VirtualFileSystem.snk</AssemblyOriginatorKeyFile>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Remove="Assets\Testfile_{2}.txt" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<EmbeddedResource Include="Assets\*" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<EmbeddedResource Include="Assets\Text\Testfile_1.txt" />
20+
<EmbeddedResource Include="Assets\Text\Testfile_{2}.txt" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
25+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
26+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
27+
<PackageReference Include="NUnit" Version="3.13.0" />
28+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<ProjectReference Include="..\Axuno.VirtualFileSystem\Axuno.VirtualFileSystem.csproj" />
33+
</ItemGroup>
34+
35+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
5+
namespace Axuno.VirtualFileSystem.Tests
6+
{
7+
public class DirectoryLocator
8+
{
9+
/// <summary>
10+
/// Gets the full path to the target project that we wish to test
11+
/// Assumes that the test project is on the same directory level as the target project
12+
/// </summary>
13+
/// <returns>The full path to the target project.</returns>
14+
public static string GetTargetProjectPath(Type classFromTargetAssembly)
15+
{
16+
var targetAssembly = classFromTargetAssembly.GetTypeInfo().Assembly;
17+
18+
// Get name of the target project which we want to test
19+
var projectName = targetAssembly.GetName().Name;
20+
21+
// Get currently executing test project path
22+
var applicationBasePath = AppContext.BaseDirectory;
23+
24+
// Find the path to the target project
25+
var directoryInfo = new DirectoryInfo(applicationBasePath);
26+
do
27+
{
28+
directoryInfo = directoryInfo.Parent;
29+
30+
var projectDirectoryInfo = new DirectoryInfo(directoryInfo.FullName);
31+
if (projectDirectoryInfo.Exists)
32+
{
33+
var projectFileInfo = new FileInfo(Path.Combine(projectDirectoryInfo.FullName, projectName, $"{projectName}.csproj"));
34+
if (projectFileInfo.Exists)
35+
{
36+
return Path.Combine(projectDirectoryInfo.FullName, projectName);
37+
}
38+
}
39+
}
40+
while (directoryInfo.Parent != null || directoryInfo.Parent.FullName.EndsWith("\\" + projectName));
41+
42+
throw new Exception($"Project root could not be located using the application root {applicationBasePath}.");
43+
}
44+
}
45+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Primitives;
6+
using Microsoft.Extensions.FileProviders;
7+
using NUnit.Framework;
8+
9+
namespace Axuno.VirtualFileSystem.Tests
10+
{
11+
[TestFixture]
12+
public class DynamicFileProviderTests
13+
{
14+
private readonly IDynamicFileProvider _dynamicFileProvider;
15+
16+
public DynamicFileProviderTests()
17+
{
18+
_dynamicFileProvider =
19+
ServiceSetup.GetVirtualFileSystemServiceProvider().GetRequiredService<IDynamicFileProvider>();
20+
}
21+
22+
[Test]
23+
public void Should_Get_Created_Files()
24+
{
25+
const string fileContent = "Hello World";
26+
27+
_dynamicFileProvider.AddOrUpdate(
28+
new InMemoryFileInfo(
29+
"/my-files/test.txt",
30+
fileContent.GetBytes(),
31+
"test.txt"
32+
)
33+
);
34+
35+
var fileInfo = _dynamicFileProvider.GetFileInfo("/my-files/test.txt");
36+
Assert.IsNotNull(fileInfo);
37+
Assert.AreEqual(fileContent, fileInfo.ReadAsString());
38+
}
39+
40+
[Test]
41+
public void Should_Create_and_Delete_Files()
42+
{
43+
const string fileContent = "Hello World";
44+
const string dynamicPath = "/to-delete/test.txt";
45+
46+
_dynamicFileProvider.AddOrUpdate(
47+
new InMemoryFileInfo(
48+
dynamicPath,
49+
fileContent.GetBytes(),
50+
"test.txt"
51+
)
52+
);
53+
54+
// Register to change on that file
55+
56+
var fileCallbackCalled = false;
57+
58+
ChangeToken.OnChange(
59+
() => _dynamicFileProvider.Watch(dynamicPath),
60+
() => { fileCallbackCalled = true; });
61+
62+
var fileInfo = _dynamicFileProvider.GetFileInfo(dynamicPath);
63+
Assert.IsTrue(fileInfo.Exists);
64+
65+
// Deleting the file should trigger the callback
66+
67+
_dynamicFileProvider.Delete(dynamicPath);
68+
fileInfo = _dynamicFileProvider.GetFileInfo(dynamicPath);
69+
Assert.IsFalse(fileInfo.Exists);
70+
71+
Assert.IsTrue(fileCallbackCalled);
72+
}
73+
74+
[Test]
75+
public void Deleting_Non_Existing_File_Just_Fails()
76+
{
77+
Assert.IsFalse(_dynamicFileProvider.Delete("does-not-exist"));
78+
}
79+
80+
[Test]
81+
public void Should_Get_Notified_On_File_Change()
82+
{
83+
// Create a dynamic file
84+
85+
_dynamicFileProvider.AddOrUpdate(
86+
new InMemoryFileInfo(
87+
"/my-files/test.txt",
88+
"Hello World".GetBytes(),
89+
"test.txt"
90+
)
91+
);
92+
93+
// Register to change on that file
94+
95+
var fileCallbackCalled = false;
96+
97+
ChangeToken.OnChange(
98+
() => _dynamicFileProvider.Watch("/my-files/test.txt"),
99+
() => { fileCallbackCalled = true; });
100+
101+
// Updating the file should trigger the callback
102+
103+
_dynamicFileProvider.AddOrUpdate(
104+
new InMemoryFileInfo(
105+
"/my-files/test.txt",
106+
"Hello World UPDATED".GetBytes(),
107+
"test.txt"
108+
)
109+
);
110+
111+
Assert.IsTrue(fileCallbackCalled);
112+
113+
// Updating the file should trigger the callback (2nd test)
114+
115+
fileCallbackCalled = false;
116+
117+
_dynamicFileProvider.AddOrUpdate(
118+
new InMemoryFileInfo(
119+
"/my-files/test.txt",
120+
"Hello World UPDATED 2".GetBytes(),
121+
"test.txt"
122+
)
123+
);
124+
125+
Assert.IsTrue(fileCallbackCalled);
126+
}
127+
}
128+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using Microsoft.Extensions.FileProviders;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Logging.Abstractions;
8+
9+
namespace Axuno.VirtualFileSystem.Tests
10+
{
11+
class ServiceSetup
12+
{
13+
public static ServiceProvider GetVirtualFileSystemServiceProvider()
14+
{
15+
return new ServiceCollection()
16+
.Configure<VirtualFileSystemOptions>(options =>
17+
{
18+
options.FileSets.AddEmbedded<ServiceSetup>("Axuno.VirtualFileSystem.Tests.Assets");
19+
options.FileSets.AddPhysical(
20+
Path.Combine(DirectoryLocator.GetTargetProjectPath(typeof(ServiceSetup)), "Assets"));
21+
})
22+
.AddSingleton<ILoggerFactory>(b => new NullLoggerFactory())
23+
.AddSingleton<IFileProvider, VirtualFileProvider>()
24+
.AddSingleton<IDynamicFileProvider, DynamicFileProvider>()
25+
.BuildServiceProvider();
26+
}
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using NUnit.Framework;
5+
6+
namespace Axuno.VirtualFileSystem.Tests
7+
{
8+
[TestFixture]
9+
public class VirtualFilePathHelperTests
10+
{
11+
[Test]
12+
public void NormalizePath()
13+
{
14+
// Hyphens should be normalized to underscores
15+
Assert.AreEqual("~/test_one/test_two/test-three.txt", VirtualFilePathHelper.NormalizePath("~/test-one/test-two/test-three.txt"));
16+
}
17+
}
18+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.FileProviders;
6+
using NUnit.Framework;
7+
8+
namespace Axuno.VirtualFileSystem.Tests
9+
{
10+
[TestFixture]
11+
public class VirtualFileProviderTests
12+
{
13+
private readonly IFileProvider _virtualFileProvider;
14+
15+
public VirtualFileProviderTests()
16+
{
17+
_virtualFileProvider =
18+
ServiceSetup.GetVirtualFileSystemServiceProvider().GetRequiredService<IFileProvider>();
19+
}
20+
21+
[Test]
22+
public void Should_Define_And_Get_Embedded_Resources()
23+
{
24+
var resource = _virtualFileProvider.GetFileInfo("/Text/Testfile_1.txt");
25+
26+
Assert.IsNotNull(resource);
27+
Assert.IsTrue(resource.Exists);
28+
29+
// file is expected as UTF8 without BOM
30+
using var stream = resource.CreateReadStream();
31+
Assert.AreEqual("Testfile_1.txt Content No <Cr><Lf>!", Encoding.UTF8.GetString(stream.GetAllBytes()));
32+
}
33+
34+
[Test]
35+
public void Should_Define_And_Get_Embedded_Resources_With_Special_Chars()
36+
{
37+
var resource = _virtualFileProvider.GetFileInfo("/Text/Testfile_{2}.txt");
38+
Assert.IsNotNull(resource);
39+
Assert.IsTrue(resource.Exists);
40+
41+
// file is expected as UTF8 without BOM
42+
using var stream = resource.CreateReadStream();
43+
Assert.AreEqual("Testfile_{2}.txt Content No <Cr><Lf>!", Encoding.UTF8.GetString(stream.GetAllBytes()));
44+
}
45+
46+
[Test]
47+
public void Should_Define_And_Get_Embedded_Directory_Contents()
48+
{
49+
var contents = _virtualFileProvider.GetDirectoryContents("/Text");
50+
51+
Assert.IsTrue(contents.Exists);
52+
53+
var contentList = contents.ToList();
54+
55+
Assert.Contains("Testfile_1.txt", contentList.Select(c => c.Name).ToList());
56+
Assert.Contains("Testfile_{2}.txt", contentList.Select(c => c.Name).ToList());
57+
}
58+
59+
[TestCase("/")]
60+
[TestCase("")]
61+
public void Should_Define_And_Get_Embedded_Root_Directory_Contents(string path)
62+
{
63+
var contents = _virtualFileProvider.GetDirectoryContents(path);
64+
65+
Assert.IsTrue(contents.Exists);
66+
67+
var contentList = contents.ToList();
68+
Assert.Contains("Text", contents.ToList().Select(c => c.Name).ToList());
69+
}
70+
}
71+
}

Src/Axuno.VirtualFileSystem/Axuno.VirtualFileSystem.csproj

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
<TargetFrameworks>netstandard2.1;net50</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<SignAssembly>true</SignAssembly>
7-
<AssemblyOriginatorKeyFile>Axuno.VirtualFileSystem.pfx</AssemblyOriginatorKeyFile>
8-
<Version>0.7.2</Version>
7+
<!-- sn -k Axuno.VirtualFileSystem.snk -->
8+
<AssemblyOriginatorKeyFile>Axuno.VirtualFileSystem.snk</AssemblyOriginatorKeyFile>
9+
<Version>1.0.0</Version>
910
<Authors>axuno gGmbH</Authors>
1011
<Company>axuno gGmbH</Company>
1112
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
1213
<PackageIcon>VirtualFileSystem.png</PackageIcon>
1314
<RepositoryUrl>https://github.com/axuno/Axuno.VirtualFileSystem</RepositoryUrl>
1415
<RepositoryType>Git</RepositoryType>
1516
<PackageTags>virtual file system netstandard c#</PackageTags>
16-
<PackageReleaseNotes>Initial release with .Net 5.0 framework added</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added unit tests
18+
Bumped to v1.0.0</PackageReleaseNotes>
1719
<Copyright>© 2013 - 2020 Volosoft. Open source license with LGPLv3.0</Copyright>
1820
<Description>The Virtual File System makes it possible to manage files that do not exist on a physical file system (e.g. disk).
1921

@@ -22,7 +24,7 @@
2224
* Virtual files can be used just like static files in an application.
2325
* JavaScript, CSS, image files and all other file types can be embedded into assemblies and used just like the static files.
2426

25-
The library is a modified version of Volo.Abp.VirtualFileSystem 3.3.1</Description>
27+
The library is a modified version of Volo.Abp.VirtualFileSystem 4.1</Description>
2628
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2729
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2830
</PropertyGroup>
@@ -54,6 +56,15 @@ The library is a modified version of Volo.Abp.VirtualFileSystem 3.3.1</Descripti
5456
</PackageReference>
5557
</ItemGroup>
5658

59+
<ItemGroup>
60+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
61+
<!-- Generate PublicKey: -->
62+
<!-- Export public key: sn -p Axuno.VirtualFileSystem.snk Axuno.VirtualFileSystem.publickey -->
63+
<!-- Display PublicKey: sn -tp Axuno.VirtualFileSystem.publickey -->
64+
<_Parameter1>Axuno.VirtualFileSystem.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010091cc5ebb97cd2f332fbb3f69e2c1b0092fdc87141c9f3085913c152796473e3772c3d0cc09f55a16bbd33eec598fd583ef5ab28f1a459672f56f1b9a9b68ead56c0a238800864d25c480d185b16a198ff19d15449ec18d2c19af4049432e3d089731430ea28d378c2c70f051d3f5356d0d1d483ad9cffb0762b748029bd3e0cb</_Parameter1>
65+
</AssemblyAttribute>
66+
</ItemGroup>
67+
5768
<ItemGroup>
5869
<None Include="..\..\VirtualFileSystem.png">
5970
<Pack>True</Pack>
596 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)