1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! A temp repo with a goal, and helpers that run jeans and jj in it.
#![allow(dead_code)]
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::Output;
pub const JEANS: &str = env!("CARGO_BIN_EXE_jeans");
pub const DONE: &str = r#"bookmarks(exact:"jeans/thing/done")"#;
pub struct Sandbox {
// Held for its Drop, which removes the directory.
_tmp: tempfile::TempDir,
pub tmp: PathBuf,
pub repo: PathBuf,
pub config: PathBuf,
pub goal: String,
}
impl Sandbox {
/// A repo at `repo` whose goal "thing" has a team size of 2.
pub fn new() -> Self {
let mut sandbox = Self::bare("repo");
let home = sandbox.tmp.join("jeans");
sandbox.goal = sandbox.jeans([
OsStr::new("init"),
OsStr::new("ship the thing"),
OsStr::new("--goal"),
OsStr::new("thing"),
OsStr::new("--team"),
OsStr::new("2"),
OsStr::new("--home"),
home.as_os_str(),
]);
sandbox
}
/// An empty jj repo at NAME, with no goal yet.
pub fn bare(name: &str) -> Self {
let tmp_dir = tempfile::tempdir().unwrap();
// Canonical, so paths jeans prints compare equal on macOS's /private/var.
let tmp = tmp_dir.path().canonicalize().unwrap();
// Keeps the developer's own jj config, like a trunk() alias, out of the tests.
let config = tmp.join("jj-config.toml");
fs_err::write(
&config,
"user.name = \"Test\"\nuser.email = \"test@example.com\"\n",
)
.unwrap();
let sandbox = Self {
_tmp: tmp_dir,
repo: tmp.join(name),
tmp,
config,
goal: String::new(),
};
sandbox.use_repo(name);
sandbox
}
/// Makes and switches to a fresh jj repo at NAME.
pub fn use_repo(&self, name: &str) -> PathBuf {
let repo = self.tmp.join(name);
fs_err::create_dir_all(&repo).unwrap();
self.sh_in(&repo, ["git", "init", "."]);
repo
}
pub fn command(&self, program: &str, dir: &Path) -> Command {
let mut command = Command::new(program);
command
.current_dir(dir)
.env("JJ_CONFIG", &self.config)
.env_remove("NO_COLOR");
command
}
pub fn run_in<I, S>(&self, dir: &Path, args: I) -> Output
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.command(JEANS, dir).args(args).output().unwrap()
}
/// Runs jeans in the repo, asserts it succeeded, and returns its stdout
/// stripped.
pub fn jeans<I, S>(&self, args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.jeans_in(&self.repo, args)
}
pub fn jeans_in<I, S>(&self, dir: &Path, args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let args: Vec<_> = args.into_iter().map(|a| a.as_ref().to_owned()).collect();
let out = self.run_in(dir, &args);
assert!(
out.status.success(),
"jeans {args:?} failed: {}",
String::from_utf8(out.stderr).unwrap()
);
String::from_utf8(out.stdout).unwrap().trim().to_owned()
}
/// Runs jeans in the repo, asserts it failed, and returns its stderr
/// stripped.
pub fn jeans_fails<I, S>(&self, args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let out = self.run_in(&self.repo, args);
assert!(!out.status.success(), "jeans succeeded: {out:?}");
String::from_utf8(out.stderr).unwrap().trim().to_owned()
}
/// Runs jj in the repo and returns its stdout.
pub fn sh<I, S>(&self, args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.sh_in(&self.repo, args)
}
pub fn sh_in<I, S>(&self, dir: &Path, args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let out = self.command("jj", dir).args(args).output().unwrap();
assert!(
out.status.success(),
"jj failed: {}",
String::from_utf8(out.stderr).unwrap()
);
String::from_utf8(out.stdout).unwrap()
}
pub fn claim(&self, task: &str) -> PathBuf {
PathBuf::from(self.jeans(["claim", task]))
}
/// Reusing the task's title as the commit title keeps the views' labels
/// stable.
pub fn finish(&self, task: &str) {
let title = self.title(task);
self.jeans(["done", task, "-m", &title]);
}
pub fn title(&self, rev: &str) -> String {
self.sh([
"log",
"--no-graph",
"-r",
rev,
"-T",
"description.first_line()",
])
}
pub fn description(&self, rev: &str) -> String {
self.sh(["log", "--no-graph", "-r", rev, "-T", "description"])
}
pub fn change_id(&self, rev: &str) -> String {
self.sh(["log", "--no-graph", "-r", rev, "-T", "change_id"])
}
pub fn parents(&self, rev: &str) -> Vec<String> {
lines(&self.sh([
"log",
"--no-graph",
"-r",
&format!("parents({rev})"),
"-T",
r#"change_id ++ "\n""#,
]))
}
pub fn ready(&self) -> Vec<String> {
lines(&self.jeans(["ready"]))
}
}
pub fn lines(text: &str) -> Vec<String> {
text.lines().map(str::to_owned).collect()
}
pub fn sorted(mut items: Vec<String>) -> Vec<String> {
items.sort();
items
}
pub fn short(id: &str, len: usize) -> &str {
&id[..len]
}
/// The 12-character change IDs in TEXT, in order.
pub fn ids_in(text: &str) -> Vec<String> {
text.split(|c: char| !c.is_ascii_alphanumeric())
.filter(|w| w.len() == 12 && w.bytes().all(|b| (b'k'..=b'z').contains(&b)))
.map(str::to_owned)
.collect()
}