History

linked::Db::init() with a few tests

Sebastian Thiel committed 5 years ago

Commit 4c77e4c1 parent e64d1484 files · +52 −9Browse files

Unified · Split

4 files
  1. git-odb/src/alternate/mod.rs +2 −2
  2. git-odb/src/linked.rs +30 −5
  3. git-odb/tests/alternate/mod.rs +4 −1
  4. git-odb/tests/linked/mod.rs +16 −1
git-odb/src/alternate/mod.rs+2−2
@@ -40,8 +40,8 @@ pub enum Error { · 39 unmodified lines
4040
4141 /// Given an objects directory, try to resolve alternate object directories possibly located in the
4242 /// `./info/alternates` file.
43−/// If no alternate object database was resolved, the resulting `Vec` is empty, and it is not an error
44−/// if there are no alternates.
43+/// If no alternate object database was resolved, the resulting `Vec` is empty (it is not an error
44+/// if there are no alternates).
4545 /// It is an error once a repository is seen again as it would lead to a cycle.
4646 pub fn resolve(objects_directory: impl Into<PathBuf>) -> Result<Vec<PathBuf>, Error> {
4747 let relative_base = objects_directory.into();
git-odb/src/linked.rs+30−5
@@ -3,14 +3,39 @@ use crate::compound; · 2 unmodified lines
33
44 #[allow(missing_docs)]
55 pub struct Db {
6− dbs: Vec<compound::Db>,
6+ pub dbs: Vec<compound::Db>,
77 }
88
9−use std::path::PathBuf;
9+pub mod init {
10+ use crate::{alternate, compound, linked};
11+ use std::path::PathBuf;
1012
11−impl Db {
13+ /// The error returned by [`linked::Db::at()`]
14+ #[derive(Debug, thiserror::Error)]
1215 #[allow(missing_docs)]
13− pub fn at(objects_directory: impl Into<PathBuf>) -> Result<Self, compound::init::Error> {
14− unimplemented!("todo")
16+ pub enum Error {
17+ #[error(transparent)]
18+ CompoundDbInit(#[from] compound::init::Error),
19+ #[error(transparent)]
20+ AlternateResolve(#[from] alternate::Error),
21+ }
22+
23+ impl linked::Db {
24+ #[allow(missing_docs)]
25+ pub fn at(objects_directory: impl Into<PathBuf>) -> Result<Self, Error> {
26+ let mut dbs = vec![compound::Db::at(objects_directory.into())?];
27+ for object_path in alternate::resolve(dbs[0].loose.path.clone())?.into_iter() {
28+ dbs.push(compound::Db::at(object_path)?);
29+ }
30+ Ok(linked::Db { dbs })
31+ }
32+ }
33+
34+ impl std::convert::TryFrom<PathBuf> for linked::Db {
35+ type Error = Error;
36+
37+ fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
38+ linked::Db::at(value)
39+ }
1540 }
1641 }
git-odb/tests/alternate/mod.rs+4−1
@@ -4,7 +4,10 @@ use std::{ · 3 unmodified lines
44 path::{Path, PathBuf},
55 };
66
7−fn alternate(objects_at: impl Into<PathBuf>, objects_to: impl Into<PathBuf>) -> Result<(PathBuf, PathBuf), io::Error> {
7+pub fn alternate(
8+ objects_at: impl Into<PathBuf>,
9+ objects_to: impl Into<PathBuf>,
10+) -> Result<(PathBuf, PathBuf), io::Error> {
811 alternate_with(objects_at, objects_to, None)
912 }
1013
git-odb/tests/linked/mod.rs+16−1
@@ -1,10 +1,25 @@
11 mod init {
2+ use crate::alternate::alternate;
23 use git_odb::linked;
4+ use std::convert::TryFrom;
5+
6+ #[test]
7+ fn multiple_linked_repositories_via_alternates() -> crate::Result {
8+ let tmp = tempdir::TempDir::new("alternates")?;
9+ let (object_path, linked_object_path) = alternate(tmp.path().join("a"), tmp.path().join("b"))?;
10+ let db = linked::Db::try_from(object_path.clone())?;
11+ assert_eq!(db.dbs.len(), 2);
12+ assert_eq!(db.dbs[0].loose.path, object_path);
13+ assert_eq!(db.dbs[1].loose.path, linked_object_path);
14+ Ok(())
15+ }
316
417 #[test]
518 fn a_linked_db_without_alternates() -> crate::Result {
619 let tmp = tempdir::TempDir::new("alternates")?;
7− let _ = linked::Db::at(tmp.path())?;
20+ let db = linked::Db::at(tmp.path())?;
21+ assert_eq!(db.dbs.len(), 1);
22+ assert_eq!(db.dbs[0].loose.path, tmp.path());
823 Ok(())
924 }
1025 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t