Skip to content

Commit

Permalink
Merge pull request #1 from cocodrips/0.1.1
Browse files Browse the repository at this point in the history
0.1.1
  • Loading branch information
cocodrips authored Feb 6, 2019
2 parents 99ea062 + 3878abf commit 73886fb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions doccov/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
doccov
"""
__version__ = '0.0.1'
__version__ = '0.1.1'
name = "doc-cov"

from .main import walk, summary
from .main import walk, summary
38 changes: 27 additions & 11 deletions doccov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import enum
import importlib
import inspect
import os
import pkgutil
import pydoc
import sys
import logging

logger = logging.getLogger(__name__)


class Type(enum.Enum):
Expand Down Expand Up @@ -136,7 +140,7 @@ def count_module(object):
for key, value in inspect.getmembers(object, inspect.isclass):
# if __all__ exists, believe it. Otherwise use old heuristic.
if (all is not None or
(inspect.getmodule(value) or object) is object):
(inspect.getmodule(value) or object) is object):
if pydoc.visiblename(key, all, object):
classes.append((key, value))
cdict[key] = cdict[value] = '#' + key
Expand All @@ -153,7 +157,7 @@ def count_module(object):
for key, value in inspect.getmembers(object, inspect.isroutine):
# if __all__ exists, believe it. Otherwise use old heuristic.
if (all is not None or
inspect.isbuiltin(value) or inspect.getmodule(value) is object):
inspect.isbuiltin(value) or inspect.getmodule(value) is object):
if pydoc.visiblename(key, all, object):
funcs.append((key, value))
fdict[key] = '#-' + key
Expand Down Expand Up @@ -184,19 +188,28 @@ def walk(root_path):
[Coverage], Coverage: Return all module coverage and summary.
"""

sys.path.insert(0, root_path)
package_name = os.path.basename(os.path.normpath(root_path))
scriptdir = os.path.abspath(os.path.join(root_path, '..'))

if scriptdir in sys.path:
sys.path.remove(scriptdir)
sys.path.insert(0, scriptdir)

packages = pkgutil.walk_packages([root_path])

coverages = []
summary = Coverage()
for importer, modname, ispkg in packages:
spec = pkgutil._get_spec(importer, modname)
try:
object = importlib._bootstrap._load(spec)
counter = count_module(object)

object = importlib._bootstrap._load(spec)
counter = count_module(object)

coverages.append(counter)
summary += counter
coverages.append(counter)
summary += counter
except ImportError as e:
logger.error(f"Failed to import {modname}: {e}")
continue

summary.name = 'coverage'
return coverages, summary
Expand Down Expand Up @@ -230,11 +243,14 @@ def entry_point():
help="[str,csv]")
parser.add_argument("--all", dest='all', action='store_true', default=False,
help="Print all module coverage")
parser.add_argument("-m", "--module", dest='module', action='store_true', default=False,
parser.add_argument("-m", "--module", dest='module', action='store_true',
default=False,
help="Print docstring coverage of modules.")
parser.add_argument("-f", "--function", dest='function', action='store_true', default=False,
parser.add_argument("-f", "--function", dest='function',
action='store_true', default=False,
help="Print docstring coverage of public functions.")
parser.add_argument("-c", "--class", dest='klass', action='store_true', default=False,
parser.add_argument("-c", "--class", dest='klass', action='store_true',
default=False,
help="Print docstring coverage of classes.")

args = parser.parse_args()
Expand Down
4 changes: 2 additions & 2 deletions tests/sample_project/module_fulldoc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Full document module."""
import package_A

from sample_project import package_A
import logging # unused

class FullDoc():
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/sample_project/package_B/module_shortdoc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from package_A import module_fulldoc
from sample_project import package_A


class ShortDoc():
Expand Down
23 changes: 2 additions & 21 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,6 @@ def nodoc_func():
pass


def test_package_A():
_, coverage = walk('tests/sample_project/package_A')
assert coverage.counters[Type.FUNCTION.name].all == 1
assert coverage.counters[Type.FUNCTION.name].true == 1
assert coverage.counters[Type.MODULE.name].all == 1
assert coverage.counters[Type.MODULE.name].true == 1
assert coverage.counters[Type.CLASS.name].all == 0
assert coverage.counters[Type.CLASS.name].true == 0


def test_package_B():
_, coverage = walk('tests/sample_project/package_B')
assert coverage.counters[Type.FUNCTION.name].all == 3
assert coverage.counters[Type.FUNCTION.name].true == 1
assert coverage.counters[Type.MODULE.name].all == 3
assert coverage.counters[Type.MODULE.name].true == 0
assert coverage.counters[Type.CLASS.name].all == 1
assert coverage.counters[Type.CLASS.name].true == 1


def test_sample_project():
_, coverage = walk('tests/sample_project')
assert coverage.counters[Type.FUNCTION.name].all == 5
Expand All @@ -70,4 +50,5 @@ def test_output_all_csv():


def test_output_str():
summary('tests/sample_project', 'str', [Type.FUNCTION, Type.MODULE, Type.CLASS], False)
summary('tests/sample_project', 'str',
[Type.FUNCTION, Type.MODULE, Type.CLASS], False)

0 comments on commit 73886fb

Please sign in to comment.