public async Task GivenALinkedFileDeleteAndRecreateVerifyFile() { using (var target = new TestFolder()) using (var cache = new LocalCache()) using (var extCache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var jsonInput = new JObject(new JProperty("abc", "xyz")); var jsonInput2 = new JObject(new JProperty("abc", "abc")); var extFile = extCache.GetNewTempPath(); File.WriteAllText(extFile.FullName, jsonInput.ToString()); var a = fileSystem.Get("a.txt"); // Link to an ext file a.Link(extFile.FullName, log, CancellationToken.None); // Overwrite with a different file await a.Write(jsonInput2, log, CancellationToken.None); var json = await a.GetJson(log, CancellationToken.None); json.ToString().Should().Be(jsonInput2.ToString()); a.HasChanges.Should().BeTrue(); File.Exists(extFile.FullName).Should().BeTrue("The original file should not be removed"); } }
public async Task GivenAFileWriteThenDeleteMultipleTimesVerifyDoesNotExist() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); a.Delete(log, CancellationToken.None); await a.Write(new JObject(), log, CancellationToken.None); a.Delete(log, CancellationToken.None); await a.Write(new JObject(), log, CancellationToken.None); await a.Write(new JObject(), log, CancellationToken.None); await a.Write(new JObject(), log, CancellationToken.None); a.Delete(log, CancellationToken.None); a.HasChanges.Should().BeTrue(); cache.Root.GetFiles().Should().BeEmpty(); await fileSystem.Commit(log, CancellationToken.None); var path = Path.Combine(target, "a.txt"); File.Exists(path).Should().BeFalse(); } }
public void GivenAFileVerifyGetPathGivesTheSameFile() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); var test1 = fileSystem.Get("a.txt"); var test2 = fileSystem.Get(new Uri(a.EntityUri.AbsoluteUri)); var test3 = fileSystem.Get("/a.txt"); ReferenceEquals(a, test1).Should().BeTrue(); ReferenceEquals(a, test2).Should().BeTrue(); ReferenceEquals(a, test3).Should().BeTrue(); } }
public async Task FileSystem_VerifyFileSystemResetOnLock() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem1 = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var settings = new LocalSettings(); var lockMessage = Guid.NewGuid().ToString(); await InitCommand.RunAsync(settings, fileSystem1, log); // Verify that files work normally var testFile = fileSystem1.Get("test.json"); await testFile.GetJsonOrNull(log, CancellationToken.None); var testFile2 = fileSystem1.Get("test2.json"); fileSystem1.Files.Count.Should().BeGreaterThan(1); // Lock the feed to reset it var lockObj1 = await SourceUtility.VerifyInitAndLock(settings, fileSystem1, lockMessage, log, CancellationToken.None); lockObj1.IsLocked.Should().BeTrue(); // 1 file should be found since it loads the index fileSystem1.Files.Count.Should().Be(1); InvalidOperationException failureEx = null; try { // Verify the old file no longer works await testFile.GetJsonOrNull(log, CancellationToken.None); await testFile2.GetJsonOrNull(log, CancellationToken.None); } catch (InvalidOperationException ex) { failureEx = ex; } failureEx.Should().NotBeNull(); } }
public async Task GivenInitVerifyBasicOutputs() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { // Arrange var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var settings = new LocalSettings(); var indexJsonOutput = new FileInfo(Path.Combine(target.Root, "index.json")); var settingsOutput = new FileInfo(Path.Combine(target.Root, "sleet.settings.json")); var autoCompleteOutput = new FileInfo(Path.Combine(target.Root, "autocomplete", "query")); var catalogOutput = new FileInfo(Path.Combine(target.Root, "catalog", "index.json")); var searchOutput = new FileInfo(Path.Combine(target.Root, "search", "query")); var packageIndexOutput = new FileInfo(Path.Combine(target.Root, "sleet.packageindex.json")); var symbolsIndexOutput = new FileInfo(Path.Combine(target.Root, "symbols", "packages", "index.json")); // Act var success = await InitCommand.RunAsync(settings, fileSystem, enableCatalog : true, enableSymbols : true, log : log, token : CancellationToken.None); var rootFile = fileSystem.Get("index.json"); var rootJson = await rootFile.GetJson(log, CancellationToken.None); success &= await FeedSettingsCommand.RunAsync( settings, fileSystem, unsetAll : false, getAll : true, getSettings : new string[] { }, unsetSettings : new string[] { }, setSettings : new string[] { }, log : log, token : CancellationToken.None); // Assert success.Should().BeTrue(); indexJsonOutput.Exists.Should().BeTrue(); settingsOutput.Exists.Should().BeTrue(); autoCompleteOutput.Exists.Should().BeTrue(); catalogOutput.Exists.Should().BeTrue(); searchOutput.Exists.Should().BeTrue(); packageIndexOutput.Exists.Should().BeTrue(); symbolsIndexOutput.Exists.Should().BeTrue(); log.GetMessages().Should().Contain("catalogpagesize : 1024"); log.GetMessages().Should().Contain("catalogenabled : true"); log.GetMessages().Should().Contain("symbolsfeedenabled : true"); rootJson.ToString().Should().Contain("catalog/index.json"); rootJson.ToString().Should().Contain("Catalog/3.0.0"); rootJson.ToString().Should().Contain("symbols/packages/index.json"); } }
public void SleetUtility_GetServiceName() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var file = fileSystem.Get("badges/v/test.json"); var result = SleetUtility.GetServiceName(file); result.Should().Be(ServiceNames.Badges); file = fileSystem.Get("registration/a/a.json"); result = SleetUtility.GetServiceName(file); result.Should().Be(ServiceNames.Registrations); file = fileSystem.Get("test.json"); result = SleetUtility.GetServiceName(file); result.Should().Be(ServiceNames.Unknown); } }
public void SubFeed_VerifySubFeedPath() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var root = UriUtility.CreateUri(target.Root, "feedA"); var fileSystem = new PhysicalFileSystem(cache, root, root, feedSubPath: "feedA"); fileSystem.Root.Should().Be(UriUtility.EnsureTrailingSlash(root)); fileSystem.LocalRoot.Should().StartWith(Path.Combine(target.Root, "feedA")); fileSystem.Get("index.json").EntityUri.Should().Be(UriUtility.GetPath(root, "index.json")); } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var file = (PhysicalFile)_fileSystem.Get(request.RequestUri); if (!await file.Exists(NullLogger.Instance, cancellationToken)) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } using (var stream = await file.GetStream(NullLogger.Instance, cancellationToken)) { return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new TestHttpContent(stream) }); } }
public async Task GivenAFileWriteVerifyGetAfter() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); var json = await a.GetJson(log, CancellationToken.None); a.HasChanges.Should().BeTrue(); cache.Root.GetFiles().Length.Should().Be(1); json.ToString().Should().Be((new JObject()).ToString()); } }
public async Task GivenThatICreateAndDeleteAFileInTheSameSessionVerifyItIsRemoved() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); a.Delete(log, CancellationToken.None); await fileSystem.Commit(log, CancellationToken.None); File.Exists(a.RootPath.LocalPath).Should().BeFalse("the file was deleted"); } }
public async Task GivenAFileVerifyExistsDoesNotDownload() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); File.WriteAllText(Path.Combine(target, "a.txt"), "."); var a = fileSystem.Get("a.txt"); var exists = await a.Exists(log, CancellationToken.None); exists.Should().BeTrue(); a.HasChanges.Should().BeFalse(); cache.Root.GetFiles().Should().BeEmpty(); } }
protected override Stream TryReadCacheFile(string uri, TimeSpan maxAge, string cacheFile) { var file = (PhysicalFile)_fileSystem.Get(Sleet.UriUtility.CreateUri(uri)); if (file.Exists(NullLogger.Instance, CancellationToken.None).Result) { using (var stream = file.GetStream(NullLogger.Instance, CancellationToken.None).Result) { var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); return(memoryStream); } } else { return(null); } }
public async Task GivenThatICallGetFilesReturnAKnownFile() { using (var target = new TestFolder()) using (var cache = new LocalCache()) using (var cache2 = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var fileSystem2 = new PhysicalFileSystem(cache2, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); await fileSystem.Commit(log, CancellationToken.None); File.Exists(a.RootPath.LocalPath).Should().BeTrue("the file was not deleted yet"); var results = await fileSystem2.GetFiles(log, CancellationToken.None); results.Select(e => Path.GetFileName(e.EntityUri.LocalPath)).ShouldBeEquivalentTo(new[] { "a.txt" }); } }
public void GivenALinkedFileDeleteVerifyFileIsNotRemoved() { using (var target = new TestFolder()) using (var cache = new LocalCache()) using (var extCache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var jsonInput = new JObject(new JProperty("abc", "xyz")); var extFile = extCache.GetNewTempPath(); File.WriteAllText(extFile.FullName, jsonInput.ToString()); var a = fileSystem.Get("a.txt"); a.Link(extFile.FullName, log, CancellationToken.None); a.Delete(log, CancellationToken.None); a.HasChanges.Should().BeTrue(); cache.Root.GetFiles().Length.Should().Be(0); File.Exists(extFile.FullName).Should().BeTrue("The original file should not be removed"); } }
public async Task GivenAFileExistsVerifyWriteOverWrites() { using (var target = new TestFolder()) using (var cache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var path = Path.Combine(target, "a.txt"); File.WriteAllText(path, "."); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); a.HasChanges.Should().BeTrue(); cache.Root.GetFiles().Length.Should().Be(1); await fileSystem.Commit(log, CancellationToken.None); File.ReadAllText(path).Should().StartWith("{"); } }
public async Task GivenAFileLinkVerifyGetAfter() { using (var target = new TestFolder()) using (var cache = new LocalCache()) using (var extCache = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var jsonInput = new JObject(new JProperty("abc", "xyz")); var extFile = extCache.GetNewTempPath(); File.WriteAllText(extFile.FullName, jsonInput.ToString()); var a = fileSystem.Get("a.txt"); a.Link(extFile.FullName, log, CancellationToken.None); var json = await a.GetJson(log, CancellationToken.None); a.HasChanges.Should().BeTrue(); cache.Root.GetFiles().Length.Should().Be(0); json.ToString().Should().Be(jsonInput.ToString()); } }
public async Task GivenThatIDestroyAFeedItIsNowEmpty() { using (var target = new TestFolder()) using (var cache = new LocalCache()) using (var cache2 = new LocalCache()) { var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var fileSystem2 = new PhysicalFileSystem(cache2, UriUtility.CreateUri(target.Root)); var a = fileSystem.Get("a.txt"); await a.Write(new JObject(), log, CancellationToken.None); await fileSystem.Commit(log, CancellationToken.None); File.Exists(a.RootPath.LocalPath).Should().BeTrue("the file was not deleted yet"); await fileSystem2.Destroy(log, CancellationToken.None); await fileSystem2.Commit(log, CancellationToken.None); Directory.Exists(target).Should().BeFalse("all files were deleted"); } }
public async Task CatalogTest_AddPackageAsync_SupportsWritingMultiplePages() { using (var packagesFolder = new TestFolder()) using (var target = new TestFolder()) using (var cache = new LocalCache()) { // Arrange var log = new TestLogger(); var fileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(target.Root)); var settings = new LocalSettings(); var context = new SleetContext() { Token = CancellationToken.None, LocalSettings = settings, Log = log, Source = fileSystem, SourceSettings = new FeedSettings() { CatalogEnabled = true, CatalogPageSize = 1, } }; var catalog = new Catalog(context); var catalogIndex = TemplateUtility.LoadTemplate( "CatalogIndex", DateTimeOffset.UtcNow, fileSystem.BaseURI); await fileSystem.Get("catalog/index.json").Write( JObject.Parse(catalogIndex), log, context.Token); var testPackageA = new TestNupkg("packageA", "1.0.0"); var testPackageB = new TestNupkg("packageB", "1.0.0"); var zipFileA = testPackageA.Save(packagesFolder.Root); var zipFileB = testPackageB.Save(packagesFolder.Root); using (var zipA = new ZipArchive(File.OpenRead(zipFileA.FullName), ZipArchiveMode.Read, false)) using (var zipB = new ZipArchive(File.OpenRead(zipFileB.FullName), ZipArchiveMode.Read, false)) { var inputA = new PackageInput(zipFileA.FullName, new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")), false) { NupkgUri = UriUtility.CreateUri("http://tempuri.org/flatcontainer/packageA/1.0.0/packageA.1.0.0.nupkg"), Zip = zipA, Package = new PackageArchiveReader(zipA) }; var inputB = new PackageInput(zipFileB.FullName, new PackageIdentity("packageB", NuGetVersion.Parse("1.0.0")), false) { NupkgUri = UriUtility.CreateUri("http://tempuri.org/flatcontainer/packageB/1.0.0/packageB.1.0.0.nupkg"), Zip = zipB, Package = new PackageArchiveReader(zipB) }; // Act await catalog.AddPackageAsync(inputA); await catalog.AddPackageAsync(inputB); await fileSystem.Commit(context.Log, context.Token); // Assert Assert.True( await fileSystem.Get("catalog/page.0.json").Exists(context.Log, context.Token), "The first catalog page should exist."); Assert.True( await fileSystem.Get("catalog/page.1.json").Exists(context.Log, context.Token), "The second catalog page should exist."); } } }