Skip to content

Commit

Permalink
fix: real header path should load (#136)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Mar 29, 2024
1 parent 209a801 commit b0edaf4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
39 changes: 30 additions & 9 deletions fmt/src/license/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,43 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::Config;
use snafu::ResultExt;

use crate::{
config::Config,
error::{InvalidConfigSnafu, LoadConfigSnafu},
Result,
};

#[derive(Debug, Clone)]
pub struct HeaderSource {
pub content: String,
}

impl HeaderSource {
pub fn from_config(config: &Config) -> Option<Self> {
config
.inline_header
.as_ref()
.map(|content| HeaderSource {
content: content.clone(),
})
.or_else(|| config.header_path.as_deref().and_then(bundled_headers))
pub fn from_config(config: &Config) -> Result<Self> {
if let Some(inline_header) = &config.inline_header {
return Ok(HeaderSource {
content: inline_header.clone(),
});
}

if let Some(header_path) = &config.header_path {
if let Some(content) = bundled_headers(header_path) {
return Ok(content);
}

let mut path = config.base_dir.clone();
path.push(header_path);
let content = std::fs::read_to_string(header_path)
.context(LoadConfigSnafu { name: header_path })?;
return Ok(HeaderSource { content });
}

InvalidConfigSnafu {
message: "no header source found in config",
}
.fail()
}
}

Expand Down
6 changes: 2 additions & 4 deletions fmt/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
path::{Path, PathBuf},
};

use snafu::{ensure, OptionExt, ResultExt};
use snafu::{ensure, ResultExt};
use tracing::debug;

use crate::{
Expand Down Expand Up @@ -71,9 +71,7 @@ pub fn check_license_header<C: Callback>(run_config: PathBuf, callback: &mut C)
);

let header_matcher = {
let header_source = HeaderSource::from_config(&config).context(InvalidConfigSnafu {
message: "no header source found in config",
})?;
let header_source = HeaderSource::from_config(&config)?;
HeaderMatcher::new(header_source.content)
};

Expand Down
5 changes: 4 additions & 1 deletion licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ headerPath = "Apache-2.0.txt"
excludes = [
# Plain text files AS IS
"*.txt",
"**/tests/content/**",

# Test files
"fmt/tests/content/**",
"tests/load_header_path/**",

# Generated files
".github/workflows/release.yml",
Expand Down
24 changes: 24 additions & 0 deletions tests/it.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# 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.

from pathlib import Path
import subprocess

basedir = Path(__file__).parent.absolute()
rootdir = basedir.parent

subprocess.run(["cargo", "build", "--bin", "hawkeye"], cwd=rootdir, check=True)
hawkeye = rootdir / "target" / "debug" / "hawkeye"
subprocess.run([hawkeye, "check"], cwd=(basedir / "load_header_path"), check=True)
11 changes: 11 additions & 0 deletions tests/load_header_path/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright ${inceptionYear} ${copyrightOwner}

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 changes: 20 additions & 0 deletions tests/load_header_path/licenserc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Mike Delaney <[email protected]>
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

baseDir = "."
headerPath = "license.txt"

excludes = [ ]

[properties]
inceptionYear = 2024
copyrightOwner = "Mike Delaney <[email protected]>"

0 comments on commit b0edaf4

Please sign in to comment.