public void ShouldNotReWriteTheFile() { // Arrange using var arena = new TestArena(); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = GetRandomString(2); var data = GetRandomBytes(100); var targetFilePath = arena.CreateResource( arena.TargetPath, relPath, data); arena.CreateResource( arena.SourcePath, relPath, data); var beforeTest = DateTime.Now; var lastWrite = beforeTest.AddMinutes(GetRandomInt(-100, -1)); File.SetLastWriteTime(targetFilePath, lastWrite); var sut = Create(); // Act sut.Synchronize(source, target); // Assert Expect(targetFilePath) .To.Be.A.File(); var targetInfo = new FileInfo(targetFilePath); Expect(targetInfo.LastWriteTime) .To.Be.Less.Than(beforeTest); }
public void ShouldResumeWhenResumeStrategySaysYes() { // Arrange using var arena = new TestArena(); var resumeStrategy = Substitute.For <IResumeStrategy>(); resumeStrategy.CanResume( Arg.Any <IFileResource>(), Arg.Any <IFileResource>(), Arg.Any <Stream>(), Arg.Any <Stream>() ) .Returns(true); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = GetRandomString(); var allData = GetRandomBytes(1024); var partialSize = GetRandomInt(384, 768); var partialData = allData.Take(partialSize) .ToArray(); var expected = allData.Skip(partialSize) .ToArray(); var targetFilePath = arena.CreateResource( arena.TargetPath, relPath, partialData); arena.CreateResource( arena.SourcePath, relPath, allData); var captured = new List <byte>(); var ended = false; var intermediate = new GenericPassThrough( (data, count) => captured.AddRange(data.Take(count)), () => ended = true ); var sut = Create( resumeStrategy, new IPassThrough[] { intermediate } ); // Act sut.Synchronize(source, target); // Assert var targetData = File.ReadAllBytes(targetFilePath); Expect(targetData) .To.Equal(allData); var transferred = captured.ToArray(); Expect(transferred) .To.Equal(expected, "unexpected transferred data"); Expect(ended) .To.Be.True(); }
public void ShouldCopyTheSourceFileToTarget() { // Arrange using var arena = new TestArena(); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = "some-file.ext"; var data = GetRandomBytes(); arena.CreateResource( arena.SourcePath, relPath, data); var sut = Create(filters: CreatePermissiveFilter()); // Act sut.Synchronize(source, target); // Assert var inTarget = target.ListResourcesRecursive(); Expect(inTarget) .To.Contain.Only(1) .Item(); var copied = inTarget.Single(); Expect(copied.RelativePath) .To.Equal(relPath); Expect(copied) .To.Have.Data(data); }
public void ShouldNotReWriteTheFileWhenInHistoryButNotOnDisk() { // Arrange using var arena = new TestArena(); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = GetRandomString(2); var data = GetRandomBytes(100); arena.CreateResource( arena.SourcePath, relPath, data); var historyRepo = Substitute.For <ITargetHistoryRepository>(); historyRepo.Exists(relPath) .Returns(true); var targetFilePath = Path.Combine(arena.TargetPath, relPath); var historyItem = new HistoryItem( relPath, data.Length); historyRepo.Find(relPath) .Returns(historyItem); var sut = Create(targetHistoryRepository: historyRepo); // Act sut.Synchronize(source, target); // Assert Expect(targetFilePath) .Not.To.Be.A.File(); }
public void ShouldUpsertHistory() { // Arrange using var arena = new TestArena(); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = "some-file.ext"; var data = GetRandomBytes(); arena.CreateResource( arena.SourcePath, relPath, data); var historyRepo = Substitute.For <ITargetHistoryRepository>(); var sut = Create( targetHistoryRepository: historyRepo, filters: CreatePermissiveFilter()); // Act sut.Synchronize(source, target); // Assert Expect(historyRepo) .To.Have.Received(1) .Upsert(Arg.Is <IHistoryItem>( o => o.Path == relPath && o.Size == data.Length )); }
public void ShouldPassThroughAllProvidedIntermediatesInOrder() { // Arrange using var arena = new TestArena(); var resumeStrategy = Substitute.For <IResumeStrategy>(); resumeStrategy.CanResume( Arg.Any <IFileResource>(), Arg.Any <IFileResource>(), Arg.Any <Stream>(), Arg.Any <Stream>()) .Returns(false); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = GetRandomString(); var allData = GetRandomBytes(100, 200); // small buffer, likely to be read in one pass arena.CreateResource( arena.SourcePath, relPath, allData); var intermediateCalls = new List <string>(); var endCalls = new List <string>(); var intermediate1 = new GenericPassThrough( (data, count) => intermediateCalls.Add("first"), () => endCalls.Add("first") ); var intermediate2 = new GenericPassThrough( (data, count) => intermediateCalls.Add("second"), () => endCalls.Add("second") ); var intermediate3 = new GenericPassThrough( (data, count) => intermediateCalls.Add("third"), () => endCalls.Add("third") ); var expected = new[] { "first", "second", "third" }; var sut = Create( resumeStrategy, new IPassThrough[] { intermediate1, intermediate2, intermediate3 }, // this test is about notifiers, so we'll // just pretend that all sources are to be required // at the target filters: CreatePermissiveFilter()); // Act sut.Synchronize(source, target); // Assert Expect(intermediateCalls) .To.Equal(expected); Expect(endCalls) .To.Equal(expected); }
public void ShouldUpsertHistory() { // Arrange using var arena = new TestArena(); var(source, target) = (arena.SourceFileSystem, arena.TargetFileSystem); var relPath = GetRandomString(2); var data = GetRandomBytes(100); var targetFilePath = arena.CreateResource( arena.TargetPath, relPath, data); arena.CreateResource( arena.SourcePath, relPath, data); var beforeTest = DateTime.Now; var lastWrite = beforeTest.AddMinutes(GetRandomInt(-100, -1)); File.SetLastWriteTime(targetFilePath, lastWrite); var historyRepo = Substitute.For <ITargetHistoryRepository>(); var sut = Create(targetHistoryRepository: historyRepo); // Act sut.Synchronize(source, target); // Assert Expect(historyRepo) .To.Have.Received(1) .Upsert(Arg.Is <IEnumerable <IHistoryItem> >( o => o.Single() .Path == relPath && o.Single() .Size == data.Length )); }