public void Convert_replaces_files_with_DirectoryPropertiesFile_instances() { var input = new Directory(Path.GetRandomFileName()); input.Add(_ => DirectoryPropertiesFile.ForDirectory(null, input)); var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(input)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(DirectoryPropertiesFile.FileName) is DirectoryPropertiesFile); } }
public void Repository_with_subdirectories() { // arrange string commitId; using (var workingDirectory = new TemporaryWorkingDirectory(m_Repository.Directory.Location, "master")) { var directory = new Directory(Path.GetFileName(workingDirectory.Location)) { root => new Directory(root, s_Dir1) { dir1 => new EmptyFile(dir1, s_File1), dir1 => new EmptyFile(dir1, s_File2) }, root => new Directory(root, s_Dir2) { dir2 => new EmptyFile(dir2, s_File1) } }; System.IO.File.Delete(Path.Combine(workingDirectory.Location, RepositoryInfoFile.RepositoryInfoFileName)); m_DirectoryCreator.CreateDirectory(directory, Path.GetDirectoryName(workingDirectory.Location)); commitId = workingDirectory.Commit(); workingDirectory.Push(); } using (var repo = new Repository(m_Repository.Directory.Location)) { // act var commit = repo.Lookup<Commit>(commitId); var gitDirectory = new GitDirectory(null, m_Repository.Directory.Name, commit); // assert Assert.Equal(m_Repository.Directory.Name, gitDirectory.Name); Assert.Equal(2, gitDirectory.Directories.Count()); Assert.Empty(gitDirectory.Files); Assert.True(gitDirectory.DirectoryExists(s_Dir1)); Assert.True(gitDirectory.GetDirectory(s_Dir1).FileExists(s_File1)); Assert.True(gitDirectory.GetDirectory(s_Dir1).FileExists(s_File2)); Assert.True(gitDirectory.DirectoryExists(s_Dir2)); Assert.True(gitDirectory.GetDirectory(s_Dir2).FileExists(s_File1)); } }
public void Convert_replaces_files_with_FilePropertiesFile_instances() { var input = new Directory(Path.GetRandomFileName()) { dir => FilePropertiesFile.ForFile(dir, new EmptyFile(s_File1)) }; var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(input)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(s_File1 + FilePropertiesFile.FileNameSuffix) is FilePropertiesFile); } }
public void CreateSnapshot_can_be_executed_multiple_times() { var directory1 = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) }; var directory2 = new Directory(s_Dir2) { d => new EmptyFile(d, s_File2) }; m_Instance.CreateSnapshot(directory1); var snapshot2 = m_Instance.CreateSnapshot(directory2); Assert.Equal(2, m_Instance.Snapshots.Count()); Assert.Equal(snapshot2, m_Instance.LatestFileSystemSnapshot); }
public void CreateSnapshot_creates_a_valid_snapshot() { var directory = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) }; var snapshot = m_Instance.CreateSnapshot(directory); FileSystemAssert.DirectoryEqual(directory, snapshot.RootDirectory); }
public void GetChangedFiles_ChangeList_Paths_are_rooted() { var state = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") }; var snapshot = m_Instance.CreateSnapshot(state); var changedFiles = m_Instance.GetChangedFiles(snapshot.Id); Assert.Single(changedFiles); Assert.True(changedFiles.Single().StartsWith("/")); }
public void GetChangedFiles_Multiple_changes_to_the_same_file() { //ARRANGE var lastWriteTime = DateTime.Now; var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(1)} }; var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(2)} }; var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot2.Id); //ASSERT Assert.Single(changedFiles); }
public void GetChangedFiles_A_file_gets_added_modified_and_deleted_between_snapshots() { //ARRANGE var lastWriteTime = DateTime.Now; var state0 = new Directory(s_Dir1); var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(1)} }; var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(2)} }; var state3 = new Directory(s_Dir1); // file1 deleted var snapshot0 = m_Instance.CreateSnapshot(state0); var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); var snapshot3 = m_Instance.CreateSnapshot(state3); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot0.Id, snapshot3.Id); //ASSERT Assert.Single(changedFiles); }
public void CreateSnapshot_returns_previous_snapshot_if_state_is_unchanged() { var state = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) { LastWriteTime = DateTime.Now, Length = 1234 } }; var snapshot1 = m_Instance.CreateSnapshot(state); var snapshot2 = m_Instance.CreateSnapshot(state); Assert.Equal(1, m_Instance.Snapshots.Count()); Assert.Equal(snapshot1.Id, snapshot2.Id); }
public void GetChangedFiles_with_single_id_gets_all_changes_since_the_initial_commit() { //ARRANGE var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") }; var snapshot = m_Instance.CreateSnapshot(state2); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot.Id); //ASSERT Assert.Single(changedFiles); }
public void UpdateItems(IEnumerable<SyncAction> syncActions) { syncActions = syncActions.ToList(); if (!syncActions.Any()) { return; } // make sure all to be updated actions exist (no need to check the state, this property might have changed) AssertSyncActionsExist(syncActions, false); using (var workingDir = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { var root = new Directory(null, "root"); foreach (var syncAction in syncActions) { PathValidator.EnsureIsRootedPath(syncAction.Path); // remove existing files for this sync action var filesToDelete = Enum.GetValues(typeof (SyncActionState)).Cast<SyncActionState>() .Where(state => state != syncAction.State) .Select(state => GetRelativeSyncActionDirectoryPath(state, syncAction.Path)) .Select(relativeDirectoryPath => Path.Combine(relativeDirectoryPath, SyncActionFile.GetFileName(syncAction))) .Select(relativePath => Path.Combine(workingDir.Location, relativePath)) .Where(System.IO.File.Exists).ToList(); foreach (var file in filesToDelete) { System.IO.File.Delete(file); } // add a new file var directory = DirectoryHelper.GetOrAddDirectory(root, GetRelativeSyncActionDirectoryPath(syncAction)); directory.Add(d => new SyncActionFile(d, syncAction)); } var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectoryInPlace(root, workingDir.Location); workingDir.Commit($"{nameof(GitSyncActionService)}: Updated {syncActions.Count()} items"); workingDir.Push(); } }
public void GetChanges_Multiple_changes_to_the_same_file() { //ARRANGE var lastWriteTime = DateTime.Now; var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(1)} }; var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(2)} }; var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); //ACT var diff = m_Instance.GetChanges(snapshot2.Id); //ASSERT Assert.NotNull(diff); Assert.Null(diff.FromSnapshot); Assert.NotNull(diff.ToSnapshot); Assert.Single(diff.ChangeLists); var changes = diff.ChangeLists.Single().Changes.ToArray(); // both changes should be contained in a single ChangeList Assert.Equal(2, changes.Length); // addition Assert.Equal(ChangeType.Added, changes[0].Type); Assert.Null(changes[0].FromVersion); FileSystemAssert.FileReferenceMatches(state1.GetFile("file1"), changes[0].ToVersion); // modification Assert.Equal(ChangeType.Modified, changes[1].Type); FileSystemAssert.FileReferenceMatches(state1.GetFile("file1"), changes[1].FromVersion); FileSystemAssert.FileReferenceMatches(state2.GetFile("file1"), changes[1].ToVersion); }
public void GetChangedFiles_throws_a_SnapshotNotFoundException_is_the_Id_is_unknown() { Assert.Throws<SnapshotNotFoundException>(() => m_Instance.GetChangedFiles("someId", "someOtherId")); var directory1 = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) }; var directory2 = new Directory(s_Dir2) { d => new EmptyFile(d, s_File1) }; var snapshot1 = m_Instance.CreateSnapshot(directory1); var snapshot2 = m_Instance.CreateSnapshot(directory2); Assert.Throws<SnapshotNotFoundException>(() => m_Instance.GetChangedFiles(snapshot1.Id, "someOtherId")); Assert.Throws<SnapshotNotFoundException>(() => m_Instance.GetChangedFiles("someId", snapshot2.Id)); Assert.Throws<SnapshotNotFoundException>(() => m_Instance.GetChangedFiles("someId")); }
public void GetChanges_ignores_Additions_of_empty_directories() { //ARRANGE var state1 = new Directory(s_Dir1); var state2 = new Directory(s_Dir1) { dir1 => new Directory(dir1, s_Dir2) }; var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); Assert.NotEqual(snapshot1.Id, snapshot2.Id); //ACT var diff = m_Instance.GetChanges(snapshot1.Id, snapshot2.Id); //ASSERT Assert.Empty(diff.ChangeLists); }
public void GetChanges_returns_filtered_list_of_changes_if_path_filter_is_supplied() { //ARRANGE var state = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1"), // Add file1 dir1 => new EmptyFile(dir1, "file2"), // Add file2 dir1 => new Directory(dir1, s_Dir2) { dir2 => new EmptyFile(dir2, "file3") } }; var snapshot = m_Instance.CreateSnapshot(state); //ACT + ASSERT // path if only applied to "new" Changes API Assert.Single(m_Instance.GetChanges(snapshot.Id, new[] { "/file1" }).ChangeLists); Assert.Single(m_Instance.GetChanges(snapshot.Id, new[] { "/file2" }).ChangeLists); Assert.Single(m_Instance.GetChanges(snapshot.Id, new[] { "/dir2/file3" }).ChangeLists); Assert.Equal(2, m_Instance.GetChanges(snapshot.Id, new[] { "/dir2/file3", "/file1" }).ChangeLists.Count()); Assert.Equal(3, m_Instance.GetChanges(snapshot.Id, null).ChangeLists.Count()); }
public void GetChages_returns_empty_result_if_empty_path_filter_is_supplied() { //ARRANGE var state = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = DateTime.Now} }; var snapshot = m_Instance.CreateSnapshot(state); //ACT var diff = m_Instance.GetChanges(snapshot.Id, Array.Empty<string>()); //ASSERT Assert.Empty(diff.ChangeLists); }
public void GetChanges_A_file_gets_added_modified_and_deleted_between_snapshots() { //ARRANGE var lastWriteTime = DateTime.Now; var state0 = new Directory(s_Dir1); var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(1)} }; var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") { LastWriteTime = lastWriteTime.AddHours(2)} }; var state3 = new Directory(s_Dir1); // file1 deleted var snapshot0 = m_Instance.CreateSnapshot(state0); var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); var snapshot3 = m_Instance.CreateSnapshot(state3); //ACT var diff = m_Instance.GetChanges(snapshot0.Id, snapshot3.Id); //ASSERT Assert.NotNull(diff); Assert.Equal(snapshot0, diff.FromSnapshot); Assert.Equal(snapshot3, diff.ToSnapshot); Assert.Single(diff.ChangeLists); var changes = diff.ChangeLists.Single().Changes.ToArray(); // both changes should be contained in a single ChangeList Assert.Equal(3, changes.Length); // addition Assert.Equal(ChangeType.Added, changes[0].Type); Assert.Null(changes[0].FromVersion); FileSystemAssert.FileReferenceMatches(state1.GetFile("file1"), changes[0].ToVersion); // modification Assert.Equal(ChangeType.Modified, changes[1].Type); FileSystemAssert.FileReferenceMatches(state1.GetFile("file1"), changes[1].FromVersion); FileSystemAssert.FileReferenceMatches(state2.GetFile("file1"), changes[1].ToVersion); // deletion Assert.Equal(ChangeType.Deleted, changes[2].Type); FileSystemAssert.FileReferenceMatches(state2.GetFile("file1"), changes[2].FromVersion); Assert.Null(changes[2].ToVersion); }
public void GetChangedFiles_throws_InvalidRangeException() { var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") }; var snapshot1 = m_Instance.CreateSnapshot(state1); var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file2") }; var snapshot2 = m_Instance.CreateSnapshot(state2); Assert.Throws<InvalidRangeException>(() => m_Instance.GetChangedFiles(snapshot2.Id, snapshot1.Id)); }
public void GetChanges_detects_deletions_of_files() { //ARRANGE var state1 = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) { LastWriteTime = DateTime.Now.AddDays(-2) } }; var state2 = new Directory(s_Dir1); var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); Assert.NotEqual(snapshot1.Id, snapshot2.Id); //ACT var diff = m_Instance.GetChanges(snapshot1.Id, snapshot2.Id); //ASSERT Assert.Equal(diff.FromSnapshot, snapshot1); Assert.Equal(diff.ToSnapshot, snapshot2); Assert.Single(diff.ChangeLists); var changes = diff.ChangeLists.Single().Changes.ToList(); Assert.Single(changes); Assert.Equal(ChangeType.Deleted, changes.Single().Type); Assert.Null(changes.Single().ToVersion); FileSystemAssert.FileReferenceMatches(state1.GetFile(s_File1), changes.Single().FromVersion); }
public void Indexer_returns_expected_snapshot() { var directory1 = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) }; var directory2 = new Directory(s_Dir2) { d => new EmptyFile(d, s_File2) }; var snapshot1 = m_Instance.CreateSnapshot(directory1); var snapshot2 = m_Instance.CreateSnapshot(directory2); Assert.Equal(snapshot1, m_Instance[snapshot1.Id]); Assert.Equal(snapshot2, m_Instance[snapshot2.Id]); // snapshot ids are case-invariant Assert.Equal(snapshot1, m_Instance[snapshot1.Id.ToLower()]); Assert.Equal(snapshot2, m_Instance[snapshot2.Id.ToLower()]); Assert.Equal(snapshot1, m_Instance[snapshot1.Id.ToUpper()]); Assert.Equal(snapshot2, m_Instance[snapshot2.Id.ToUpper()]); }
public void GetChanges_with_single_id_gets_all_changes_since_the_initial_commit() { //ARRANGE var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") }; var snapshot = m_Instance.CreateSnapshot(state2); //ACT var diff = m_Instance.GetChanges(snapshot.Id); //ASSERT Assert.NotNull(diff); Assert.Null(diff.FromSnapshot); Assert.NotNull(diff.ToSnapshot); // "new" Changes API Assert.Single(diff.ChangeLists); var changes = diff.ChangeLists.Single().Changes.ToList(); Assert.Equal(ChangeType.Added, changes.Single().Type); }
public void AddItems(IEnumerable<SyncAction> syncActions) { syncActions = syncActions.ToArray(); if (!syncActions.Any()) { return; } // make sure, the action does not already exist foreach (var action in syncActions) { var exisitingActions = this[action.State, action.Path].Where(a => a.Id == action.Id); if (exisitingActions.Any()) { throw new DuplicateSyncActionException($"A sync action with id {action.Id} already exists"); } } // create the branch if it does not already exist EnsureBranchExists(); // create a file system tree for the actions var root = new Directory(null, "root"); foreach (var syncAction in syncActions) { PathValidator.EnsureIsRootedPath(syncAction.Path); var directory = DirectoryHelper.GetOrAddDirectory(root, GetRelativeSyncActionDirectoryPath(syncAction)); directory.Add(d => new SyncActionFile(d, syncAction)); } // store the actions using (var workingDir = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { // create the actions on disk var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectoryInPlace(root, workingDir.Location); // commit (no check if there are pending changes, because there will always be changes (we made sure that syncActions is not empty and action do not yet exist)) workingDir.Commit($"{nameof(GitSyncActionService)}: Added {syncActions.Count()} items"); workingDir.Push(); } }
public void GetChangedFiles_detects_deletions_of_files() { //ARRANGE var state1 = new Directory(s_Dir1) { d => new EmptyFile(d, s_File1) { LastWriteTime = DateTime.Now.AddDays(-2) } }; var state2 = new Directory(s_Dir1); var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot1.Id, snapshot2.Id); //ASSERT Assert.Single(changedFiles); Assert.Equal(state1.GetFile(s_File1).Path, changedFiles.Single()); }
public void GetChangedFiles_ignores_Deletions_of_empty_directories() { //ARRANGE var state1 = new Directory(s_Dir1) { dir1 => new Directory(dir1, s_Dir2) }; var state2 = new Directory(s_Dir1); var snapshot1 = m_Instance.CreateSnapshot(state1); var snapshot2 = m_Instance.CreateSnapshot(state2); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot1.Id, snapshot2.Id); //ASSERT Assert.Empty(changedFiles); }
public void GetChangedFiles_ignores_Changes_outside_of_the_Snapshot_directory() { //ARRANGE var state1 = new Directory(s_Dir1); var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, "file1") }; var snapshot1 = m_Instance.CreateSnapshot(state1); // create unrelated change between two snapshots using (var workingDirectory = new TemporaryWorkingDirectory(m_Repository.Info.Path, m_Instance.Id)) { NativeFile.WriteAllText(Path.Combine(workingDirectory.Location, "foo"), "Hello World"); workingDirectory.Commit(); workingDirectory.Push(); } var snapshot2 = m_Instance.CreateSnapshot(state2); Assert.NotEqual(snapshot1.Id, snapshot2.Id); //ACT var changedFiles = m_Instance.GetChangedFiles(snapshot1.Id, snapshot2.Id); //ASSERT Assert.Single(changedFiles); }
public void Convert_detection_of_directory_properties_files_is_case_invariant() { var directory = new Directory(Path.GetRandomFileName()); var directoryPropertiesFile = DirectoryPropertiesFile.ForDirectory(null, directory); var mock = new Mock<IReadableFile>(); mock.Setup(f => f.Name).Returns(DirectoryPropertiesFile.FileName.ToUpper()); mock.Setup(f => f.OpenRead()).Returns(directoryPropertiesFile.OpenRead()); directory.Add(dir => mock.Object); var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(directory)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(DirectoryPropertiesFile.FileName) is DirectoryPropertiesFile); } }
public void CreateSnapshot_creates_a_new_snapshot_if_state_was_modified() { var dateTime1 = DateTime.Now; var dateTime2 = dateTime1.AddDays(-1); var state1 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, s_File1) { LastWriteTime = dateTime1 } }; var state2 = new Directory(s_Dir1) { dir1 => new EmptyFile(dir1, s_File1) {LastWriteTime = dateTime2 } }; var commitCount = m_Repository.GetAllCommits().Count(); var snapshot1 = m_Instance.CreateSnapshot(state1); Assert.Equal(commitCount + 1 , m_Repository.GetAllCommits().Count()); var snapshot2 = m_Instance.CreateSnapshot(state2); Assert.Equal(commitCount + 2, m_Repository.GetAllCommits().Count()); Assert.NotEqual(snapshot1.Id, snapshot2.Id); }