// File helpers private string FilePath(Uri uri, FileBlobStorage storage) => uri .ChangeBase( storage.BaseUri, new Uri(Configuration.Path).EnsurePathTrailingSlash() ) .LocalPath;
public void PutAsync_NullStream() { var storage = new FileBlobStorage(Configuration); storage .Awaiting(s => s.PutAsync(null, ".dat")) .Should().Throw <ArgumentNullException>(); }
public void DeleteAsync_NullUri() { var storage = new FileBlobStorage(Configuration); storage .Awaiting(s => s.DeleteAsync(null)) .Should().Throw <ArgumentNullException>(); }
public void DeleteAsync_NotMyUri() { var storage = new FileBlobStorage(Configuration); var uri = new Uri("some://other/base/file.txt"); storage .Awaiting(s => s.DeleteAsync(uri)) .Should().Throw <ArgumentException>(); }
public void DeleteAsync_RelativeUri() { var storage = new FileBlobStorage(Configuration); var uri = new Uri("relative/file.txt", UriKind.Relative); storage .Awaiting(s => s.DeleteAsync(uri)) .Should().Throw <ArgumentException>(); }
public async Task DeleteAsync_DoesNotExist() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); var result = await storage.DeleteAsync(uri); result.Should().BeFalse(); }
public void PutAsync_UnreadableStream() { var storage = new FileBlobStorage(Configuration); var stream = Mock.Of <Stream>(s => s.CanRead == false); storage .Awaiting(s => s.PutAsync(stream, ".dat")) .Should().Throw <ArgumentException>(); }
public void GetAsync_NotFound() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "does/not/exist.txt"); storage .Awaiting(s => s.GetAsync(uri)) .Should().Throw <IOException>(); }
public async Task DeleteAsync_Exists_DirectoryEmpty() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); CreateDirectory(@"a", "b"); WriteFile(@"a", "b", "file.txt"); FileExists(@"a", "b", "file.txt").Should().BeTrue("file should exist prior to deletion"); var result = await storage.DeleteAsync(uri); result.Should().BeTrue(); FileExists(@"a", "b", "file.txt").Should().BeFalse("file should have been deleted"); DirectoryExists(@"a", "b").Should().BeFalse("empty subdirectory should have been deleted"); DirectoryExists(@"a").Should().BeFalse("empty subdirectory should have been deleted"); DirectoryExists(@"").Should().BeTrue("repository base directory should NOT have been deleted"); }
public async Task PutAsync(string extension) { var storage = new FileBlobStorage(Configuration); var bytes = Utf8.GetBytes(TestText); Uri uri; using (var stream = new MemoryStream(bytes)) uri = await storage.PutAsync(stream, extension); uri.Should().NotBeNull(); uri.Should().Match <Uri>(u => storage.BaseUri.IsBaseOf(u)); uri.AbsolutePath.Should().EndWith(".txt"); var path = FilePath(uri, storage); ReadFile(path).Should().Be(TestText); }
public void DeleteAsync_PathTooLong() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); storage.FileSystem = FileSystem.Object; FileSystem .Setup(f => f.FileExists(It.IsAny <string>())) .Returns(true); FileSystem .Setup(f => f.DeleteFile(It.IsAny <string>())) .Throws <PathTooLongException>(); // should never happen, but coverage storage .Awaiting(s => s.DeleteAsync(uri)) .Should().Throw <PathTooLongException>(); }
public void DeleteAsync_Retry_Fail() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); storage.FileSystem = FileSystem.Object; FileSystem .Setup(f => f.FileExists(It.IsAny <string>())) .Returns(true); FileSystem .Setup(f => f.DeleteFile(It.IsAny <string>())) .Throws <IOException>(); storage .Awaiting(s => s.DeleteAsync(uri)) .Should().Throw <IOException>(); }
public async Task GetAsync() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/file.txt"); CreateDirectory(@"a"); WriteFile(@"a", "file.txt"); byte[] bytes; using (var stream = await storage.GetAsync(uri)) using (var memory = new MemoryStream()) { await stream.CopyToAsync(memory); bytes = memory.ToArray(); } Utf8.GetString(bytes).Should().Be(TestText); }
public async Task DeleteAsync_Retry_Succeed() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); storage.FileSystem = FileSystem.Object; FileSystem .Setup(f => f.FileExists(It.IsAny <string>())) .Returns(true); FileSystem .SetupSequence(f => f.DeleteFile(It.IsAny <string>())) .Throws <IOException>() .Pass(); var result = await storage.DeleteAsync(uri); result.Should().BeTrue(); }
public async Task DeleteAsync_DirectoryNotFound() { var storage = new FileBlobStorage(Configuration); var uri = new Uri(storage.BaseUri, "a/b/file.txt"); storage.FileSystem = FileSystem.Object; FileSystem .Setup(f => f.FileExists(It.IsAny <string>())) .Returns(true) .Verifiable(); FileSystem .Setup(f => f.DeleteFile(It.IsAny <string>())) .Throws <DirectoryNotFoundException>() .Verifiable(); // directory deleted after existence check var result = await storage.DeleteAsync(uri); result.Should().BeTrue(); FileSystem.Verify(); }
public void PutAsync_FileSystemChanged() { try { var storage = new FileBlobStorage(Configuration); var bytes = Utf8.GetBytes(TestText); DeleteDirectory(@""); WriteFile(@""); // same name as repository directory using (var stream = new MemoryStream(bytes)) { storage .Awaiting(s => s.PutAsync(stream, ".txt")) .Should().Throw <IOException>() .Which.Message.Should().Contain("already exists"); } } finally { DeleteFile(@""); } }