From 54f1d310c4d685a2b5621a00650098fd508c44ca Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Mon, 14 Aug 2023 16:34:46 -0700 Subject: [PATCH] testutils: propagate snapshot errors --- lib/tests/test_working_copy.rs | 28 +++++++++++------------ lib/tests/test_working_copy_concurrent.rs | 2 +- lib/tests/test_working_copy_sparse.rs | 6 ++--- lib/testutils/src/lib.rs | 13 +++++------ 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/tests/test_working_copy.rs b/lib/tests/test_working_copy.rs index d29cde0a3..bbc6d1805 100644 --- a/lib/tests/test_working_copy.rs +++ b/lib/tests/test_working_copy.rs @@ -29,7 +29,7 @@ use jj_lib::repo::{ReadonlyRepo, Repo}; use jj_lib::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin}; use jj_lib::settings::UserSettings; use jj_lib::tree_builder::TreeBuilder; -use jj_lib::working_copy::{LockedWorkingCopy, SnapshotOptions, WorkingCopy}; +use jj_lib::working_copy::{LockedWorkingCopy, SnapshotError, SnapshotOptions, WorkingCopy}; use test_case::test_case; use testutils::{create_tree, write_random_commit, TestWorkspace}; @@ -42,7 +42,7 @@ fn test_root(use_git: bool) { let wc = test_workspace.workspace.working_copy(); assert_eq!(wc.sparse_patterns().unwrap(), vec![RepoPath::root()]); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); let repo = &test_workspace.repo; let wc_commit_id = repo .view() @@ -199,7 +199,7 @@ fn test_checkout_file_transitions(use_git: bool) { .unwrap(); // Check that the working copy is clean. - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(*new_tree.id(), right_tree_id); for (_left_kind, right_kind, path) in &files { @@ -341,7 +341,7 @@ fn test_reset() { locked_wc.finish(op_id.clone()).unwrap(); assert!(ignored_path.to_fs_path(&workspace_root).is_file()); assert!(!wc.file_states().unwrap().contains_key(&ignored_path)); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(new_tree.id(), tree_without_file.id()); // Now test the opposite direction: resetting to a commit where the file is @@ -352,7 +352,7 @@ fn test_reset() { locked_wc.finish(op_id.clone()).unwrap(); assert!(ignored_path.to_fs_path(&workspace_root).is_file()); assert!(wc.file_states().unwrap().contains_key(&ignored_path)); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(new_tree.id(), tree_with_file.id()); } @@ -479,7 +479,7 @@ fn test_snapshot_special_file() { // Replace a regular file by a socket and snapshot the working copy again std::fs::remove_file(&file1_disk_path).unwrap(); UnixListener::bind(&file1_disk_path).unwrap(); - let tree = test_workspace.snapshot(); + let tree = test_workspace.snapshot().unwrap(); // Only the regular file should be in the tree assert_eq!( tree.entries().map(|(path, _value)| path).collect_vec(), @@ -515,7 +515,7 @@ fn test_gitignores(use_git: bool) { std::fs::create_dir(workspace_root.join("dir")).unwrap(); testutils::write_working_copy_file(&workspace_root, &subdir_modified_path, "1"); - let tree1 = test_workspace.snapshot(); + let tree1 = test_workspace.snapshot().unwrap(); let files1 = tree1.entries().map(|(name, _value)| name).collect_vec(); assert_eq!( files1, @@ -539,7 +539,7 @@ fn test_gitignores(use_git: bool) { testutils::write_working_copy_file(&workspace_root, &subdir_modified_path, "2"); testutils::write_working_copy_file(&workspace_root, &subdir_ignored_path, "2"); - let tree2 = test_workspace.snapshot(); + let tree2 = test_workspace.snapshot().unwrap(); let files2 = tree2.entries().map(|(name, _value)| name).collect_vec(); assert_eq!( files2, @@ -574,7 +574,7 @@ fn test_gitignores_in_ignored_dir(use_git: bool) { testutils::write_working_copy_file(&workspace_root, &nested_gitignore_path, "!file\n"); testutils::write_working_copy_file(&workspace_root, &ignored_path, "contents"); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!( new_tree.entries().collect_vec(), tree1.entries().collect_vec() @@ -593,7 +593,7 @@ fn test_gitignores_in_ignored_dir(use_git: bool) { locked_wc.reset(&tree2).unwrap(); locked_wc.finish(OperationId::from_hex("abc123")).unwrap(); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!( new_tree.entries().collect_vec(), tree2.entries().collect_vec() @@ -666,7 +666,7 @@ fn test_gitignores_ignored_directory_already_tracked(use_git: bool) { // deleted from the resulting tree. std::fs::write(modified_path.to_fs_path(&workspace_root), "modified").unwrap(); std::fs::remove_file(deleted_path.to_fs_path(&workspace_root)).unwrap(); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); let expected_tree = create_tree( &repo, &[ @@ -701,7 +701,7 @@ fn test_dotgit_ignored(use_git: bool) { &RepoPath::from_internal_string(".git/file"), "contents", ); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(new_tree.id(), store.empty_tree_id()); std::fs::remove_dir_all(&dotgit_path).unwrap(); @@ -711,7 +711,7 @@ fn test_dotgit_ignored(use_git: bool) { &RepoPath::from_internal_string(".git"), "contents", ); - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(new_tree.id(), store.empty_tree_id()); } @@ -763,7 +763,7 @@ fn test_gitsubmodule() { // Check that the files present in the submodule are not tracked // when we snapshot - let new_tree = test_workspace.snapshot(); + let new_tree = test_workspace.snapshot().unwrap(); assert_eq!(*new_tree.id(), tree_id); // Check that the files in the submodule are not deleted diff --git a/lib/tests/test_working_copy_concurrent.rs b/lib/tests/test_working_copy_concurrent.rs index 177512249..37e70609a 100644 --- a/lib/tests/test_working_copy_concurrent.rs +++ b/lib/tests/test_working_copy_concurrent.rs @@ -166,7 +166,7 @@ fn test_racy_checkout() { // A file written right after checkout (hopefully, from the test's perspective, // within the file system timestamp granularity) is detected as changed. write_working_copy_file(&workspace_root, &path, "x"); - let modified_tree = test_workspace.snapshot(); + let modified_tree = test_workspace.snapshot().unwrap(); if modified_tree.id() == tree.id() { num_matches += 1; } diff --git a/lib/tests/test_working_copy_sparse.rs b/lib/tests/test_working_copy_sparse.rs index 8a4cea87a..de675ddfd 100644 --- a/lib/tests/test_working_copy_sparse.rs +++ b/lib/tests/test_working_copy_sparse.rs @@ -167,7 +167,7 @@ fn test_sparse_commit() { // Create a tree from the working copy. Only dir1/file1 should be updated in the // tree. - let modified_tree = test_workspace.snapshot(); + let modified_tree = test_workspace.snapshot().unwrap(); let diff = tree.diff(&modified_tree, &EverythingMatcher).collect_vec(); assert_eq!(diff.len(), 1); assert_eq!(diff[0].0, dir1_file1_path); @@ -181,7 +181,7 @@ fn test_sparse_commit() { // Create a tree from the working copy. Only dir1/file1 and dir2/file1 should be // updated in the tree. - let modified_tree = test_workspace.snapshot(); + let modified_tree = test_workspace.snapshot().unwrap(); let diff = tree.diff(&modified_tree, &EverythingMatcher).collect_vec(); assert_eq!(diff.len(), 2); assert_eq!(diff[0].0, dir1_file1_path); @@ -216,7 +216,7 @@ fn test_sparse_commit_gitignore() { // Create a tree from the working copy. Only dir1/file2 should be updated in the // tree because dir1/file1 is ignored. - let modified_tree = test_workspace.snapshot(); + let modified_tree = test_workspace.snapshot().unwrap(); let entries = modified_tree.entries().collect_vec(); assert_eq!(entries.len(), 1); assert_eq!(entries[0].0, dir1_file2_path); diff --git a/lib/testutils/src/lib.rs b/lib/testutils/src/lib.rs index 5516dda0c..940e4dc47 100644 --- a/lib/testutils/src/lib.rs +++ b/lib/testutils/src/lib.rs @@ -31,7 +31,7 @@ use jj_lib::store::Store; use jj_lib::transaction::Transaction; use jj_lib::tree::Tree; use jj_lib::tree_builder::TreeBuilder; -use jj_lib::working_copy::SnapshotOptions; +use jj_lib::working_copy::{SnapshotOptions, SnapshotError}; use jj_lib::workspace::Workspace; use tempfile::TempDir; @@ -162,18 +162,17 @@ impl TestWorkspace { /// Snapshots the working copy and returns the tree. Updates the working /// copy state on disk, but does not update the working-copy commit (no /// new operation). - pub fn snapshot(&mut self) -> Tree { + pub fn snapshot(&mut self) -> Result { let mut locked_wc = self.workspace.working_copy_mut().start_mutation().unwrap(); let tree_id = locked_wc - .snapshot(SnapshotOptions::empty_for_test()) - .unwrap(); + .snapshot(SnapshotOptions::empty_for_test()); // arbitrary operation id locked_wc.finish(self.repo.op_id().clone()).unwrap(); - return self + Ok(self .repo .store() - .get_tree(&RepoPath::root(), &tree_id) - .unwrap(); + .get_tree(&RepoPath::root(), &tree_id?) + .unwrap()) } }