add TryFrom<&PathBuf> for tests

This commit is contained in:
Nikita Galaiko 2024-02-13 12:16:24 +01:00 committed by Kiril Videlov
parent ddbeca298c
commit eb193a32fe
19 changed files with 159 additions and 54 deletions

View File

@ -33,6 +33,15 @@ impl TryFrom<&AppHandle> for Database {
}
}
#[cfg(test)]
impl TryFrom<&path::PathBuf> for Database {
type Error = anyhow::Error;
fn try_from(value: &path::PathBuf) -> Result<Self, Self::Error> {
Self::open(value.join("database.sqlite3"))
}
}
impl Database {
fn open<P: AsRef<path::Path>>(path: P) -> Result<Self> {
let path = path.as_ref();

View File

@ -145,7 +145,7 @@ mod tests {
#[test]
fn insert_query() -> Result<()> {
let db = test_utils::test_database();
let database = Database::from(db);
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();
@ -171,7 +171,7 @@ mod tests {
#[test]
fn insert_update() -> Result<()> {
let db = test_utils::test_database();
let database = Database::from(db);
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();
@ -204,7 +204,7 @@ mod tests {
#[test]
fn aggregate_deltas_by_file() -> Result<()> {
let db = test_utils::test_database();
let database = Database::from(db);
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();

View File

@ -93,6 +93,17 @@ impl TryFrom<&AppHandle> for Helper {
}
}
impl TryFrom<&std::path::PathBuf> for Helper {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
let keys = keys::Controller::try_from(value)?;
let users = users::Controller::try_from(value)?;
let home_dir = env::var_os("HOME").map(path::PathBuf::from);
Ok(Helper::new(keys, users, home_dir))
}
}
#[derive(Debug, thiserror::Error)]
pub enum HelpError {
#[error("no url set for remote")]
@ -420,14 +431,14 @@ mod tests {
fn run(&self) -> Vec<(String, Vec<Credential>)> {
let local_app_data = test_utils::temp_dir();
let users = users::Controller::from(&local_app_data);
let users = users::Controller::try_from(&local_app_data).unwrap();
let user = users::User {
github_access_token: self.github_access_token.map(ToString::to_string),
..Default::default()
};
users.set_user(&user).unwrap();
let keys = keys::Controller::from(&local_app_data);
let keys = keys::Controller::try_from(&local_app_data).unwrap();
let helper = Helper::new(keys, users, self.home_dir.clone());
let repo = test_repository();

View File

@ -22,6 +22,14 @@ impl TryFrom<&AppHandle> for Controller {
}
}
impl TryFrom<&std::path::PathBuf> for Controller {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Controller::new(Storage::try_from(value)?))
}
}
impl Controller {
fn new(storage: Storage) -> Self {
Self { storage }
@ -58,7 +66,7 @@ mod tests {
#[test]
fn test_get_or_create() {
let suite = Suite::default();
let controller = Controller::from(&suite.local_app_data);
let controller = Controller::try_from(&suite.local_app_data).unwrap();
let once = controller.get_or_create().unwrap();
let twice = controller.get_or_create().unwrap();

View File

@ -31,6 +31,14 @@ impl TryFrom<&AppHandle> for Storage {
}
}
impl TryFrom<&std::path::PathBuf> for Storage {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Storage::new(storage::Storage::try_from(value)?))
}
}
impl Storage {
fn new(storage: storage::Storage) -> Storage {
Storage { storage }

View File

@ -34,6 +34,19 @@ impl TryFrom<&AppHandle> for Controller {
}
}
impl TryFrom<&std::path::PathBuf> for Controller {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Self {
local_data_dir: value.clone(),
projects_storage: storage::Storage::try_from(value)?,
users: users::Controller::try_from(value)?,
watchers: None,
})
}
}
impl Controller {
pub fn add(&self, path: &path::Path) -> Result<Project, AddError> {
let all_projects = self

View File

@ -27,6 +27,14 @@ impl TryFrom<&AppHandle> for Storage {
}
}
impl TryFrom<&std::path::PathBuf> for Storage {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Storage::new(storage::Storage::try_from(value)?))
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct UpdateRequest {
pub id: ProjectId,

View File

@ -205,7 +205,9 @@ mod tests {
#[test]
fn test_insert_query() -> Result<()> {
let db = test_utils::test_database();
let database = Database::from(db);
println!("0");
let database = Database::new(db);
println!("1");
let project_id = ProjectId::generate();
let session1 = session::Session {
@ -246,7 +248,7 @@ mod tests {
#[test]
fn test_update() -> Result<()> {
let db = test_utils::test_database();
let database = Database::from(db);
let database = Database::new(db);
let project_id = ProjectId::generate();
let session = session::Session {

View File

@ -36,6 +36,14 @@ impl TryFrom<&AppHandle> for Storage {
}
}
impl TryFrom<&PathBuf> for Storage {
type Error = anyhow::Error;
fn try_from(value: &PathBuf) -> Result<Self, Self::Error> {
Ok(Storage::new(value))
}
}
impl Storage {
fn new<P: AsRef<Path>>(local_data_dir: P) -> Storage {
Storage {

View File

@ -15,10 +15,14 @@ pub struct Suite {
impl Default for Suite {
fn default() -> Self {
let local_app_data = temp_dir();
let storage = storage::Storage::from(&local_app_data);
let users = users::Controller::from(&storage);
let projects = projects::Controller::from(&local_app_data);
let keys = keys::Controller::from(&storage);
let storage =
storage::Storage::try_from(&local_app_data).expect("failed to create storage");
let users = users::Controller::try_from(&local_app_data)
.expect("failed to create users controller");
let projects = projects::Controller::try_from(&local_app_data)
.expect("failed to create projects controller");
let keys =
keys::Controller::try_from(&local_app_data).expect("failed to create keys controller");
Self {
storage,
local_app_data,
@ -86,7 +90,8 @@ impl<'a> Case<'a> {
let gb_repository =
gb_repository::Repository::open(&suite.local_app_data, &project_repository, None)
.expect("failed to open gb repository");
let credentials = git::credentials::Helper::from(&suite.local_app_data);
let credentials = git::credentials::Helper::try_from(&suite.local_app_data)
.expect("failed to get credentials");
Case {
suite,
project,
@ -105,7 +110,8 @@ impl<'a> Case<'a> {
let project_repository = project_repository::Repository::open(&project)
.expect("failed to create project repository");
let user = self.suite.users.get_user().expect("failed to get user");
let credentials = git::credentials::Helper::from(&self.suite.local_app_data);
let credentials = git::credentials::Helper::try_from(&self.suite.local_app_data)
.expect("failed to get credentials");
Self {
suite: self.suite,
gb_repository: gb_repository::Repository::open(
@ -122,8 +128,7 @@ impl<'a> Case<'a> {
}
pub fn test_database() -> database::Database {
let path = temp_dir().join("test.db");
database::Database::try_from(&path).unwrap()
database::Database::try_from(&temp_dir()).unwrap()
}
pub fn temp_dir() -> path::PathBuf {

View File

@ -22,6 +22,14 @@ impl TryFrom<&AppHandle> for Controller {
}
}
impl TryFrom<&std::path::PathBuf> for Controller {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Controller::new(Storage::try_from(value)?))
}
}
impl Controller {
fn new(storage: Storage) -> Controller {
Controller { storage }

View File

@ -32,6 +32,14 @@ impl TryFrom<&AppHandle> for Storage {
}
}
impl TryFrom<&std::path::PathBuf> for Storage {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
Ok(Storage::new(storage::Storage::try_from(value)?))
}
}
impl Storage {
fn new(storage: storage::Storage) -> Storage {
Storage { storage }

View File

@ -65,6 +65,7 @@ impl TryFrom<&AppHandle> for Handler {
}
impl Handler {
#[allow(clippy::too_many_arguments)]
fn new(
git_file_change_handler: git_file_change::Handler,
tick_handler: tick_handler::Handler,

View File

@ -38,6 +38,21 @@ impl TryFrom<&AppHandle> for Handler {
}
}
#[cfg(test)]
impl TryFrom<&std::path::PathBuf> for Handler {
type Error = anyhow::Error;
fn try_from(value: &std::path::PathBuf) -> Result<Self, Self::Error> {
let app_data_dir = value.clone();
let handler = Self::new(
app_data_dir,
projects::Controller::try_from(value)?,
users::Controller::try_from(value)?,
);
Ok(handler)
}
}
impl Handler {
fn new(
local_data_dir: path::PathBuf,
@ -262,7 +277,7 @@ mod test {
project,
..
} = suite.new_case_with_files(HashMap::from([(path::PathBuf::from("test.txt"), "test")]));
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "test2")?;
listener.handle("test.txt", &project.id)?;
@ -293,7 +308,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "test")?;
listener.handle("test.txt", &project.id)?;
@ -311,7 +326,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "test")?;
listener.handle("test.txt", &project.id)?;
@ -334,7 +349,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(
project.path.join("test.bin"),
@ -366,7 +381,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "")?;
@ -394,7 +409,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "test")?;
@ -427,7 +442,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
// file change, wd and deltas are written
std::fs::write(project.path.join("test.txt"), "test")?;
@ -455,7 +470,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
std::fs::write(project.path.join("test.txt"), "test")?;
listener.handle("test.txt", &project.id)?;
@ -507,7 +522,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
{
// write file
@ -586,7 +601,7 @@ mod test {
project_repository,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
let size = 10;
let relative_file_path = std::path::Path::new("one/two/test.txt");
@ -673,7 +688,7 @@ mod test {
project_repository,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
let size = 10;
let relative_file_path = std::path::Path::new("one/two/test.txt");
@ -758,7 +773,7 @@ mod test {
project,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
let size = 10_i32;
let relative_file_path = std::path::Path::new("one/two/test.txt");
@ -816,7 +831,7 @@ mod test {
path::PathBuf::from("test.txt"),
"hello world",
)]));
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?;
let target_writer = virtual_branches::target::Writer::new(&gb_repository)?;
@ -872,7 +887,7 @@ mod test {
path::PathBuf::from("test.txt"),
"hello world",
)]));
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?;
let target_writer = virtual_branches::target::Writer::new(&gb_repository)?;
@ -931,7 +946,7 @@ mod test {
project_repository,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
// write a file into session
std::fs::write(project.path.join("test.txt"), "hello world!").unwrap();
@ -1007,7 +1022,7 @@ mod test {
project_repository,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
// write a file into session
std::fs::write(project.path.join("test.txt"), "hello world!").unwrap();
@ -1083,7 +1098,7 @@ mod test {
project_repository,
..
} = suite.new_case();
let listener = Handler::from(&suite.local_app_data);
let listener = Handler::try_from(&suite.local_app_data).unwrap();
// write a file into session
std::fs::write(project.path.join("test.txt"), "hello world!").unwrap();

View File

@ -198,8 +198,9 @@ mod test {
fn create_new_session_via_new_file(project: &projects::Project, suite: &Suite) {
fs::write(project.path.join("test.txt"), "test").unwrap();
let file_change_listener =
handlers::calculate_deltas_handler::Handler::from(&suite.local_app_data);
handlers::calculate_deltas_handler::Handler::try_from(&suite.local_app_data).unwrap();
file_change_listener
.handle("test.txt", &project.id)
.unwrap();

View File

@ -199,7 +199,7 @@ mod tests {
file.write_all(b"test").unwrap();
let zipper_cache = tempdir().unwrap();
let zipper = Zipper::from(&zipper_cache.path().to_path_buf());
let zipper = Zipper::new(zipper_cache.path()).unwrap();
let zip_file_path = zipper.zip(tmp_dir).unwrap();
assert!(zip_file_path.exists());
}
@ -213,7 +213,7 @@ mod tests {
file.write_all(b"test").unwrap();
let zipper_cache = tempdir().unwrap();
let zipper = Zipper::from(&zipper_cache.path().to_path_buf());
let zipper = Zipper::new(zipper_cache.path()).unwrap();
zipper.zip(file_path).unwrap_err();
}
@ -226,7 +226,7 @@ mod tests {
file.write_all(b"test").unwrap();
let zipper_cache = tempdir().unwrap();
let zipper = Zipper::from(&zipper_cache.path().to_path_buf());
let zipper = Zipper::new(zipper_cache.path()).unwrap();
assert_eq!(zipper.zip(&tmp_dir).unwrap(), zipper.zip(&tmp_dir).unwrap());
assert_eq!(WalkDir::new(tmp_dir).into_iter().count(), 1);
}

View File

@ -12,7 +12,7 @@ mod init {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())
@ -31,7 +31,7 @@ mod init {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())
@ -52,7 +52,7 @@ mod init {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())
@ -82,7 +82,7 @@ mod flush {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())
@ -104,7 +104,7 @@ mod flush {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())
@ -128,7 +128,7 @@ mod flush {
let test_project = TestProject::default();
let data_dir = paths::data_dir();
let projects = projects::Controller::from(&data_dir);
let projects = projects::Controller::try_from(&data_dir).unwrap();
let project = projects
.add(test_project.path())

View File

@ -5,7 +5,7 @@ use gblib::projects::Controller;
pub fn new() -> Controller {
let data_dir = paths::data_dir();
Controller::from(&data_dir)
Controller::try_from(&data_dir).unwrap()
}
mod add {

View File

@ -29,10 +29,10 @@ struct Test {
impl Default for Test {
fn default() -> Self {
let data_dir = paths::data_dir();
let keys = keys::Controller::from(&data_dir);
let projects = projects::Controller::from(&data_dir);
let users = users::Controller::from(&data_dir);
let helper = git::credentials::Helper::from(&data_dir);
let keys = keys::Controller::try_from(&data_dir).unwrap();
let projects = projects::Controller::try_from(&data_dir).unwrap();
let users = users::Controller::try_from(&data_dir).unwrap();
let helper = git::credentials::Helper::try_from(&data_dir).unwrap();
let test_project = TestProject::default();
let project = projects
@ -42,7 +42,7 @@ impl Default for Test {
Self {
repository: test_project,
project_id: project.id,
controller: Controller::new(&data_dir, &projects, &users, &keys, &helper),
controller: Controller::new(data_dir, projects.clone(), users, keys, helper),
projects,
}
}
@ -4496,14 +4496,14 @@ mod init {
#[tokio::test]
async fn twice() {
let data_dir = paths::data_dir();
let keys = keys::Controller::from(&data_dir);
let projects = projects::Controller::from(&data_dir);
let users = users::Controller::from(&data_dir);
let helper = git::credentials::Helper::from(&data_dir);
let keys = keys::Controller::try_from(&data_dir).unwrap();
let projects = projects::Controller::try_from(&data_dir).unwrap();
let users = users::Controller::try_from(&data_dir).unwrap();
let helper = git::credentials::Helper::try_from(&data_dir).unwrap();
let test_project = TestProject::default();
let controller = Controller::new(&data_dir, &projects, &users, &keys, &helper);
let controller = Controller::new(data_dir, projects.clone(), users, keys, helper);
{
let project = projects