From dedf056a842fc7c5b116f72faac53921a8b1d647 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 19 Sep 2024 02:22:34 -0700 Subject: [PATCH] remove unwrap --- src/compiler.rs | 21 +++++++++++++++++++-- src/error.rs | 7 ++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index d7d6f04638..7728500723 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -84,9 +84,10 @@ impl Compiler { require_literal_separator: false, require_literal_leading_dot: false, }; - let import_paths = glob::glob_with(&import_base_str, glob_options).unwrap(); + let import_paths = glob::glob_with(&import_base_str, glob_options) + .map_err(|error| Error::ImportGlob { error, path: *path })?; for import in import_paths { - let import = import.unwrap(); + let Ok(import) = import else { continue }; if import.is_file() { if current.file_path.contains(&import) { @@ -354,6 +355,22 @@ x: ); } + #[test] + fn invalid_glob_imports() { + let justfile = r#" +import "./subdir/***.just" + "#; + let tmp = temptree! { + justfile: justfile, + }; + let loader = Loader::new(); + let justfile_path = tmp.path().join("justfile"); + let error = Compiler::compile(&loader, &justfile_path).unwrap_err(); + let expected = "error: import path glob: Pattern syntax error"; + let actual = error.color_display(Color::never()).to_string(); + assert!(actual.contains(expected), "Actual: {actual}"); + } + #[test] fn find_module_file() { #[track_caller] diff --git a/src/error.rs b/src/error.rs index cd22311e7b..fc10c56e58 100644 --- a/src/error.rs +++ b/src/error.rs @@ -107,6 +107,10 @@ pub(crate) enum Error<'src> { io_error: io::Error, }, Homedir, + ImportGlob { + error: glob::PatternError, + path: Token<'src>, + }, InitExists { justfile: PathBuf, }, @@ -209,7 +213,7 @@ impl<'src> Error<'src> { Self::Backtick { token, .. } => Some(*token), Self::Compile { compile_error } => Some(compile_error.context()), Self::FunctionCall { function, .. } => Some(function.token), - Self::MissingImportFile { path } => Some(*path), + Self::MissingImportFile { path } | Self::ImportGlob { path, .. } => Some(*path), _ => None, } } @@ -389,6 +393,7 @@ impl<'src> ColorDisplay for Error<'src> { Homedir => { write!(f, "Failed to get homedir")?; } + ImportGlob { error, .. } => write!(f, "import path glob: {error}")?, InitExists { justfile } => { write!(f, "Justfile `{}` already exists", justfile.display())?; }