Skip to content

Commit

Permalink
remove unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Sep 25, 2024
1 parent 16ff8cc commit 8a265e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub(crate) enum Error<'src> {
io_error: io::Error,
},
Homedir,
ImportGlob {
error: glob::PatternError,
path: Token<'src>,
},
InitExists {
justfile: PathBuf,
},
Expand Down Expand Up @@ -212,7 +216,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,
}
}
Expand Down Expand Up @@ -395,6 +399,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())?;
}
Expand Down

0 comments on commit 8a265e0

Please sign in to comment.