Skip to content
Permalink
Browse files

DirectoryWatcherBase - UpdateRelevantFileOneSubscription

  • Loading branch information
bobbymcr committed Feb 10, 2020
1 parent 862304b commit f5f83fff2b8d066fc35206b5695f055b8333ee4c
@@ -9,16 +9,25 @@ namespace DirectoryWatcherSample

public abstract class DirectoryWatcherBase : IDisposable
{
private readonly string path;

private string file;
private Action<FileInfo> onUpdate;

protected DirectoryWatcherBase(DirectoryInfo path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

this.path = path.FullName;
}

public void Subscribe(string file, Action<FileInfo> onUpdate)
{
this.file = Path.Combine(this.path, file);
this.onUpdate = onUpdate;
}

public void Dispose()
@@ -30,5 +39,13 @@ public void Dispose()
protected virtual void Dispose(bool disposing)
{
}

protected void OnUpdated(string fullPath)
{
if (this.file == fullPath)
{
this.onUpdate(new FileInfo(fullPath));
}
}
}
}
@@ -56,16 +56,27 @@ public void UpdateIrrelevantFileOneSubscription()
updates.Should().BeEmpty();
}

[TestMethod]
public void UpdateRelevantFileOneSubscription()
{
List<string> updates = new List<string>();
FakeDirectoryWatcher watcher = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
DirectoryWatcherBase watcherBase = watcher;
watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));

watcher.Update(@"X:\root\file1.txt");

updates.Should().ContainSingle().Which.Should().Be(@"X:\root\file1.txt");
}

private sealed class FakeDirectoryWatcher : DirectoryWatcherBase
{
public FakeDirectoryWatcher(DirectoryInfo path)
: base(path)
{
}

public void Update(string fullPath)
{
}
public void Update(string fullPath) => this.OnUpdated(fullPath);
}
}
}

0 comments on commit f5f83ff

Please sign in to comment.
You can’t perform that action at this time.