-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
24 changed files
with
373 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Copyright (C) 2023 author | ||
|
||
import os |
3 changes: 3 additions & 0 deletions
3
crates/ruff/resources/test/fixtures/flake8_copyright/C801_custom_author_fail.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Copyright 2023 (c) rufffff | ||
|
||
import os |
5 changes: 5 additions & 0 deletions
5
crates/ruff/resources/test/fixtures/flake8_copyright/C801_custom_regexp_pass.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# filling | ||
# filling | ||
# filling | ||
|
||
# Copyright 2023 (c) ruff |
3 changes: 3 additions & 0 deletions
3
crates/ruff/resources/test/fixtures/flake8_copyright/C801_default_fail.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
|
||
# Copyright (c) 2023 ruff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! Rules from [flake8-copyright](https://github.com/savoirfairelinux/flake8-copyright). | ||
pub(crate) mod rules; | ||
|
||
pub mod settings; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::registry::Rule; | ||
use crate::test::test_path; | ||
use crate::{assert_messages, settings}; | ||
|
||
#[test] | ||
fn test_default_fail() -> Result<()> { | ||
let diagnostics = test_path( | ||
Path::new("flake8_copyright/C801_default_fail.py"), | ||
&settings::Settings::for_rules(vec![Rule::HeaderLacksCopyright]), | ||
)?; | ||
assert_messages!("test_default_fail", diagnostics); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_default_pass() -> Result<()> { | ||
let diagnostics = test_path( | ||
Path::new("flake8_copyright/C801.py"), | ||
&settings::Settings::for_rules(vec![Rule::HeaderLacksCopyright]), | ||
)?; | ||
assert!(diagnostics.is_empty()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_custom_regex_fail() -> Result<()> { | ||
let diagnostics = test_path( | ||
Path::new("flake8_copyright/C801_custom_author_fail.py"), | ||
&settings::Settings { | ||
flake8_copyright: super::settings::Settings { | ||
copyright_author: "ruff".to_string(), | ||
copyright_regexp: "(?i)Copyright \\d{4} \\(C\\".to_string(), | ||
copyright_min_file_size: 0, | ||
}, | ||
..settings::Settings::for_rules(vec![Rule::HeaderLacksCopyright]) | ||
}, | ||
)?; | ||
assert_messages!("test_custom_regex_fail", diagnostics); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_custom_regex_pass() -> Result<()> { | ||
let diagnostics = test_path( | ||
Path::new("flake8_copyright/C801_custom_regexp_pass.py"), | ||
&settings::Settings { | ||
flake8_copyright: super::settings::Settings { | ||
copyright_author: "ruff".to_string(), | ||
copyright_regexp: "(?i)Copyright \\d{4} \\(C\\)".to_string(), | ||
copyright_min_file_size: 300, | ||
}, | ||
..settings::Settings::for_rules(vec![Rule::HeaderLacksCopyright]) | ||
}, | ||
)?; | ||
assert!(diagnostics.is_empty()); | ||
Ok(()) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
crates/ruff/src/rules/flake8_copyright/rules/copyright_header_absent.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::newlines::Line; | ||
|
||
use crate::settings::Settings; | ||
|
||
use lazy_regex::Regex; | ||
#[violation] | ||
pub struct HeaderLacksCopyright; | ||
|
||
impl Violation for HeaderLacksCopyright { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Copyright notice not present") | ||
} | ||
} | ||
/// ## What it does | ||
/// Checks for Copyright Header to exist within at the top of a file within `copyright_min_file_size chars` | ||
/// format Copyright (C) <year> <author> | ||
/// | ||
/// Error code C801 | ||
pub(crate) fn copyright_header_absent( | ||
line: &Line, | ||
settings: &Settings, | ||
current_char_index: i64, | ||
) -> Option<bool> { | ||
let copyright_regexp = format!( | ||
"{} {}", | ||
settings.flake8_copyright.copyright_regexp, settings.flake8_copyright.copyright_author | ||
); | ||
let regex = Regex::new(copyright_regexp.trim()).unwrap(); | ||
|
||
let out_of_range = | ||
current_char_index > (settings.flake8_copyright.copyright_min_file_size as i64); | ||
let copyright_missing = regex.find(line.as_str()).is_none(); | ||
|
||
if copyright_missing && out_of_range { | ||
// Missing copyright header | ||
return Some(true); | ||
} | ||
if !copyright_missing { | ||
// Found copyright header, should stop checking | ||
return Some(false); | ||
} | ||
// Missing copyright header, but need to keep checking | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub(crate) use copyright_header_absent::{copyright_header_absent, HeaderLacksCopyright}; | ||
|
||
mod copyright_header_absent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// //! Settings for the `flake8-copyright` plugin. | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; | ||
#[derive( | ||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, | ||
)] | ||
#[serde( | ||
deny_unknown_fields, | ||
rename_all = "kebab-case", | ||
rename = "Flake8CopyrightOptions" | ||
)] | ||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | ||
pub struct Options { | ||
#[option( | ||
default = "(?i)Copyright \\(C\\) \\d{4}", | ||
value_type = "str", | ||
example = r#"copyright-regexp = "(?i)Copyright \\(C\\) \\d{4}""# | ||
)] | ||
/// Regexp to check for copyright headers. | ||
/// | ||
/// Default is "Copyright\s+(\(C\)\s+)?\d{4}([-,]\d{4})*\s+%(author)s" | ||
/// E.g # Copyright (c) 2023 ruff | ||
pub copyright_regexp: Option<String>, | ||
#[option( | ||
default = "", | ||
value_type = "str", | ||
example = r#"copyright-author = "ruff""# | ||
)] | ||
/// Author to check for specifically within the header | ||
/// | ||
/// Default is "" | ||
/// E.g # Copyright (c) 2023 ruff | ||
pub copyright_author: Option<String>, | ||
#[option( | ||
default = r#"0"#, | ||
value_type = "int", | ||
example = r#" | ||
# Ensure copyright exists within 1024 characters of header | ||
copyright-min-file-size = 1024 | ||
"# | ||
)] | ||
/// Minimum number of characters in a file before requiring a copyright notice | ||
pub copyright_min_file_size: Option<isize>, | ||
} | ||
|
||
#[derive(Debug, CacheKey)] | ||
pub struct Settings { | ||
pub copyright_regexp: String, | ||
pub copyright_author: String, | ||
pub copyright_min_file_size: isize, | ||
} | ||
|
||
impl Default for Settings { | ||
fn default() -> Self { | ||
Self { | ||
copyright_regexp: String::from("(?i)Copyright \\(C\\) \\d{4}"), | ||
copyright_author: String::new(), | ||
copyright_min_file_size: 0, | ||
} | ||
} | ||
} | ||
|
||
impl From<Options> for Settings { | ||
fn from(options: Options) -> Self { | ||
Self { | ||
copyright_regexp: options.copyright_regexp.unwrap_or_default(), | ||
copyright_author: options.copyright_author.unwrap_or_default(), | ||
copyright_min_file_size: options.copyright_min_file_size.unwrap_or_default(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Settings> for Options { | ||
fn from(settings: Settings) -> Self { | ||
Self { | ||
copyright_regexp: Some(settings.copyright_regexp), | ||
copyright_author: Some(settings.copyright_author), | ||
copyright_min_file_size: Some(settings.copyright_min_file_size), | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ke8_copyright/snapshots/ruff__rules__flake8_copyright__tests__test_custom_regex_fail.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_copyright/mod.rs | ||
--- | ||
C801_custom_author_fail.py:2:1: C801 Copyright notice not present | ||
| | ||
2 | # Copyright 2023 (c) rufffff | ||
3 | | ||
| C801 | ||
4 | import os | ||
| | ||
|
||
|
Oops, something went wrong.