Skip to content

Commit

Permalink
test: rebase_conflict test
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Feb 11, 2024
1 parent 1d0183e commit e740ce7
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 225 deletions.
108 changes: 73 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,66 +411,69 @@ fn goto_refs_screen(config: &Config, screens: &mut Vec<Screen>) {

#[cfg(test)]
mod tests {
use std::process::Command;

use crate::{cli::Args, update, State};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::{backend::TestBackend, prelude::Rect, Terminal};
use std::{fs, process::Command};
use temp_dir::TempDir;

use crate::{cli::Args, update, Res, State};

#[test]
fn no_repo() {
let (terminal, _state, _dir) = setup(80, 30);
insta::assert_debug_snapshot!(terminal.backend().buffer());
let (ref mut terminal, _state, dir) = setup(60, 20);
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

#[test]
fn help_menu() {
let (ref mut terminal, ref mut state, _dir) = setup(80, 30);
let (ref mut terminal, ref mut state, dir) = setup(60, 20);
update(terminal, state, &[key('h')]).unwrap();
insta::assert_debug_snapshot!(terminal.backend().buffer());
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

#[test]
fn fresh_init() -> Res<()> {
let (ref mut terminal, ref mut state, dir) = setup(80, 30);
assert!(run(&dir, &["git", "init", "--initial-branch", "master"])?);
fn fresh_init() {
let (ref mut terminal, ref mut state, dir) = setup(60, 20);
run(&dir, &["git", "init", "--initial-branch", "master"]);
update(terminal, state, &[key('g')]).unwrap();
insta::assert_debug_snapshot!(terminal.backend().buffer());
Ok(())
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

#[test]
fn new_file() -> Res<()> {
let (ref mut terminal, ref mut state, dir) = setup(80, 30);
assert!(run(&dir, &["git", "init", "--initial-branch", "master"])?);
assert!(run(&dir, &["touch", "new-file"])?);
fn new_file() {
let (ref mut terminal, ref mut state, dir) = setup(60, 20);
run(&dir, &["git", "init", "--initial-branch", "master"]);
run(&dir, &["touch", "new-file"]);
update(terminal, state, &[key('g')]).unwrap();
insta::assert_debug_snapshot!(terminal.backend().buffer());
Ok(())
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

#[test]
fn stage_file() -> Res<()> {
let (ref mut terminal, ref mut state, dir) = setup(80, 30);
assert!(run(&dir, &["git", "init", "--initial-branch", "master"])?);
assert!(run(&dir, &["touch", "new-file"])?);
fn staged_file() {
let (ref mut terminal, ref mut state, dir) = setup(60, 20);
run(&dir, &["git", "init", "--initial-branch", "master"]);
run(&dir, &["touch", "new-file"]);
update(terminal, state, &[key('g'), key('j'), key('s'), key('g')]).unwrap();
insta::assert_debug_snapshot!(terminal.backend().buffer());
Ok(())
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

fn run(dir: &TempDir, cmd: &[&str]) -> Res<bool> {
Ok(Command::new(cmd[0])
.args(&cmd[1..])
.current_dir(dir.path())
.status()?
.success())
}
#[test]
fn rebase_conflict() {
let (ref mut terminal, ref mut state, dir) = setup(60, 20);
run(&dir, &["git", "init", "--initial-branch", "master"]);

fn key(char: char) -> Event {
Event::Key(KeyEvent::new(KeyCode::Char(char), KeyModifiers::empty()))
commit(&dir, "new-file", "hello");

run(&dir, &["git", "checkout", "-b", "other-branch"]);
commit(&dir, "new-file", "hey");

run(&dir, &["git", "checkout", "master"]);
commit(&dir, "new-file", "hi");

run(&dir, &["git", "checkout", "other-branch"]);
run(&dir, &["git", "rebase", "master"]);

update(terminal, state, &[key('g')]).unwrap();
insta::assert_snapshot!(redact_hashes(terminal, dir));
}

fn setup(width: u16, height: u16) -> (Terminal<TestBackend>, State, TempDir) {
Expand All @@ -484,12 +487,47 @@ mod tests {
Rect::new(0, 0, width, height),
Args {
command: None,
status: true,
status: false,
exit_immediately: false,
},
)
.unwrap();

(terminal, state, dir)
}

fn commit(dir: &TempDir, file_name: &str, contents: &str) {
let path = dir.child(file_name);
let message = match path.try_exists() {
Ok(true) => format!("modify {}", file_name),
_ => format!("add {}", file_name),
};
fs::write(path, contents).expect("error writing to file");
run(dir, &["git", "add", file_name]);
run(dir, &["git", "commit", "-m", &message]);
}

fn run(dir: &TempDir, cmd: &[&str]) -> String {
String::from_utf8(
Command::new(cmd[0])
.args(&cmd[1..])
.current_dir(dir.path())
.output()
.expect(&format!("failed to execute {:?}", cmd))
.stdout,
)
.expect("failed converting output to String")
}

fn key(char: char) -> Event {
Event::Key(KeyEvent::new(KeyCode::Char(char), KeyModifiers::empty()))
}

fn redact_hashes(terminal: &mut Terminal<TestBackend>, dir: TempDir) -> String {
let mut debug_output = format!("{:#?}", terminal.backend().buffer());
for hash in run(&dir, &["git", "log", "--all", "--format=%h", "HEAD"]).lines() {
debug_output = debug_output.replace(hash, "_______");
}
debug_output
}
}
62 changes: 26 additions & 36 deletions src/snapshots/gitu__tests__fresh_init.snap
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
---
source: src/main.rs
expression: terminal.backend().buffer()
expression: "redact_hashes(terminal, dir)"
---
Buffer {
area: Rect { x: 0, y: 0, width: 80, height: 30 },
area: Rect { x: 0, y: 0, width: 60, height: 20 },
content: [
" On branch master ",
" ",
" No commits yet ",
" ",
" nothing to commit (create/copy files and use "git add" to track) ",
" ",
"🢒Recent commits ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"🢒Recent commits ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 6, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 6, fg: Rgb(216, 166, 87), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 15, y: 6, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 7, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 1, y: 1, fg: Rgb(216, 166, 87), bg: Rgb(80, 73, 69), underline: Reset, modifier: BOLD,
x: 15, y: 1, fg: Reset, bg: Rgb(80, 73, 69), underline: Reset, modifier: NONE,
x: 0, y: 2, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
]
}
Loading

0 comments on commit e740ce7

Please sign in to comment.