Skip to content

Commit

Permalink
fix: also detect renamed files (#38)
Browse files Browse the repository at this point in the history
This is needed because some Software, i.e. Chrome, downloads a file to the desired directory with a name like "XXX.csv.crdownload"
and renames it to "XXX.csv" after the download was finished. The service did not detect such actions before

Closes #33
  • Loading branch information
wgnf authored Nov 11, 2021
1 parent 2fa1c39 commit 9d707cb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void Dispose_Disposes_FileSystemWatcher()
sut.Dispose();

fileSystemWatcher.VerifyRemove(watcher => watcher.Created -= It.IsAny<FileSystemEventHandler>());
fileSystemWatcher.VerifyRemove(watcher => watcher.Renamed -= It.IsAny<RenamedEventHandler>());
}

[Fact]
Expand Down Expand Up @@ -119,6 +120,44 @@ public void Add_Created_Files_To_Pool()
job.TargetFile == fileInfoMock.Object)), Times.Once);
}

[Fact]
public void Not_Add_Renamed_Files_To_Pool_When_Wrong_Change_Type()
{
var (context, fileSystemWatcher) = CreateContext();
_ = context.Build();

fileSystemWatcher
.Raise(watcher => watcher.Renamed += null,
new RenamedEventArgs(WatcherChangeTypes.Deleted, "something", "some name", "some old name"));

context
.For<IJobPool>()
.Verify(pool => pool.Add(It.IsAny<IJob>()), Times.Never);
}

[Fact]
public void Add_Renamed_Files_To_Pool()
{
var (context, fileSystemWatcher) = CreateContext();
_ = context.Build();

var fileInfoMock = new Mock<IFileInfo>();

context
.For<IFileSystem>()
.Setup(fileSystem => fileSystem.FileInfo.FromFileName(It.IsAny<string>()))
.Returns(fileInfoMock.Object);

fileSystemWatcher
.Raise(watcher => watcher.Renamed += null,
new RenamedEventArgs(WatcherChangeTypes.All, "something", "some file", "some old name"));

context
.For<IJobPool>()
.Verify(pool => pool.Add(It.Is<CsvProcessJob>(job =>
job.TargetFile == fileInfoMock.Object)), Times.Once);
}

private static (ArrangeContext<CsvFileWatcherBackgroundService>, Mock<IFileSystemWatcher>) CreateContext()
{
var options = new CsvProcessorOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public CsvFileWatcherBackgroundService(
_fileSystemWatcher.Filter = "*.csv";

_fileSystemWatcher.Created += OnFileCreated;
_fileSystemWatcher.Renamed += OnFileRenamed;
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -60,6 +61,7 @@ public override void Dispose()
base.Dispose();

_fileSystemWatcher.Created -= OnFileCreated;
_fileSystemWatcher.Renamed -= OnFileRenamed;
_fileSystemWatcher.Dispose();
}

Expand All @@ -68,11 +70,24 @@ private void OnFileCreated(object sender, FileSystemEventArgs eventArgs)
if (!eventArgs.ChangeType.HasFlag(WatcherChangeTypes.Created)) return;

_logger.LogDebug("FileWatcher: File Created: {File}", eventArgs.FullPath);
AddJobForFile(eventArgs.FullPath);
}

var file = _fileSystem.FileInfo.FromFileName(eventArgs.FullPath);
private void OnFileRenamed(object sender, RenamedEventArgs eventArgs)
{
if (!eventArgs.ChangeType.HasFlag(WatcherChangeTypes.Renamed)) return;

_logger.LogDebug("FileWatcher: File Renamed: from {OldFile} to {File}",
eventArgs.OldFullPath, eventArgs.FullPath);
AddJobForFile(eventArgs.FullPath);
}

private void AddJobForFile(string filePath)
{
var file = _fileSystem.FileInfo.FromFileName(filePath);
var job = new CsvProcessJob(file);

_logger.LogDebug("FileWatcher: Added CsvProcessJob #{JobId}", job.Id);
_logger.LogDebug("FileWatcher: Added CsvProcessJob #{JobId} for {File}", job.Id, filePath);

_jobPool.Add(job);
}
Expand Down

0 comments on commit 9d707cb

Please sign in to comment.