Skip to content

Commit

Permalink
CLOCBear: Add CLOCBear
Browse files Browse the repository at this point in the history
This bear gives statistics about number of lines,
number of comment lines, and total number of lines.

Closes coala#1577
  • Loading branch information
shreyans800755 committed Apr 30, 2017
1 parent 6078e05 commit 49ac973
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ esac
# apt-get commands
export DEBIAN_FRONTEND=noninteractive

deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts"
deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts cloc"
deps_infer="m4 opam"

case $CIRCLE_BUILD_IMAGE in
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ addons:
- opam
- php-codesniffer
- verilator
- cloc

cache:
pip: true
Expand Down
60 changes: 60 additions & 0 deletions bears/general/CLOCBear.py
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)
71 changes: 71 additions & 0 deletions tests/general/CLOCBearTest.py
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))
14 changes: 14 additions & 0 deletions tests/general/cloc_test_files/example1.cpp
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;
}
7 changes: 7 additions & 0 deletions tests/general/cloc_test_files/example2.py
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
14 changes: 14 additions & 0 deletions tests/general/cloc_test_files/example3.cpp
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;
}
2 changes: 2 additions & 0 deletions tests/general/cloc_test_files/example4.txt
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.

0 comments on commit 49ac973

Please sign in to comment.