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.
This bear gives statistics about number of lines, number of comment lines, and total number of lines. Closes coala#1577
- Loading branch information
1 parent
6078e05
commit 97b5756
Showing
7 changed files
with
182 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -43,6 +43,7 @@ addons: | |
- opam | ||
- php-codesniffer | ||
- verilator | ||
- cloc | ||
|
||
cache: | ||
pip: true | ||
|
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,52 @@ | ||
import re | ||
|
||
from coalib.bearlib.abstractions.Linter import linter | ||
from coalib.results.Result import Result | ||
from dependency_management.requirements.DistributionRequirement import ( | ||
DistributionRequirement) | ||
|
||
|
||
@linter(executable='cloc', | ||
use_stdout=True, | ||
use_stderr=False) | ||
class CLOCBear: | ||
""" | ||
Summarises file of or number of files in directory structure | ||
with total lines, number comment lines, actual code lines using tool cloc | ||
""" | ||
LANGUAGES = {'All'} | ||
REQUIREMENTS = {DistributionRequirement(apt_get='cloc')} | ||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'[email protected]'} | ||
LICENSE = 'AGPL-3.0' | ||
CAN_DETECT = {'Comment lines','Empty lines'} | ||
|
||
@staticmethod | ||
def create_arguments(filename, file, config_file): | ||
return ('', filename) | ||
|
||
def process_output(self, output, filename, file): | ||
out = output | ||
lines = out.split('\n') | ||
files = (int)(re.search(r'(\d+)', lines[1]).group(1)) | ||
ignored_files = (int)(re.search(r'(\d+)', lines[2]).group(1)) | ||
if(files == ignored_files): | ||
msg = 'No valid files. File should belong to valid programming language.' | ||
yield Result.from_values(origin = self, | ||
message = msg, | ||
file=filename) | ||
else: | ||
regex = r'(^\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)' | ||
for row in lines[8:-2]: | ||
match = re.search(regex, row) | ||
lang = match.group(1) | ||
nfiles = match.group(2) | ||
blank = (int)(match.group(3)) | ||
comment = (int)(match.group(4)) | ||
code = (int)(match.group(5)) | ||
total = blank + comment + code | ||
report = 'Language: {0}\nTotal files: {1}\nTotal lines: {2}\nCode lines: {3}({4:.2f}%)\nComment lines: {5}({6:.2f}%)\nBlank lines: {7}({8:.2f}%)\n'\ | ||
.format(lang, nfiles, total, code, code*100./total, comment, comment*100./total, blank, blank*100./total) | ||
yield Result.from_values(origin = self, | ||
message = report, | ||
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,93 @@ | ||
import unittest | ||
import os | ||
from queue import Queue | ||
|
||
from coalib.settings.Section import Section | ||
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper | ||
from coalib.testing.BearTestHelper import generate_skip_decorator | ||
from coalib.results.Result import Result | ||
|
||
from bears.general.CLOCBear import CLOCBear | ||
|
||
|
||
def get_absolute_test_path(file): | ||
return os.path.join(os.path.dirname(__file__), | ||
'cloc_test_files', file) | ||
|
||
def load_testfile(name): | ||
return open(get_absolute_test_path(name)).readlines() | ||
|
||
@generate_skip_decorator(CLOCBear) | ||
class CLOCBearTest(LocalBearTestHelper): | ||
|
||
def setUp(self): | ||
self.uut = CLOCBear(Section('name'), Queue()) | ||
self.test_files = ['example1.cpp', 'example2.py', 'example3.cpp'] | ||
self.message_format = 'd' | ||
self.expected_results = {self.test_files[0] : {'LANGUAGE': 'C++', 'FILES': 1, 'CODE': 6, 'COMMENTS': 5, 'BLANK': 3}, | ||
self.test_files[1] : {'LANGUAGE': 'Python', 'FILES': 1, 'CODE': 3, 'COMMENTS': 2, 'BLANK': 3}, | ||
self.test_files[2] : {'LANGUAGE': 'C++', 'FILES': 1, 'CODE': 10, 'COMMENTS': 0, 'BLANK': 3} | ||
} | ||
|
||
def build_message(self, filename): | ||
result = self.expected_results[filename] | ||
lang = result['LANGUAGE'] | ||
nfiles = result['FILES'] | ||
code = result['CODE'] | ||
comment = result['COMMENTS'] | ||
blank = result['BLANK'] | ||
total = code + comment + blank | ||
message = "\n".join(["Language: {0}".format(lang), | ||
"Total files: {0}".format(nfiles), | ||
"Total lines: {0}".format(total), | ||
"Code lines: {0}({1:.2f}%)".format(code, code * 100. / total), | ||
"Comment lines: {0}({1:.2f}%)".format(comment, comment * 100. / total), | ||
"Blank lines: {0}({1:.2f}%)".format(blank, blank * 100. / total), | ||
"" | ||
]) | ||
return((str)(message)) | ||
|
||
def test_all(self): | ||
for filename in self.test_files: | ||
file_contents = load_testfile(filename) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('CLOCBear', | ||
self.build_message(filename), | ||
file=get_absolute_test_path(filename))], | ||
filename=get_absolute_test_path(filename)) | ||
|
||
# def test_license_without_copyright(self): | ||
# file_contents = load_testfile('apache_license_without_copyright.py') | ||
# self.check_results( | ||
# self.uut, | ||
# file_contents, | ||
# [], | ||
# filename=get_testfile_path('apache_license_without_copyright.py'), | ||
# settings={'licensecheck_lines': 70, | ||
# 'licensecheck_tail': 0}) | ||
# | ||
# def copyright_without_license(self): | ||
# file_contents = load_testfile('copyright_without_license.py') | ||
# self.check_results( | ||
# self.uut, | ||
# file_contents, | ||
# [Result.from_values('LicenseCheckBear', | ||
# 'No license found.', | ||
# file=get_testfile_path('copyright_without_' | ||
# 'license.py'))], | ||
# filename=get_testfile_path('copyright_without_license.py'), | ||
# settings={'licensecheck_lines': 0, | ||
# 'licensecheck_tail': 0}) # Parse entire file | ||
# | ||
# def no_license(self): | ||
# file_contents = load_testfile('no_license.py') | ||
# self.check_results( | ||
# self.uut, | ||
# file_contents, | ||
# [Result.from_values('LicenseCheckBear', | ||
# 'No license found.', | ||
# file=get_testfile_path('no_license.py'))], | ||
# filename=get_testfile_path('no_license.py'), | ||
# settings={'licensecheck_lines': 10}) |
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,14 @@ | ||
// This comments are for testing purpose. | ||
|
||
/* | ||
Just like any other programmer, | ||
This example file will also try to print only two words | ||
*/ | ||
|
||
#include <cstdio> | ||
|
||
int main | ||
{ | ||
std::cout << "Hello World!" << std::endl; | ||
return 0; | ||
} |
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,8 @@ | ||
# This comments are for testing purpose | ||
# This program will just try to add two static numbers | ||
|
||
first = 5 # First number | ||
second = 10 # Second number | ||
|
||
sum = first + second | ||
|
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,13 @@ | ||
#include <cstdio> | ||
|
||
#define PI 3.1415926 | ||
using namespace std; | ||
|
||
|
||
int main() | ||
{ | ||
const double radii = 1.0; | ||
const double area = PI * radii * radii; | ||
cout << "Area for the circle with radii " << radii << " is " << area << endl; | ||
return 0; | ||
} |