aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2020-07-27 23:28:48 +0200
committerTeddy Wing2020-07-27 23:45:03 +0200
commit1283d0511bac89957163097d1a68bb9753208f09 (patch)
tree002dd2a11328c48511b85595e3a84a9f487dedbe
parent8c6a6c99ec88780ce1e1aa2bf235be8fb4777bb7 (diff)
downloadgit-suggestion-1283d0511bac89957163097d1a68bb9753208f09.tar.bz2
Test `Suggestion.diff()`
Add a test for diff generation. Split `diff()` into `diff_with_repo()` to allow us to test with a new repo instead of defaulting to the current one. In the test, create a new Git repo using `git2`, add and commit a file, and check that the diff generated by a Suggestion is correct. Got the `IndexEntry` format from https://docs.rs/git2/0.13.8/src/git2/index.rs.html#858-871.
-rw-r--r--src/lib.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 4670493..7612857 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -116,6 +116,11 @@ impl Suggestion {
pub fn diff(&self) -> String {
let repo = Repository::open(".").unwrap();
+
+ self.diff_with_repo(&repo)
+ }
+
+ fn diff_with_repo(&self, repo: &Repository) -> String {
let commit = repo.find_commit(self.commit.parse().unwrap()).unwrap();
let path = Path::new(&self.path);
@@ -379,6 +384,97 @@ mod tests {
}
#[test]
+ fn suggestion_diff_with_repo_generates_diff() {
+ use tempfile::tempdir;
+
+
+ let git_root = tempdir().unwrap();
+ let repo = Repository::init(git_root.path()).unwrap();
+
+ let file = r#"
+ ‘Beware the Jabberwock, my son!
+ The jaws that bite, the claws that catch!
+ Beware the Jubjub bird, and shun
+ The frumious Bandersnatch!’
+
+ He took his vorpal blade in hand:
+ Long time the manxome foe he sought--
+ So rested he by the Tumtum tree,
+ And stood awhile in thought.
+"#;
+
+ let path = "poems/Jabberwocky.txt";
+
+ let mut index = repo.index().unwrap();
+ index.add_frombuffer(
+ &git2::IndexEntry {
+ ctime: git2::IndexTime::new(0, 0),
+ mtime: git2::IndexTime::new(0, 0),
+ dev: 0,
+ ino: 0,
+ mode: 0o100644,
+ uid: 0,
+ gid: 0,
+ file_size: file.len() as u32,
+ id: git2::Oid::zero(),
+ flags: 0,
+ flags_extended: 0,
+ path: path.as_bytes().to_vec(),
+ },
+ file.as_bytes(),
+ ).unwrap();
+ let tree_oid = index.write_tree().unwrap();
+ let tree = repo.find_tree(tree_oid).unwrap();
+
+ let author = git2::Signature::now(
+ "Oshino Shinobu",
+ "oshino.shinobu@example.com",
+ ).unwrap();
+
+ let commit = repo.commit(
+ Some("HEAD"),
+ &author,
+ &author,
+ "Sample commit",
+ &tree,
+ &[],
+ ).unwrap();
+
+ let suggestion = Suggestion {
+ diff: "".to_owned(),
+ comment: r#"``` suggestion
+ He took his vorpal sword in hand:
+ Long time the manxome foe he sought—
+```"#.to_owned(),
+ commit: commit.to_string(),
+ path: path.to_owned(),
+ original_start_line: Some(7),
+ original_end_line: 8,
+ };
+
+ let expected = r#"diff --git a/poems/Jabberwocky.txt b/poems/Jabberwocky.txt
+index 89840a2..06acdfc 100644
+--- a/poems/Jabberwocky.txt
++++ b/poems/Jabberwocky.txt
+@@ -4,7 +4,7 @@
+ Beware the Jubjub bird, and shun
+ The frumious Bandersnatch!’
+
+- He took his vorpal blade in hand:
+- Long time the manxome foe he sought--
++ He took his vorpal sword in hand:
++ Long time the manxome foe he sought—
+ So rested he by the Tumtum tree,
+ And stood awhile in thought.
+"#;
+
+ assert_eq!(
+ suggestion.diff_with_repo(&repo),
+ expected,
+ );
+ }
+
+ #[test]
fn suggestion_apply_to_writes_patch_to_writer() {
use std::io::Cursor;