-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement copyright notice detection #4701
Merged
charliermarsh
merged 9 commits into
astral-sh:main
from
Ryang20718:implement-flake8-copyright
Jun 11, 2023
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d4dc6f
Add flake8-copyright to ruff
Ryang20718 d7ca0db
add default for flake8 copyright file size
Ryang20718 3747505
rename to CPY801, remove lazy regex crate
Ryang20718 0bd37ca
shift away from line checking to file checking
Ryang20718 a18b4d8
Merge branch 'main' into implement-flake8-copyright
Ryang20718 3e31eab
add flake8-copyright to ignore
Ryang20718 f6902bc
Merge branch 'main' into implement-flake8-copyright
charliermarsh f91dd7f
Ignore small files; use regex in settings; move tests into snippets
charliermarsh 1676299
Delete snapshots
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(()) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
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; | ||
Ryang20718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Three states are possible: | ||
// 1. Found copyright header | ||
// 2. Missing copyright header | ||
// 3. file length < chars_before_copyright_header | ||
#[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
pub(crate) enum CopyrightHeaderKind { | ||
Missing, | ||
Present, | ||
NotFoundInRange, | ||
} | ||
|
||
#[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( | ||
Ryang20718 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
line: &Line, | ||
settings: &Settings, | ||
current_char_index: u32, | ||
) -> CopyrightHeaderKind { | ||
let copyright_regexp = format!( | ||
"{} {}", | ||
settings.flake8_copyright.copyright_regexp, settings.flake8_copyright.copyright_author | ||
); | ||
|
||
// use default string if we panic | ||
let regex = match Regex::new(copyright_regexp.trim()) { | ||
Ok(regex) => regex, | ||
Err(_) => Regex::new("(?i)Copyright \\(C\\) \\d{4}").unwrap(), | ||
}; | ||
|
||
// flake8 copyright uses maximum allowed chars to be 1024 before copyright | ||
let copyright_file_size: u32 = match settings.flake8_copyright.copyright_min_file_size { | ||
x if x <= 1024 => settings.flake8_copyright.copyright_min_file_size, | ||
_ => 1024, // max is 1024 in flake8 rule | ||
}; | ||
|
||
let out_of_range = current_char_index > copyright_file_size; | ||
let copyright_missing = regex.find(line.as_str()).is_none(); | ||
|
||
if copyright_missing && out_of_range { | ||
// Missing copyright header | ||
return CopyrightHeaderKind::Missing; | ||
} | ||
if !copyright_missing { | ||
// Found copyright header, should stop checking | ||
return CopyrightHeaderKind::Present; | ||
} | ||
// Missing copyright header, but need to keep checking | ||
CopyrightHeaderKind::NotFoundInRange | ||
} |
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 @@ | ||
pub(crate) use copyright_header_absent::{ | ||
copyright_header_absent, CopyrightHeaderKind, HeaderLacksCopyright, | ||
}; | ||
|
||
mod copyright_header_absent; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also do dep injection here and pass regexp as an arg. However, that would essentially put all the logic in physical_lines.rs. Let me know which you would prefer