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
|
use anyhow::Result;
use serial_test::serial;
use test_utils::{git::GitTestRepo, *};
#[test]
fn when_to_branch_doesnt_exist_return_error() -> Result<()> {
let test_repo = GitTestRepo::default();
test_repo.populate()?;
let mut p = CliTester::new_from_dir(
&test_repo.dir,
["prs", "create", "--to-branch", "nonexistant"],
);
p.expect("Error: cannot find to_branch 'nonexistant'")?;
Ok(())
}
#[test]
fn when_no_to_branch_specified_and_no_main_or_master_branch_return_error() -> Result<()> {
let test_repo = GitTestRepo::new("notmain")?;
test_repo.populate()?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
p.expect("Error: a destination branch (to_branch) is not specified and the defaults (main or master) do not exist")?;
Ok(())
}
#[test]
fn when_from_branch_doesnt_exist_return_error() -> Result<()> {
let test_repo = GitTestRepo::default();
test_repo.populate()?;
let mut p = CliTester::new_from_dir(
&test_repo.dir,
["prs", "create", "--from-branch", "nonexistant"],
);
p.expect("Error: cannot find from_branch 'nonexistant'")?;
Ok(())
}
#[test]
fn when_no_commits_ahead_of_main_return_error() -> Result<()> {
let test_repo = GitTestRepo::default();
test_repo.populate()?;
// create feature branch with 1 commit ahead
test_repo.create_branch("feature")?;
test_repo.checkout("feature")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
p.expect("Error: 'head' is 0 commits ahead of 'main' so no patches were created")?;
Ok(())
}
mod when_commits_behind_ask_to_proceed {
use super::*;
fn prep_test_repo() -> Result<GitTestRepo> {
let test_repo = GitTestRepo::default();
test_repo.populate()?;
// create feature branch with 2 commit ahead
test_repo.create_branch("feature")?;
test_repo.checkout("feature")?;
std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
test_repo.stage_and_commit("add t3.md")?;
std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
test_repo.stage_and_commit("add t4.md")?;
// checkout main and add 1 commit
test_repo.checkout("main")?;
std::fs::write(test_repo.dir.join("t5.md"), "some content")?;
test_repo.stage_and_commit("add t5.md")?;
// checkout feature branch
test_repo.checkout("feature")?;
Ok(test_repo)
}
static BEHIND_LEN: u8 = 1;
static AHEAD_LEN: u8 = 2;
fn expect_confirm_prompt(
p: &mut CliTester,
behind: u8,
ahead: u8,
) -> Result<CliTesterConfirmPrompt> {
p.expect_confirm(
format!("'head' is {behind} commits behind 'main' and {ahead} ahead. Consider rebasing before sending patches. Proceed anyway?").as_str(),
Some(false),
)
}
#[test]
fn asked_with_default_no() -> Result<()> {
let test_repo = prep_test_repo()?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?;
p.exit()?;
Ok(())
}
#[test]
fn when_response_is_false_aborts() -> Result<()> {
let test_repo = prep_test_repo()?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?.succeeds_with(Some(false))?;
p.expect_end_with("Error: aborting so branch can be rebased\r\n")?;
Ok(())
}
#[test]
#[serial]
fn when_response_is_true_proceeds() -> Result<()> {
let test_repo = prep_test_repo()?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?.succeeds_with(Some(true))?;
p.expect(
format!("creating patch for {AHEAD_LEN} commits from 'head' that are {BEHIND_LEN} behind 'main'",)
.as_str(),
)?;
p.exit()?;
Ok(())
}
}
mod when_no_commits_behind {
use super::*;
#[test]
#[serial]
fn message_for_creating_patches() -> Result<()> {
let test_repo = GitTestRepo::default();
test_repo.populate()?;
// create feature branch with 2 commit ahead
test_repo.create_branch("feature")?;
test_repo.checkout("feature")?;
std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
test_repo.stage_and_commit("add t3.md")?;
std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
test_repo.stage_and_commit("add t4.md")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]);
p.expect("creating patch for 2 commits from 'head' that can be merged into 'main'")?;
p.exit()?;
Ok(())
}
}
// #[test]
// #[serial]
// fn succeeds_with_text_logged_in_as_npub() -> Result<()> {
// with_fresh_config(|| {
// let mut p = CliTester::new(["login"]);
// p.expect_input(EXPECTED_NSEC_PROMPT)?
// .succeeds_with(TEST_KEY_1_NSEC)?;
// p.expect_password(EXPECTED_SET_PASSWORD_PROMPT)?
// .with_confirmation(EXPECTED_SET_PASSWORD_CONFIRM_PROMPT)?
// .succeeds_with(TEST_PASSWORD)?;
// p.expect_end_with(format!("logged in as {}\r\n",
// TEST_KEY_1_NPUB).as_str()) })
// }
|