Merge pull request #3467 from Byron/tests-shared

share test code via separate crate
This commit is contained in:
Josh Junon 2024-04-10 04:59:08 -07:00 committed by GitHub
commit a4bc539f56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
37 changed files with 73 additions and 51 deletions

14
Cargo.lock generated
View File

@ -2038,6 +2038,7 @@ dependencies = [
"git2",
"git2-hooks",
"gitbutler-git",
"gitbutler-testsupport",
"itertools 0.12.1",
"lazy_static",
"log",
@ -2099,6 +2100,7 @@ dependencies = [
"futures",
"git2",
"gitbutler-core",
"gitbutler-testsupport",
"governor",
"itertools 0.12.1",
"log",
@ -2129,6 +2131,18 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "gitbutler-testsupport"
version = "0.0.0"
dependencies = [
"anyhow",
"git2",
"gitbutler-core",
"once_cell",
"pretty_assertions",
"tempfile",
]
[[package]]
name = "glib"
version = "0.15.12"

View File

@ -4,6 +4,7 @@ members = [
"crates/gitbutler-tauri",
"crates/gitbutler-changeset",
"crates/gitbutler-git",
"crates/gitbutler-testsupport",
]
resolver = "2"
@ -17,6 +18,7 @@ tokio = { version = "1.37.0" }
gitbutler-git = { path = "crates/gitbutler-git" }
gitbutler-core = { path = "crates/gitbutler-core" }
gitbutler-testsupport = { path = "crates/gitbutler-testsupport" }
[profile.release]
codegen-units = 1 # Compile crates one after another so the compiler can optimize better

View File

@ -8,6 +8,7 @@ publish = false
[dev-dependencies]
once_cell = "1.19"
pretty_assertions = "1.4"
gitbutler-testsupport.workspace = true
[dependencies]
toml = "0.8.12"

View File

@ -1,5 +1,3 @@
pub mod shared;
mod suite {
mod gb_repository;
mod projects;

View File

@ -1,6 +1,6 @@
use gitbutler_core::database::Database;
use crate::shared::temp_dir;
use gitbutler_testsupport::temp_dir;
#[test]
fn smoke() {

View File

@ -7,7 +7,7 @@ mod database {
sessions::SessionId,
};
use crate::shared::test_database;
use gitbutler_testsupport::test_database;
#[test]
fn insert_query() -> anyhow::Result<()> {
@ -115,7 +115,7 @@ mod writer {
use gitbutler_core::{deltas, deltas::operations::Operation, sessions};
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
#[test]
fn write_no_vbranches() -> anyhow::Result<()> {

View File

@ -10,7 +10,7 @@ use gitbutler_core::{
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use crate::shared::{init_opts_bare, Case, Suite};
use gitbutler_testsupport::{init_opts_bare, Case, Suite};
mod repository {
use std::path::PathBuf;
@ -18,7 +18,7 @@ mod repository {
use anyhow::Result;
use pretty_assertions::assert_eq;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
#[test]
fn alternates_file_being_set() -> Result<()> {

View File

@ -1,4 +1,4 @@
use crate::shared::test_repository;
use gitbutler_testsupport::test_repository;
#[test]
pub fn set_str() {

View File

@ -5,7 +5,7 @@ use gitbutler_core::{
keys, project_repository, projects, users,
};
use crate::shared::{temp_dir, test_repository};
use gitbutler_testsupport::{temp_dir, test_repository};
#[derive(Default)]
struct TestCase<'a> {

View File

@ -10,7 +10,7 @@ use gitbutler_core::{
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use crate::shared::{init_opts_bare, Case, Suite};
use gitbutler_testsupport::{init_opts_bare, Case, Suite};
fn new_test_remote_repository() -> Result<(git2::Repository, TempDir)> {
let tmp = tempfile::tempdir()?;

View File

@ -9,7 +9,7 @@ mod controller {
use gitbutler_core::keys::{storage::Storage, Controller};
use crate::shared::Suite;
use gitbutler_testsupport::Suite;
#[test]
fn get_or_create() {

View File

@ -1,6 +1,6 @@
use gitbutler_core::lock::Dir;
use crate::shared::temp_dir;
use gitbutler_testsupport::temp_dir;
#[tokio::test]
async fn lock_same_instance() {

View File

@ -3,7 +3,7 @@ use std::{fs, path::Path};
use anyhow::Result;
use gitbutler_core::reader::{CommitReader, Content, Reader};
use crate::shared::{commit_all, temp_dir, test_repository};
use gitbutler_testsupport::{commit_all, temp_dir, test_repository};
#[test]
fn directory_reader_read_file() -> Result<()> {

View File

@ -3,7 +3,7 @@ use gitbutler_core::{
sessions::{session, Database, Session, SessionId},
};
use crate::shared::test_database;
use gitbutler_testsupport::test_database;
#[test]
fn insert_query() -> anyhow::Result<()> {

View File

@ -3,7 +3,7 @@ mod database;
use anyhow::Result;
use gitbutler_core::sessions::{self, session::SessionId};
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
#[test]
fn should_not_write_session_with_hash() {

View File

@ -2,7 +2,7 @@ use std::path;
use gitbutler_core::{gb_repository, git, project_repository, projects};
use crate::shared::{paths, TestProject};
use gitbutler_testsupport::{paths, TestProject};
mod init {
use super::*;

View File

@ -1,7 +1,7 @@
use gitbutler_core::projects::Controller;
use tempfile::TempDir;
use crate::shared::{self, paths};
use gitbutler_testsupport::{self, paths};
pub fn new() -> (Controller, TempDir) {
let data_dir = paths::data_dir();
@ -15,7 +15,7 @@ mod add {
#[test]
fn success() {
let (controller, _tmp) = new();
let repository = shared::TestProject::default();
let repository = gitbutler_testsupport::TestProject::default();
let path = repository.path();
let project = controller.add(path).unwrap();
assert_eq!(project.path, path);
@ -62,7 +62,7 @@ mod add {
#[test]
fn twice() {
let (controller, _tmp) = new();
let repository = shared::TestProject::default();
let repository = gitbutler_testsupport::TestProject::default();
let path = repository.path();
controller.add(path).unwrap();
assert!(matches!(controller.add(path), Err(AddError::AlreadyExists)));

View File

@ -8,7 +8,7 @@ use gitbutler_core::{
};
use tempfile::TempDir;
use crate::shared::{paths, TestProject, VAR_NO_CLEANUP};
use gitbutler_testsupport::{paths, TestProject, VAR_NO_CLEANUP};
struct Test {
repository: TestProject,

View File

@ -7,7 +7,7 @@ use gitbutler_core::virtual_branches::{
};
use once_cell::sync::Lazy;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
static TEST_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -9,7 +9,7 @@ use once_cell::sync::Lazy;
use self::branch::BranchId;
use super::*;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
static TEST_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -4,7 +4,7 @@ use anyhow::Result;
use gitbutler_core::virtual_branches::{self, VirtualBranchesHandle};
use once_cell::sync::Lazy;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
static TEST_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -28,7 +28,7 @@ use gitbutler_core::{
};
use pretty_assertions::assert_eq;
use crate::shared::{commit_all, virtual_branches::set_test_target, Case, Suite};
use gitbutler_testsupport::{commit_all, virtual_branches::set_test_target, Case, Suite};
#[test]
fn commit_on_branch_then_change_file_then_get_status() -> Result<()> {

View File

@ -7,7 +7,7 @@ use gitbutler_core::virtual_branches::{
};
use once_cell::sync::Lazy;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
static TEST_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -11,7 +11,7 @@ use gitbutler_core::virtual_branches::{
};
use once_cell::sync::Lazy;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
static TEST_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -20,6 +20,7 @@ tauri-build = { version = "1.5", features = [] }
#once_cell = "1.19"
pretty_assertions = "1.4"
tempfile = "3.10"
gitbutler-testsupport.workspace = true
[dependencies]
anyhow = "1.0.81"

View File

@ -17,7 +17,6 @@ use tauri::{generate_context, Manager, Wry};
use tauri_plugin_log::LogTarget;
use tauri_plugin_store::{with_store, JsonValue, StoreCollection};
#[allow(clippy::too_many_lines)]
fn main() {
let tauri_context = generate_context!();

View File

@ -473,8 +473,6 @@ pub mod commands {
Ok(())
}
// XXX(qix-): Is this command used?
#[allow(dead_code)]
pub async fn update_commit_message(
handle: tauri::AppHandle,
project_id: ProjectId,

View File

@ -1,4 +1 @@
// TODO(ST): move test code into crate and use that, but wait for `crates/`
#[path = "../../gitbutler-core/tests/shared/mod.rs"]
pub mod shared;
mod watcher;

View File

@ -14,7 +14,7 @@ use gitbutler_tauri::watcher::handlers::calculate_deltas_handler::Handler;
use once_cell::sync::Lazy;
use self::branch::BranchId;
use crate::shared::{commit_all, Case, Suite};
use gitbutler_testsupport::{commit_all, Case, Suite};
static TEST_TARGET_INDEX: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));

View File

@ -4,10 +4,8 @@ use gitbutler_core::projects;
use gitbutler_tauri::watcher::handlers::fetch_gitbutler_data::Handler;
use pretty_assertions::assert_eq;
use crate::{
shared::{Case, Suite},
watcher::handler::test_remote_repository,
};
use crate::watcher::handler::test_remote_repository;
use gitbutler_testsupport::{Case, Suite};
#[tokio::test]
async fn fetch_success() -> anyhow::Result<()> {

View File

@ -5,7 +5,7 @@ use gitbutler_core::projects;
use gitbutler_tauri::watcher::{handlers, handlers::git_file_change::Handler, Event};
use pretty_assertions::assert_eq;
use crate::shared::{Case, Suite};
use gitbutler_testsupport::{Case, Suite};
#[test]
fn flush_session() -> Result<()> {

View File

@ -1,6 +1,6 @@
use tempfile::TempDir;
use crate::shared::init_opts_bare;
use gitbutler_testsupport::init_opts_bare;
fn test_remote_repository() -> anyhow::Result<(git2::Repository, TempDir)> {
let tmp = tempfile::tempdir()?;

View File

@ -4,10 +4,8 @@ use anyhow::Result;
use gitbutler_core::{git, project_repository::LogUntil, projects};
use gitbutler_tauri::watcher::handlers::push_project_to_gitbutler::Handler;
use crate::{
shared::{virtual_branches::set_test_target, Case, Suite},
watcher::handler::test_remote_repository,
};
use crate::watcher::handler::test_remote_repository;
use gitbutler_testsupport::{virtual_branches::set_test_target, Case, Suite};
fn log_walk(repo: &git2::Repository, head: git::Oid) -> Vec<git::Oid> {
let mut walker = repo.revwalk().unwrap();

View File

@ -0,0 +1,18 @@
[package]
name = "gitbutler-testsupport"
version = "0.0.0"
edition = "2021"
publish = false
[lib]
path = "src/lib.rs"
doctest = false
test = false
[dependencies]
anyhow = "1.0.81"
once_cell = "1.19"
git2.workspace = true
pretty_assertions = "1.4"
tempfile = "3.10.1"
gitbutler-core = { path = "../gitbutler-core" }

View File

@ -22,7 +22,7 @@ pub mod virtual_branches {
virtual_branches::{self, VirtualBranchesHandle},
};
use crate::shared::empty_bare_repository;
use crate::empty_bare_repository;
pub fn set_test_target(
gb_repo: &gb_repository::Repository,

View File

@ -6,7 +6,7 @@ use std::{
use tempfile::{tempdir, TempDir};
use crate::shared::{init_opts, init_opts_bare, VAR_NO_CLEANUP};
use crate::{init_opts, init_opts_bare, VAR_NO_CLEANUP};
pub struct Suite {
pub local_app_data: Option<TempDir>,

View File

@ -1,11 +1,9 @@
#![allow(unused)]
use std::{path, str::from_utf8};
use std::path;
use gitbutler_core::git;
use tempfile::TempDir;
use crate::shared::{init_opts, VAR_NO_CLEANUP};
use crate::{init_opts, VAR_NO_CLEANUP};
pub fn temp_dir() -> TempDir {
tempfile::tempdir().unwrap()
@ -115,7 +113,7 @@ impl TestProject {
});
let head_ref = head.name().unwrap();
let head_ref = self.local_repository.find_reference(&head_ref).unwrap();
self.local_repository.find_reference(&head_ref).unwrap();
self.local_repository
.reset(&commit, git2::ResetType::Hard, None)
@ -342,7 +340,7 @@ impl TestProject {
// be sure that `HEAD` points to the actual head - `git2` seems to initialize it
// with `init.defaultBranch`, causing failure otherwise.
repo.set_head("refs/heads/master");
repo.set_head("refs/heads/master").unwrap();
submodule.add_finalize().unwrap();
}
}