@@ -0,0 +1,82 @@
1+//! The trial's edges, on a throwaway library: a branch that doesn't build, two that conflict,
2+//! a follow-up built on the trial, and a discard. It builds the real app, into its own target
3+//! dir, so the first run takes minutes: cargo test --release -p conveyor trial -- --nocapture
4+use super::*;
5+use std::path::PathBuf;
6+
7+const WIDGET: &str = "use super::api::*;\nuse rune::{El, col};\n\
8+ pub const WIDGET: Widget = Widget { title: \"T\", size: (10.0, 10.0), new: |_| Box::new(T) };\n\
9+ pub struct T;\nimpl View for T { fn view(&self, _: &Cx) -> El { col() } }\n";
10+
11+fn run(dir: &Path, args: &[&str]) -> String {
12+ git(dir, "", &[&COMMIT[..], args].concat()).unwrap()
13+}
14+
15+/// Whether `rev` is in `of`'s history.
16+fn has(rev: &str, of: &str) -> bool {
17+ git(&paths::library(), "", &["merge-base", "--is-ancestor", rev, of]).is_ok()
18+}
19+
20+fn state(id: u32) -> String {
21+ store::ticket(SLUG, id).unwrap().state
22+}
23+
24+/// A build ticket whose agent wrote `text` to widgets/`file` on branch t-<id>, from `from`.
25+fn build(from: &str, file: &str, text: &str) -> (u32, String) {
26+ let id = store::create(SLUG, file, "", "running");
27+ let (lib, w, branch) = (paths::library(), paths::data().join("author"), format!("t-{id}"));
28+ run(&lib, &["worktree", "add", "-q", "-B", &branch, &w.to_string_lossy(), from]);
29+ fs::write(w.join("widgets").join(file), text).unwrap();
30+ run(&w, &["add", "-A"]);
31+ run(&w, &["commit", "-qm", file]);
32+ run(&lib, &["worktree", "remove", "--force", &w.to_string_lossy()]);
33+ (id, branch)
34+}
35+
36+#[test]
37+fn trial_edges() {
38+ let root = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/trial-test"));
39+ let _ = fs::remove_dir_all(root.join("home"));
40+ // Safe: this is the only test that touches the environment. cargo and rustup keep their
41+ // real homes, so the app's build finds the toolchain.
42+ let real = PathBuf::from(std::env::var("HOME").unwrap());
43+ unsafe {
44+ std::env::set_var("CARGO_HOME", std::env::var("CARGO_HOME").map_or(real.join(".cargo"), PathBuf::from));
45+ std::env::set_var("RUSTUP_HOME", std::env::var("RUSTUP_HOME").map_or(real.join(".rustup"), PathBuf::from));
46+ std::env::set_var("HOME", root.join("home"));
47+ std::env::set_var("CARGO_TARGET_DIR", root.join("target"));
48+ }
49+ fs::create_dir_all(paths::projects()).unwrap();
50+ ensure().unwrap();
51+ let p = store::project(SLUG).unwrap();
52+ let main_root = fs::read_to_string(paths::library().join("widgets/root.rs")).unwrap();
53+
54+ // A branch that doesn't build never goes on trial, and the trial is main again.
55+ let (bad, branch) = build("main", "broken.rs", "not rust");
56+ let err = try_out(&p, bad, &branch).unwrap_err();
57+ assert!(err.contains("doesn't build") && err.contains("broken.rs"), "{err}");
58+ assert!(!has(&branch, "trial") && state(bad) == "running");
59+
60+ // Two branches that change the same lines: the second stays off the trial.
61+ let (b, branch_b) = build("main", "root.rs", &format!("{main_root}// b\n"));
62+ try_out(&p, b, &branch_b).unwrap();
63+ assert_eq!(state(b), "review");
64+ let (c, branch_c) = build("main", "root.rs", &format!("{main_root}// c\n"));
65+ assert!(try_out(&p, c, &branch_c).unwrap_err().contains("conflicts"));
66+ assert!(has(&branch_b, "trial") && !has(&branch_c, "trial"));
67+
68+ // A follow-up starts from the trial; keeping it keeps what it was built on.
69+ let (d, branch_d) = build("trial", "d.rs", WIDGET);
70+ try_out(&p, d, &branch_d).unwrap();
71+ keep(d).unwrap();
72+ assert!(state(d) == "done" && has(&branch_d, "main") && has(&branch_b, "main"));
73+ keep(b).unwrap();
74+ assert_eq!(state(b), "done");
75+
76+ // Discard: back to draft, out of the trial.
77+ let (e, branch_e) = build("main", "e.rs", WIDGET);
78+ try_out(&p, e, &branch_e).unwrap();
79+ assert!(has(&branch_e, "trial"));
80+ discard(e).unwrap();
81+ assert!(state(e) == "draft" && !has(&branch_e, "trial") && !has(&branch_e, "main"));
82+}