Skip to content

Commit 3bb2661

Browse files
calebcartwrighttopecongiro
authored andcommitted
fix: handling of empty str for license template path (#3804)
1 parent 6b0a447 commit 3bb2661

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

src/config/config_type.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@ macro_rules! create_config {
293293
fn set_license_template(&mut self) {
294294
if self.was_set().license_template_path() {
295295
let lt_path = self.license_template_path();
296-
match license::load_and_compile_template(&lt_path) {
297-
Ok(re) => self.license_template = Some(re),
298-
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
299-
lt_path, msg),
296+
if lt_path.len() > 0 {
297+
match license::load_and_compile_template(&lt_path) {
298+
Ok(re) => self.license_template = Some(re),
299+
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
300+
lt_path, msg),
301+
}
300302
}
301303
}
302304
}

src/config/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,20 @@ mod test {
467467
assert_eq!(s.contains("(unstable)"), true);
468468
}
469469

470+
#[test]
471+
fn test_empty_string_license_template_path() {
472+
let toml = r#"license_template_path = """#;
473+
let config = Config::from_toml(toml, Path::new("")).unwrap();
474+
assert!(config.license_template.is_none());
475+
}
476+
477+
#[test]
478+
fn test_valid_license_template_path() {
479+
let toml = r#"license_template_path = "tests/license-template/lt.txt""#;
480+
let config = Config::from_toml(toml, Path::new("")).unwrap();
481+
assert!(config.license_template.is_some());
482+
}
483+
470484
#[test]
471485
fn test_dump_default_config() {
472486
let default_config = format!(

tests/config/issue-3802.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
unstable_features = true
2+
license_template_path = ""

tests/license-template/lt.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// rustfmt-license_template_path: tests/license-template/lt.txt
2+
// Copyright {\d+} The rustfmt developers.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-config: issue-3802.toml
2+
3+
fn main() {
4+
println!("Hello world!");
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-license_template_path: tests/license-template/lt.txt
2+
// Copyright 2019 The rustfmt developers.
3+
4+
fn main() {
5+
println!("Hello world!");
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-config: issue-3802.toml
2+
3+
fn main() {
4+
println!("Hello world!");
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-license_template_path: tests/license-template/lt.txt
2+
// Copyright 2019 The rustfmt developers.
3+
4+
fn main() {
5+
println!("Hello world!");
6+
}

0 commit comments

Comments
 (0)