Merge pull request #3112 from gitbutlerapp/dont-use-results-in-tests

don't use results in tests
This commit is contained in:
Josh Junon 2024-03-11 18:41:15 +01:00 committed by GitHub
commit 940059ef95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -123,26 +123,28 @@ fn test_list_deltas_from_current_session() -> Result<()> {
}
#[test]
fn test_list_deltas_from_flushed_session() -> Result<()> {
fn test_list_deltas_from_flushed_session() {
let Case {
gb_repository,
project_repository,
..
} = Suite::default().new_case();
let writer = deltas::Writer::new(&gb_repository)?;
writer.write(
"test.txt",
&vec![deltas::Delta {
operations: vec![Operation::Insert((0, "Hello World".to_string()))],
timestamp_ms: 0,
}],
)?;
let session = gb_repository.flush(&project_repository, None)?;
let writer = deltas::Writer::new(&gb_repository).unwrap();
writer
.write(
"test.txt",
&vec![deltas::Delta {
operations: vec![Operation::Insert((0, "Hello World".to_string()))],
timestamp_ms: 0,
}],
)
.unwrap();
let session = gb_repository.flush(&project_repository, None).unwrap();
let session_reader = sessions::Reader::open(&gb_repository, &session.unwrap())?;
let session_reader = sessions::Reader::open(&gb_repository, &session.unwrap()).unwrap();
let deltas_reader = deltas::Reader::new(&session_reader);
let deltas = deltas_reader.read(None)?;
let deltas = deltas_reader.read(None).unwrap();
assert_eq!(deltas.len(), 1);
assert_eq!(
@ -153,32 +155,28 @@ fn test_list_deltas_from_flushed_session() -> Result<()> {
deltas[&path::PathBuf::from("test.txt")][0].operations[0],
Operation::Insert((0, "Hello World".to_string()))
);
Ok(())
}
#[test]
fn test_list_files_from_current_session() -> Result<()> {
fn test_list_files_from_current_session() {
let Case { gb_repository, .. } = Suite::default().new_case_with_files(HashMap::from([(
path::PathBuf::from("test.txt"),
"Hello World",
)]));
let current = gb_repository.get_or_create_current_session()?;
let reader = sessions::Reader::open(&gb_repository, &current)?;
let files = reader.files(None)?;
let current = gb_repository.get_or_create_current_session().unwrap();
let reader = sessions::Reader::open(&gb_repository, &current).unwrap();
let files = reader.files(None).unwrap();
assert_eq!(files.len(), 1);
assert_eq!(
files[&path::PathBuf::from("test.txt")],
reader::Content::UTF8("Hello World".to_string())
);
Ok(())
}
#[test]
fn test_list_files_from_flushed_session() -> Result<()> {
fn test_list_files_from_flushed_session() {
let Case {
gb_repository,
project_repository,
@ -188,24 +186,25 @@ fn test_list_files_from_flushed_session() -> Result<()> {
"Hello World",
)]));
gb_repository.get_or_create_current_session()?;
let session = gb_repository.flush(&project_repository, None)?.unwrap();
let reader = sessions::Reader::open(&gb_repository, &session)?;
let files = reader.files(None)?;
gb_repository.get_or_create_current_session().unwrap();
let session = gb_repository
.flush(&project_repository, None)
.unwrap()
.unwrap();
let reader = sessions::Reader::open(&gb_repository, &session).unwrap();
let files = reader.files(None).unwrap();
assert_eq!(files.len(), 1);
assert_eq!(
files[&path::PathBuf::from("test.txt")],
reader::Content::UTF8("Hello World".to_string())
);
Ok(())
}
#[tokio::test]
async fn test_remote_syncronization() -> Result<()> {
async fn test_remote_syncronization() {
// first, crate a remote, pretending it's a cloud
let cloud = test_remote_repository()?;
let cloud = test_remote_repository().unwrap();
let api_project = ApiProject {
name: "test-sync".to_string(),
description: None,
@ -232,20 +231,24 @@ async fn test_remote_syncronization() -> Result<()> {
api: Some(api_project.clone()),
..Default::default()
})
.await?;
.await
.unwrap();
let case_one = case_one.refresh();
let writer = deltas::Writer::new(&case_one.gb_repository)?;
writer.write(
"test.txt",
&vec![deltas::Delta {
operations: vec![Operation::Insert((0, "Hello World".to_string()))],
timestamp_ms: 0,
}],
)?;
let writer = deltas::Writer::new(&case_one.gb_repository).unwrap();
writer
.write(
"test.txt",
&vec![deltas::Delta {
operations: vec![Operation::Insert((0, "Hello World".to_string()))],
timestamp_ms: 0,
}],
)
.unwrap();
let session_one = case_one
.gb_repository
.flush(&case_one.project_repository, Some(&user))?
.flush(&case_one.project_repository, Some(&user))
.unwrap()
.unwrap();
case_one.gb_repository.push(Some(&user)).unwrap();
@ -258,24 +261,26 @@ async fn test_remote_syncronization() -> Result<()> {
api: Some(api_project.clone()),
..Default::default()
})
.await?;
.await
.unwrap();
let case_two = case_two.refresh();
case_two.gb_repository.fetch(Some(&user))?;
case_two.gb_repository.fetch(Some(&user)).unwrap();
// now it should have the session from the first local project synced
let sessions_two = case_two
.gb_repository
.get_sessions_iterator()?
.get_sessions_iterator()
.unwrap()
.map(Result::unwrap)
.collect::<Vec<_>>();
assert_eq!(sessions_two.len(), 1);
assert_eq!(sessions_two[0].id, session_one.id);
let session_reader = sessions::Reader::open(&case_two.gb_repository, &sessions_two[0])?;
let session_reader = sessions::Reader::open(&case_two.gb_repository, &sessions_two[0]).unwrap();
let deltas_reader = deltas::Reader::new(&session_reader);
let deltas = deltas_reader.read(None)?;
let files = session_reader.files(None)?;
let deltas = deltas_reader.read(None).unwrap();
let files = session_reader.files(None).unwrap();
assert_eq!(deltas.len(), 1);
assert_eq!(files.len(), 1);
assert_eq!(
@ -289,14 +294,12 @@ async fn test_remote_syncronization() -> Result<()> {
timestamp_ms: 0,
}]
);
Ok(())
}
#[tokio::test]
async fn test_remote_sync_order() -> Result<()> {
async fn test_remote_sync_order() {
// first, crate a remote, pretending it's a cloud
let cloud = test_remote_repository()?;
let cloud = test_remote_repository().unwrap();
let api_project = projects::ApiProject {
name: "test-sync".to_string(),
description: None,
@ -318,7 +321,8 @@ async fn test_remote_sync_order() -> Result<()> {
api: Some(api_project.clone()),
..Default::default()
})
.await?;
.await
.unwrap();
let case_one = case_one.refresh();
let case_two = suite.new_case();
@ -329,60 +333,79 @@ async fn test_remote_sync_order() -> Result<()> {
api: Some(api_project.clone()),
..Default::default()
})
.await?;
.await
.unwrap();
let case_two = case_two.refresh();
let user = suite.sign_in();
// create session in the first project
case_one.gb_repository.get_or_create_current_session()?;
case_one
.gb_repository
.get_or_create_current_session()
.unwrap();
let session_one_first = case_one
.gb_repository
.flush(&case_one.project_repository, Some(&user))?
.flush(&case_one.project_repository, Some(&user))
.unwrap()
.unwrap();
case_one.gb_repository.push(Some(&user)).unwrap();
thread::sleep(time::Duration::from_secs(1));
// create session in the second project
case_two.gb_repository.get_or_create_current_session()?;
case_two
.gb_repository
.get_or_create_current_session()
.unwrap();
let session_two_first = case_two
.gb_repository
.flush(&case_two.project_repository, Some(&user))?
.flush(&case_two.project_repository, Some(&user))
.unwrap()
.unwrap();
case_two.gb_repository.push(Some(&user)).unwrap();
thread::sleep(time::Duration::from_secs(1));
// create second session in the first project
case_one.gb_repository.get_or_create_current_session()?;
case_one
.gb_repository
.get_or_create_current_session()
.unwrap();
let session_one_second = case_one
.gb_repository
.flush(&case_one.project_repository, Some(&user))?
.flush(&case_one.project_repository, Some(&user))
.unwrap()
.unwrap();
case_one.gb_repository.push(Some(&user)).unwrap();
thread::sleep(time::Duration::from_secs(1));
// create second session in the second project
case_two.gb_repository.get_or_create_current_session()?;
case_two
.gb_repository
.get_or_create_current_session()
.unwrap();
let session_two_second = case_two
.gb_repository
.flush(&case_two.project_repository, Some(&user))?
.flush(&case_two.project_repository, Some(&user))
.unwrap()
.unwrap();
case_two.gb_repository.push(Some(&user)).unwrap();
case_one.gb_repository.fetch(Some(&user))?;
case_one.gb_repository.fetch(Some(&user)).unwrap();
let sessions_one = case_one
.gb_repository
.get_sessions_iterator()?
.get_sessions_iterator()
.unwrap()
.map(Result::unwrap)
.collect::<Vec<_>>();
case_two.gb_repository.fetch(Some(&user))?;
case_two.gb_repository.fetch(Some(&user)).unwrap();
let sessions_two = case_two
.gb_repository
.get_sessions_iterator()?
.get_sessions_iterator()
.unwrap()
.map(Result::unwrap)
.collect::<Vec<_>>();
@ -394,30 +417,30 @@ async fn test_remote_sync_order() -> Result<()> {
assert_eq!(sessions_one[1].id, session_one_second.id);
assert_eq!(sessions_one[2].id, session_two_first.id);
assert_eq!(sessions_one[3].id, session_one_first.id);
Ok(())
}
#[test]
fn test_gitbutler_file() -> Result<()> {
fn test_gitbutler_file() {
let Case {
gb_repository,
project_repository,
..
} = Suite::default().new_case();
let session = gb_repository.get_or_create_current_session()?;
let session = gb_repository.get_or_create_current_session().unwrap();
let gitbutler_file_path = project_repository.path().join(".git/gitbutler.json");
assert!(gitbutler_file_path.exists());
let file_content: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&gitbutler_file_path)?)?;
let sid: SessionId = file_content["sessionId"].as_str().unwrap().parse()?;
serde_json::from_str(&std::fs::read_to_string(&gitbutler_file_path).unwrap()).unwrap();
let sid: SessionId = file_content["sessionId"].as_str().unwrap().parse().unwrap();
assert_eq!(sid, session.id);
let pid: ProjectId = file_content["repositoryId"].as_str().unwrap().parse()?;
let pid: ProjectId = file_content["repositoryId"]
.as_str()
.unwrap()
.parse()
.unwrap();
assert_eq!(pid, project_repository.project().id);
Ok(())
}