Skip to content

Commit cb3a23c

Browse files
committed
test(cli): cover normal and staged-diff playback startup paths via non-interactive integration tests
1 parent ba4942e commit cb3a23c

1 file changed

Lines changed: 101 additions & 9 deletions

File tree

‎tests/cli_non_interactive.rs‎

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use git2::Repository;
2+
use git2::{Repository, Signature, Time};
33
use std::fs;
44
use std::path::{Path, PathBuf};
55
use std::process::{Command, Output};
@@ -26,6 +26,7 @@ struct TempHome {
2626

2727
struct TestRepo {
2828
path: PathBuf,
29+
repo: Repository,
2930
}
3031

3132
impl TempHome {
@@ -44,8 +45,60 @@ impl TestRepo {
4445
fn new() -> Result<Self> {
4546
let path = unique_path("gitlogue-cli-repo");
4647
fs::create_dir_all(&path)?;
47-
Repository::init(&path)?;
48-
Ok(Self { path })
48+
let repo = Repository::init(&path)?;
49+
Ok(Self { path, repo })
50+
}
51+
52+
fn write_file(&self, relative_path: &str, content: &str) -> Result<()> {
53+
let file_path = self.path.join(relative_path);
54+
if let Some(parent) = file_path.parent() {
55+
fs::create_dir_all(parent)?;
56+
}
57+
fs::write(file_path, content)?;
58+
Ok(())
59+
}
60+
61+
fn stage_file(&self, relative_path: &str) -> Result<()> {
62+
let mut index = self.repo.index()?;
63+
index.add_path(Path::new(relative_path))?;
64+
index.write()?;
65+
Ok(())
66+
}
67+
68+
fn commit_file(
69+
&self,
70+
relative_path: &str,
71+
content: &str,
72+
message: &str,
73+
timestamp: i64,
74+
) -> Result<String> {
75+
self.write_file(relative_path, content)?;
76+
self.stage_file(relative_path)?;
77+
78+
let mut index = self.repo.index()?;
79+
let tree_id = index.write_tree()?;
80+
let tree = self.repo.find_tree(tree_id)?;
81+
let signature = Signature::new("Test User", "[email protected]", &Time::new(timestamp, 0))?;
82+
let parent = self
83+
.repo
84+
.head()
85+
.ok()
86+
.and_then(|head| head.peel_to_commit().ok());
87+
let oid = match parent.as_ref() {
88+
Some(parent_commit) => self.repo.commit(
89+
Some("HEAD"),
90+
&signature,
91+
&signature,
92+
message,
93+
&tree,
94+
&[parent_commit],
95+
)?,
96+
None => self
97+
.repo
98+
.commit(Some("HEAD"), &signature, &signature, message, &tree, &[])?,
99+
};
100+
101+
Ok(oid.to_string())
49102
}
50103
}
51104

@@ -79,8 +132,12 @@ fn run_command(command: &mut Command) -> Result<Output> {
79132
Ok(command.output()?)
80133
}
81134

82-
fn stdout(output: Output) -> String {
83-
String::from_utf8(output.stdout).unwrap()
135+
fn stdout(output: &Output) -> String {
136+
String::from_utf8(output.stdout.clone()).unwrap()
137+
}
138+
139+
fn stderr(output: &Output) -> String {
140+
String::from_utf8(output.stderr.clone()).unwrap()
84141
}
85142

86143
fn repo_path(repo: &TestRepo) -> &Path {
@@ -91,7 +148,7 @@ fn repo_path(repo: &TestRepo) -> &Path {
91148
fn license_flag_prints_third_party_licenses() -> Result<()> {
92149
let output = run_command(gitlogue_command().arg("--license"))?;
93150
assert!(output.status.success());
94-
let stdout = stdout(output);
151+
let stdout = stdout(&output);
95152

96153
assert!(stdout.starts_with(include_str!("../LICENSE-THIRD-PARTY")));
97154

@@ -104,13 +161,13 @@ fn theme_subcommands_list_and_set_default_theme() -> Result<()> {
104161

105162
let list_output = run_command(command_with_home(&home).args(["theme", "list"]))?;
106163
assert!(list_output.status.success());
107-
let list_stdout = stdout(list_output);
164+
let list_stdout = stdout(&list_output);
108165
assert!(list_stdout.contains("Available themes:"));
109166
assert!(list_stdout.contains(" - nord"));
110167

111168
let set_output = run_command(command_with_home(&home).args(["theme", "set", "nord"]))?;
112169
assert!(set_output.status.success());
113-
let set_stdout = stdout(set_output);
170+
let set_stdout = stdout(&set_output);
114171
let config = fs::read_to_string(home.config_path())?;
115172

116173
assert!(set_stdout.contains("Theme set to 'nord'"));
@@ -130,7 +187,42 @@ fn diff_subcommand_reports_no_changes_for_clean_repo() -> Result<()> {
130187
]))?;
131188
assert!(output.status.success());
132189

133-
assert_eq!(stdout(output), "No changes to display\n");
190+
assert_eq!(stdout(&output), "No changes to display\n");
191+
192+
Ok(())
193+
}
194+
195+
#[test]
196+
fn diff_subcommand_with_staged_changes_fails_only_after_ui_startup_without_tty() -> Result<()> {
197+
let repo = TestRepo::new()?;
198+
repo.commit_file("src/lib.rs", "fn clean() {}\n", "initial", 1)?;
199+
repo.write_file("src/lib.rs", "fn staged() {}\n")?;
200+
repo.stage_file("src/lib.rs")?;
201+
202+
let output = run_command(gitlogue_command().args([
203+
"--path",
204+
repo_path(&repo).to_str().unwrap(),
205+
"diff",
206+
]))?;
207+
208+
assert!(!output.status.success());
209+
assert_eq!(stdout(&output), "");
210+
assert!(stderr(&output).contains("No such device or address"));
211+
212+
Ok(())
213+
}
214+
215+
#[test]
216+
fn default_playback_fails_only_after_ui_startup_without_tty() -> Result<()> {
217+
let repo = TestRepo::new()?;
218+
repo.commit_file("src/main.rs", "fn main() {}\n", "initial", 1)?;
219+
220+
let output =
221+
run_command(gitlogue_command().args(["--path", repo_path(&repo).to_str().unwrap()]))?;
222+
223+
assert!(!output.status.success());
224+
assert_eq!(stdout(&output), "");
225+
assert!(stderr(&output).contains("No such device or address"));
134226

135227
Ok(())
136228
}

0 commit comments

Comments
 (0)