Skip to content

Commit

Permalink
fix: handling of empty str for license template path (rust-lang#3804)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored and topecongiro committed Sep 24, 2019
1 parent 6b0a447 commit 3bb2661
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ macro_rules! create_config {
fn set_license_template(&mut self) {
if self.was_set().license_template_path() {
let lt_path = self.license_template_path();
match license::load_and_compile_template(&lt_path) {
Ok(re) => self.license_template = Some(re),
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
lt_path, msg),
if lt_path.len() > 0 {
match license::load_and_compile_template(&lt_path) {
Ok(re) => self.license_template = Some(re),
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
lt_path, msg),
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@ mod test {
assert_eq!(s.contains("(unstable)"), true);
}

#[test]
fn test_empty_string_license_template_path() {
let toml = r#"license_template_path = """#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert!(config.license_template.is_none());
}

#[test]
fn test_valid_license_template_path() {
let toml = r#"license_template_path = "tests/license-template/lt.txt""#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert!(config.license_template.is_some());
}

#[test]
fn test_dump_default_config() {
let default_config = format!(
Expand Down
2 changes: 2 additions & 0 deletions tests/config/issue-3802.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
unstable_features = true
license_template_path = ""
2 changes: 2 additions & 0 deletions tests/license-template/lt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// rustfmt-license_template_path: tests/license-template/lt.txt
// Copyright {\d+} The rustfmt developers.
5 changes: 5 additions & 0 deletions tests/source/license-templates/empty_license_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-config: issue-3802.toml

fn main() {
println!("Hello world!");
}
6 changes: 6 additions & 0 deletions tests/source/license-templates/license.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-license_template_path: tests/license-template/lt.txt
// Copyright 2019 The rustfmt developers.

fn main() {
println!("Hello world!");
}
5 changes: 5 additions & 0 deletions tests/target/license-templates/empty_license_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-config: issue-3802.toml

fn main() {
println!("Hello world!");
}
6 changes: 6 additions & 0 deletions tests/target/license-templates/license.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-license_template_path: tests/license-template/lt.txt
// Copyright 2019 The rustfmt developers.

fn main() {
println!("Hello world!");
}

0 comments on commit 3bb2661

Please sign in to comment.