Skip to content

Commit

Permalink
fix: heuristic find header - blank lines after first comments should …
Browse files Browse the repository at this point in the history
…terminate (#112)

Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Jan 3, 2024
1 parent 2a52946 commit 17ca54d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private int findBeginPosition() {

private boolean hasHeader() {
// skip blank lines
while (line != null && "".equals(line.trim())) {
while (line != null && line.trim().isEmpty()) {
line = fileContent.nextLine();
}
// check if there is already a header
Expand All @@ -91,7 +91,7 @@ private boolean hasHeader() {

// skip blank lines before header text
if (headerDefinition.isAllowBlankLines()) {
while (line != null && "".equals(line.trim())) {
while (line != null && line.trim().isEmpty()) {
line = fileContent.nextLine();
}
}
Expand All @@ -102,20 +102,20 @@ private boolean hasHeader() {
// we detected previously a one line comment block that matches the header detection
// it is not a header it is a comment
return false;

} else {
inPlaceHeader.append(line.toLowerCase());
}

String before = headerDefinition.getBeforeEachLine().stripTrailing();
if ("".equals(before) && !headerDefinition.isMultipleLines()) {
if (before.isEmpty() && !headerDefinition.isMultipleLines()) {
before = headerDefinition.getBeforeEachLine();
}

boolean foundEnd = false;
if (headerDefinition.isMultipleLines() && headerDefinition.isLastHeaderLine(line)) {
foundEnd = true;

} else if (line.trim().isEmpty()) {
foundEnd = true;
} else {
while ((line = fileContent.nextLine()) != null && line.startsWith(before)) {
inPlaceHeader.append(line.toLowerCase());
Expand All @@ -130,9 +130,8 @@ private boolean hasHeader() {
if (headerDefinition.isMultipleLines() && headerDefinition.isAllowBlankLines() && !foundEnd) {
do {
line = fileContent.nextLine();
} while (line != null && "".equals(line.trim()));
} while (line != null && line.trim().isEmpty());
fileContent.rewind();

} else if (!headerDefinition.isMultipleLines() && !foundEnd) {
fileContent.rewind();
}
Expand All @@ -143,7 +142,7 @@ private boolean hasHeader() {
// check if the line is the end line
while (line != null
&& !headerDefinition.isLastHeaderLine(line)
&& (headerDefinition.isAllowBlankLines() || !"".equals(line.trim()))
&& (headerDefinition.isAllowBlankLines() || !line.trim().isEmpty())
&& line.startsWith(before)) {
line = fileContent.nextLine();
}
Expand Down Expand Up @@ -175,12 +174,14 @@ private int findEndPosition() {
int end = fileContent.getPosition();
line = fileContent.nextLine();
if (beginPosition == 0) {
while (line != null && "".equals(line.trim())) {
while (line != null && line.trim().isEmpty()) {
end = fileContent.getPosition();
line = fileContent.nextLine();
}
}
if (headerDefinition.getEndLine().endsWith("EOL") && line != null && "".equals(line.trim())) {
if (headerDefinition.getEndLine().endsWith("EOL")
&& line != null
&& line.trim().isEmpty()) {
end = fileContent.getPosition();
}
return end;
Expand Down
1 change: 1 addition & 0 deletions hawkeye-core/src/test/data/issue-110/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.rs.formatted
11 changes: 11 additions & 0 deletions hawkeye-core/src/test/data/issue-110/licenserc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
baseDir = "src/test/data/issue-110"

headerPath = "Apache-2.0.txt"

includes = [
"*.rs",
]

[properties]
inceptionYear = 2023
copyrightOwner = "The CopyrightOwner"
5 changes: 5 additions & 0 deletions hawkeye-core/src/test/data/issue-110/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2022-2023 The Authors. Licensed under Apache-2.0.

//! Load balancer

use macros::define_result;
17 changes: 17 additions & 0 deletions hawkeye-core/src/test/data/issue-110/main.rs.formatted.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The CopyrightOwner
//
// 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.

//! Load balancer

use macros::define_result;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 Korandoru Contributors
*
* 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.
*/

package io.korandoru.hawkeye.core;

import static org.assertj.core.api.Assertions.assertThat;
import io.korandoru.hawkeye.core.config.HawkEyeConfig;
import java.io.File;
import org.junit.jupiter.api.Test;

public class RegressionTest {
@Test
void testIssue110() {
final File file = new File("src/test/data/issue-110/licenserc.toml");
final HawkEyeConfig config = HawkEyeConfig.of(file).dryRun(true).build();
final LicenseFormatter formatter = new LicenseFormatter(config);
formatter.call();

final File expected = new File("src/test/data/issue-110/main.rs.formatted.expected");
final File actual = new File("src/test/data/issue-110/main.rs.formatted");
assertThat(actual).hasSameTextualContentAs(expected);
}
}
7 changes: 4 additions & 3 deletions licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ baseDir = "."
headerPath = "Apache-2.0.txt"

excludes = [
"**/*.txt",
"**/src/test/resources/**/*.toml",
"**/src/test/resources/**/*.xml",
"*.txt",
"hawkeye-core/src/test/resources/**/*.toml",
"hawkeye-core/src/test/resources/**/*.xml",
"hawkeye-core/src/test/data/**",
]

[mapping.SCRIPT_STYLE]
Expand Down

0 comments on commit 17ca54d

Please sign in to comment.