Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an enum for backend to use in tests #2265

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
testutils: add an enum for TestRepo backend
I plan to add another backend for use in tests.
  • Loading branch information
martinvonz committed Sep 18, 2023
commit 69050158de57ca72e1ad785e8ef7344b0974b63c
63 changes: 36 additions & 27 deletions lib/testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,50 @@ pub struct TestRepo {
pub repo: Arc<ReadonlyRepo>,
}

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum TestRepoBackend {
Git,
Local,
}

impl TestRepoBackend {
fn init_backend(&self, store_path: &Path) -> Result<Box<dyn Backend>, BackendInitError> {
match self {
TestRepoBackend::Git => Ok(Box::new(GitBackend::init_internal(store_path)?)),
TestRepoBackend::Local => Ok(Box::new(LocalBackend::init(store_path))),
}
}
}

impl TestRepo {
pub fn init(use_git: bool) -> Self {
let backend = if use_git {
TestRepoBackend::Git
} else {
TestRepoBackend::Local
};
Self::init_with_backend(backend)
}

pub fn init_with_backend(backend: TestRepoBackend) -> Self {
let settings = user_settings();
let temp_dir = new_temp_dir();

let repo_dir = temp_dir.path().join("repo");
fs::create_dir(&repo_dir).unwrap();

let repo = if use_git {
ReadonlyRepo::init(
&settings,
&repo_dir,
|store_path| -> Result<Box<dyn Backend>, BackendInitError> {
Ok(Box::new(GitBackend::init_internal(store_path)?))
},
ReadonlyRepo::default_op_store_factory(),
ReadonlyRepo::default_op_heads_store_factory(),
ReadonlyRepo::default_index_store_factory(),
ReadonlyRepo::default_submodule_store_factory(),
)
.unwrap()
} else {
ReadonlyRepo::init(
&settings,
&repo_dir,
|store_path| -> Result<Box<dyn Backend>, BackendInitError> {
Ok(Box::new(LocalBackend::init(store_path)))
},
ReadonlyRepo::default_op_store_factory(),
ReadonlyRepo::default_op_heads_store_factory(),
ReadonlyRepo::default_index_store_factory(),
ReadonlyRepo::default_submodule_store_factory(),
)
.unwrap()
};
let repo = ReadonlyRepo::init(
&settings,
&repo_dir,
|store_path| -> Result<Box<dyn Backend>, BackendInitError> {
backend.init_backend(store_path)
},
ReadonlyRepo::default_op_store_factory(),
ReadonlyRepo::default_op_heads_store_factory(),
ReadonlyRepo::default_index_store_factory(),
ReadonlyRepo::default_submodule_store_factory(),
)
.unwrap();

Self {
_temp_dir: temp_dir,
Expand Down