Skip to content

Commit

Permalink
feat(bin): Receive language as an input instead of guessing it
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Mar 1, 2024
1 parent 38ad55b commit d0ef5c1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 32 deletions.
4 changes: 4 additions & 0 deletions bin/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ pub struct CliArgs {
/// Path where the merged file should be written
#[arg(short, long)]
pub(crate) merge_path: std::path::PathBuf,

/// The language that the files being merged are written in
#[arg(long)]
pub(crate) language: String,
}
35 changes: 4 additions & 31 deletions bin/src/language.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
pub fn get_language_by_file_path(file_path: &std::path::Path) -> Result<model::Language, String> {
file_path
.extension()
.and_then(std::ffi::OsStr::to_str)
.and_then(|extension| match extension {
"java" => Some(model::Language::Java),
_ => None,
})
.ok_or(format!(
"Could not retrieve parsing configuration for file {}",
file_path.display()
))
}

#[cfg(test)]
mod tests {
use crate::language::get_language_by_file_path;

#[test]
fn if_the_file_extension_has_no_parser_available_it_returns_error() {
let file_path = std::path::PathBuf::from("/path/without/extension");
assert!(get_language_by_file_path(&file_path).is_err())
}

#[test]
fn if_the_file_extension_has_a_parser_available_it_returns_a_parser_configuration() {
let file_path = std::path::PathBuf::from("/path/for/java/file/Example.java");
assert_eq!(
get_language_by_file_path(&file_path).unwrap(),
model::Language::Java
)
pub fn get_language_from_name(name: &str) -> Result<model::Language, &'static str> {
match name {
"java" => Ok(model::Language::Java),
_ => Err("Invalid language provided"),
}
}
2 changes: 1 addition & 1 deletion bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
std::process::exit(cli_exit_codes::READING_FILE_ERROR)
});

let language = language::get_language_by_file_path(&args.base_path).unwrap_or_else(|error| {
let language = language::get_language_from_name(&args.language).unwrap_or_else(|error| {
log::error!("Error while guessing language: {}", error);
std::process::exit(cli_exit_codes::GUESS_LANGUAGE_ERROR)
});
Expand Down
2 changes: 2 additions & 0 deletions bin/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn if_there_is_a_conflict_it_returns_valid_exit_code() {
.arg("--left-path=tests/scenarios/smoke_java/left.java")
.arg("--right-path=tests/scenarios/smoke_java/right.java")
.arg("--merge-path=tests/scenarios/smoke_java/merge.java")
.arg("--language=java")
.assert()
.code(bin::SUCCESS_WITH_CONFLICTS);
}
Expand All @@ -20,6 +21,7 @@ fn if_there_is_no_conflict_it_returns_valid_exit_code() {
.arg("--left-path=tests/scenarios/no_conflicts/left.java")
.arg("--right-path=tests/scenarios/no_conflicts/right.java")
.arg("--merge-path=tests/scenarios/no_conflicts/merge.java")
.arg("--language=java")
.assert()
.code(bin::SUCCESS_WITHOUT_CONFLICTS);
}

0 comments on commit d0ef5c1

Please sign in to comment.