Skip to content

Commit

Permalink
Add overlap and no_overlap, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoJr committed Jul 14, 2020
1 parent 4f689d5 commit 4ad8139
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/cli_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ pub fn get_app() -> App<'static, 'static> {
.help("use default config")
.hidden(true), // only useful for testing
)
.arg(
Arg::with_name("overlap")
.long("overlap")
.required(false)
.conflicts_with("default")
.conflicts_with("no_overlap")
.help("allow overlapping activities"),
)
.arg(
Arg::with_name("no_overlap")
.long("no_overlap")
.required(false)
.conflicts_with("overlap")
.conflicts_with("default")
.help("disallow overlapping activities"),
)
.arg(
Arg::with_name("dry-run")
.short("n")
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ fn main() -> anyhow::Result<()> {
let clock = ChronoClock {};
let app = get_app();
let matches = app.get_matches();
let config = load_config()?;
let config = if matches.is_present("default") {
RTWConfig::default()
} else {
load_config()?
config
};
let config = if matches.is_present("overlap") {
config.deny_overlapping(false)
} else {
config
};
let config = if matches.is_present("no_overlap") {
config.deny_overlapping(true)
} else {
config
};
let storage_dir = match matches.value_of("directory") {
None => config.storage_dir_path.clone(),
Expand Down
8 changes: 8 additions & 0 deletions src/rtw_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ impl RTWConfig {
deny_overlapping: true,
}
}

pub fn deny_overlapping(self, deny: bool) -> Self {
RTWConfig {
storage_dir_path: self.storage_dir_path,
timeline_colors: self.timeline_colors,
deny_overlapping: deny,
}
}
}

fn load_config_from_config_dir(
Expand Down
2 changes: 1 addition & 1 deletion src/rtw_core/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait ActivityService {
///
/// May fail depending on backend implementation
///
/// Returns new current activity
/// Returns new current activity and optionally the previously ongoing activity
fn start_activity(
&mut self,
activity: OngoingActivity,
Expand Down
33 changes: 31 additions & 2 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ mod tests {
}

#[test]
fn track_overlap() {
fn track_overlap_not_allowed() {
let test_dir = tempdir().expect("could not create temp directory");
let test_dir_path = test_dir.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("rtw").unwrap();
Expand All @@ -452,7 +452,7 @@ mod tests {
let mut cmd = Command::cargo_bin("rtw").unwrap();
cmd.arg("-d")
.arg(test_dir_path)
.arg("--default") // use default config ie deny overlapping
.arg("--no_overlap") // deny overlapping
.arg("track")
.arg("09:30")
.arg("-")
Expand All @@ -463,6 +463,35 @@ mod tests {
.stderr(predicates::str::contains("would overlap"));
}

#[test]
fn track_overlap_allowed() {
let test_dir = tempdir().expect("could not create temp directory");
let test_dir_path = test_dir.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("rtw").unwrap();
cmd.arg("-d")
.arg(test_dir_path)
.arg("track")
.arg("09:00")
.arg("-")
.arg("10:00")
.arg("foo")
.assert()
.success()
.stdout(predicates::str::contains("Recorded foo"));
let mut cmd = Command::cargo_bin("rtw").unwrap();
cmd.arg("-d")
.arg(test_dir_path)
.arg("--overlap") // deny overlapping
.arg("track")
.arg("09:30")
.arg("-")
.arg("11:00")
.arg("bar")
.assert()
.success()
.stdout(predicates::str::contains("Recorded bar"));
}

#[test]
fn start_nothing_now() {
let test_dir = tempdir().expect("could not create temp directory");
Expand Down

0 comments on commit 4ad8139

Please sign in to comment.