public void TempFolderStartsInitiallyEmpty() { using (var scenario = new DisposableFileSystem()) { var contents = scenario.DirectoryInfo.EnumerateFileSystemInfos(); Assert.Equal(Path.GetFileName(scenario.RootPath), scenario.DirectoryInfo.Name); Assert.Equal(scenario.RootPath, scenario.DirectoryInfo.FullName); Assert.Equal(0, contents.Count()); } }
public void FilesAreEnumerated() { using (var scenario = new DisposableFileSystem() .CreateFile("alpha.txt")) { var contents = new DirectoryInfoWrapper(scenario.DirectoryInfo).EnumerateFileSystemInfos(); var alphaTxt = contents.OfType<FileInfoBase>().Single(); Assert.Equal(1, contents.Count()); Assert.Equal("alpha.txt", alphaTxt.Name); } }
public void FoldersAreEnumerated() { using (var scenario = new DisposableFileSystem() .CreateFolder("beta")) { var contents1 = new DirectoryInfoWrapper(scenario.DirectoryInfo).EnumerateFileSystemInfos(); var beta = contents1.OfType<DirectoryInfoBase>().Single(); var contents2 = beta.EnumerateFileSystemInfos(); Assert.Equal(1, contents1.Count()); Assert.Equal("beta", beta.Name); Assert.Equal(0, contents2.Count()); } }
public void GetFileInfoReturnsNotFoundFileInfoForFileNameStartingWithPeriod() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileName = "." + Guid.NewGuid().ToString(); var filePath = Path.Combine(root.RootPath, fileName); var info = provider.GetFileInfo(fileName); Assert.IsType(typeof(NotFoundFileInfo), info); } } }
public void SubFoldersAreEnumerated() { using (var scenario = new DisposableFileSystem() .CreateFolder("beta") .CreateFile(Path.Combine("beta", "alpha.txt"))) { var contents1 = new DirectoryInfoWrapper(scenario.DirectoryInfo).EnumerateFileSystemInfos(); var beta = contents1.OfType<DirectoryInfoBase>().Single(); var contents2 = beta.EnumerateFileSystemInfos(); var alphaTxt = contents2.OfType<FileInfoBase>().Single(); Assert.Equal(1, contents1.Count()); Assert.Equal("beta", beta.Name); Assert.Equal(1, contents2.Count()); Assert.Equal("alpha.txt", alphaTxt.Name); } }
public void GetFileInfoReturnsNotFoundFileInfoForHiddenFile() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileName = Guid.NewGuid().ToString(); var filePath = Path.Combine(root.RootPath, fileName); File.Create(filePath); var fileInfo = new FileInfo(filePath); File.SetAttributes(filePath, fileInfo.Attributes | FileAttributes.Hidden); var info = provider.GetFileInfo(fileName); Assert.IsType(typeof(NotFoundFileInfo), info); } } }
public void GetDirectoryCanTakeDotDot() { using (var scenario = new DisposableFileSystem() .CreateFolder("gamma") .CreateFolder("beta") .CreateFile(Path.Combine("beta", "alpha.txt"))) { var gamma = scenario.DirectoryInfo.GetDirectory("gamma"); var dotdot = gamma.GetDirectory(".."); var contents1 = dotdot.EnumerateFileSystemInfos(); var beta = dotdot.GetDirectory("beta"); var contents2 = beta.EnumerateFileSystemInfos(); var alphaTxt = contents2.OfType<FileInfoBase>().Single(); Assert.Equal("..", dotdot.Name); Assert.Equal(2, contents1.Count()); Assert.Equal("beta", beta.Name); Assert.Equal(1, contents2.Count()); Assert.Equal("alpha.txt", alphaTxt.Name); } }
public void GetDirectoryContentsDoesNotReturnFileInfoForFileNameStartingWithPeriod() { using (var root = new DisposableFileSystem()) { var directoryName = Guid.NewGuid().ToString(); var directoryPath = Path.Combine(root.RootPath, directoryName); Directory.CreateDirectory(directoryPath); var fileName = "." + Guid.NewGuid().ToString(); var filePath = Path.Combine(directoryPath, fileName); File.Create(filePath); using (var provider = new PhysicalFileProvider(root.RootPath)) { var contents = provider.GetDirectoryContents(directoryName); Assert.Empty(contents); } } }
public async Task TokenNotFiredForFileNameStartingWithPeriod() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var fileName = "." + Guid.NewGuid().ToString(); var token = provider.Watch(Path.GetFileName(fileName)); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.False(token.HasChanged); } } } } }
public async Task TokensFiredForNewDirectoryContentsOnRename() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var oldDirectoryName = Guid.NewGuid().ToString(); var oldSubDirectoryName = Guid.NewGuid().ToString(); var oldSubDirectoryPath = Path.Combine(oldDirectoryName, oldSubDirectoryName); var oldFileName = Guid.NewGuid().ToString(); var oldFilePath = Path.Combine(oldDirectoryName, oldSubDirectoryName, oldFileName); var newDirectoryName = Guid.NewGuid().ToString(); var newSubDirectoryName = Guid.NewGuid().ToString(); var newSubDirectoryPath = Path.Combine(newDirectoryName, newSubDirectoryName); var newFileName = Guid.NewGuid().ToString(); var newFilePath = Path.Combine(newDirectoryName, newSubDirectoryName, newFileName); Directory.CreateDirectory(Path.Combine(root.RootPath, newDirectoryName)); Directory.CreateDirectory(Path.Combine(root.RootPath, newDirectoryName, newSubDirectoryName)); File.Create(Path.Combine(root.RootPath, newDirectoryName, newSubDirectoryName, newFileName)); await Task.Delay(WaitTimeForTokenToFire); var oldDirectoryToken = provider.Watch(oldDirectoryName); var oldSubDirectoryToken = provider.Watch(oldSubDirectoryPath); var oldFileToken = provider.Watch(oldFilePath); var newDirectoryToken = provider.Watch(newDirectoryName); var newSubDirectoryToken = provider.Watch(newSubDirectoryPath); var newFileToken = provider.Watch(newFilePath); await Task.Delay(WaitTimeForTokenToFire); Assert.False(oldDirectoryToken.HasChanged); Assert.False(oldSubDirectoryToken.HasChanged); Assert.False(oldFileToken.HasChanged); Assert.False(newDirectoryToken.HasChanged); Assert.False(newSubDirectoryToken.HasChanged); Assert.False(newFileToken.HasChanged); fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, newDirectoryName, oldDirectoryName)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(oldDirectoryToken.HasChanged); Assert.False(oldSubDirectoryToken.HasChanged); Assert.False(oldFileToken.HasChanged); Assert.True(newDirectoryToken.HasChanged); Assert.True(newSubDirectoryToken.HasChanged); Assert.True(newFileToken.HasChanged); } } } } }
public async Task CorrectTokensFiredForMultipleFiles() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var fileName1 = Guid.NewGuid().ToString(); var token1 = provider.Watch(fileName1); var fileName2 = Guid.NewGuid().ToString(); var token2 = provider.Watch(fileName2); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName1)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token1.HasChanged); Assert.False(token2.HasChanged); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName2)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token2.HasChanged); } } } } }
public async Task TokensFiredOnFileDeleted() { using (var root = new DisposableFileSystem()) { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(root.RootPath, fileName); using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var token = provider.Watch(fileName); Assert.NotNull(token); Assert.False(token.HasChanged); Assert.True(token.ActiveChangeCallbacks); fileSystemWatcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, root.RootPath, fileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token.HasChanged); } } } } }
public void GetDirectoryContentsDoesNotReturnFileInfoForSystemFile() { using (var root = new DisposableFileSystem()) { var directoryName = Guid.NewGuid().ToString(); var directoryPath = Path.Combine(root.RootPath, directoryName); Directory.CreateDirectory(directoryPath); var fileName = Guid.NewGuid().ToString(); var filePath = Path.Combine(directoryPath, fileName); File.Create(filePath); var fileInfo = new FileInfo(filePath); File.SetAttributes(filePath, fileInfo.Attributes | FileAttributes.System); using (var provider = new PhysicalFileProvider(root.RootPath)) { var contents = provider.GetDirectoryContents(directoryName); Assert.Empty(contents); } } }
public FunctionalTests() { _context = CreateContext(); }
public async Task TokenFiredForFilesUnderPathEndingWithSlash() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var directoryName = Guid.NewGuid().ToString(); var token = provider.Watch(directoryName + Path.DirectorySeparatorChar); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, Path.Combine(root.RootPath, directoryName), Guid.NewGuid().ToString())); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token.HasChanged); } } } } }
public async Task TokenFiredOnDeletion() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var name = Guid.NewGuid().ToString(); var token = provider.Watch(name); fileSystemWatcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, root.RootPath, name)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token.HasChanged); } } } } }
public void NoopChangeTokenForAbsolutePathFilters() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var path = Path.Combine(root.RootPath, Guid.NewGuid().ToString()); var token = provider.Watch(path); Assert.Same(NoopChangeToken.Singleton, token); } } }
public void TokenForWhitespaceFilters() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var token = provider.Watch(" "); Assert.False(token.HasChanged); Assert.True(token.ActiveChangeCallbacks); } } }
public void NoopChangeTokenForNullFilter() { using (var root = new DisposableFileSystem()) { using (var provider = new PhysicalFileProvider(root.RootPath)) { var token = provider.Watch(null); Assert.Same(NoopChangeToken.Singleton, token); } } }
public async Task TokenNotAffectedByExceptions() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var fileName = Guid.NewGuid().ToString(); var token = provider.Watch(fileName); token.RegisterChangeCallback(_ => { throw new Exception(); }, null); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token.HasChanged); } } } } }
public async Task TokensNotFiredForHiddenAndSystemFiles() { using (var root = new DisposableFileSystem()) { var hiddenFileName = Guid.NewGuid().ToString(); var hiddenFilePath = Path.Combine(root.RootPath, hiddenFileName); File.Create(hiddenFilePath); var fileInfo = new FileInfo(hiddenFilePath); File.SetAttributes(hiddenFilePath, fileInfo.Attributes | FileAttributes.Hidden); var systemFileName = Guid.NewGuid().ToString(); var systemFilePath = Path.Combine(root.RootPath, systemFileName); File.Create(systemFilePath); fileInfo = new FileInfo(systemFilePath); File.SetAttributes(systemFilePath, fileInfo.Attributes | FileAttributes.System); using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var hiddenFiletoken = provider.Watch(Path.GetFileName(hiddenFileName)); var systemFiletoken = provider.Watch(Path.GetFileName(systemFileName)); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, hiddenFileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.False(hiddenFiletoken.HasChanged); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, systemFileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.False(systemFiletoken.HasChanged); } } } } }
public async Task TokensFiredForAllEntriesOnError() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var token1 = provider.Watch(Guid.NewGuid().ToString()); var token2 = provider.Watch(Guid.NewGuid().ToString()); var token3 = provider.Watch(Guid.NewGuid().ToString()); fileSystemWatcher.CallOnError(new ErrorEventArgs(new Exception())); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token1.HasChanged); Assert.True(token2.HasChanged); Assert.True(token3.HasChanged); } } } } }
public void TokenIsSameForSamePath() { using (var root = new DisposableFileSystem()) { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(root.RootPath, fileName); using (var provider = new PhysicalFileProvider(root.RootPath)) { var fileInfo = provider.GetFileInfo(fileName); var token1 = provider.Watch(fileName); var token2 = provider.Watch(fileName); Assert.NotNull(token1); Assert.NotNull(token2); Assert.Equal(token2, token1); } } }
private DisposableFileSystem CreateContext() { var context = new DisposableFileSystem(); context.CreateFiles( "src/project/source1.cs", "src/project/sub/source2.cs", "src/project/sub/source3.cs", "src/project/sub2/source4.cs", "src/project/sub2/source5.cs", "src/project/compiler/preprocess/preprocess-source1.cs", "src/project/compiler/preprocess/sub/preprocess-source2.cs", "src/project/compiler/preprocess/sub/sub/preprocess-source3.cs", "src/project/compiler/preprocess/sub/sub/preprocess-source3.txt", "src/project/compiler/shared/shared1.cs", "src/project/compiler/shared/shared1.txt", "src/project/compiler/shared/sub/shared2.cs", "src/project/compiler/shared/sub/shared2.txt", "src/project/compiler/shared/sub/sub/sharedsub.cs", "src/project/compiler/resources/resource.res", "src/project/compiler/resources/sub/resource2.res", "src/project/compiler/resources/sub/sub/resource3.res", "src/project/content1.txt", "src/project/obj/object.o", "src/project/bin/object", "src/project/.hidden/file1.hid", "src/project/.hidden/sub/file2.hid", "src/project2/source1.cs", "src/project2/sub/source2.cs", "src/project2/sub/source3.cs", "src/project2/sub2/source4.cs", "src/project2/sub2/source5.cs", "src/project2/compiler/preprocess/preprocess-source1.cs", "src/project2/compiler/preprocess/sub/preprocess-source2.cs", "src/project2/compiler/preprocess/sub/sub/preprocess-source3.cs", "src/project2/compiler/preprocess/sub/sub/preprocess-source3.txt", "src/project2/compiler/shared/shared1.cs", "src/project2/compiler/shared/shared1.txt", "src/project2/compiler/shared/sub/shared2.cs", "src/project2/compiler/shared/sub/shared2.txt", "src/project2/compiler/shared/sub/sub/sharedsub.cs", "src/project2/compiler/resources/resource.res", "src/project2/compiler/resources/sub/resource2.res", "src/project2/compiler/resources/sub/sub/resource3.res", "src/project2/content1.txt", "src/project2/obj/object.o", "src/project2/bin/object", "lib/source6.cs", "lib/sub3/source7.cs", "lib/sub4/source8.cs", "res/resource1.text", "res/resource2.text", "res/resource3.text", ".hidden/file1.hid", ".hidden/sub/file2.hid"); return context; }
public async Task TokensFiredForRegularExpressionPatterns() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var token1 = provider.Watch("**/*"); var pattern1tokenCount = 0; token1.RegisterChangeCallback(_ => { pattern1tokenCount++; }, null); var token2 = provider.Watch("*.cshtml"); var pattern2tokenCount = 0; token2.RegisterChangeCallback(_ => { pattern2tokenCount++; }, null); var fileName = Guid.NewGuid().ToString(); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName + ".cshtml")); await Task.Delay(WaitTimeForTokenToFire); Assert.Equal(1, pattern1tokenCount); Assert.Equal(1, pattern2tokenCount); token1 = provider.Watch("**/*"); token1.RegisterChangeCallback(_ => { pattern1tokenCount++; }, null); fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName + ".txt")); await Task.Delay(WaitTimeForTokenToFire); Assert.Equal(2, pattern1tokenCount); Assert.Equal(1, pattern2tokenCount); } } } } }
public void GetDirectoryContentsReturnsRootDirectoryContentsForEmptyPath() { using (var root = new DisposableFileSystem()) { File.Create(Path.Combine(root.RootPath, "File" + Guid.NewGuid().ToString())); Directory.CreateDirectory(Path.Combine(root.RootPath, "Dir" + Guid.NewGuid().ToString())); using (var provider = new PhysicalFileProvider(root.RootPath)) { var contents = provider.GetDirectoryContents(string.Empty); Assert.Collection(contents.OrderBy(c => c.Name), item => Assert.IsType<PhysicalDirectoryInfo>(item), item => Assert.IsType<PhysicalFileInfo>(item)); } } }
public async Task TokenFiredForRegularExpressionPatternsPointingToSubDirectory() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var subDirectoryName = Guid.NewGuid().ToString(); var pattern = string.Format(Path.Combine(subDirectoryName, "**", "*.cshtml")); var token = provider.Watch(pattern); var subSubDirectoryName = Guid.NewGuid().ToString(); var fileName = Guid.NewGuid().ToString() + ".cshtml"; fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, Path.Combine(root.RootPath, subDirectoryName, subSubDirectoryName), fileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(token.HasChanged); } } } } }
public async Task TokensFiredForOldAndNewNamesOnRename() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var oldFileName = Guid.NewGuid().ToString(); var oldToken = provider.Watch(oldFileName); var newFileName = Guid.NewGuid().ToString(); var newToken = provider.Watch(newFileName); fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, newFileName, oldFileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.True(oldToken.HasChanged); Assert.True(newToken.HasChanged); } } } } }
public async Task FileChangeTokenNotNotifiedAfterExpiry() { using (var root = new DisposableFileSystem()) { using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath)) { using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher)) { using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher)) { var fileName = Guid.NewGuid().ToString(); var changeToken = provider.Watch(fileName); var invocationCount = 0; changeToken.RegisterChangeCallback(_ => { invocationCount++; }, null); // Callback expected. fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName)); await Task.Delay(WaitTimeForTokenToFire); // Callback not expected. fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName)); await Task.Delay(WaitTimeForTokenToFire); Assert.Equal(1, invocationCount); } } } } }