manifest: move TestStore to the testutil module

Summary:
The TestStore can be leveraged by methods outside the manifest-tree crate.
Anything that wants to instantiate a manifest for test purposes could
leverage it.

Reviewed By: quark-zju

Differential Revision: D19204894

fbshipit-source-id: 4ac42d09855c70f829feefc6c71dcdbf7211cae3
This commit is contained in:
Stefan Filip 2020-01-10 11:10:52 -08:00 committed by Facebook Github Bot
parent 88357d163a
commit 526b029faf
5 changed files with 54 additions and 65 deletions

View File

@ -390,7 +390,7 @@ mod tests {
use pathmatcher::{AlwaysMatcher, TreeMatcher};
use types::testutil::*;
use crate::{link::DirLink, store::TestStore, testutil::*, Link};
use crate::{link::DirLink, testutil::*, Link};
#[test]
fn test_diff_entry_from_file() {

View File

@ -266,7 +266,7 @@ mod tests {
use pathmatcher::{AlwaysMatcher, TreeMatcher};
use types::testutil::*;
use crate::{store::TestStore, testutil::*};
use crate::testutil::*;
#[test]
fn test_items_empty() {

View File

@ -679,7 +679,7 @@ mod tests {
use manifest::FileType;
use types::{hgid::NULL_ID, testutil::*};
use self::{store::TestStore, testutil::*};
use self::testutil::*;
#[test]
fn test_insert() {

View File

@ -257,61 +257,6 @@ impl Element {
}
}
#[cfg(test)]
use parking_lot::{Mutex, RwLock};
#[cfg(test)]
use std::collections::HashMap;
#[cfg(test)]
use types::RepoPathBuf;
#[cfg(test)]
/// An in memory `Store` implementation backed by HashMaps. Primarily intended for tests.
pub struct TestStore {
entries: RwLock<HashMap<RepoPathBuf, HashMap<HgId, Bytes>>>,
pub prefetched: Mutex<Vec<Vec<Key>>>,
}
#[cfg(test)]
impl TestStore {
pub fn new() -> Self {
TestStore {
entries: RwLock::new(HashMap::new()),
prefetched: Mutex::new(Vec::new()),
}
}
#[allow(unused)]
pub fn fetches(&self) -> Vec<Vec<Key>> {
self.prefetched.lock().clone()
}
}
#[cfg(test)]
impl TreeStore for TestStore {
fn get(&self, path: &RepoPath, hgid: HgId) -> Result<Bytes> {
let underlying = self.entries.read();
let result = underlying
.get(path)
.and_then(|hgid_hash| hgid_hash.get(&hgid))
.map(|entry| entry.clone());
result.ok_or_else(|| format_err!("Could not find manifest entry for ({}, {})", path, hgid))
}
fn insert(&self, path: &RepoPath, hgid: HgId, data: Bytes) -> Result<()> {
let mut underlying = self.entries.write();
underlying
.entry(path.to_owned())
.or_insert(HashMap::new())
.insert(hgid, data);
Ok(())
}
fn prefetch(&self, keys: Vec<Key>) -> Result<()> {
self.prefetched.lock().push(keys);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -5,17 +5,16 @@
* GNU General Public License version 2.
*/
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};
use anyhow::Result;
use anyhow::{format_err, Result};
use bytes::Bytes;
use parking_lot::{Mutex, RwLock};
use manifest::{File, FileMetadata, Manifest};
use types::{testutil::*, HgId, RepoPath};
use types::{testutil::*, HgId, Key, RepoPath, RepoPathBuf};
use crate::{
store::{self, TestStore},
Link, TreeManifest,
};
use crate::{store, Link, TreeManifest, TreeStore};
pub(crate) fn store_element(path: &str, hex: &str, flag: store::Flag) -> Result<store::Element> {
Ok(store::Element::new(
@ -54,3 +53,48 @@ pub(crate) fn make_tree<'a>(
}
tree
}
/// An in memory `Store` implementation backed by HashMaps. Primarily intended for tests.
pub struct TestStore {
entries: RwLock<HashMap<RepoPathBuf, HashMap<HgId, Bytes>>>,
pub prefetched: Mutex<Vec<Vec<Key>>>,
}
impl TestStore {
pub fn new() -> Self {
TestStore {
entries: RwLock::new(HashMap::new()),
prefetched: Mutex::new(Vec::new()),
}
}
#[allow(unused)]
pub fn fetches(&self) -> Vec<Vec<Key>> {
self.prefetched.lock().clone()
}
}
impl TreeStore for TestStore {
fn get(&self, path: &RepoPath, hgid: HgId) -> Result<Bytes> {
let underlying = self.entries.read();
let result = underlying
.get(path)
.and_then(|hgid_hash| hgid_hash.get(&hgid))
.map(|entry| entry.clone());
result.ok_or_else(|| format_err!("Could not find manifest entry for ({}, {})", path, hgid))
}
fn insert(&self, path: &RepoPath, hgid: HgId, data: Bytes) -> Result<()> {
let mut underlying = self.entries.write();
underlying
.entry(path.to_owned())
.or_insert(HashMap::new())
.insert(hgid, data);
Ok(())
}
fn prefetch(&self, keys: Vec<Key>) -> Result<()> {
self.prefetched.lock().push(keys);
Ok(())
}
}