Skip to content

Commit c3508ff

Browse files
committed
Better argument validation in DirectoryBuilder, resolve #1
1 parent 2559c23 commit c3508ff

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Wazzy.Tests/VfsTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Wasmtime;
2+
using Wazzy.Extensions;
3+
using Wazzy.WasiSnapshotPreview1.FileSystem.Implementations.VirtualFileSystem.Builder;
4+
5+
namespace Wazzy.Tests;
6+
7+
[TestClass]
8+
public class VfsTests
9+
{
10+
[TestMethod]
11+
public void MapDirectory()
12+
{
13+
Assert.ThrowsException<ArgumentException>(() =>
14+
{
15+
new VirtualFileSystemBuilder()
16+
.WithVirtualRoot(builder => { builder.MapDirectory("", ".", true); })
17+
.Build();
18+
});
19+
}
20+
}

Wazzy.sln.DotSettings.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\Martin\AppData\Local\Temp\JetBrains\ReSharperPlatformVs17\vAny_ac125f2d\CoverageData\_Wazzy.1727317920\Snapshot\snapshot.utdcvr</s:String>
2+
33
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=72b34e5d_002D828f_002D4842_002Db55e_002D5786e03b2df9/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;Wazzy.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
44
&lt;Project Location="C:\Users\Martin\Documents\dotnet\Wazzy\Wazzy.Tests" Presentation="&amp;lt;Wazzy.Tests&amp;gt;" /&gt;&#xD;
55
&lt;/SessionState&gt;</s:String>

Wazzy/WasiSnapshotPreview1/FileSystem/Implementations/VirtualFileSystem/Builder/DirectoryBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ private string ValidatePath(string path)
7373

7474
public DirectoryBuilder CreateVirtualDirectory(string name, Action<DirectoryBuilder> content)
7575
{
76+
if (string.IsNullOrEmpty(name))
77+
throw new ArgumentException("Cannot create a virtual directory with an empty name");
78+
7679
var path = _fullPath + '/' + ValidatePath(name);
7780

7881
_contentConstructors.Add(
@@ -96,6 +99,9 @@ public DirectoryBuilder CreateInMemoryFile(
9699
MemoryStream? backing = null,
97100
bool isReadOnly = false)
98101
{
102+
if (string.IsNullOrEmpty(name))
103+
throw new ArgumentException("Cannot create an in memory file with an empty name");
104+
99105
_contentConstructors.Add(
100106
(_fullPath + '/' + ValidatePath(name), context =>
101107
{
@@ -114,6 +120,8 @@ public DirectoryBuilder MapFile(
114120
string hostPath,
115121
bool isReadonly = false)
116122
{
123+
if (string.IsNullOrEmpty(name))
124+
throw new ArgumentException("Cannot map a file with an empty name");
117125
if (!File.Exists(hostPath))
118126
throw new ArgumentException($"file {hostPath} does not exist", nameof(hostPath));
119127

@@ -129,6 +137,8 @@ public DirectoryBuilder MapDirectory(
129137
string hostPath,
130138
bool isReadOnly = false)
131139
{
140+
if (string.IsNullOrEmpty(name))
141+
throw new ArgumentException("Cannot map a directory with an empty name");
132142
if (!Directory.Exists(hostPath))
133143
throw new ArgumentException($"directory {hostPath} does not exist", nameof(hostPath));
134144

0 commit comments

Comments
 (0)