Skip to content

Commit

Permalink
Move exception to setter for UsePollingFileWatcher (dotnet/extensions…
Browse files Browse the repository at this point in the history
…#2099)

Commit migrated from dotnet/extensions@62536db
  • Loading branch information
maryamariyan authored and BrennanConroy committed Aug 22, 2019
1 parent c97d65f commit 63cc57a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,22 @@ public bool UsePollingFileWatcher
{
if (_fileWatcher != null)
{
throw new InvalidOperationException($"Cannot modify {nameof(UsePollingFileWatcher)} once file watcher has been initialized.");
return false;
}

if (_usePollingFileWatcher == null)
{
ReadPollingEnvironmentVariables();
}

return _usePollingFileWatcher.Value;
return _usePollingFileWatcher ?? false;
}
set
{
if (_fileWatcher != null)
{
throw new InvalidOperationException($"Cannot modify {nameof(UsePollingFileWatcher)} once file watcher has been initialized.");
}
_usePollingFileWatcher = value;
}
set => _usePollingFileWatcher = value;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,63 @@ public async Task WildCardToken_RaisesEventsWhenFileSystemWatcherDoesNotFire()
}
}

[Fact]
public void UsePollingFileWatcher_FileWatcherNull_SetsSuccessfully()
{
// Arrange
using (var root = new DisposableFileSystem())
{
using (var provider = new PhysicalFileProvider(root.RootPath))
{
Assert.False(provider.UsePollingFileWatcher);

// Act / Assert
provider.UsePollingFileWatcher = true;
Assert.True(provider.UsePollingFileWatcher);
}
}
}

[Fact]
public void UsePollingFileWatcher_FileWatcherNotNull_SetterThrows()
{
// Arrange
using (var root = new DisposableFileSystem())
{
using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
{
using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
{
using (var provider = new PhysicalFileProvider(root.RootPath) { FileWatcher = physicalFilesWatcher })
{
// Act / Assert
Assert.Throws<InvalidOperationException>(() => { provider.UsePollingFileWatcher = true; });
}
}
}
}
}

[Fact]
public void UsePollingFileWatcher_FileWatcherNotNull_ReturnsFalse()
{
// Arrange
using (var root = new DisposableFileSystem())
{
using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
{
using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
{
using (var provider = new PhysicalFileProvider(root.RootPath) { FileWatcher = physicalFilesWatcher })
{
// Act / Assert
Assert.False(provider.UsePollingFileWatcher);
}
}
}
}
}

[Fact]
public void CreateFileWatcher_CreatesWatcherWithPollingAndActiveFlags()
{
Expand Down

0 comments on commit 63cc57a

Please sign in to comment.