jj/lib/tests/test_view.rs
Martin von Zweigbergk f1666375bd repo: replace Repo trait by enum with readonly and mutable variants
I want to keep the index updated within the transaction. I tried doing
that by adding a `trait Index`, implemented by `ReadonlyIndex` and
`MutableIndex`. However, `ReadonlyRepo::index` is of type
`Mutex<Option<Arc<IndexFile>>>` (because it is lazily initialized),
and we cannot get a `&dyn Index` that lives long enough to be returned
from a `Repo::index()` from that. It seems the best solution is to
instead create an `Index` enum (instead of a trait), with one readonly
and one mutable variant. This commit starts the migration to that
design by replacing the `Repo` trait by an enum. I never intended for
there there to be more implementations of `Repo` than `ReadonlyRepo`
and `MutableRepo` anyway.
2021-02-13 08:31:23 -08:00

86 lines
3.1 KiB
Rust

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use jujube_lib::testutils;
use jujube_lib::view::View;
use maplit::hashset;
use test_case::test_case;
#[test_case(false ; "local store")]
#[test_case(true ; "git store")]
fn test_heads_empty(use_git: bool) {
let settings = testutils::user_settings();
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let wc = repo.working_copy_locked();
assert_eq!(*repo.view().heads(), hashset! {wc.current_commit_id()});
}
#[test_case(false ; "local store")]
#[test_case(true ; "git store")]
fn test_heads_fork(use_git: bool) {
let settings = testutils::user_settings();
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let mut tx = repo.start_transaction("test");
let initial = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![repo.store().root_commit_id().clone()])
.write_to_transaction(&mut tx);
let child1 = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![initial.id().clone()])
.write_to_transaction(&mut tx);
let child2 = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![initial.id().clone()])
.write_to_transaction(&mut tx);
let wc = repo.working_copy_locked();
assert_eq!(
*tx.as_repo_ref().view().heads(),
hashset! {
wc.current_commit_id(),
child1.id().clone(),
child2.id().clone(),
}
);
tx.discard();
}
#[test_case(false ; "local store")]
#[test_case(true ; "git store")]
fn test_heads_merge(use_git: bool) {
let settings = testutils::user_settings();
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let mut tx = repo.start_transaction("test");
let initial = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![repo.store().root_commit_id().clone()])
.write_to_transaction(&mut tx);
let child1 = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![initial.id().clone()])
.write_to_transaction(&mut tx);
let child2 = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![initial.id().clone()])
.write_to_transaction(&mut tx);
let merge = testutils::create_random_commit(&settings, &repo)
.set_parents(vec![child1.id().clone(), child2.id().clone()])
.write_to_transaction(&mut tx);
let wc = repo.working_copy_locked();
assert_eq!(
*tx.as_repo_ref().view().heads(),
hashset! {wc.current_commit_id(), merge.id().clone()}
);
tx.discard();
}