Skip to content

Commit 948f4bb

Browse files
committed
clap unittest
1 parent 7d9e6f8 commit 948f4bb

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

src/args.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,46 @@ pub struct CliArgs {
1818
}
1919

2020
pub fn process_cmdline() -> Result<CliArgs> {
21+
let app = app();
22+
23+
let arg_matches = app.get_matches();
24+
if arg_matches.is_present("bugreport") {
25+
bug_report::generate_bugreport();
26+
std::process::exit(0);
27+
}
28+
if arg_matches.is_present("logging") {
29+
setup_logging()?;
30+
}
31+
32+
let workdir = arg_matches.value_of("workdir").map(PathBuf::from);
33+
let gitdir = arg_matches
34+
.value_of("directory")
35+
.map_or_else(|| PathBuf::from("."), PathBuf::from);
36+
37+
#[allow(clippy::option_if_let_else)]
38+
let repo_path = if let Some(w) = workdir {
39+
RepoPath::Workdir { gitdir, workdir: w }
40+
} else {
41+
RepoPath::Path(gitdir)
42+
};
43+
44+
let arg_theme =
45+
arg_matches.value_of("theme").unwrap_or("theme.ron");
46+
47+
if get_app_config_path()?.join(arg_theme).is_file() {
48+
Ok(CliArgs {
49+
theme: get_app_config_path()?.join(arg_theme),
50+
repo_path,
51+
})
52+
} else {
53+
Ok(CliArgs {
54+
theme: get_app_config_path()?.join("theme.ron"),
55+
repo_path,
56+
})
57+
}
58+
}
59+
60+
fn app() -> ClapApp<'static> {
2161
let app = ClapApp::new(crate_name!())
2262
.author(crate_authors!())
2363
.version(crate_version!())
@@ -57,42 +97,7 @@ pub fn process_cmdline() -> Result<CliArgs> {
5797
.env("GIT_WORK_TREE")
5898
.takes_value(true),
5999
);
60-
61-
let arg_matches = app.get_matches();
62-
if arg_matches.is_present("bugreport") {
63-
bug_report::generate_bugreport();
64-
std::process::exit(0);
65-
}
66-
if arg_matches.is_present("logging") {
67-
setup_logging()?;
68-
}
69-
70-
let workdir = arg_matches.value_of("workdir").map(PathBuf::from);
71-
let gitdir = arg_matches
72-
.value_of("directory")
73-
.map_or_else(|| PathBuf::from("."), PathBuf::from);
74-
75-
#[allow(clippy::option_if_let_else)]
76-
let repo_path = if let Some(w) = workdir {
77-
RepoPath::Workdir { gitdir, workdir: w }
78-
} else {
79-
RepoPath::Path(gitdir)
80-
};
81-
82-
let arg_theme =
83-
arg_matches.value_of("theme").unwrap_or("theme.ron");
84-
85-
if get_app_config_path()?.join(arg_theme).is_file() {
86-
Ok(CliArgs {
87-
theme: get_app_config_path()?.join(arg_theme),
88-
repo_path,
89-
})
90-
} else {
91-
Ok(CliArgs {
92-
theme: get_app_config_path()?.join("theme.ron"),
93-
repo_path,
94-
})
95-
}
100+
app
96101
}
97102

98103
fn setup_logging() -> Result<()> {
@@ -129,3 +134,8 @@ pub fn get_app_config_path() -> Result<PathBuf> {
129134
fs::create_dir_all(&path)?;
130135
Ok(path)
131136
}
137+
138+
#[test]
139+
fn verify_app() {
140+
app().debug_assert();
141+
}

0 commit comments

Comments
 (0)