mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-11-29 06:00:28 +08:00
promote src/git/ files to src/
Signed-off-by: Han Xu <keepsimple@gmail.com>
This commit is contained in:
@@ -0,0 +1,342 @@
|
||||
use git_ref_format::refname;
|
||||
use pretty_assertions::assert_eq;
|
||||
use radicle_git_ext::Oid;
|
||||
use radicle_surf::{
|
||||
diff::{
|
||||
Added,
|
||||
Addition,
|
||||
Deleted,
|
||||
Deletion,
|
||||
Diff,
|
||||
EofNewLine,
|
||||
FileDiff,
|
||||
Hunk,
|
||||
Line,
|
||||
Modification,
|
||||
Modified,
|
||||
Moved,
|
||||
Stats,
|
||||
},
|
||||
Branch,
|
||||
Error,
|
||||
Repository,
|
||||
};
|
||||
use std::{path::Path, str::FromStr};
|
||||
|
||||
use super::GIT_PLATINUM;
|
||||
|
||||
#[test]
|
||||
fn test_initial_diff() -> Result<(), Error> {
|
||||
let repo = Repository::open(GIT_PLATINUM)?;
|
||||
let oid = Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?;
|
||||
let commit = repo.commit(oid).unwrap();
|
||||
assert!(commit.parents.is_empty());
|
||||
|
||||
let diff = repo.diff_commit(oid)?;
|
||||
|
||||
let expected_diff = Diff {
|
||||
added: vec![Added {
|
||||
path: Path::new("README.md").to_path_buf(),
|
||||
diff: FileDiff::Plain {
|
||||
hunks: vec![Hunk {
|
||||
header: Line::from(b"@@ -0,0 +1 @@\n".to_vec()),
|
||||
lines: vec![Addition {
|
||||
line:
|
||||
b"This repository is a data source for the Upstream front-end tests.\n"
|
||||
.to_vec()
|
||||
.into(),
|
||||
line_no: 1,
|
||||
}],
|
||||
}]
|
||||
.into(),
|
||||
},
|
||||
}],
|
||||
deleted: vec![],
|
||||
moved: vec![],
|
||||
copied: vec![],
|
||||
modified: vec![],
|
||||
stats: Stats {
|
||||
files_changed: 1,
|
||||
insertions: 1,
|
||||
deletions: 0,
|
||||
},
|
||||
};
|
||||
assert_eq!(expected_diff, diff);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_of_rev() -> Result<(), Error> {
|
||||
let repo = Repository::open(GIT_PLATINUM)?;
|
||||
let diff = repo.diff_commit("80bacafba303bf0cdf6142921f430ff265f25095")?;
|
||||
assert_eq!(diff.added.len(), 0);
|
||||
assert_eq!(diff.deleted.len(), 0);
|
||||
assert_eq!(diff.moved.len(), 0);
|
||||
assert_eq!(diff.modified.len(), 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff() -> Result<(), Error> {
|
||||
let repo = Repository::open(GIT_PLATINUM)?;
|
||||
let oid = "80bacafba303bf0cdf6142921f430ff265f25095";
|
||||
let commit = repo.commit(oid).unwrap();
|
||||
let parent_oid = commit.parents.get(0).unwrap();
|
||||
let diff = repo.diff(*parent_oid, oid)?;
|
||||
|
||||
let expected_diff = Diff {
|
||||
added: vec![],
|
||||
deleted: vec![],
|
||||
moved: vec![],
|
||||
copied: vec![],
|
||||
modified: vec unit tests.\n".to_vec(), 2),
|
||||
]
|
||||
}].into()
|
||||
},
|
||||
eof: None,
|
||||
}],
|
||||
stats: Stats {
|
||||
files_changed: 1,
|
||||
insertions: 2,
|
||||
deletions: 1,
|
||||
},
|
||||
};
|
||||
assert_eq!(expected_diff, diff);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_branch_diff() -> Result<(), Error> {
|
||||
let repo = Repository::open(GIT_PLATINUM)?;
|
||||
let diff = repo.diff(
|
||||
Branch::local(refname!("master")),
|
||||
Branch::local(refname!("dev")),
|
||||
)?;
|
||||
|
||||
println!("Diff two branches: master -> dev");
|
||||
println!(
|
||||
"result: added {} deleted {} moved {} modified {}",
|
||||
diff.added.len(),
|
||||
diff.deleted.len(),
|
||||
diff.moved.len(),
|
||||
diff.modified.len()
|
||||
);
|
||||
assert_eq!(diff.added.len(), 1);
|
||||
assert_eq!(diff.deleted.len(), 11);
|
||||
assert_eq!(diff.moved.len(), 1);
|
||||
assert_eq!(diff.modified.len(), 2);
|
||||
for c in diff.added.iter() {
|
||||
println!("added: {:?}", &c.path);
|
||||
}
|
||||
for d in diff.deleted.iter() {
|
||||
println!("deleted: {:?}", &d.path);
|
||||
}
|
||||
for m in diff.moved.iter() {
|
||||
println!("moved: {:?} -> {:?}", &m.old_path, &m.new_path);
|
||||
}
|
||||
for m in diff.modified.iter() {
|
||||
println!("modified: {:?}", &m.path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_serde() {
|
||||
let diff = Diff {
|
||||
added: vec![ Added {
|
||||
path: Path::new("LICENSE").to_path_buf(),
|
||||
diff: FileDiff::Plain {
|
||||
hunks: vec![Hunk {
|
||||
header: Line::from(b"@@ -0,0 +1,1".to_vec()),
|
||||
lines: vec![
|
||||
Addition { line: Line::from(b"MIT".to_vec()), line_no: 1 }
|
||||
]
|
||||
}].into()
|
||||
}
|
||||
}],
|
||||
deleted: vec![ Deleted {
|
||||
path: Path::new("DCO").to_path_buf(),
|
||||
diff: FileDiff::Plain {
|
||||
hunks: vec![Hunk {
|
||||
header: Line::from(b"@@ -0,0 +1,1".to_vec()),
|
||||
lines: vec![
|
||||
Deletion { line: Line::from(b"TODO".to_vec()), line_no: 1 }
|
||||
]
|
||||
}].into()
|
||||
}
|
||||
}],
|
||||
moved: vec![ Moved {
|
||||
old_path: Path::new("CONTRIBUTING").to_path_buf(),
|
||||
new_path: Path::new("CONTRIBUTING.md").to_path_buf(),
|
||||
}],
|
||||
copied: vec![],
|
||||
modified: vec unit tests.\n".to_vec(), 2),
|
||||
Modification::context(b"\n".to_vec(), 3, 4),
|
||||
]
|
||||
}].into()
|
||||
},
|
||||
eof: None,
|
||||
}],
|
||||
stats: Stats {
|
||||
files_changed: 3,
|
||||
insertions: 2,
|
||||
deletions: 1,
|
||||
},
|
||||
};
|
||||
|
||||
let eof: Option<u8> = None;
|
||||
let json = serde_json::json!({
|
||||
"added": [{
|
||||
"path": "LICENSE",
|
||||
"diff": {
|
||||
"type": "plain",
|
||||
"hunks": [{
|
||||
"header": "@@ -0,0 +1,1",
|
||||
"lines": [{
|
||||
"line": "MIT",
|
||||
"lineNo": 1,
|
||||
"type": "addition",
|
||||
}]
|
||||
}]
|
||||
},
|
||||
}],
|
||||
"deleted": [{
|
||||
"path": "DCO",
|
||||
"diff": {
|
||||
"type": "plain",
|
||||
"hunks": [{
|
||||
"header": "@@ -0,0 +1,1",
|
||||
"lines": [{
|
||||
"line": "TODO",
|
||||
"lineNo": 1,
|
||||
"type": "deletion",
|
||||
}]
|
||||
}]
|
||||
},
|
||||
}],
|
||||
"moved": [{ "oldPath": "CONTRIBUTING", "newPath": "CONTRIBUTING.md" }],
|
||||
"copied": [],
|
||||
"modified": [{
|
||||
"path": "README.md",
|
||||
"diff": {
|
||||
"type": "plain",
|
||||
"hunks": [{
|
||||
"header": "@@ -1 +1,2 @@\n",
|
||||
"lines": [
|
||||
{ "lineNo": 1,
|
||||
"line": "This repository is a data source for the Upstream front-end tests.\n",
|
||||
"type": "deletion"
|
||||
},
|
||||
{ "lineNo": 1,
|
||||
"line": "This repository is a data source for the Upstream front-end tests and the\n",
|
||||
"type": "addition"
|
||||
},
|
||||
{ "lineNo": 2,
|
||||
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
|
||||
"type": "addition"
|
||||
},
|
||||
{ "lineNoOld": 3, "lineNoNew": 4,
|
||||
"line": "\n",
|
||||
"type": "context"
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
"eof" : eof,
|
||||
}],
|
||||
"stats": {
|
||||
"deletions": 1,
|
||||
"filesChanged": 3,
|
||||
"insertions": 2,
|
||||
}
|
||||
});
|
||||
assert_eq!(serde_json::to_value(&diff).unwrap(), json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_both_missing_eof_newline() {
|
||||
let buf = r#"
|
||||
diff --git a/.env b/.env
|
||||
index f89e4c0..7c56eb7 100644
|
||||
--- a/.env
|
||||
+++ b/.env
|
||||
@@ -1 +1 @@
|
||||
-hello=123
|
||||
\ No newline at end of file
|
||||
+hello=1234
|
||||
\ No newline at end of file
|
||||
"#;
|
||||
let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
let diff = Diff::try_from(diff).unwrap();
|
||||
assert_eq!(diff.modified[0].eof, Some(EofNewLine::BothMissing));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_none_missing_eof_newline() {
|
||||
let buf = r#"
|
||||
diff --git a/.env b/.env
|
||||
index f89e4c0..7c56eb7 100644
|
||||
--- a/.env
|
||||
+++ b/.env
|
||||
@@ -1 +1 @@
|
||||
-hello=123
|
||||
+hello=1234
|
||||
"#;
|
||||
let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
let diff = Diff::try_from(diff).unwrap();
|
||||
assert_eq!(diff.modified[0].eof, None);
|
||||
}
|
||||
|
||||
// TODO(xphoniex): uncomment once libgit2 has fixed the bug
|
||||
//#[test]
|
||||
// fn test_old_missing_eof_newline() {
|
||||
// let buf = r#"
|
||||
// diff --git a/.env b/.env
|
||||
// index f89e4c0..7c56eb7 100644
|
||||
// --- a/.env
|
||||
// +++ b/.env
|
||||
// @@ -1 +1 @@
|
||||
// -hello=123
|
||||
// \ No newline at end of file
|
||||
// +hello=1234
|
||||
// "#;
|
||||
// let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
// let diff = Diff::try_from(diff).unwrap();
|
||||
// assert_eq!(diff.modified[0].eof, Some(EofNewLine::OldMissing));
|
||||
// }
|
||||
|
||||
// TODO(xphoniex): uncomment once libgit2 has fixed the bug
|
||||
//#[test]
|
||||
// fn test_new_missing_eof_newline() {
|
||||
// let buf = r#"
|
||||
// diff --git a/.env b/.env
|
||||
// index f89e4c0..7c56eb7 100644
|
||||
// --- a/.env
|
||||
// +++ b/.env
|
||||
// @@ -1 +1 @@
|
||||
// -hello=123
|
||||
// +hello=1234
|
||||
// \ No newline at end of file
|
||||
// "#;
|
||||
// let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
// let diff = Diff::try_from(diff).unwrap();
|
||||
// assert_eq!(diff.modified[0].eof, Some(EofNewLine::NewMissing));
|
||||
// }
|
||||
Reference in New Issue
Block a user