forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bears/general: Add LicenseHeaderBear
This adds a Copyright(LicenseHeader) notice check bear. User can specify author if required. Closes coala#2442
- Loading branch information
1 parent
c77ee65
commit a66a8b8
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
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,36 @@ | ||
import re | ||
|
||
from coalib.bears.LocalBear import LocalBear | ||
from coalib.results.Result import Result | ||
|
||
|
||
class LicenseHeaderBear(LocalBear): | ||
""" | ||
Checks for copyright notice in a file. | ||
""" | ||
LANGUAGES = {'All'} | ||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'[email protected]'} | ||
LICENSE = 'AGPL-3.0' | ||
CAN_DETECT = {'License'} | ||
|
||
def run(self, filename, file, | ||
author_name: str = ''): | ||
""" | ||
:param author: pass the name of the author | ||
""" | ||
copyright_regexp = \ | ||
r'Copyright\s+(\(C\)\s+)?\d{4}([-,]\d{4})*\s+%(author)s' | ||
re_copyright = re.compile(copyright_regexp % | ||
{'author': author_name}, re.IGNORECASE) | ||
if not(re_copyright.search(''.join(file))): | ||
message = 'Copyright notice not present.' | ||
re_copyright = re.compile(copyright_regexp % | ||
{'author': ''}, re.IGNORECASE) | ||
if author_name and re_copyright.search(''.join(file)): | ||
yield Result.from_values(self, | ||
'Copyright notice ' | ||
'with different/no author present.', | ||
file=filename) | ||
else: | ||
yield Result.from_values(self, message, file=filename) |
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,62 @@ | ||
import os | ||
from queue import Queue | ||
|
||
from bears.general.LicenseHeaderBear import LicenseHeaderBear | ||
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper | ||
from coalib.results.Result import Result | ||
from coalib.settings.Section import Section | ||
from coalib.settings.Setting import Setting | ||
|
||
|
||
def get_testfile_path(name): | ||
return os.path.join(os.path.dirname(__file__), | ||
'licenseheader_test_files', | ||
name) | ||
|
||
|
||
def load_testfile(name): | ||
with open(get_testfile_path(name)) as f: | ||
output = f.readlines() | ||
return output | ||
|
||
|
||
class LicenseHeaderBearTest(LocalBearTestHelper): | ||
|
||
def setUp(self): | ||
self.section = Section('name') | ||
self.uut = LicenseHeaderBear(self.section, Queue()) | ||
|
||
def test_copyright_without_author(self): | ||
file_contents = load_testfile('CopyrightWithoutAuthor.java') | ||
self.check_validity(self.uut, file_contents) | ||
|
||
def test_copyright_with_given_author(self): | ||
file_contents = load_testfile('copyright_with_given_author.txt') | ||
self.section.append(Setting('author_name', 'The coala developers')) | ||
self.check_validity( | ||
self.uut, | ||
file_contents) | ||
|
||
def test_copyright_with_different_author(self): | ||
file_contents = load_testfile('copyright_with_different_author.txt') | ||
self.section.append(Setting('author_name', 'The coala developers')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('LicenseHeaderBear', | ||
'Copyright notice with different/no author ' | ||
'present.', | ||
file=get_testfile_path('copyright_with_diff' | ||
'erent_author.txt'))], | ||
filename=get_testfile_path('copyright_with_' | ||
'different_author.txt')) | ||
|
||
def test_no_copyright(self): | ||
file_contents = load_testfile('no_copyright.py') | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('LicenseHeaderBear', | ||
'Copyright notice not present.', | ||
file=get_testfile_path('no_copyright.py'))], | ||
filename=get_testfile_path('no_copyright.py')) |
10 changes: 10 additions & 0 deletions
10
tests/general/licenseheader_test_files/CopyrightWithoutAuthor.java
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,10 @@ | ||
//Copyright (C) 2014 - A copyright notice | ||
|
||
public class CopyrightWithoutAuthor { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
System.out.println("Hello World"); | ||
} | ||
|
||
} |
Empty file.
2 changes: 2 additions & 0 deletions
2
tests/general/licenseheader_test_files/copyright_with_different_author.txt
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,2 @@ | ||
This file contains copyright notice but without the specified author. | ||
Copyright 2016 abc - The specified author was 'The coala developers'. |
2 changes: 2 additions & 0 deletions
2
tests/general/licenseheader_test_files/copyright_with_given_author.txt
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,2 @@ | ||
This file contains copyright notice along with the author name. | ||
Copyright 2018 The coala developers and hence this file should not give any output. |
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,11 @@ | ||
# This file does not contain any copyright notice | ||
|
||
|
||
def factorial(n): | ||
if n == 0: | ||
return 1 | ||
else: | ||
return n*factorial(n-1) | ||
|
||
|
||
print(factorial(4)) |