|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Primitives; |
| 6 | +using Microsoft.Extensions.FileProviders; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace Axuno.VirtualFileSystem.Tests |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class DynamicFileProviderTests |
| 13 | + { |
| 14 | + private readonly IDynamicFileProvider _dynamicFileProvider; |
| 15 | + |
| 16 | + public DynamicFileProviderTests() |
| 17 | + { |
| 18 | + _dynamicFileProvider = |
| 19 | + ServiceSetup.GetVirtualFileSystemServiceProvider().GetRequiredService<IDynamicFileProvider>(); |
| 20 | + } |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void Should_Get_Created_Files() |
| 24 | + { |
| 25 | + const string fileContent = "Hello World"; |
| 26 | + |
| 27 | + _dynamicFileProvider.AddOrUpdate( |
| 28 | + new InMemoryFileInfo( |
| 29 | + "/my-files/test.txt", |
| 30 | + fileContent.GetBytes(), |
| 31 | + "test.txt" |
| 32 | + ) |
| 33 | + ); |
| 34 | + |
| 35 | + var fileInfo = _dynamicFileProvider.GetFileInfo("/my-files/test.txt"); |
| 36 | + Assert.IsNotNull(fileInfo); |
| 37 | + Assert.AreEqual(fileContent, fileInfo.ReadAsString()); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void Should_Create_and_Delete_Files() |
| 42 | + { |
| 43 | + const string fileContent = "Hello World"; |
| 44 | + const string dynamicPath = "/to-delete/test.txt"; |
| 45 | + |
| 46 | + _dynamicFileProvider.AddOrUpdate( |
| 47 | + new InMemoryFileInfo( |
| 48 | + dynamicPath, |
| 49 | + fileContent.GetBytes(), |
| 50 | + "test.txt" |
| 51 | + ) |
| 52 | + ); |
| 53 | + |
| 54 | + // Register to change on that file |
| 55 | + |
| 56 | + var fileCallbackCalled = false; |
| 57 | + |
| 58 | + ChangeToken.OnChange( |
| 59 | + () => _dynamicFileProvider.Watch(dynamicPath), |
| 60 | + () => { fileCallbackCalled = true; }); |
| 61 | + |
| 62 | + var fileInfo = _dynamicFileProvider.GetFileInfo(dynamicPath); |
| 63 | + Assert.IsTrue(fileInfo.Exists); |
| 64 | + |
| 65 | + // Deleting the file should trigger the callback |
| 66 | + |
| 67 | + _dynamicFileProvider.Delete(dynamicPath); |
| 68 | + fileInfo = _dynamicFileProvider.GetFileInfo(dynamicPath); |
| 69 | + Assert.IsFalse(fileInfo.Exists); |
| 70 | + |
| 71 | + Assert.IsTrue(fileCallbackCalled); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void Deleting_Non_Existing_File_Just_Fails() |
| 76 | + { |
| 77 | + Assert.IsFalse(_dynamicFileProvider.Delete("does-not-exist")); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void Should_Get_Notified_On_File_Change() |
| 82 | + { |
| 83 | + // Create a dynamic file |
| 84 | + |
| 85 | + _dynamicFileProvider.AddOrUpdate( |
| 86 | + new InMemoryFileInfo( |
| 87 | + "/my-files/test.txt", |
| 88 | + "Hello World".GetBytes(), |
| 89 | + "test.txt" |
| 90 | + ) |
| 91 | + ); |
| 92 | + |
| 93 | + // Register to change on that file |
| 94 | + |
| 95 | + var fileCallbackCalled = false; |
| 96 | + |
| 97 | + ChangeToken.OnChange( |
| 98 | + () => _dynamicFileProvider.Watch("/my-files/test.txt"), |
| 99 | + () => { fileCallbackCalled = true; }); |
| 100 | + |
| 101 | + // Updating the file should trigger the callback |
| 102 | + |
| 103 | + _dynamicFileProvider.AddOrUpdate( |
| 104 | + new InMemoryFileInfo( |
| 105 | + "/my-files/test.txt", |
| 106 | + "Hello World UPDATED".GetBytes(), |
| 107 | + "test.txt" |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + Assert.IsTrue(fileCallbackCalled); |
| 112 | + |
| 113 | + // Updating the file should trigger the callback (2nd test) |
| 114 | + |
| 115 | + fileCallbackCalled = false; |
| 116 | + |
| 117 | + _dynamicFileProvider.AddOrUpdate( |
| 118 | + new InMemoryFileInfo( |
| 119 | + "/my-files/test.txt", |
| 120 | + "Hello World UPDATED 2".GetBytes(), |
| 121 | + "test.txt" |
| 122 | + ) |
| 123 | + ); |
| 124 | + |
| 125 | + Assert.IsTrue(fileCallbackCalled); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments