Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: split IncludeSubdirectories_SetToTrue_ArgsNameShouldContainRela…
…tivePath for better control
  • Loading branch information
pw-sgr committed Nov 26, 2025
commit 38065c062f78631fa0f3dc9572e65c7e8810fbd0
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;

Expand Down Expand Up @@ -126,7 +125,7 @@ string subdirectoryName
[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public async Task IncludeSubdirectories_SetToTrue_ArgsNameShouldContainRelativePath(
public async Task IncludeSubdirectories_SetToTrue_Created_ArgsNameShouldContainRelativePath(
bool watchRootedPath,
string baseDirectory,
string subdirectoryName,
Expand All @@ -144,26 +143,16 @@ string fileName
baseDirectory, subdirectoryName, subSubdirectoryName, fileName
);

string newFilePath = filePath + ".new";

string expectedFileName = FileSystem.Path.Combine(
subdirectoryName, subSubdirectoryName, fileName
);

string expectedNewFileName = expectedFileName + ".new";

string watchPath = watchRootedPath
? FileSystem.Path.Combine(FileSystem.Directory.GetCurrentDirectory(), baseDirectory)
: baseDirectory;

using ManualResetEventSlim createdMre = new();
using ManualResetEventSlim changedMre = new();
using ManualResetEventSlim renamedMre = new();
using ManualResetEventSlim deletedMre = new();
FileSystemEventArgs? createdArgs = null;
FileSystemEventArgs? changedArgs = null;
RenamedEventArgs? renamedArgs = null;
FileSystemEventArgs? deletedArgs = null;

using IFileSystemWatcher fileSystemWatcher = FileSystem.FileSystemWatcher.New(watchPath);

Expand All @@ -172,7 +161,7 @@ string fileName
// ReSharper disable once AccessToDisposedClosure
try
{
createdArgs ??= eventArgs;
createdArgs = eventArgs;
createdMre.Set();
}
catch (ObjectDisposedException)
Expand All @@ -181,6 +170,56 @@ string fileName
}
};

fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;

// Act

FileSystem.File.Create(filePath).Dispose();

// Assert

await That(createdMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();

await That(createdArgs).IsNotNull().And
.Satisfies(args => string.Equals(args?.Name, expectedFileName, StringComparison.Ordinal)
);
}

[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public async Task IncludeSubdirectories_SetToTrue_Changed_ArgsNameShouldContainRelativePath(
bool watchRootedPath,
string baseDirectory,
string subdirectoryName,
string subSubdirectoryName,
string fileName
)
{
// Arrange
FileSystem.Initialize().WithSubdirectory(baseDirectory)
.Initialized(s => s.WithSubdirectory(subdirectoryName)
.Initialized(ss => ss.WithSubdirectory(subSubdirectoryName))
);

string filePath = FileSystem.Path.Combine(
baseDirectory, subdirectoryName, subSubdirectoryName, fileName
);

string expectedFileName = FileSystem.Path.Combine(
subdirectoryName, subSubdirectoryName, fileName
);

string watchPath = watchRootedPath
? FileSystem.Path.Combine(FileSystem.Directory.GetCurrentDirectory(), baseDirectory)
: baseDirectory;

using ManualResetEventSlim changedMre = new();
FileSystemEventArgs? changedArgs = null;

using IFileSystemWatcher fileSystemWatcher = FileSystem.FileSystemWatcher.New(watchPath);

fileSystemWatcher.Changed += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
Expand All @@ -200,13 +239,68 @@ string fileName
// Ignore any ObjectDisposedException
}
};

fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;

// Act

FileSystem.File.Create(filePath).Dispose();
FileSystem.File.WriteAllText(filePath, "Hello World!");

// Assert

await That(changedMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();

await That(changedArgs).IsNotNull().And
.Satisfies(args => string.Equals(args?.Name, expectedFileName, StringComparison.Ordinal)
);
}

