use std::path::Path;
use crate::run_task::diff::{Snapshot, capture_snapshot, diff_snapshots};
use crate::session::{SessionStatus, TaskResult, TaskResultStatus};
pub(super) fn build_task_result(
status: SessionStatus,
before: &Snapshot,
root: &Path,
) -> TaskResult {
let after = capture_snapshot(root);
let changed = !diff_snapshots(before, &after).trim().is_empty();
let diff_ref = if changed { tree_ref(&after) } else { None };
let branch = trusty_common::catchup::git::capture_git_status(root).branch;
let result_status = TaskResultStatus::from_session_status(status);
let summary = summarise(
result_status,
changed,
diff_ref.as_deref(),
branch.as_deref(),
);
TaskResult::new(result_status)
.with_diff_ref(diff_ref)
.with_branch(branch)
.with_pr_ref(None)
.with_summary(Some(summary))
}
fn tree_ref(after: &Snapshot) -> Option<String> {
match after {
Snapshot::Git { tree, .. } if !tree.is_empty() => Some(tree.clone()),
_ => None,
}
}
fn summarise(
status: TaskResultStatus,
changed: bool,
diff_ref: Option<&str>,
branch: Option<&str>,
) -> String {
let change = match (changed, diff_ref) {
(true, Some(r)) => format!("changes captured at {r}"),
(true, None) => "changes captured".to_string(),
(false, _) => "no changes".to_string(),
};
match branch {
Some(b) => format!("{}: {change} on branch {b}", status.as_str()),
None => format!("{}: {change}", status.as_str()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
fn git_repo() -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
for args in [
vec!["init", "--initial-branch=main"],
vec!["config", "user.email", "t@example.invalid"],
vec!["config", "user.name", "t"],
] {
let ok = Command::new("git")
.arg("-C")
.arg(root)
.args(&args)
.output()
.expect("git runs")
.status
.success();
assert!(ok, "git {args:?} failed");
}
std::fs::write(root.join("seed.txt"), "seed\n").expect("write seed");
for args in [
vec!["add", "-A"],
vec!["commit", "-m", "seed", "--no-gpg-sign"],
] {
let ok = Command::new("git")
.arg("-C")
.arg(root)
.args(&args)
.output()
.expect("git runs")
.status
.success();
assert!(ok, "git {args:?} failed");
}
dir
}
#[test]
fn a_run_that_changed_a_file_reports_a_diff_ref_and_branch() {
let dir = git_repo();
let root = dir.path();
let before = capture_snapshot(root);
std::fs::write(root.join("written-by-the-run.txt"), "hello\n").expect("write");
let result = build_task_result(SessionStatus::Finished, &before, root);
assert_eq!(result.status, TaskResultStatus::Success);
let diff_ref = result.diff_ref.expect("a changed git tree yields a ref");
assert_eq!(
diff_ref.len(),
40,
"expected a git tree object SHA, got {diff_ref:?}"
);
assert_eq!(result.branch.as_deref(), Some("main"));
let summary = result.summary.expect("summary");
assert!(
summary.contains(&diff_ref) && summary.contains("main"),
"summary must name the ref and the branch: {summary}"
);
}
#[test]
fn a_run_that_changed_nothing_reports_none_refs() {
let dir = git_repo();
let root = dir.path();
let before = capture_snapshot(root);
let result = build_task_result(SessionStatus::TurnCapExceeded, &before, root);
assert_eq!(result.status, TaskResultStatus::Partial);
assert_eq!(result.diff_ref, None);
assert_eq!(result.pr_ref, None);
assert_eq!(
result.summary.as_deref(),
Some("partial: no changes on branch main")
);
}
#[test]
fn a_non_git_root_reports_no_diff_ref() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
let before = capture_snapshot(root);
std::fs::write(root.join("a.txt"), "a\n").expect("write");
let result = build_task_result(SessionStatus::Finished, &before, root);
assert_eq!(result.status, TaskResultStatus::Success);
assert_eq!(result.diff_ref, None);
assert_eq!(result.branch, None);
assert_eq!(result.summary.as_deref(), Some("success: changes captured"));
}
#[test]
fn pr_ref_is_never_invented() {
let dir = git_repo();
let root = dir.path();
let before = capture_snapshot(root);
std::fs::write(root.join("b.txt"), "b\n").expect("write");
for status in [
SessionStatus::Finished,
SessionStatus::Failed,
SessionStatus::Cancelled,
SessionStatus::DeadlineExceeded,
] {
assert_eq!(build_task_result(status, &before, root).pr_ref, None);
}
}
}