From 17ca54def3bb7d90b8199edbbaaaf56620faf862 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 3 Jan 2024 16:13:19 +0800 Subject: [PATCH] fix: heuristic find header - blank lines after first comments should terminate (#112) Signed-off-by: tison --- .../hawkeye/core/header/HeaderParser.java | 21 +++++------ .../src/test/data/issue-110/.gitignore | 1 + .../src/test/data/issue-110/licenserc.toml | 11 ++++++ hawkeye-core/src/test/data/issue-110/main.rs | 5 +++ .../data/issue-110/main.rs.formatted.expected | 17 +++++++++ .../hawkeye/core/RegressionTest.java | 36 +++++++++++++++++++ licenserc.toml | 7 ++-- 7 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 hawkeye-core/src/test/data/issue-110/.gitignore create mode 100644 hawkeye-core/src/test/data/issue-110/licenserc.toml create mode 100644 hawkeye-core/src/test/data/issue-110/main.rs create mode 100644 hawkeye-core/src/test/data/issue-110/main.rs.formatted.expected create mode 100644 hawkeye-core/src/test/java/io/korandoru/hawkeye/core/RegressionTest.java diff --git a/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/header/HeaderParser.java b/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/header/HeaderParser.java index 3d0dbd0d..b51b0b49 100644 --- a/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/header/HeaderParser.java +++ b/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/header/HeaderParser.java @@ -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 @@ -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(); } } @@ -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()); @@ -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(); } @@ -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(); } @@ -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; diff --git a/hawkeye-core/src/test/data/issue-110/.gitignore b/hawkeye-core/src/test/data/issue-110/.gitignore new file mode 100644 index 00000000..ea7134a8 --- /dev/null +++ b/hawkeye-core/src/test/data/issue-110/.gitignore @@ -0,0 +1 @@ +main.rs.formatted diff --git a/hawkeye-core/src/test/data/issue-110/licenserc.toml b/hawkeye-core/src/test/data/issue-110/licenserc.toml new file mode 100644 index 00000000..6c135005 --- /dev/null +++ b/hawkeye-core/src/test/data/issue-110/licenserc.toml @@ -0,0 +1,11 @@ +baseDir = "src/test/data/issue-110" + +headerPath = "Apache-2.0.txt" + +includes = [ + "*.rs", +] + +[properties] +inceptionYear = 2023 +copyrightOwner = "The CopyrightOwner" diff --git a/hawkeye-core/src/test/data/issue-110/main.rs b/hawkeye-core/src/test/data/issue-110/main.rs new file mode 100644 index 00000000..0a908d60 --- /dev/null +++ b/hawkeye-core/src/test/data/issue-110/main.rs @@ -0,0 +1,5 @@ +// Copyright 2022-2023 The Authors. Licensed under Apache-2.0. + +//! Load balancer + +use macros::define_result; diff --git a/hawkeye-core/src/test/data/issue-110/main.rs.formatted.expected b/hawkeye-core/src/test/data/issue-110/main.rs.formatted.expected new file mode 100644 index 00000000..87d00fff --- /dev/null +++ b/hawkeye-core/src/test/data/issue-110/main.rs.formatted.expected @@ -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; diff --git a/hawkeye-core/src/test/java/io/korandoru/hawkeye/core/RegressionTest.java b/hawkeye-core/src/test/java/io/korandoru/hawkeye/core/RegressionTest.java new file mode 100644 index 00000000..51a7c017 --- /dev/null +++ b/hawkeye-core/src/test/java/io/korandoru/hawkeye/core/RegressionTest.java @@ -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); + } +} diff --git a/licenserc.toml b/licenserc.toml index d8fe857c..873a4661 100644 --- a/licenserc.toml +++ b/licenserc.toml @@ -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]