From d4786a3256f55ab222473ce8e313c1d23cc19eb3 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 1 Nov 2024 14:57:34 +0900 Subject: [PATCH] testutils: move load_repo_at_head() to TestEnvironment It will depend on the TestBackendData mapping. --- lib/tests/test_bad_locking.rs | 6 +++--- lib/tests/test_commit_concurrent.rs | 6 +++--- lib/tests/test_git.rs | 9 +++++---- lib/tests/test_index.rs | 22 ++++++++++++++-------- lib/testutils/src/lib.rs | 18 +++++++++++------- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/tests/test_bad_locking.rs b/lib/tests/test_bad_locking.rs index 2fb3ca4870..784df6dbec 100644 --- a/lib/tests/test_bad_locking.rs +++ b/lib/tests/test_bad_locking.rs @@ -21,7 +21,6 @@ use jj_lib::workspace::default_working_copy_factories; use jj_lib::workspace::Workspace; use test_case::test_case; use testutils::create_random_commit; -use testutils::load_repo_at_head; use testutils::TestRepoBackend; use testutils::TestWorkspace; @@ -189,6 +188,7 @@ fn test_bad_locking_interrupted(backend: TestRepoBackend) { // operation. let settings = testutils::user_settings(); let test_workspace = TestWorkspace::init_with_backend(&settings, backend); + let test_env = &test_workspace.env; let repo = &test_workspace.repo; let mut tx = repo.start_transaction(&settings); @@ -214,10 +214,10 @@ fn test_bad_locking_interrupted(backend: TestRepoBackend) { copy_directory(&backup_path, &op_heads_dir); // Reload the repo and check that only the new head is present. - let reloaded_repo = load_repo_at_head(&settings, test_workspace.repo_path()); + let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); assert_eq!(reloaded_repo.op_id(), &op_id); // Reload once more to make sure that the .jj/op_heads/ directory was updated // correctly. - let reloaded_repo = load_repo_at_head(&settings, test_workspace.repo_path()); + let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); assert_eq!(reloaded_repo.op_id(), &op_id); } diff --git a/lib/tests/test_commit_concurrent.rs b/lib/tests/test_commit_concurrent.rs index 540bd01748..e2ff2551b5 100644 --- a/lib/tests/test_commit_concurrent.rs +++ b/lib/tests/test_commit_concurrent.rs @@ -20,7 +20,6 @@ use jj_lib::dag_walk; use jj_lib::repo::ReadonlyRepo; use jj_lib::repo::Repo; use test_case::test_case; -use testutils::load_repo_at_head; use testutils::write_random_commit; use testutils::TestRepoBackend; use testutils::TestWorkspace; @@ -81,12 +80,13 @@ fn test_commit_parallel_instances(backend: TestRepoBackend) { // makes it behave very similar to separate processes. let settings = testutils::user_settings(); let test_workspace = TestWorkspace::init_with_backend(&settings, backend); + let test_env = &test_workspace.env; let num_threads = max(num_cpus::get(), 4); thread::scope(|s| { for _ in 0..num_threads { let settings = settings.clone(); - let repo = load_repo_at_head(&settings, test_workspace.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); s.spawn(move || { let mut tx = repo.start_transaction(&settings); write_random_commit(tx.repo_mut(), &settings); @@ -96,7 +96,7 @@ fn test_commit_parallel_instances(backend: TestRepoBackend) { }); // One commit per thread plus the commit from the initial working-copy commit on // top of the root commit - let repo = load_repo_at_head(&settings, test_workspace.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); assert_eq!(repo.view().heads().len(), num_threads + 1); // One additional operation for the root operation, one for checking out the diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index 4d51d773b2..7a87f3835f 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -66,7 +66,6 @@ use tempfile::TempDir; use test_case::test_case; use testutils::commit_transactions; use testutils::create_random_commit; -use testutils::load_repo_at_head; use testutils::write_random_commit; use testutils::TestRepo; use testutils::TestRepoBackend; @@ -3223,6 +3222,7 @@ fn test_rewrite_imported_commit() { fn test_concurrent_write_commit() { let settings = &testutils::user_settings(); let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Try to create identical commits with different change ids. Timestamp of the @@ -3232,7 +3232,7 @@ fn test_concurrent_write_commit() { thread::scope(|s| { let barrier = Arc::new(Barrier::new(num_thread)); for i in 0..num_thread { - let repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader + let repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader let barrier = barrier.clone(); let sender = sender.clone(); s.spawn(move || { @@ -3285,6 +3285,7 @@ fn test_concurrent_write_commit() { fn test_concurrent_read_write_commit() { let settings = &testutils::user_settings(); let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Create unique commits and load them concurrently. In this test, we assume @@ -3318,7 +3319,7 @@ fn test_concurrent_read_write_commit() { // Writer assigns random change id for (i, commit_id) in commit_ids.iter().enumerate() { - let repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader + let repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader let barrier = barrier.clone(); s.spawn(move || { barrier.wait(); @@ -3334,7 +3335,7 @@ fn test_concurrent_read_write_commit() { // Reader may generate change id (if not yet assigned by the writer) for i in 0..num_reader_thread { - let mut repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader + let mut repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader let barrier = barrier.clone(); let mut pending_commit_ids = commit_ids.clone(); pending_commit_ids.rotate_left(i); // start lookup from different place diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index a5d8891e45..7f71a7305c 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -42,7 +42,6 @@ use jj_lib::settings::UserSettings; use maplit::hashset; use testutils::commit_transactions; use testutils::create_random_commit; -use testutils::load_repo_at_head; use testutils::test_backend::TestBackend; use testutils::write_random_commit; use testutils::CommitGraphBuilder; @@ -295,6 +294,7 @@ fn test_index_commits_previous_operations() { // Test that commits visible only in previous operations are indexed. let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Remove commit B and C in one operation and make sure they're still @@ -322,7 +322,7 @@ fn test_index_commits_previous_operations() { repo.index_store().as_any().downcast_ref().unwrap(); default_index_store.reinit().unwrap(); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); let index = as_readonly_composite(&repo); // There should be the root commit, plus 3 more assert_eq!(index.num_commits(), 1 + 3); @@ -342,6 +342,7 @@ fn test_index_commits_hidden_but_referenced() { // Test that hidden-but-referenced commits are indexed. let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Remote bookmarks are usually visible at a certain point in operation @@ -378,7 +379,7 @@ fn test_index_commits_hidden_but_referenced() { repo.index_store().as_any().downcast_ref().unwrap(); default_index_store.reinit().unwrap(); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); // All commits should be reindexed assert!(repo.index().has_id(commit_a.id())); assert!(repo.index().has_id(commit_b.id())); @@ -389,6 +390,7 @@ fn test_index_commits_hidden_but_referenced() { fn test_index_commits_incremental() { let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Create A in one operation, then B and C in another. Check that the index is @@ -420,7 +422,7 @@ fn test_index_commits_incremental() { .unwrap(); tx.commit("test"); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); let index = as_readonly_composite(&repo); // There should be the root commit, plus 3 more assert_eq!(index.num_commits(), 1 + 3); @@ -442,6 +444,7 @@ fn test_index_commits_incremental() { fn test_index_commits_incremental_empty_transaction() { let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; // Create A in one operation, then just an empty transaction. Check that the @@ -464,7 +467,7 @@ fn test_index_commits_incremental_empty_transaction() { repo.start_transaction(&settings).commit("test"); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); let index = as_readonly_composite(&repo); // There should be the root commit, plus 1 more assert_eq!(index.num_commits(), 1 + 1); @@ -613,6 +616,7 @@ fn test_index_commits_incremental_squashed() { fn test_reindex_no_segments_dir() { let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; let mut tx = repo.start_transaction(&settings); @@ -625,7 +629,7 @@ fn test_reindex_no_segments_dir() { assert!(segments_dir.is_dir()); fs::remove_dir_all(&segments_dir).unwrap(); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); assert!(repo.index().has_id(commit_a.id())); } @@ -633,6 +637,7 @@ fn test_reindex_no_segments_dir() { fn test_reindex_corrupt_segment_files() { let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; let mut tx = repo.start_transaction(&settings); @@ -653,7 +658,7 @@ fn test_reindex_corrupt_segment_files() { fs::write(entry.path(), b"\0".repeat(24)).unwrap(); } - let repo = load_repo_at_head(&settings, test_repo.repo_path()); + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); assert!(repo.index().has_id(commit_a.id())); } @@ -710,6 +715,7 @@ fn test_reindex_from_merged_operation() { fn test_reindex_missing_commit() { let settings = testutils::user_settings(); let test_repo = TestRepo::init(); + let test_env = &test_repo.env; let repo = &test_repo.repo; let mut tx = repo.start_transaction(&settings); @@ -724,7 +730,7 @@ fn test_reindex_missing_commit() { // Remove historical head commit to simulate bad GC. let test_backend: &TestBackend = repo.store().backend_impl().downcast_ref().unwrap(); test_backend.remove_commit_unchecked(missing_commit.id()); - let repo = load_repo_at_head(&settings, test_repo.repo_path()); // discard cache + let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); // discard cache assert!(repo.store().get_commit(missing_commit.id()).is_err()); // Reindexing error should include the operation id where the commit diff --git a/lib/testutils/src/lib.rs b/lib/testutils/src/lib.rs index f0b6f56bc5..d5fafc20c0 100644 --- a/lib/testutils/src/lib.rs +++ b/lib/testutils/src/lib.rs @@ -133,6 +133,17 @@ impl TestEnvironment { pub fn root(&self) -> &Path { self.temp_dir.path() } + + pub fn load_repo_at_head( + &self, + settings: &UserSettings, + repo_path: &Path, + ) -> Arc { + RepoLoader::init_from_file_system(settings, repo_path, &TestRepo::default_store_factories()) + .unwrap() + .load_at_head(settings) + .unwrap() + } } pub struct TestRepo { @@ -295,13 +306,6 @@ impl TestWorkspace { } } -pub fn load_repo_at_head(settings: &UserSettings, repo_path: &Path) -> Arc { - RepoLoader::init_from_file_system(settings, repo_path, &TestRepo::default_store_factories()) - .unwrap() - .load_at_head(settings) - .unwrap() -} - pub fn commit_transactions(settings: &UserSettings, txs: Vec) -> Arc { let repo_loader = txs[0].base_repo().loader().clone(); let mut op_ids = vec![];