Skip to content

Commit

Permalink
testutils: move default_store_factories() to TestEnvironment
Browse files Browse the repository at this point in the history
It will capture the TestBackendData mapping.
  • Loading branch information
yuja committed Nov 1, 2024
1 parent d4786a3 commit 7b5df93
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/tests/test_load_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_load_at_operation() {
let loader = RepoLoader::init_from_file_system(
&settings,
test_repo.repo_path(),
&TestRepo::default_store_factories(),
&test_repo.env.default_store_factories(),
)
.unwrap();
let head_repo = loader.load_at_head(&settings).unwrap();
Expand All @@ -46,7 +46,7 @@ fn test_load_at_operation() {
let loader = RepoLoader::init_from_file_system(
&settings,
test_repo.repo_path(),
&TestRepo::default_store_factories(),
&test_repo.env.default_store_factories(),
)
.unwrap();
let old_repo = loader.load_at(repo.operation()).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions lib/tests/test_local_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use test_case::test_case;
use testutils::commit_with_tree;
use testutils::create_tree;
use testutils::write_random_commit;
use testutils::TestRepo;
use testutils::TestRepoBackend;
use testutils::TestWorkspace;

Expand Down Expand Up @@ -384,7 +383,7 @@ fn test_acl() {
let mut ws = Workspace::load(
&settings,
&workspace_root,
&TestRepo::default_store_factories(),
&test_workspace.env.default_store_factories(),
&default_working_copy_factories(),
)
.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions lib/tests/test_local_working_copy_concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use jj_lib::workspace::Workspace;
use testutils::commit_with_tree;
use testutils::create_tree;
use testutils::write_working_copy_file;
use testutils::TestRepo;
use testutils::TestWorkspace;

#[test]
Expand Down Expand Up @@ -58,7 +57,7 @@ fn test_concurrent_checkout() {
let mut ws2 = Workspace::load(
&settings,
&workspace1_root,
&TestRepo::default_store_factories(),
&test_workspace1.env.default_store_factories(),
&default_working_copy_factories(),
)
.unwrap();
Expand All @@ -75,7 +74,7 @@ fn test_concurrent_checkout() {
let ws3 = Workspace::load(
&settings,
&workspace1_root,
&TestRepo::default_store_factories(),
&test_workspace1.env.default_store_factories(),
&default_working_copy_factories(),
)
.unwrap();
Expand Down Expand Up @@ -113,6 +112,7 @@ fn test_checkout_parallel() {

thread::scope(|s| {
for tree_id in &tree_ids {
let test_env = &test_workspace.env;
let op_id = repo.op_id().clone();
let tree_ids = tree_ids.clone();
let commit = commit_with_tree(repo.store(), tree_id.clone());
Expand All @@ -122,7 +122,7 @@ fn test_checkout_parallel() {
let mut workspace = Workspace::load(
&settings,
&workspace_root,
&TestRepo::default_store_factories(),
&test_env.default_store_factories(),
&default_working_copy_factories(),
)
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions lib/tests/test_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ use jj_lib::workspace::default_working_copy_factories;
use jj_lib::workspace::default_working_copy_factory;
use jj_lib::workspace::Workspace;
use jj_lib::workspace::WorkspaceLoadError;
use testutils::TestRepo;
use testutils::TestEnvironment;
use testutils::TestWorkspace;

#[test]
fn test_load_bad_path() {
let settings = testutils::user_settings();
let temp_dir = testutils::new_temp_dir();
let workspace_root = temp_dir.path().to_owned();
let test_env = TestEnvironment::init();
let workspace_root = test_env.root().to_owned();
// We haven't created a repo in the workspace_root, so it should fail to load.
let result = Workspace::load(
&settings,
&workspace_root,
&TestRepo::default_store_factories(),
&test_env.default_store_factories(),
&default_working_copy_factories(),
);
assert_matches!(
Expand Down Expand Up @@ -77,7 +77,7 @@ fn test_init_additional_workspace() {
let same_workspace = Workspace::load(
&settings,
&ws2_root,
&TestRepo::default_store_factories(),
&test_workspace.env.default_store_factories(),
&default_working_copy_factories(),
);
assert!(same_workspace.is_ok());
Expand Down
32 changes: 16 additions & 16 deletions lib/testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,27 @@ impl TestEnvironment {
self.temp_dir.path()
}

pub fn default_store_factories(&self) -> StoreFactories {
let mut factories = StoreFactories::default();
factories.add_backend(
"test",
Box::new(|_settings, store_path| Ok(Box::new(TestBackend::load(store_path)))),
);
factories.add_backend(
SecretBackend::name(),
Box::new(|settings, store_path| {
Ok(Box::new(SecretBackend::load(settings, store_path)?))
}),
);
factories
}

pub fn load_repo_at_head(
&self,
settings: &UserSettings,
repo_path: &Path,
) -> Arc<ReadonlyRepo> {
RepoLoader::init_from_file_system(settings, repo_path, &TestRepo::default_store_factories())
RepoLoader::init_from_file_system(settings, repo_path, &self.default_store_factories())
.unwrap()
.load_at_head(settings)
.unwrap()
Expand Down Expand Up @@ -214,21 +229,6 @@ impl TestRepo {
}
}

pub fn default_store_factories() -> StoreFactories {
let mut factories = StoreFactories::default();
factories.add_backend(
"test",
Box::new(|_settings, store_path| Ok(Box::new(TestBackend::load(store_path)))),
);
factories.add_backend(
SecretBackend::name(),
Box::new(|settings, store_path| {
Ok(Box::new(SecretBackend::load(settings, store_path)?))
}),
);
factories
}

pub fn repo_path(&self) -> &Path {
&self.repo_path
}
Expand Down

0 comments on commit 7b5df93

Please sign in to comment.