[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public async Task IncludeSubdirectories_SetToTrue_Renamed_ArgsNameShouldContainRelativePath(
bool watchRootedPath,
string baseDirectory,
string subdirectoryName,
string subSubdirectoryName,
string fileName
)
{
// Arrange
FileSystem.Initialize().WithSubdirectory(baseDirectory)
.Initialized(s => s.WithSubdirectory(subdirectoryName)
.Initialized(ss => ss.WithSubdirectory(subSubdirectoryName))
);

string filePath = FileSystem.Path.Combine(
baseDirectory, subdirectoryName, subSubdirectoryName, fileName
);

string newFilePath = filePath + ".new";

string expectedFileName = FileSystem.Path.Combine(
subdirectoryName, subSubdirectoryName, fileName
);

string expectedNewFileName = expectedFileName + ".new";

string watchPath = watchRootedPath
? FileSystem.Path.Combine(FileSystem.Directory.GetCurrentDirectory(), baseDirectory)
: baseDirectory;

using ManualResetEventSlim renamedMre = new();
RenamedEventArgs? renamedArgs = null;

using IFileSystemWatcher fileSystemWatcher = FileSystem.FileSystemWatcher.New(watchPath);

fileSystemWatcher.Renamed += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
renamedArgs ??= eventArgs;
renamedArgs = eventArgs;
renamedMre.Set();
}
catch (ObjectDisposedException)
Expand All @@ -215,6 +309,62 @@ string fileName
}
};

fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;

// Act

FileSystem.File.Create(filePath).Dispose();
FileSystem.File.Move(filePath, newFilePath);

// Assert

await That(renamedMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();

await That(renamedArgs).IsNotNull().And
.Satisfies(args => string.Equals(
args?.Name, expectedNewFileName, StringComparison.Ordinal
)
).And.Satisfies(args => string.Equals(
args?.OldName, expectedFileName, StringComparison.Ordinal
)
);
}

[Theory]
[InlineAutoData(true)]
[InlineAutoData(false)]
public async Task IncludeSubdirectories_SetToTrue_Deleted_ArgsNameShouldContainRelativePath(
bool watchRootedPath,
string baseDirectory,
string subdirectoryName,
string subSubdirectoryName,
string fileName
)
{
// Arrange
FileSystem.Initialize().WithSubdirectory(baseDirectory)
.Initialized(s => s.WithSubdirectory(subdirectoryName)
.Initialized(ss => ss.WithSubdirectory(subSubdirectoryName))
);

string filePath = FileSystem.Path.Combine(
baseDirectory, subdirectoryName, subSubdirectoryName, fileName
);

string expectedFileName = FileSystem.Path.Combine(
subdirectoryName, subSubdirectoryName, fileName
);

string watchPath = watchRootedPath
? FileSystem.Path.Combine(FileSystem.Directory.GetCurrentDirectory(), baseDirectory)
: baseDirectory;

using ManualResetEventSlim deletedMre = new();
FileSystemEventArgs? deletedArgs = null;

using IFileSystemWatcher fileSystemWatcher = FileSystem.FileSystemWatcher.New(watchPath);

fileSystemWatcher.Deleted += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
Expand All @@ -235,36 +385,14 @@ string fileName
// Act

FileSystem.File.Create(filePath).Dispose();
FileSystem.File.WriteAllText(filePath, "Hello World!");
FileSystem.File.Move(filePath, newFilePath);
FileSystem.File.Delete(newFilePath);
FileSystem.File.Delete(filePath);

// Assert

await That(createdMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();
await That(changedMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();
await That(renamedMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();
await That(deletedMre.Wait(ExpectTimeout, TestContext.Current.CancellationToken)).IsTrue();

await That(createdArgs).IsNotNull().And
.Satisfies(args => string.Equals(args?.Name, expectedFileName, StringComparison.Ordinal)
);

await That(changedArgs).IsNotNull().And
.Satisfies(args => string.Equals(args?.Name, expectedFileName, StringComparison.Ordinal)
);

await That(renamedArgs).IsNotNull().And
.Satisfies(args => string.Equals(
args?.Name, expectedNewFileName, StringComparison.Ordinal
)
).And.Satisfies(args => string.Equals(
args?.OldName, expectedFileName, StringComparison.Ordinal
)
);

await That(deletedArgs).IsNotNull().And.Satisfies(args => string.Equals(
args?.Name, expectedNewFileName,
args?.Name, expectedFileName,
StringComparison.Ordinal
)
);
Expand Down
Loading