Merge pull request #1346 from gitbutlerapp/init-integrations-tests

init integration tests
This commit is contained in:
Nikita Galaiko 2023-10-12 10:45:00 +02:00 committed by GitHub
commit 4a2db963b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use std::{path, time}; use std::{path, time};
use anyhow::Context;
use futures::executor::block_on; use futures::executor::block_on;
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
@ -41,7 +42,7 @@ impl Controller {
let all_projects = self let all_projects = self
.projects_storage .projects_storage
.list() .list()
.map_err(|error| AddError::Other(error.into()))?; .context("failed to list projects from storage")?;
if all_projects.iter().any(|project| project.path == path) { if all_projects.iter().any(|project| project.path == path) {
return Err(AddError::AlreadyExists); return Err(AddError::AlreadyExists);
} }
@ -73,7 +74,7 @@ impl Controller {
self.projects_storage self.projects_storage
.add(&project) .add(&project)
.map_err(|error| AddError::Other(error.into()))?; .context("failed to add project to storage")?;
if let Some(watchers) = &self.watchers { if let Some(watchers) = &self.watchers {
block_on(watchers.watch(&project))?; block_on(watchers.watch(&project))?;
@ -200,7 +201,7 @@ pub enum AddError {
NotADirectory, NotADirectory,
#[error("not a git repository")] #[error("not a git repository")]
NotAGitRepository, NotAGitRepository,
#[error("failed to create project")] #[error("path not found")]
PathNotFound, PathNotFound,
#[error("project already exists")] #[error("project already exists")]
AlreadyExists, AlreadyExists,

View File

@ -0,0 +1,26 @@
use gitbutler::git;
pub fn temp_dir() -> std::path::PathBuf {
tempfile::tempdir()
.expect("failed to create temp dir")
.into_path()
}
pub fn git_repository() -> git::Repository {
let path = temp_dir();
let repository = git::Repository::init(path).expect("failed to init repository");
let mut index = repository.index().expect("failed to get index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git::Signature::now("test", "test@email.com").unwrap();
repository
.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit",
&repository.find_tree(oid).expect("failed to find tree"),
&[],
)
.expect("failed to commit");
repository
}

View File

@ -0,0 +1,3 @@
mod common;
mod paths;
mod projects;

View File

@ -0,0 +1,7 @@
use gitbutler::paths::DataDir;
use crate::common::temp_dir;
pub fn data_dir() -> DataDir {
DataDir::from(temp_dir())
}

View File

@ -0,0 +1,62 @@
use gitbutler::projects::Controller;
use crate::{common, paths};
pub fn new() -> Controller {
let data_dir = paths::data_dir();
Controller::from(&data_dir)
}
mod add {
use super::*;
#[test]
fn success() {
let controller = new();
let repository = common::git_repository();
let path = repository.workdir().unwrap();
let project = controller.add(path).unwrap();
assert_eq!(project.path, path);
assert_eq!(project.title, path.iter().last().unwrap().to_str().unwrap());
assert_eq!(project.id.len(), 36);
}
mod error {
use super::*;
#[test]
fn missing() {
let controller = new();
let path = tempfile::tempdir().unwrap().into_path();
let result = controller.add(&path.join("missing"));
assert_eq!(result.unwrap_err().to_string(), "path not found");
}
#[test]
fn not_git() {
let controller = new();
let path = tempfile::tempdir().unwrap().into_path();
std::fs::write(path.join("file.txt"), "hello world").unwrap();
let result = controller.add(&path);
assert_eq!(result.unwrap_err().to_string(), "not a git repository");
}
#[test]
fn empty() {
let controller = new();
let path = tempfile::tempdir().unwrap().into_path();
let result = controller.add(&path);
assert_eq!(result.unwrap_err().to_string(), "not a git repository");
}
#[test]
fn twice() {
let controller = new();
let repository = common::git_repository();
let path = repository.workdir().unwrap();
controller.add(path).unwrap();
let result = controller.add(path);
assert_eq!(result.unwrap_err().to_string(), "project already exists");
}
}
}