Skip to content

Commit

Permalink
bears/general: Add LicenseHeaderBear
Browse files Browse the repository at this point in the history
This adds a Copyright(LicenseHeader) notice check bear.
User can specify author if required.

Closes coala#2442
  • Loading branch information
Mohit17067 authored and gitmate-bot committed Jun 15, 2018
1 parent c77ee65 commit a66a8b8
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
36 changes: 36 additions & 0 deletions bears/general/LicenseHeaderBear.py
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)
62 changes: 62 additions & 0 deletions tests/general/LicenseHeaderBearTest.py
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 tests/general/licenseheader_test_files/CopyrightWithoutAuthor.java
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.
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'.
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.
11 changes: 11 additions & 0 deletions tests/general/licenseheader_test_files/no_copyright.py
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))

0 comments on commit a66a8b8

Please sign in to comment.