Impeove invalid config_file_path handling

1. Besides opt.config_file_path, also consider MEILI_CONFIG_FILE_PATH in the Err path because they are both user input.
2. Print out the incorrect file path in error message.
3. Add tests
https://github.com/meilisearch/meilisearch/pull/2804#discussion_r991999888
This commit is contained in:
Lawrence Chou 2022-10-12 11:58:28 +08:00
parent 91accc0194
commit 3c3ae3ff98
No known key found for this signature in database
GPG Key ID: 56B65EC9D482438D
3 changed files with 49 additions and 8 deletions

10
Cargo.lock generated
View File

@ -2095,6 +2095,7 @@ dependencies = [
"static-files", "static-files",
"sysinfo", "sysinfo",
"tar", "tar",
"temp-env",
"tempfile", "tempfile",
"thiserror", "thiserror",
"time", "time",
@ -3451,6 +3452,15 @@ dependencies = [
"xattr", "xattr",
] ]
[[package]]
name = "temp-env"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a30d48359f77fbb6af3d7b928cc2d092e1dc90b44f397e979ef08ae15733ed65"
dependencies = [
"once_cell",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.3.0" version = "3.3.0"

View File

@ -89,6 +89,7 @@ manifest-dir-macros = "0.1.16"
maplit = "1.0.2" maplit = "1.0.2"
urlencoding = "2.1.2" urlencoding = "2.1.2"
yaup = "0.2.1" yaup = "0.2.1"
temp-env = "0.3.1"
[features] [features]
default = ["analytics", "meilisearch-lib/default", "mini-dashboard"] default = ["analytics", "meilisearch-lib/default", "mini-dashboard"]

View File

@ -263,10 +263,12 @@ impl Opt {
// Parse the args to get the config_file_path. // Parse the args to get the config_file_path.
let mut opts = Opt::parse(); let mut opts = Opt::parse();
let mut config_read_from = None; let mut config_read_from = None;
let config_file_path = opts let user_specified_config_file_path = opts
.config_file_path .config_file_path
.clone() .clone()
.or_else(|| env::var("MEILI_CONFIG_FILE_PATH").map(PathBuf::from).ok()) .or_else(|| env::var("MEILI_CONFIG_FILE_PATH").map(PathBuf::from).ok());
let config_file_path = user_specified_config_file_path
.clone()
.unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_FILE_PATH)); .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_FILE_PATH));
match std::fs::read(&config_file_path) { match std::fs::read(&config_file_path) {
@ -284,12 +286,17 @@ impl Opt {
opts = Opt::parse(); opts = Opt::parse();
config_read_from = Some(config_file_path); config_read_from = Some(config_file_path);
} }
Err(e) => {
match user_specified_config_file_path {
// If we have an error while reading the file defined by the user. // If we have an error while reading the file defined by the user.
Err(_) if opts.config_file_path.is_some() => anyhow::bail!( Some(path) => anyhow::bail!(
"unable to open or read the {:?} configuration file.", "unable to open or read the {:?} configuration file: {}.",
opts.config_file_path.unwrap().display().to_string() path,
e,
), ),
_ => (), None => (),
}
}
} }
Ok((opts, config_read_from)) Ok((opts, config_read_from))
@ -529,4 +536,27 @@ mod test {
fn test_valid_opt() { fn test_valid_opt() {
assert!(Opt::try_parse_from(Some("")).is_ok()); assert!(Opt::try_parse_from(Some("")).is_ok());
} }
#[test]
fn test_meilli_config_file_path_valid() {
temp_env::with_vars(
vec![("MEILI_CONFIG_FILE_PATH", Some("../config.toml"))], // Relative path in meilisearch_http package
|| {
assert!(Opt::try_build().is_ok());
},
);
}
#[test]
fn test_meilli_config_file_path_invalid() {
temp_env::with_vars(
vec![("MEILI_CONFIG_FILE_PATH", Some("../configgg.toml"))],
|| {
assert_eq!(
Opt::try_build().unwrap_err().to_string(),
"unable to open or read the \"../configgg.toml\" configuration file: No such file or directory (os error 2)."
);
},
);
}
} }