public void GetFileInfoReturnsNotFoundFileInfoForRelativePathAboveRootPath() { using (var provider = new PhysicalFileProvider(Path.GetTempPath())) { var info = provider.GetFileInfo(Path.Combine("..", Guid.NewGuid().ToString())); Assert.IsType(typeof(NotFoundFileInfo), info); } }
public void GetFileInfoReturnsNotFoundFileInfoForEmptyPath() { using (var provider = new PhysicalFileProvider(Path.GetTempPath())) { var info = provider.GetFileInfo(string.Empty); Assert.IsType(typeof(NotFoundFileInfo), info); } }
private static void UseDocumentation(IApplicationBuilder app, IApplicationEnvironment appEnv) { var documentationFilesProvider = new PhysicalFileProvider(appEnv.ApplicationBasePath); app.UseDocumentation(new DocumentationOptions() { DefaultFileName = "index", RequestPath = "/docs", NotFoundHtmlFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\NotFound.html"), LayoutFile = documentationFilesProvider.GetFileInfo("DocumentationTemplates\\Layout.html") }); }
public void ExistingFilesReturnTrue() { var provider = new PhysicalFileProvider(Environment.CurrentDirectory); var info = provider.GetFileInfo("File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); info = provider.GetFileInfo("/File.txt"); info.ShouldNotBe(null); info.Exists.ShouldBe(true); }
public void ExistingFilesReturnTrue() { var provider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); var info = provider.GetFileInfo("File.txt"); Assert.NotNull(info); Assert.True(info.Exists); info = provider.GetFileInfo("/File.txt"); Assert.NotNull(info); Assert.True(info.Exists); }
public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl) { using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir))) { var server = StaticFilesTestServer.Create(app => app.UseStaticFiles(options => { options.RequestPath = new PathString(baseUrl); options.FileProvider = fileProvider; })); var response = await server.CreateRequest(requestUrl).GetAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } }
public void Configure(IApplicationBuilder app, IApplicationEnvironment environemnt) { app.UseIISPlatformHandler(); app.UseFileServer(); var provider = new PhysicalFileProvider(Path.Combine(environemnt.ApplicationBasePath, "node_modules")); var options = new FileServerOptions(); options.RequestPath = "/node_modules"; options.StaticFileOptions.FileProvider = provider; options.EnableDirectoryBrowsing = true; app.UseFileServer(options); app.UseMvc(); }
public void DisplaysSourceCodeLines_ForAbsolutePaths(string absoluteFilePath) { // Arrange var rootPath = Directory.GetCurrentDirectory(); // PhysicalFileProvider handles only relative paths but we fall back to work with absolute paths too var provider = new PhysicalFileProvider(rootPath); // Act var middleware = GetErrorPageMiddleware(provider); var stackFrame = middleware.GetStackFrame("func1", absoluteFilePath, lineNumber: 10); // Assert // Lines 4-16 (inclusive) is the code block Assert.Equal(4, stackFrame.PreContextLine); Assert.Equal(GetCodeLines(4, 9), stackFrame.PreContextCode); Assert.Equal(GetCodeLines(10, 10), stackFrame.ContextCode); Assert.Equal(GetCodeLines(11, 16), stackFrame.PostContextCode); }
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 async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl) { using (var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), baseDir))) { var server = StaticFilesTestServer.Create(app => { app.UseDefaultFiles(options => { options.RequestPath = new PathString(baseUrl); options.FileProvider = fileProvider; }); app.Run(context => context.Response.WriteAsync(context.Request.Path.Value)); }); var response = await server.CreateClient().GetAsync(requestUrl); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(requestUrl, await response.Content.ReadAsStringAsync()); // Should not be modified } }
/// <inheritdoc /> /// <remarks>Pre-compiles all Razor views in the application.</remarks> public virtual void BeforeCompile(IBeforeCompileContext context) { var compilerOptionsProvider = _appServices.GetRequiredService<ICompilerOptionsProvider>(); var loadContextAccessor = _appServices.GetRequiredService<IAssemblyLoadContextAccessor>(); var compilationSettings = GetCompilationSettings(compilerOptionsProvider, context.ProjectContext); var fileProvider = new PhysicalFileProvider(context.ProjectContext.ProjectDirectory); var viewCompiler = new RazorPreCompiler( context, loadContextAccessor, fileProvider, _memoryCache, compilationSettings) { GenerateSymbols = GenerateSymbols }; viewCompiler.CompileViews(); }
private static JSPool.IJsPool CreateJSEngine(IServiceProvider provider) { var ieConfig = new JavaScriptEngineSwitcher.Msie.Configuration.MsieConfiguration { EngineMode = JavaScriptEngineSwitcher.Msie.JsEngineMode.ChakraEdgeJsRt }; var appEnv = provider.GetRequiredService<IApplicationEnvironment>(); var fileProvider = new PhysicalFileProvider(appEnv.ApplicationBasePath); var jsPath = fileProvider.GetFileInfo("wwwroot/js/server.js").PhysicalPath; var poolConfig = new JSPool.JsPoolConfig(); poolConfig.MaxUsagesPerEngine = 20; poolConfig.StartEngines = 2; poolConfig.EngineFactory = () => new JavaScriptEngineSwitcher.Msie.MsieJsEngine(ieConfig); poolConfig.Initializer = engine => InitialiseJSRuntime(jsPath, engine); poolConfig.WatchFiles = new[] { jsPath }; return new JSPool.JsPool(poolConfig); }
public static IConfigurationRoot ReloadOnChanged( this IConfigurationRoot config, string basePath, string filename) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (basePath == null) { throw new ArgumentNullException(nameof(basePath)); } if (filename == null) { throw new ArgumentNullException(nameof(filename)); } var fileProvider = new PhysicalFileProvider(basePath); return ReloadOnChanged(config, fileProvider, filename); }
public void Main(string[] args) { Console.WriteLine("Root path : {0}", _appEnvironment.ApplicationBasePath); Console.WriteLine(); var physicalFileProvider = new PhysicalFileProvider(_appEnvironment.ApplicationBasePath); physicalFileProvider.GetInfo(@"c:\github\kichalla\FileProvidersExample\src\FileProvidersExample\Program.cs"); physicalFileProvider.GetInfo("Program.cs"); physicalFileProvider.GetInfo("/Program.cs"); physicalFileProvider.GetInfo(@"\Program.cs"); physicalFileProvider.GetInfo("FileProvidersExample/Program.cs"); physicalFileProvider.GetInfo("/FileProvidersExample/Program.cs"); physicalFileProvider.GetInfo(@"\FileProvidersExample\Program.cs"); Console.WriteLine("***********************************************************"); var embeddedProvider = new EmbeddedFileProvider(this.GetType().GetTypeInfo().Assembly, "FileProvidersExample.EmbeddedResources"); embeddedProvider.GetInfo("Blah.cshtml"); embeddedProvider.GetInfo("/Views/Home/Index.cshtml"); embeddedProvider.GetInfo("Views/Home/Index.cshtml"); embeddedProvider.GetInfo(@"\Views\Home\Index.cshtml"); embeddedProvider.GetInfo(@"Views\Home\Index.cshtml"); }
public async Task Triggers_With_Regular_Expression_Pointing_To_SubFolder() { var subFolderName = Guid.NewGuid().ToString(); var pattern1 = "**/*"; var pattern2 = string.Format("{0}/**/*.cshtml", subFolderName); var root = Path.GetTempPath(); var fileName = Guid.NewGuid().ToString(); var subFolder = Path.Combine(root, subFolderName); Directory.CreateDirectory(subFolder); int pattern1TriggerCount = 0, pattern2TriggerCount = 0; var provider = new PhysicalFileProvider(root); var trigger1 = provider.Watch(pattern1); trigger1.RegisterExpirationCallback(_ => { pattern1TriggerCount++; }, null); var trigger2 = provider.Watch(pattern2); trigger2.RegisterExpirationCallback(_ => { pattern2TriggerCount++; }, null); File.WriteAllText(Path.Combine(root, fileName + ".cshtml"), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); pattern1TriggerCount.ShouldBe(1); pattern2TriggerCount.ShouldBe(0); trigger1 = provider.Watch(pattern1); trigger1.RegisterExpirationCallback(_ => { pattern1TriggerCount++; }, null); // Register this trigger again. var trigger3 = provider.Watch(pattern2); trigger3.RegisterExpirationCallback(_ => { pattern2TriggerCount++; }, null); trigger3.ShouldBe(trigger2); File.WriteAllText(Path.Combine(subFolder, fileName + ".cshtml"), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); pattern1TriggerCount.ShouldBe(2); pattern2TriggerCount.ShouldBe(2); Directory.Delete(subFolder, true); File.Delete(Path.Combine(root, fileName + ".cshtml")); }
public async Task Triggers_With_Regular_Expression_Filters() { var pattern1 = "**/*"; var pattern2 = "*.cshtml"; var root = Path.GetTempPath(); var fileName = Guid.NewGuid().ToString(); var subFolder = Path.Combine(root, Guid.NewGuid().ToString()); Directory.CreateDirectory(subFolder); int pattern1TriggerCount = 0, pattern2TriggerCount = 0; Action<object> callback1 = _ => { pattern1TriggerCount++; }; Action<object> callback2 = _ => { pattern2TriggerCount++; }; var provider = new PhysicalFileProvider(root); var trigger1 = provider.Watch(pattern1); trigger1.RegisterExpirationCallback(callback1, null); var trigger2 = provider.Watch(pattern2); trigger2.RegisterExpirationCallback(callback2, null); File.WriteAllText(Path.Combine(root, fileName + ".cshtml"), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); pattern1TriggerCount.ShouldBe(1); pattern2TriggerCount.ShouldBe(1); trigger1 = provider.Watch(pattern1); trigger1.RegisterExpirationCallback(callback1, null); trigger2 = provider.Watch(pattern2); trigger2.RegisterExpirationCallback(callback2, null); File.WriteAllText(Path.Combine(subFolder, fileName + ".txt"), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); pattern1TriggerCount.ShouldBe(2); pattern2TriggerCount.ShouldBe(1); Directory.Delete(subFolder, true); File.Delete(Path.Combine(root, fileName + ".cshtml")); }
public async Task Triggers_With_Path_Not_Ending_With_Slash() { var provider = new PhysicalFileProvider(Path.GetTempPath()); string directoryName = Guid.NewGuid().ToString(); string fileName = Guid.NewGuid().ToString(); int triggerCount = 0; // Matches file/directory with this name. var fileTrigger = provider.Watch("/" + directoryName); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName)); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(1); // Matches file/directory with this name. fileTrigger = provider.Watch("/" + fileName); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); File.WriteAllText(Path.Combine(Path.GetTempPath(), fileName), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(2); }
public async Task Triggers_With_Path_Ending_With_Slash() { var provider = new PhysicalFileProvider(Path.GetTempPath()); string fileName = Guid.NewGuid().ToString(); string folderName = Guid.NewGuid().ToString(); int triggerCount = 0; var fileTrigger = provider.Watch("/" + folderName + "/"); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); var folderPath = Path.Combine(Path.GetTempPath(), folderName); Directory.CreateDirectory(folderPath); File.WriteAllText(Path.Combine(folderPath, fileName), "Content"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(1); fileTrigger = provider.Watch("/" + folderName + "/"); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); File.AppendAllText(Path.Combine(folderPath, fileName), "UpdatedContent"); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(2); fileTrigger = provider.Watch("/" + folderName + "/"); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); File.Delete(Path.Combine(folderPath, fileName)); await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(3); }
public async Task Trigger_Fired_For_File_Or_Directory_Create_And_Delete() { var root = Path.GetTempPath(); var provider = new PhysicalFileProvider(root); string fileName = Guid.NewGuid().ToString(); string directoryName = Guid.NewGuid().ToString(); int triggerCount = 0; var fileTrigger = provider.Watch(fileName); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); var directoryTrigger = provider.Watch(directoryName); directoryTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); fileTrigger.ShouldNotBe(directoryTrigger); File.WriteAllText(Path.Combine(root, fileName), "Content"); Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName)); // Wait for triggers to fire. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(2); fileTrigger.IsExpired.ShouldBe(true); directoryTrigger.IsExpired.ShouldBe(true); fileTrigger = provider.Watch(fileName); fileTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); directoryTrigger = provider.Watch(directoryName); directoryTrigger.RegisterExpirationCallback(_ => { triggerCount++; }, null); File.Delete(Path.Combine(root, fileName)); Directory.Delete(Path.Combine(Path.GetTempPath(), directoryName)); // Wait for triggers to fire. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); triggerCount.ShouldBe(4); }
public void Trigger_For_Null_Empty_Whitespace_Filters() { var provider = new PhysicalFileProvider(Path.GetTempPath()); var trigger = provider.Watch(null); trigger.IsExpired.ShouldBe(false); trigger.ActiveExpirationCallbacks.ShouldBe(false); trigger = provider.Watch(string.Empty); trigger.IsExpired.ShouldBe(false); trigger.ActiveExpirationCallbacks.ShouldBe(true); // White space. trigger = provider.Watch(" "); trigger.IsExpired.ShouldBe(false); trigger.ActiveExpirationCallbacks.ShouldBe(true); // Absolute path. trigger = provider.Watch(Path.Combine(Path.GetTempPath() + "filename")); trigger.IsExpired.ShouldBe(false); trigger.ActiveExpirationCallbacks.ShouldBe(false); }
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "OldContent"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var fileInfo = provider.GetFileInfo(fileName); Assert.Equal(new FileInfo(fileInfo.PhysicalPath).Length, fileInfo.Length); Assert.True(fileInfo.Exists); IExpirationTrigger trigger3 = null, trigger4 = null; var trigger1 = provider.Watch(fileName); var trigger2 = provider.Watch(fileName); // Valid trigger1 created. Assert.NotNull(trigger1); Assert.False(trigger1.IsExpired); Assert.True(trigger1.ActiveExpirationCallbacks); // Valid trigger2 created. Assert.NotNull(trigger2); Assert.False(trigger2.IsExpired); Assert.True(trigger2.ActiveExpirationCallbacks); // Trigger is the same for a specific file. Assert.Equal(trigger2, trigger1); trigger1.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger3 = provider.Watch(infoFromState.Name); Assert.NotNull(trigger3); trigger3.RegisterExpirationCallback(_ => { }, null); Assert.False(trigger3.IsExpired); }, state: fileInfo); trigger2.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger4 = provider.Watch(infoFromState.Name); Assert.NotNull(trigger4); trigger4.RegisterExpirationCallback(_ => { }, null); Assert.False(trigger4.IsExpired); }, state: fileInfo); // Write new content. File.WriteAllText(fileLocation, "OldContent + NewContent"); Assert.True(fileInfo.Exists); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); Assert.True(trigger1.IsExpired); Assert.True(trigger2.IsExpired); // Trigger is the same for a specific file. Assert.Equal(trigger4, trigger3); // A new trigger is created. Assert.NotEqual(trigger1, trigger3); // Delete the file and verify file info is updated. File.Delete(fileLocation); fileInfo = provider.GetFileInfo(fileName); Assert.False(fileInfo.Exists); Assert.False(new FileInfo(fileLocation).Exists); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); Assert.True(trigger3.IsExpired); Assert.True(trigger4.IsExpired); }
public async Task Triggers_NotFired_For_FileNames_Starting_With_Period_And_Hidden_Files() { var root = Path.GetTempPath(); var hiddenFileName = Path.Combine(root, Guid.NewGuid().ToString()); File.WriteAllText(hiddenFileName, "Content"); var systemFileName = Path.Combine(root, Guid.NewGuid().ToString()); File.WriteAllText(systemFileName, "Content"); var fileNameStartingWithPeriod = Path.Combine(root, "." + Guid.NewGuid().ToString()); File.WriteAllText(fileNameStartingWithPeriod, "Content"); var fileInfo = new FileInfo(hiddenFileName); File.SetAttributes(hiddenFileName, fileInfo.Attributes | FileAttributes.Hidden); fileInfo = new FileInfo(systemFileName); File.SetAttributes(systemFileName, fileInfo.Attributes | FileAttributes.System); var provider = new PhysicalFileProvider(Path.GetTempPath()); var hiddenFileTrigger = provider.Watch(Path.GetFileName(hiddenFileName)); var triggerFileNameStartingPeriod = provider.Watch(Path.GetFileName(fileNameStartingWithPeriod)); var systemFileTrigger = provider.Watch(Path.GetFileName(systemFileName)); hiddenFileTrigger.IsExpired.ShouldBe(false); triggerFileNameStartingPeriod.IsExpired.ShouldBe(false); systemFileTrigger.IsExpired.ShouldBe(false); File.AppendAllText(hiddenFileName, "Appending text"); File.WriteAllText(fileNameStartingWithPeriod, "Updated Contents"); File.AppendAllText(systemFileName, "Appending text"); // Wait for triggers to fire. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); hiddenFileTrigger.IsExpired.ShouldBe(false); triggerFileNameStartingPeriod.IsExpired.ShouldBe(false); systemFileTrigger.IsExpired.ShouldBe(false); }
public async Task FileTrigger_NotTriggered_After_Expiry() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "Content"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var expirationTrigger = provider.Watch(fileName); int invocationCount = 0; expirationTrigger.RegisterExpirationCallback(_ => { invocationCount++; }, null); // Callback expected for this change. File.AppendAllText(fileLocation, "UpdatedContent1"); // Callback not expected for this change. File.AppendAllText(fileLocation, "UpdatedContent2"); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); invocationCount.ShouldBe(1); File.Delete(fileLocation); }
public async Task Createdtrigger_Same_For_A_File_And_Callsback_AllRegisteredTriggers_OnChange() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "Content"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var count = 10; var tasks = new List<Task>(count); var triggers = new IExpirationTrigger[count]; var callbackResults = new bool[count]; for (int i = 0; i < count; i++) { tasks.Add(new Task(index => { var expirationTrigger = provider.Watch(fileName); triggers[(int)index] = expirationTrigger; expirationTrigger.ShouldNotBe(null); expirationTrigger.IsExpired.ShouldNotBe(true); expirationTrigger.RegisterExpirationCallback(_ => { callbackResults[(int)index] = true; }, index); }, state: i)); } // Simulating multiple concurrent requests to the same file. Parallel.ForEach(tasks, task => task.Start()); await Task.WhenAll(tasks); File.AppendAllText(fileLocation, "UpdatedContent"); // Some warm up time for the callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); for (int index = 1; index < count; index++) { triggers[index].ShouldBe(triggers[index - 1]); } callbackResults.All(c => c == true).ShouldBe(true); File.Delete(fileLocation); }
public void Triggers_With_Forward_And_Backward_Slash() { var provider = new PhysicalFileProvider(Path.GetTempPath()); var trigger1 = provider.Watch("/a/b"); var trigger2 = provider.Watch("a/b"); var trigger3 = provider.Watch(@"a\b"); trigger1.ShouldBe(trigger2); trigger2.ShouldBe(trigger3); trigger1.ActiveExpirationCallbacks.ShouldBe(true); trigger2.ActiveExpirationCallbacks.ShouldBe(true); trigger3.ActiveExpirationCallbacks.ShouldBe(true); trigger1.IsExpired.ShouldBe(false); trigger2.IsExpired.ShouldBe(false); trigger3.IsExpired.ShouldBe(false); }
public async Task Trigger_Fired_On_Directory_Name_Change() { var provider = new PhysicalFileProvider(Path.GetTempPath()); var oldDirectoryName = Guid.NewGuid().ToString(); var newDirectoryName = Guid.NewGuid().ToString(); var oldDirectoryFullPath = Path.Combine(Path.GetTempPath(), oldDirectoryName); var newDirectoryFullPath = Path.Combine(Path.GetTempPath(), newDirectoryName); Directory.CreateDirectory(oldDirectoryFullPath); var oldDirectoryTrigger = provider.Watch("**/" + oldDirectoryName); var newDirectoryTrigger = provider.Watch("**/" + newDirectoryName); var oldTriggers = new List<IExpirationTrigger>(); var newTriggers = new List<IExpirationTrigger>(); oldTriggers.Add(provider.Watch(Path.Combine("**", oldDirectoryName, "*.txt"))); newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, "*.txt"))); for (int i = 0; i < 5; i++) { var fileName = string.Format("test{0}.txt", i); File.WriteAllText(Path.Combine(oldDirectoryFullPath, fileName), "test content"); oldTriggers.Add(provider.Watch(Path.Combine("**", oldDirectoryName, fileName))); newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, fileName))); } await Task.Delay(2 * 100); // Give it a while before trying rename. Directory.Move(oldDirectoryFullPath, newDirectoryFullPath); // Wait for triggers to fire. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); oldDirectoryTrigger.IsExpired.ShouldBe(true); newDirectoryTrigger.IsExpired.ShouldBe(true); oldTriggers.All(t => t.IsExpired).ShouldBe(true); newTriggers.All(t => t.IsExpired).ShouldBe(true); newDirectoryTrigger = provider.Watch(newDirectoryName); newTriggers = new List<IExpirationTrigger>(); newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, "*.txt"))); for (int i = 0; i < 5; i++) { var fileName = string.Format("test{0}.txt", i); newTriggers.Add(provider.Watch(Path.Combine("**", newDirectoryName, fileName))); } Directory.Delete(newDirectoryFullPath, true); // Wait for triggers to fire. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); newDirectoryTrigger.IsExpired.ShouldBe(true); newTriggers.All(t => t.IsExpired).ShouldBe(true); }
public async Task ModifyContent_And_Delete_File_Succeeds_And_Callsback_Registered_Triggers() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "OldContent"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var fileInfo = provider.GetFileInfo(fileName); fileInfo.Length.ShouldBe(new FileInfo(fileInfo.PhysicalPath).Length); fileInfo.Exists.ShouldBe(true); IExpirationTrigger trigger3 = null, trigger4 = null; var trigger1 = provider.Watch(fileName); var trigger2 = provider.Watch(fileName); // Valid trigger1 created. trigger1.ShouldNotBe(null); trigger1.IsExpired.ShouldBe(false); trigger1.ActiveExpirationCallbacks.ShouldBe(true); // Valid trigger2 created. trigger2.ShouldNotBe(null); trigger2.IsExpired.ShouldBe(false); trigger2.ActiveExpirationCallbacks.ShouldBe(true); // Trigger is the same for a specific file. trigger1.ShouldBe(trigger2); trigger1.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger3 = provider.Watch(infoFromState.Name); trigger3.ShouldNotBe(null); trigger3.RegisterExpirationCallback(_ => { }, null); trigger3.IsExpired.ShouldBe(false); }, state: fileInfo); trigger2.RegisterExpirationCallback(state => { var infoFromState = state as IFileInfo; trigger4 = provider.Watch(infoFromState.Name); trigger4.ShouldNotBe(null); trigger4.RegisterExpirationCallback(_ => { }, null); trigger4.IsExpired.ShouldBe(false); }, state: fileInfo); // Write new content. File.WriteAllText(fileLocation, "OldContent + NewContent"); fileInfo.Exists.ShouldBe(true); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); trigger1.IsExpired.ShouldBe(true); trigger2.IsExpired.ShouldBe(true); // Trigger is the same for a specific file. trigger3.ShouldBe(trigger4); // A new trigger is created. trigger3.ShouldNotBe(trigger1); // Delete the file and verify file info is updated. File.Delete(fileLocation); fileInfo = provider.GetFileInfo(fileName); fileInfo.Exists.ShouldBe(false); new FileInfo(fileLocation).Exists.ShouldBe(false); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); trigger3.IsExpired.ShouldBe(true); trigger4.IsExpired.ShouldBe(true); }
public DocumentFileProvider(string root, string content) { _physicalFileProvider = new PhysicalFileProvider(root); _content = content; }
public async Task Trigger_With_MultipleFiles() { var fileName1 = Guid.NewGuid().ToString(); var fileName2 = Guid.NewGuid().ToString(); var fileLocation1 = Path.Combine(Path.GetTempPath(), fileName1); var fileLocation2 = Path.Combine(Path.GetTempPath(), fileName2); File.WriteAllText(fileLocation1, "Content1"); File.WriteAllText(fileLocation2, "Content2"); var provider = new PhysicalFileProvider(Path.GetTempPath()); int invocationCount1 = 0, invocationCount2 = 0; var trigger1 = provider.Watch(fileName1); trigger1.RegisterExpirationCallback(_ => { invocationCount1++; }, null); var trigger2 = provider.Watch(fileName2); trigger2.RegisterExpirationCallback(_ => { invocationCount2++; }, null); trigger1.ShouldNotBe(null); trigger1.IsExpired.ShouldNotBe(true); trigger1.ActiveExpirationCallbacks.ShouldBe(true); trigger2.ShouldNotBe(null); trigger2.IsExpired.ShouldNotBe(true); trigger2.ActiveExpirationCallbacks.ShouldBe(true); trigger1.ShouldNotBe(trigger2); File.AppendAllText(fileLocation1, "Update1"); File.AppendAllText(fileLocation2, "Update2"); // Wait for callbacks to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); invocationCount1.ShouldBe(1); invocationCount2.ShouldBe(1); trigger1.IsExpired.ShouldBe(true); trigger2.IsExpired.ShouldBe(true); File.Delete(fileLocation1); File.Delete(fileLocation2); // Callbacks not invoked on expired triggers. invocationCount1.ShouldBe(1); invocationCount2.ShouldBe(1); }
public void Trigger_Is_FileName_Case_Insensitive() { var fileName = Guid.NewGuid().ToString() + 'A'; var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "Content"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var expirationTrigger = provider.Watch(fileName); var lowerCaseExpirationTrigger = provider.Watch(fileName.ToLowerInvariant()); expirationTrigger.ShouldBe(lowerCaseExpirationTrigger); File.Delete(fileLocation); }
public async Task Trigger_Callbacks_Are_Async_And_TriggerNotAffected_By_Exceptions() { var fileName = Guid.NewGuid().ToString(); var fileLocation = Path.Combine(Path.GetTempPath(), fileName); File.WriteAllText(fileLocation, "Content"); var provider = new PhysicalFileProvider(Path.GetTempPath()); var expirationTrigger = provider.Watch(fileName); expirationTrigger.RegisterExpirationCallback(async _ => { await Task.Delay(10 * 1000); throw new Exception("Callback throwing exception"); }, null); File.AppendAllText(fileLocation, "UpdatedContent"); // Wait for callback to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); expirationTrigger.IsExpired.ShouldBe(true); // Verify file system watcher is stable. int callbackCount = 0; var expirationTriggerAfterCallbackException = provider.Watch(fileName); expirationTriggerAfterCallbackException.RegisterExpirationCallback(_ => { callbackCount++; }, null); File.AppendAllText(fileLocation, "UpdatedContent"); // Wait for callback to be fired. await Task.Delay(WAIT_TIME_FOR_TRIGGER_TO_FIRE); expirationTrigger.IsExpired.ShouldBe(true); callbackCount.ShouldBe(1); File.Delete(fileLocation); }