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 49ac973
Showing
8 changed files
with
170 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,60 @@ | ||
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 = {'Number of comment lines', 'Number of 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 = '\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) | ||
]) | ||
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,71 @@ | ||
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', 'example4.txt'] | ||
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': 2}, | ||
self.test_files[2] : {'LANGUAGE': 'C++', 'FILES': 1, 'CODE': 11, 'COMMENTS': 0, 'BLANK': 3}, | ||
self.test_files[3] : 'No valid files. File should belong to valid programming language.' | ||
} | ||
|
||
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_valid(self): | ||
for filename in self.test_files[:-1]: | ||
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_invalid(self): | ||
filename = self.test_files[3] | ||
file_contents = load_testfile(filename) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('CLOCBear', | ||
(str)(self.expected_results[filename]), | ||
file=get_absolute_test_path(filename))], | ||
filename=get_absolute_test_path(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,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,7 @@ | ||
# 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,14 @@ | ||
#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; | ||
} |
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 is normal text file. And it is not specific to any programming language. | ||
So, this has to detected as invalid file by cloc. |