Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip leading bom if exist #166

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions fmt/src/document/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;
use gix::date::time::format;
use gix::date::time::CustomFormat;

use crate::config::Mapping;
use crate::document::Document;
Expand Down Expand Up @@ -79,15 +79,14 @@ impl DocumentFactory {
properties.insert("hawkeye.core.filename".to_string(), filename);

if let Some(attrs) = self.git_file_attrs.get(filepath) {
const YEAR_FORMAT: CustomFormat = CustomFormat::new("%Y");
properties.insert(
"hawkeye.git.fileCreatedYear".to_string(),
// TODO(tisonkun): hack until git-date provide external constructor for
// CustomFormat.
attrs.created_time.format(format::SHORT)[0..4].to_string(),
attrs.created_time.format(YEAR_FORMAT),
);
properties.insert(
"hawkeye.git.fileModifiedYear".to_string(),
attrs.modified_time.format(format::SHORT)[0..4].to_string(),
attrs.modified_time.format(YEAR_FORMAT),
);
}

Expand Down
21 changes: 21 additions & 0 deletions fmt/src/header/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ fn find_first_position(
file_content: &mut FileContent,
header_def: &HeaderDef,
) -> usize {
const UTF8_BOM: [u8; 3] = [0xEF, 0xBB, 0xBF];

let mut begin_pos = 0;

if let Some(l) = line.as_ref() {
// skip UTF-8 BOM if exists
if l.as_bytes().starts_with(&UTF8_BOM) {
log::debug!("Detected UTF-8 BOM for {}; skip", file_content);
begin_pos = 3;
file_content.reset_to(3);
}
}

if header_def.skip_line_pattern.is_some() {
// the format expect to find lines to be skipped
while line
Expand Down Expand Up @@ -104,8 +116,17 @@ fn find_first_position(
begin_pos = 0;
file_content.reset();
*line = file_content.next_line();

// recheck for UTF-8 BOM
if let Some(l) = line.as_ref() {
if l.as_bytes().starts_with(&UTF8_BOM) {
begin_pos = 3;
file_content.reset_to(3);
}
}
}
}

begin_pos
}

Expand Down
1 change: 1 addition & 0 deletions licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ excludes = [
"fmt/tests/content/**",
"tests/load_header_path/**",
"tests/regression_blank_line/**",
"tests/bom_issue/**",

# Generated files
".github/workflows/release.yml",
Expand Down
11 changes: 11 additions & 0 deletions tests/bom_issue/headerless_bom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Some.Fake.Namespace;

public class SomeFakeClass
{
public void SomeFakeMethod()
{
Console.WriteLine("Hello, World!");
}
}
15 changes: 15 additions & 0 deletions tests/bom_issue/headerless_bom.cs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -------------------------------------------------------
// Static Copyright
// -------------------------------------------------------

using System;

namespace Some.Fake.Namespace;

public class SomeFakeClass
{
public void SomeFakeMethod()
{
Console.WriteLine("Hello, World!");
}
}
1 change: 1 addition & 0 deletions tests/bom_issue/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Static Copyright
11 changes: 11 additions & 0 deletions tests/bom_issue/licenserc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
baseDir = "."
headerPath = "license.txt"

excludes = ["*.toml", "*.expected"]

additionalHeaders = [
"style.toml"
]

[mapping.HASH_SOURCE_STYLE]
extensions = ["cs"]
9 changes: 9 additions & 0 deletions tests/bom_issue/style.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[HASH_SOURCE_STYLE]
firstLine = "// -------------------------------------------------------"
endLine = "// -------------------------------------------------------\n"
skipLinePattern = "^#!.*$"
allowBlankLines = false
multipleLines = false
beforeEachLine = "// "
firstLineDetectionPattern = "//\\s+\\-+"
lastLineDetectionPattern = "//\\s+\\-+"
3 changes: 3 additions & 0 deletions tests/it.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ def diff_files(file1, file2):

subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=(basedir / "regression_blank_line"), check=True)
diff_files(basedir / "regression_blank_line" / "main.rs.expected", basedir / "regression_blank_line" / "main.rs.formatted")

subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=(basedir / "bom_issue"), check=True)
diff_files(basedir / "bom_issue" / "headerless_bom.cs.expected", basedir / "bom_issue" / "headerless_bom.cs.formatted")