Skip to content

Commit 8f0b256

Browse files
authored
Merge pull request #6 from matsoftware/issue-n5
Issue #5 - Fix for empty projects or code without modules
2 parents 9634a52 + b5f2562 commit 8f0b256

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

swift_code_metrics/_analyzer.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77

88

99
class Inspector:
10-
def __init__(self, directory, artifacts, tests_default_suffixes, exclude_paths=None):
11-
if exclude_paths is None:
12-
exclude_paths = []
10+
def __init__(self, directory, artifacts, tests_default_suffixes, exclude_paths):
11+
self.exclude_paths = exclude_paths
12+
self.directory = directory
13+
self.artifacts = artifacts
14+
self.tests_default_suffixes = tests_default_suffixes
1315
self.frameworks = []
14-
if directory is not None:
16+
self.report = None
17+
18+
def analyze(self) -> bool:
19+
if self.directory is not None:
1520
# Initialize report
16-
self.__analyze_directory(directory, exclude_paths, tests_default_suffixes)
17-
self.report = self._generate_report()
18-
self._save_report(artifacts)
21+
self.__analyze_directory(self.directory, self.exclude_paths, self.tests_default_suffixes)
22+
if len(self.frameworks) > 0:
23+
self.report = self._generate_report()
24+
self._save_report(self.artifacts)
25+
return True
26+
return False
1927

2028
def filtered_frameworks(self, is_test=False):
2129
return seq(self.frameworks) \

swift_code_metrics/_metrics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ def percentage_of_comments(noc, loc):
114114
:param loc: the number of lines of code
115115
:return: The POC value (double)
116116
"""
117-
return 100 * noc / (noc + loc)
117+
noc_loc = noc + loc
118+
if noc_loc == 0:
119+
return 0
120+
return 100 * noc / noc_loc
118121

119122
# Analysis
120123

swift_code_metrics/scm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from argparse import ArgumentParser
44

5-
from ._helpers import ReportingHelpers
5+
from ._helpers import Log,ReportingHelpers
66
from ._analyzer import Inspector
77
from ._presenter import GraphPresenter
88
from .version import VERSION
@@ -66,6 +66,10 @@ def main():
6666
# Inspects the provided directory
6767
analyzer = Inspector(directory, artifacts, default_tests_paths, exclude)
6868

69+
if not analyzer.analyze():
70+
Log.warn('No valid swift files found in the project')
71+
sys.exit(0)
72+
6973
if not should_generate_graphs:
7074
sys.exit(0)
7175

swift_code_metrics/tests/test_integration.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,32 @@ def test_sample_app(self):
2828
generated_json = IntegrationTest.read_json_file(output_file)
2929
self.assertEqual(generated_json, expected_json)
3030

31-
3231
@staticmethod
3332
def read_json_file(path):
3433
with open(path, 'r') as fp:
3534
return json.load(fp)
3635

3736

37+
class IntegrationUnhappyTest(unittest.TestCase):
38+
39+
def setUp(self):
40+
self.maxDiff = None
41+
sys.argv.clear()
42+
sys.argv.append(os.path.dirname(os.path.realpath(__file__)))
43+
sys.argv.append("--source")
44+
sys.argv.append("any")
45+
sys.argv.append("--artifacts")
46+
sys.argv.append("any")
47+
48+
def tearDown(self):
49+
sys.argv.clear()
50+
51+
def test_sample_app(self):
52+
with self.assertRaises(SystemExit) as cm:
53+
scm.main() # should not throw exception and return 0
54+
55+
self.assertEqual(cm.exception.code, 0)
56+
57+
3858
if __name__ == '__main__':
3959
unittest.main()

swift_code_metrics/tests/test_metrics.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def test_total_dependencies(self):
111111
self.assertEqual(expected_deps,
112112
Metrics.total_dependencies(self.foundation_kit))
113113

114+
def test_poc_valid_loc_noc(self):
115+
self.assertEqual(50, Metrics.percentage_of_comments(loc=2, noc=2))
116+
117+
def test_poc_invalid_loc_noc(self):
118+
self.assertEqual(0, Metrics.percentage_of_comments(loc=0, noc=0))
119+
114120
@property
115121
def __dummy_external_frameworks(self):
116122
return [

0 commit comments

Comments
 (0)