Skip to content

Commit ae9ca60

Browse files
committed
Added integration tests
1 parent ef9c6f5 commit ae9ca60

14 files changed

Lines changed: 684 additions & 96 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cmd/done.rs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
use crate::{command, config::Config, git, session};
1+
use crate::{command, config::Config, git, prompt::Prompter, session};
22
use anyhow::Result;
33
use session::State;
44

55
pub struct Done<'a> {
66
git: &'a dyn git::Git,
77
store: &'a dyn session::Store,
8+
prompter: &'a dyn Prompter,
89
config: Config,
910
}
1011

1112
impl<'a> Done<'a> {
12-
pub fn new(git: &'a impl git::Git, store: &'a impl session::Store, config: Config) -> Done<'a> {
13-
Self { git, store, config }
13+
pub fn new(
14+
git: &'a impl git::Git,
15+
store: &'a impl session::Store,
16+
prompter: &'a impl Prompter,
17+
config: Config,
18+
) -> Done<'a> {
19+
Self {
20+
git,
21+
store,
22+
prompter,
23+
config,
24+
}
1425
}
1526

1627
pub fn run(&self) -> Result<()> {
@@ -25,10 +36,9 @@ impl<'a> Done<'a> {
2536
State::Working { driver } if driver == me.as_str() => self.done(session)?,
2637
State::Working { driver } => {
2738
log::warn!("{} is currently working", driver);
28-
let take_over = dialoguer::Confirm::new()
29-
.with_prompt("Merge anyway with risk of loosing work?")
30-
.default(false)
31-
.interact()?;
39+
let take_over = self
40+
.prompter
41+
.confirm("Merge anyway with risk of loosing work?", false)?;
3242

3343
if take_over {
3444
self.done(session)?;

‎src/cmd/order.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::session;
1+
use crate::{prompt::Prompter, session};
22
use anyhow::Result;
3-
use dialoguer::{Confirm, Select, Sort};
43

54
pub struct Order<'a> {
65
store: &'a dyn session::Store,
6+
prompter: &'a dyn Prompter,
77
}
88

99
impl<'a> Order<'a> {
10-
pub fn new(store: &'a impl session::Store) -> Order<'a> {
11-
Self { store }
10+
pub fn new(store: &'a impl session::Store, prompter: &'a impl Prompter) -> Order<'a> {
11+
Self { store, prompter }
1212
}
1313

1414
pub fn run(&self) -> Result<()> {
@@ -25,10 +25,10 @@ impl<'a> Order<'a> {
2525
return Ok(());
2626
}
2727

28-
let order = Sort::new()
29-
.with_prompt("Use [space] and ↓↑ to move driver")
30-
.items(drivers.as_slice())
31-
.interact()?;
28+
let drivers_strs: Vec<&str> = drivers.iter().map(|s| s.as_str()).collect();
29+
let order = self
30+
.prompter
31+
.sort("Use [space] and ↓↑ to move driver", &drivers_strs)?;
3232

3333
let ordered_drivers =
3434
session::Drivers::new(order.into_iter().map(|i| drivers[i].clone()).collect());
@@ -55,17 +55,17 @@ impl<'a> Order<'a> {
5555
let next_driver = ordered_drivers.next(previous_driver.as_str());
5656
let next_driver_name = next_driver.as_ref().unwrap();
5757

58-
let next_driver = if Confirm::new()
59-
.with_prompt(format!("So {} should be next?", next_driver_name))
60-
.interact()?
58+
let next_driver = if self
59+
.prompter
60+
.confirm(&format!("So {} should be next?", next_driver_name), true)?
6161
{
6262
next_driver
6363
} else {
6464
let ordered = ordered_drivers.all();
65-
let next = Select::new()
66-
.with_prompt("Who should be next?")
67-
.items(ordered.as_slice())
68-
.interact()?;
65+
let ordered_strs: Vec<&str> = ordered.iter().map(|s| s.as_str()).collect();
66+
let next = self
67+
.prompter
68+
.select_with_prompt("Who should be next?", &ordered_strs, 0)?;
6969

7070
Some(ordered[next].clone())
7171
};

‎src/cmd/start.rs‎

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use crate::{command, config::Config, git, session, timer};
2-
use anyhow::{anyhow, Result};
1+
use crate::{command, config::Config, git, prompt::Prompter, session, timer};
2+
use anyhow::{Result, anyhow};
33
use clap::Parser;
44
use session::State;
55

66
#[derive(Parser, Debug)]
77
pub struct StartOpts {
88
/// How long you want this work session to last
99
#[clap(name = "MINUTES")]
10-
minutes: Option<i64>,
10+
pub minutes: Option<i64>,
1111
}
1212

1313
pub struct Start<'a> {
1414
git: &'a dyn git::Git,
1515
store: &'a dyn session::Store,
16+
prompter: &'a dyn Prompter,
1617
opts: StartOpts,
1718
config: Config,
1819
}
@@ -21,12 +22,14 @@ impl<'a> Start<'a> {
2122
pub fn new(
2223
git: &'a impl git::Git,
2324
store: &'a impl session::Store,
25+
prompter: &'a impl Prompter,
2426
opts: StartOpts,
2527
config: Config,
2628
) -> Start<'a> {
2729
Self {
2830
git,
2931
store,
32+
prompter,
3033
opts,
3134
config,
3235
}
@@ -48,11 +51,9 @@ impl<'a> Start<'a> {
4851
State::Working { driver } => {
4952
log::warn!("{} has not run mob next", driver);
5053
let selections = &["Retry", "Take turn with the risk of losing work"];
51-
let selection = dialoguer::Select::new()
52-
.with_prompt("What do you want to do?")
53-
.default(0)
54-
.items(&selections[..])
55-
.interact()?;
54+
let selection =
55+
self.prompter
56+
.select_with_prompt("What do you want to do?", selections, 0)?;
5657

5758
match selection {
5859
0 => return self.run(),
@@ -83,27 +84,29 @@ impl<'a> Start<'a> {
8384

8485
log::warn!("Working tree is dirty:\n{}", status);
8586

86-
let selection = dialoguer::Select::new()
87-
.default(0)
88-
.items(&["Quit", "Stash changes", "Discard changes"])
89-
.interact()?;
87+
let selection = self
88+
.prompter
89+
.select(&["Quit", "Stash changes", "Discard changes"], 0)?;
9090

9191
match selection {
9292
0 => Err(anyhow!("Working tree is not clean")),
93-
1 => self.git.run(&["stash"]),
94-
2 => self.git.run(&["reset", "HEAD", "--hard"]),
93+
1 => self.git.run(&["stash", "--include-untracked"]),
94+
2 => self
95+
.git
96+
.run(&["reset", "HEAD", "--hard"])
97+
.and_then(|_| self.git.run(&["clean", "-fdx"])),
9598
_ => unreachable!("could not come here"),
9699
}
97100
}
98101

99102
fn take_over(&self, from: &str, session: session::Session) -> Result<()> {
100103
let take_and_remove = format!("Take turn and remove {} from the mob", from);
101104
let selections = &["Take turn", take_and_remove.as_str(), "Abort"];
102-
let selection = dialoguer::Select::new()
103-
.with_prompt(format!("It's {}s turn. What do you want to do?", from).as_str())
104-
.default(0)
105-
.items(&selections[..])
106-
.interact()?;
105+
let selection = self.prompter.select_with_prompt(
106+
&format!("It's {}s turn. What do you want to do?", from),
107+
selections,
108+
0,
109+
)?;
107110

108111
match selection {
109112
0 => self.start(session),
@@ -158,7 +161,7 @@ impl<'a> Start<'a> {
158161

159162
let settings = match session.settings {
160163
Some(settings) => settings,
161-
None => session::Settings::ask()?,
164+
None => session::Settings::ask(self.prompter)?,
162165
};
163166

164167
let default_branches = session::Branches {
@@ -170,20 +173,26 @@ impl<'a> Start<'a> {
170173
..session.branches
171174
};
172175

173-
let branches = session::Branches::ask(default_branches)?;
176+
let branches = session::Branches::ask(self.prompter, default_branches)?;
174177

175178
let remote_branches = branches.with_remote(&self.config.remote);
176179

177180
self.git.run(&["fetch", "--all", "--prune"])?;
178181

179-
if !self.git.has_branch(remote_branches.base_branch.as_str())? {
182+
println!(
183+
"Git status: {:?}, {}",
184+
self.git.current_branch()?,
185+
remote_branches.base_branch
186+
);
187+
188+
if !self.git.has_branch(&remote_branches.base_branch)? {
180189
return Err(anyhow!(
181190
"You need to push your branch `{}` first",
182191
branches.base_branch
183192
));
184193
}
185194

186-
self.git.run(&["checkout", branches.base_branch.as_str()])?;
195+
self.git.run(&["checkout", &branches.base_branch])?;
187196
self.git
188197
.run(&["merge", remote_branches.base_branch.as_str(), "--ff-only"])?;
189198

@@ -235,11 +244,7 @@ impl<'a> Start<'a> {
235244
"Remove local branch and checkout remote",
236245
"Delete local and remote branch and start fresh",
237246
];
238-
let selection = dialoguer::Select::new()
239-
.with_prompt(prompt)
240-
.default(0)
241-
.items(&selections[..])
242-
.interact()?;
247+
let selection = self.prompter.select_with_prompt(&prompt, selections, 0)?;
243248

244249
match selection {
245250
0 => {
@@ -270,11 +275,7 @@ impl<'a> Start<'a> {
270275
branches.branch
271276
);
272277
let selections = &["Push local branch", "Delete local branch and start fresh"];
273-
let selection = dialoguer::Select::new()
274-
.with_prompt(prompt)
275-
.default(0)
276-
.items(&selections[..])
277-
.interact()?;
278+
let selection = self.prompter.select_with_prompt(&prompt, selections, 0)?;
278279

279280
if selection == 0 {
280281
self.git.run(&[
@@ -297,11 +298,7 @@ impl<'a> Start<'a> {
297298
"Checkout remote branch",
298299
"Delete remote branch and start fresh",
299300
];
300-
let selection = dialoguer::Select::new()
301-
.with_prompt(prompt)
302-
.default(0)
303-
.items(&selections[..])
304-
.interact()?;
301+
let selection = self.prompter.select_with_prompt(&prompt, selections, 0)?;
305302

306303
match selection {
307304
0 => {

‎src/command.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::config;
1313
use crate::os;
1414

1515
/// The decoded output after running a command.
16+
#[derive(Debug)]
1617
pub struct Output {
1718
pub status: process::ExitStatus,
1819
pub stdout: String,
@@ -127,6 +128,7 @@ impl<'name> Command<'name> {
127128
S: AsRef<OsStr>,
128129
{
129130
let mut cmd = self.command(args);
131+
log::trace!("running: {:?}", &cmd);
130132
let output = cmd.output()?;
131133

132134
let output = Output {
@@ -138,6 +140,7 @@ impl<'name> Command<'name> {
138140
io::Error::new(io::ErrorKind::Other, "Cannot decode stderr as utf-8")
139141
})?,
140142
};
143+
log::trace!("output: {:?}", &output);
141144

142145
Ok(output)
143146
}

‎src/git/mod.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod store;
22
use crate::command;
33
use crate::os;
4-
use anyhow::{anyhow, Result};
4+
use anyhow::{Result, anyhow};
55
use std::env;
66
use std::path::PathBuf;
77
pub use store::Store;
@@ -68,13 +68,19 @@ impl GitCommand {
6868
let parent = self.last_commit_oid(commit.reference);
6969
let commit_oid = match parent {
7070
Some(parent_oid) => self.command.run_with_stdin(
71-
["commit-tree", tree_oid, "-p", &parent_oid, "-m", commit.message],
72-
&[],
73-
)?,
74-
None => self.command.run_with_stdin(
75-
["commit-tree", tree_oid, "-m", commit.message],
71+
[
72+
"commit-tree",
73+
tree_oid,
74+
"-p",
75+
&parent_oid,
76+
"-m",
77+
commit.message,
78+
],
7679
&[],
7780
)?,
81+
None => self
82+
.command
83+
.run_with_stdin(["commit-tree", tree_oid, "-m", commit.message], &[])?,
7884
};
7985
let commit_oid = commit_oid.trim().to_string();
8086

@@ -110,7 +116,6 @@ impl GitCommand {
110116

111117
impl Git for GitCommand {
112118
fn run(&self, args: &[&str]) -> Result<()> {
113-
log::debug!("git {}", args.join(" "));
114119
self.command.run_checked(args)
115120
}
116121

@@ -119,10 +124,9 @@ impl Git for GitCommand {
119124
}
120125

121126
fn has_branch(&self, branch: &str) -> Result<bool> {
122-
let absolute_ref = format!("refs/heads/{}", branch);
123127
let output = self
124128
.command
125-
.run(["rev-parse", "--verify", "--quiet", &absolute_ref])?;
129+
.run(["rev-parse", "--verify", "--quiet", branch])?;
126130
Ok(output.status.success())
127131
}
128132

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ pub mod duration;
55
pub mod emoji_logger;
66
pub mod git;
77
mod os;
8+
pub mod prompt;
89
pub mod session;
910
pub mod timer;

‎src/main.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Result;
22
use clap::Parser;
3-
use remotemob::{cmd, config, emoji_logger, git, session, session::Store};
3+
use remotemob::{cmd, config, emoji_logger, git, prompt::DialoguerPrompter, session, session::Store};
44

55
#[derive(Parser)]
66
#[clap(version = clap::crate_version!(), author = clap::crate_authors!())]
@@ -55,14 +55,15 @@ fn run() -> Result<()> {
5555

5656
let git = git::GitCommand::new(None, config.remote.clone())?;
5757
let store = session::SessionStore::new(&git);
58+
let prompter = DialoguerPrompter;
5859

5960
match opts.subcmd {
60-
SubCommand::Start(opts) => cmd::Start::new(&git, &store, opts, config).run()?,
61+
SubCommand::Start(opts) => cmd::Start::new(&git, &store, &prompter, opts, config).run()?,
6162
SubCommand::Next => cmd::Next::new(&git, &store, config).run()?,
62-
SubCommand::Done => cmd::Done::new(&git, &store, config).run()?,
63+
SubCommand::Done => cmd::Done::new(&git, &store, &prompter, config).run()?,
6364
SubCommand::Clean => store.clean()?,
6465
SubCommand::Status(opts) => cmd::Status::new(opts, &store, config).run()?,
65-
SubCommand::Order => cmd::Order::new(&store).run()?,
66+
SubCommand::Order => cmd::Order::new(&store, &prompter).run()?,
6667
};
6768
Ok(())
6869
}

0 commit comments

Comments
 (0)