Files
dotnet-runtime/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Changed.cs
T
Ian Hays febb25693e FileSystemWatcher test overhaul
Reworks the FSW tests to be 100% passing.
- Adds path validation to most tests.
- Adds a Retry mechanism to handle expected intermittent failures due to untrustworthy underlying API calls.
- Adds more test cases to NotifyFilter and splits existing tests into separate dir/file tests to cover either behavior.
- Enable test parallelization


Commit migrated from https://github.com/dotnet/corefx/commit/28d10fa256f40c3d37a8cb82d96a1e5520df03d2
2016-05-09 11:24:59 -07:00

83 lines
3.7 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using Xunit;
namespace System.IO.Tests
{
public class Directory_Changed_Tests : FileSystemWatcherTest
{
[Fact]
public void FileSystemWatcher_Directory_Changed_LastWrite()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(testDirectory.Path, Path.GetFileName(dir.Path)))
{
Action action = () => Directory.SetLastWriteTime(dir.Path, DateTime.Now + TimeSpan.FromSeconds(10));
WatcherChangeTypes expected = WatcherChangeTypes.Changed;
ExpectEvent(watcher, expected, action, expectedPath: dir.Path);
}
}
[Fact]
public void FileSystemWatcher_Directory_Changed_WatchedFolder()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
Action action = () => Directory.SetLastWriteTime(dir.Path, DateTime.Now + TimeSpan.FromSeconds(10));
ExpectEvent(watcher, 0, action, expectedPath: dir.Path);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void FileSystemWatcher_Directory_Changed_Nested(bool includeSubdirectories)
{
using (var dir = new TempDirectory(GetTestFilePath()))
using (var firstDir = new TempDirectory(Path.Combine(dir.Path, "dir1")))
using (var nestedDir = new TempDirectory(Path.Combine(firstDir.Path, "nested")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
watcher.IncludeSubdirectories = includeSubdirectories;
watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.Attributes;
var attributes = File.GetAttributes(nestedDir.Path);
Action action = () => File.SetAttributes(nestedDir.Path, attributes | FileAttributes.ReadOnly);
Action cleanup = () => File.SetAttributes(nestedDir.Path, attributes);
WatcherChangeTypes expected = includeSubdirectories ? WatcherChangeTypes.Changed : 0;
ExpectEvent(watcher, expected, action, cleanup, nestedDir.Path);
}
}
[ConditionalFact(nameof(CanCreateSymbolicLinks))]
public void FileSystemWatcher_Directory_Changed_SymLink()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var tempDir = new TempDirectory(Path.Combine(testDirectory.Path, "tempDir")))
using (var file = new TempFile(Path.Combine(tempDir.Path, "test")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
// Setup the watcher
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.IncludeSubdirectories = true;
Assert.True(CreateSymLink(tempDir.Path, Path.Combine(dir.Path, "link"), true));
Action action = () => File.AppendAllText(file.Path, "longtext");
Action cleanup = () => File.AppendAllText(file.Path, "short");
ExpectEvent(watcher, 0, action, cleanup, dir.Path);
}
}
}
}