Skip to content

Commit

Permalink
fix: remove file only header should take the last license line (#119)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Mar 24, 2024
1 parent ee523e1 commit badc166
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fmt/src/header/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ pub fn parse_header(
break;
}
}

if line.as_ref().map(|l| l.trim().is_empty()).unwrap_or(true) {
found_end = true;
}
}
found_end
};
Expand Down
14 changes: 14 additions & 0 deletions fmt/tests/content/empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 tison <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

32 changes: 32 additions & 0 deletions fmt/tests/content/two_headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file also contains some code from prometheus project.
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Implementations of `rate`, `increase` and `delta` functions in PromQL.

use std::fmt::Display;
use std::sync::Arc;
43 changes: 43 additions & 0 deletions fmt/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 tison <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::Path;

use hawkeye_fmt::header::{model::default_headers, parser::parse_header};

#[test]
fn test_remove_file_only_header() {
let file = Path::new("tests/content/empty.py");
let defs = default_headers().unwrap();
let def = defs.get("script_style").unwrap().clone();
let keywords = vec!["copyright".to_string()];

let document = parse_header(file, def, &keywords).unwrap();
let end_pos = document.end_pos.unwrap();
let content = document.file_content.content();
assert!(content[end_pos..].trim().is_empty());
}

#[test]
fn test_two_headers_should_only_remove_the_first() {
let file = Path::new("tests/content/two_headers.rs");
let defs = default_headers().unwrap();
let def = defs.get("doubleslash_style").unwrap().clone();
let keywords = vec!["copyright".to_string()];

let document = parse_header(file, def, &keywords).unwrap();
let end_pos = document.end_pos.unwrap();
let content = document.file_content.content();
assert!(content[end_pos..].contains("Copyright 2015 The Prometheus Authors"));
}
1 change: 1 addition & 0 deletions licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ headerPath = "Apache-2.0.txt"

excludes = [
"*.txt",
"**/tests/content/**",
]

[properties]
Expand Down
1 change: 0 additions & 1 deletion release.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

# Copyright 2024 tison <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit badc166

Please sign in to comment.