Skip to content

Commit

Permalink
refactor(Project Paths): Use std::default for Args
Browse files Browse the repository at this point in the history
From the days when `new`, `load`, and `snapshot` were in separate repositories and before `common` existed code for getting the default directory and project path, as well as checking first run was duplicated in each crate.

Finally refactor the duplication out. Additionally stop using string formed arguments and utilize real structs. Uses `std::default` to make struct creation simpler and less noisy in tests.
  • Loading branch information
brianp committed Dec 11, 2019
1 parent f5bce1c commit 9a0ec6d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 66 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ doctest = false

[dependencies]
dirs = "1.0.5"
rand = "0.3.15"
rustc-serialize = "0.3"
20 changes: 20 additions & 0 deletions common/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! The struct managing cli args
use rand::random;

/// The args struct for taking arguments passed in from the command line
/// and making it easier to pass around.
Expand Down Expand Up @@ -26,3 +27,22 @@ pub struct Args {
pub cmd_new: bool,
pub cmd_snapshot: bool,
}

impl Default for Args {
fn default() -> Self {
let name = format!("{}", random::<u16>());

Args {
arg_project: name,
cmd_edit: false,
cmd_new: true,
cmd_snapshot: false,
flag_d: true,
flag_debug: false,
flag_f: false,
flag_p: None,
flag_t: None,
flag_v: false,
}
}
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate dirs;
extern crate rand;

pub mod args;
pub mod first_run;
Expand Down
58 changes: 8 additions & 50 deletions common/src/project_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,81 +69,39 @@ mod test {

#[test]
fn expects_tmp_as_default_homedir() {
let args = Args {
arg_project: "projectname".to_string(),
cmd_edit: false,
cmd_new: false,
cmd_snapshot: false,
flag_d: false,
flag_debug: false,
flag_dryrun: false,
flag_f: false,
flag_p: None,
flag_t: None,
flag_v: false,
};

let args: Args = Default::default();
let project_paths = project_paths(&args);

assert_eq!(project_paths.home_directory, PathBuf::from("/tmp"))
}

#[test]
fn expects_muxed_as_default_project_dir() {
let args = Args {
arg_project: "projectname".to_string(),
cmd_edit: false,
cmd_new: false,
cmd_snapshot: false,
flag_d: false,
flag_debug: false,
flag_dryrun: false,
flag_f: false,
flag_p: None,
flag_t: None,
flag_v: false,
};

let args: Args = Default::default();
let project_paths = project_paths(&args);

assert_eq!(project_paths.project_directory, PathBuf::from("/tmp/.muxed"))
}

#[test]
fn expects_spacey_as_homedir() {
let args = Args {
arg_project: "projectname".to_string(),
cmd_edit: false,
cmd_new: false,
cmd_snapshot: false,
flag_d: false,
flag_debug: false,
flag_dryrun: false,
flag_f: false,
flag_p: Some("/spacey".to_string()),
flag_t: None,
flag_v: false,
..Default::default()
};

let project_paths = project_paths(&args);

assert_eq!(project_paths.project_directory, PathBuf::from("/spacey"))
}

#[test]
fn expects_projectname_as_yml_file() {
let args = Args {
arg_project: "projectname".to_string(),
cmd_edit: false,
cmd_new: false,
cmd_snapshot: false,
flag_d: false,
flag_debug: false,
flag_dryrun: false,
flag_f: false,
flag_p: None,
flag_t: None,
flag_v: false,
..Default::default()
};

let project_paths = project_paths(&args);

assert_eq!(project_paths.project_file, PathBuf::from("/tmp/.muxed/projectname.yml"))
}
}
11 changes: 2 additions & 9 deletions load/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,9 @@ pub fn test_with_contents(contents: &[u8]) -> snapshot::tmux::session::Session {

fn open_muxed(project: &str, project_root: &Path) -> Result<(), String> {
let args = Args {
flag_debug: false,
flag_d: true,
flag_v: false,
flag_f: false,
flag_p: Some(format!("{}", project_root.display())),
flag_t: None,
arg_project: project.to_string(),
cmd_edit: false,
cmd_new: false,
cmd_snapshot: false,
flag_p: Some(format!("{}", project_root.display())),
..Default::default()
};

load::exec(args)
Expand Down
8 changes: 1 addition & 7 deletions new/tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ mod test {

pub fn new(project: &str, project_root: &PathBuf) -> Result<(), String> {
let args = Args {
flag_debug: false,
flag_d: true,
flag_v: false,
flag_f: false,
flag_p: Some(format!("{}", project_root.display())),
flag_t: None,
arg_project: project.to_string(),
cmd_edit: false,
cmd_new: true,
cmd_snapshot: false,
..Default::default()
};

new::exec(args)
Expand Down

0 comments on commit 9a0ec6d

Please sign in to comment.