diff --git a/fmt/src/license/mod.rs b/fmt/src/license/mod.rs index 3e7ddac..5f77cd7 100644 --- a/fmt/src/license/mod.rs +++ b/fmt/src/license/mod.rs @@ -12,7 +12,13 @@ // 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 { @@ -20,14 +26,29 @@ pub struct HeaderSource { } impl HeaderSource { - pub fn from_config(config: &Config) -> Option { - 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 { + 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() } } diff --git a/fmt/src/processor.rs b/fmt/src/processor.rs index c045a47..602b95f 100644 --- a/fmt/src/processor.rs +++ b/fmt/src/processor.rs @@ -17,7 +17,7 @@ use std::{ path::{Path, PathBuf}, }; -use snafu::{ensure, OptionExt, ResultExt}; +use snafu::{ensure, ResultExt}; use tracing::debug; use crate::{ @@ -71,9 +71,7 @@ pub fn check_license_header(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) }; diff --git a/licenserc.toml b/licenserc.toml index 1600501..003f7fe 100644 --- a/licenserc.toml +++ b/licenserc.toml @@ -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", diff --git a/tests/it.py b/tests/it.py new file mode 100755 index 0000000..e8caaef --- /dev/null +++ b/tests/it.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# Copyright 2024 tison +# +# 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) diff --git a/tests/load_header_path/license.txt b/tests/load_header_path/license.txt new file mode 100644 index 0000000..71e7c22 --- /dev/null +++ b/tests/load_header_path/license.txt @@ -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. diff --git a/tests/load_header_path/licenserc.toml b/tests/load_header_path/licenserc.toml new file mode 100644 index 0000000..c8b6c51 --- /dev/null +++ b/tests/load_header_path/licenserc.toml @@ -0,0 +1,20 @@ +# Copyright 2024 Mike Delaney +# +# 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 "