on master help sign in
Add error handling
commit866b9e16a866a51644d10760ea2fdd062f0c902f authorsharkdp <davidpeter@web.de> dateThu 11 Oct 2018, 22:50 (8 years ago) parent5af176c
3 files changed+33−22
src/app.rs+8−7 src/config.rs+24−14 src/main.rs+1−1
src/app.rs+8−7
@@ -81,19 +81,19 @@ pub struct App {
8181}
8282
8383impl App {
84− pub fn new() -> Self {
84+ pub fn new() -> Result<Self> {
8585 #[cfg(windows)]
8686 let _ = ansi_term::enable_ansi_support();
8787
8888 let interactive_output = atty::is(Stream::Stdout);
8989
90− App {
91− matches: Self::matches(interactive_output),
90+ Ok(App {
91+ matches: Self::matches(interactive_output)?,
9292 interactive_output,
93− }
93+ })
9494 }
9595
96− fn matches(interactive_output: bool) -> ArgMatches<'static> {
96+ fn matches(interactive_output: bool) -> Result<ArgMatches<'static>> {
9797 let args = if wild::args_os().nth(1) == Some("cache".into())
9898 || wild::args_os().any(|arg| arg == OsStr::new("--no-config"))
9999 {
@@ -104,7 +104,8 @@ impl App {
104104 let mut cli_args = wild::args_os();
105105
106106 // Read arguments from bats config file
107− let mut args = get_args_from_config_file();
107+ let mut args =
108+ get_args_from_config_file().chain_err(|| "Could not parse configuration file")?;
108109
109110 // Put the zero-th CLI argument (program name) first
110111 args.insert(0, cli_args.next().unwrap());
@@ -115,7 +116,7 @@ impl App {
115116 args
116117 };
117118
118− clap_app::build_app(interactive_output).get_matches_from(args)
119+ Ok(clap_app::build_app(interactive_output).get_matches_from(args))
119120 }
120121
121122 pub fn config(&self) -> Result<Config> {
src/config.rs+24−14
@@ -1,44 +1,54 @@
1+use std::env;
12use std::ffi::OsString;
23use std::fs;
34
45use shell_words;
56
67use dirs::PROJECT_DIRS;
8+use util::transpose;
79
8−pub fn get_args_from_config_file() -> Vec<OsString> {
10+pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
911 let config_file = PROJECT_DIRS.config_dir().join("config");
10− fs::read_to_string(config_file)
11− .map(|content| get_args_from_str(&content))
12− .unwrap_or(vec![])
12+ Ok(transpose(
13+ fs::read_to_string(config_file)
14+ .ok()
15+ .map(|content| get_args_from_str(&content)),
16+ )?
17+ .unwrap_or(vec![]))
1318}
1419
15−fn get_args_from_str<'a>(content: &'a str) -> Vec<OsString> {
16− content
20+fn get_args_from_str<'a>(content: &'a str) -> Result<Vec<OsString>, shell_words::ParseError> {
21+ let args_per_line = content
1722 .split('\n')
1823 .map(|line| line.trim())
1924 .filter(|line| !line.is_empty())
2025 .filter(|line| !line.starts_with("#"))
21− .flat_map(|line| shell_words::split(line).unwrap())
26+ .map(|line| shell_words::split(line))
27+ .collect::<Result<Vec<_>, _>>()?;
28+
29+ Ok(args_per_line
30+ .iter()
31+ .flatten()
2232 .map(|line| line.into())
23− .collect()
33+ .collect())
2434}
2535
2636#[test]
2737fn empty() {
28− let args = get_args_from_str("");
38+ let args = get_args_from_str("").unwrap();
2939 assert!(args.is_empty());
3040}
3141
3242#[test]
3343fn single() {
34− assert_eq!(vec!["--plain"], get_args_from_str("--plain"));
44+ assert_eq!(vec!["--plain"], get_args_from_str("--plain").unwrap());
3545}
3646
3747#[test]
3848fn multiple() {
3949 assert_eq!(
4050 vec!["--plain", "--language=cpp"],
41− get_args_from_str("--plain --language=cpp")
51+ get_args_from_str("--plain --language=cpp").unwrap()
4252 );
4353}
4454
@@ -46,7 +56,7 @@ fn multiple() {
4656fn quotes() {
4757 assert_eq!(
4858 vec!["--theme", "Sublime Snazzy"],
49− get_args_from_str("--theme \"Sublime Snazzy\"")
59+ get_args_from_str("--theme \"Sublime Snazzy\"").unwrap()
5060 );
5161}
5262
@@ -60,7 +70,7 @@ fn multi_line() {
6070 ";
6171 assert_eq!(
6272 vec!["-p", "--style", "numbers,changes", "--color=always"],
63− get_args_from_str(config)
73+ get_args_from_str(config).unwrap()
6474 );
6575}
6676
@@ -78,6 +88,6 @@ fn comments() {
7888 ";
7989 assert_eq!(
8090 vec!["-p", "--style", "numbers,changes", "--color=always"],
81− get_args_from_str(config)
91+ get_args_from_str(config).unwrap()
8292 );
8393}
src/main.rs+1−1
@@ -196,7 +196,7 @@ fn run_controller(config: &Config) -> Result<bool> {
196196/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
197197/// `Some(false)` if any intermediate errors occurred (were printed).
198198fn run() -> Result<bool> {
199− let app = App::new();
199+ let app = App::new()?;
200200
201201 match app.matches.subcommand() {
202202 ("cache", Some(cache_matches)) => {