Skip to content

Issue #5 - Fix for empty projects or code without modules #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions swift_code_metrics/_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@


class Inspector:
def __init__(self, directory, artifacts, tests_default_suffixes, exclude_paths=None):
if exclude_paths is None:
exclude_paths = []
def __init__(self, directory, artifacts, tests_default_suffixes, exclude_paths):
self.exclude_paths = exclude_paths
self.directory = directory
self.artifacts = artifacts
self.tests_default_suffixes = tests_default_suffixes
self.frameworks = []
if directory is not None:
self.report = None

def analyze(self) -> bool:
if self.directory is not None:
# Initialize report
self.__analyze_directory(directory, exclude_paths, tests_default_suffixes)
self.report = self._generate_report()
self._save_report(artifacts)
self.__analyze_directory(self.directory, self.exclude_paths, self.tests_default_suffixes)
if len(self.frameworks) > 0:
self.report = self._generate_report()
self._save_report(self.artifacts)
return True
return False

def filtered_frameworks(self, is_test=False):
return seq(self.frameworks) \
Expand Down
5 changes: 4 additions & 1 deletion swift_code_metrics/_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ def percentage_of_comments(noc, loc):
:param loc: the number of lines of code
:return: The POC value (double)
"""
return 100 * noc / (noc + loc)
noc_loc = noc + loc
if noc_loc == 0:
return 0
return 100 * noc / noc_loc

# Analysis

Expand Down
6 changes: 5 additions & 1 deletion swift_code_metrics/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from argparse import ArgumentParser

from ._helpers import ReportingHelpers
from ._helpers import Log,ReportingHelpers
from ._analyzer import Inspector
from ._presenter import GraphPresenter
from .version import VERSION
Expand Down Expand Up @@ -66,6 +66,10 @@ def main():
# Inspects the provided directory
analyzer = Inspector(directory, artifacts, default_tests_paths, exclude)

if not analyzer.analyze():
Log.warn('No valid swift files found in the project')
sys.exit(0)

if not should_generate_graphs:
sys.exit(0)

Expand Down
22 changes: 21 additions & 1 deletion swift_code_metrics/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,32 @@ def test_sample_app(self):
generated_json = IntegrationTest.read_json_file(output_file)
self.assertEqual(generated_json, expected_json)


@staticmethod
def read_json_file(path):
with open(path, 'r') as fp:
return json.load(fp)


class IntegrationUnhappyTest(unittest.TestCase):

def setUp(self):
self.maxDiff = None
sys.argv.clear()
sys.argv.append(os.path.dirname(os.path.realpath(__file__)))
sys.argv.append("--source")
sys.argv.append("any")
sys.argv.append("--artifacts")
sys.argv.append("any")

def tearDown(self):
sys.argv.clear()

def test_sample_app(self):
with self.assertRaises(SystemExit) as cm:
scm.main() # should not throw exception and return 0

self.assertEqual(cm.exception.code, 0)


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions swift_code_metrics/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def test_total_dependencies(self):
self.assertEqual(expected_deps,
Metrics.total_dependencies(self.foundation_kit))

def test_poc_valid_loc_noc(self):
self.assertEqual(50, Metrics.percentage_of_comments(loc=2, noc=2))

def test_poc_invalid_loc_noc(self):
self.assertEqual(0, Metrics.percentage_of_comments(loc=0, noc=0))

@property
def __dummy_external_frameworks(self):
return [
Expand Down