def _get_contributors(self, file_path, exts): contributor_stats = collections.defaultdict(int) for ext in exts: # Check the file/path exists. full_file_path = file_path + ext if os.path.isfile(full_file_path) or (full_file_path.endswith("*") and os.path.isdir( full_file_path[:-2])): # Use gitfame to fetch commit stats, captured from STDOUT. output = io.StringIO() with redirect_stdout(output): gitfame.main(args=[ f"--incl={full_file_path}", "-s", "-e", "--log=ERROR", "--format=json" ]) gitfame_commit_stats = json.loads(output.getvalue()) for contributor_stat in gitfame_commit_stats["data"]: contributor = contributor_stat[0] loc = contributor_stat[1] if loc == 0: break contributor_stats[contributor] += loc else: logging.error( f"""(contributors) file path ({full_file_path}) """ """does not exist.""") sys.exit(1) return contributor_stats
def test_main(): """Test command line pipes""" import subprocess from os.path import dirname as dn res = subprocess.Popen((sys.executable, '-c', 'import gitfame; import sys;\ sys.argv = ["", "--silent-progress", "%s"];\ gitfame.main()' % dn(dn(dn(__file__)))), stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0] # actual test: assert ('Total commits' in str(res)) # semi-fake test which gets coverage: _SYS_AOE = sys.argv, sys.stdout, sys.stderr sys.stdout = StringIO() sys.stderr = sys.stdout # sys.argv = ['', '--silent-progress'] # import gitfame.__main__ # NOQA main(['--silent-progress']) try: main(['--bad', 'arg']) except SystemExit: if """usage: gitfame [-h] [""" not in sys.stdout.getvalue(): raise else: raise ValueError("Expected --bad arg to fail") sys.stdout.seek(0) # import logging # logging.basicConfig(level=logging.INFO, stream=sys.stdout) main(['-s', '--sort', 'badSortArg']) # if "--sort argument (badSortArg) unrecognised" \ # not in sys.stdout.getvalue(): # raise ValueError("Expected --sort argument (badSortArg) unrecognised") for params in [['--sort', 'commits'], ['--no-regex'], ['--no-regex', '--incl', 'setup.py,README.rst'], ['--excl', r'.*\.py'], ['-w'], ['-M'], ['-C'], ['-t']]: main(['-s'] + params) # test --manpath tmp = mkdtemp() man = path.join(tmp, "git-fame.1") assert not path.exists(man) try: main(['--manpath', tmp]) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) rmtree(tmp, True) sys.argv, sys.stdout, sys.stderr = _SYS_AOE
def _get_contributors(self, file_paths, exts): contributor_stats = collections.defaultdict(int) for file_path, ext in itertools.product(file_paths, exts): full_file_path = file_path.with_suffix(ext) output = io.StringIO() try: # Use gitfame to fetch commit stats, captured from STDOUT. with redirect_stdout(output): gitfame.main(args=[ f"--incl={full_file_path}", "-s", "-e", "--log=ERROR", "--format=json" ]) except FileNotFoundError: logging.error(f"(contributors) file path ({full_file_path}) " "does not exist.") sys.exit(1) gitfame_commit_stats = json.loads(output.getvalue()) for contributor_stat in gitfame_commit_stats["data"]: contributor = contributor_stat[0] loc = contributor_stat[1] if loc == 0: break contributor_stats[contributor] += loc return contributor_stats
def test_main(): """ Test command line pipes """ import subprocess from docopt import DocoptExit from copy import deepcopy from os.path import dirname as dn res = subprocess.Popen( (sys.executable, '-c', "import gitfame; import sys;" + ' sys.argv = ["", "--silent-progress", "' + dn(dn(dn(__file__))) + '"]; gitfame.main()'), stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0] # actual test: assert ('Total commits' in str(res)) # semi-fake test which gets coverage: try: _SYS = deepcopy(sys.argv) except: pass sys.argv = ['', '--silent-progress'] import gitfame.__main__ # NOQA sys.argv = ['', '--bad', 'arg'] try: main() except DocoptExit as e: if """Usage: gitfame [--help | options] [<gitdir>]""" not in str(e): raise sys.argv = ['', '-s', '--sort', 'badSortArg'] try: main() except Warning as e: if "--sort argument (badSortArg) unrecognised" not in str(e): raise for params in [['--sort', 'commits'], ['--no-regex'], ['--no-regex', '--incl', '.*py'], ['-w'], ['-M'], ['-C'], ['-t']]: sys.argv = ['', '-s'] + params main() try: sys.argv = _SYS except: pass
def test_main(): """Test command line pipes""" import subprocess from os.path import dirname as dn res = subprocess.Popen((sys.executable, '-c', 'import gitfame; import sys;\ sys.argv = ["", "--silent-progress", "%s"];\ gitfame.main()' % dn(dn(dn(__file__)))), stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0] # actual test: assert ('Total commits' in str(res)) # semi-fake test which gets coverage: _SYS_AOE = sys.argv, sys.stdout, sys.stderr sys.stdout = StringIO() sys.stderr = sys.stdout # sys.argv = ['', '--silent-progress'] # import gitfame.__main__ # NOQA main(['--silent-progress']) sys.stdout.seek(0) try: main(['--bad', 'arg']) except SystemExit: res = ' '.join(sys.stdout.getvalue().strip().split()[:2]) if res != "usage: gitfame": raise ValueError(sys.stdout.getvalue()) else: raise ValueError("Expected --bad arg to fail") sys.stdout.seek(0) try: main(['-s', '--sort', 'badSortArg']) except KeyError as e: if "badSortArg" not in str(e): raise ValueError("Expected `--sort=badSortArg` to fail") for params in [['--sort', 'commits'], ['--no-regex'], ['--no-regex', '--incl', 'setup.py,README.rst'], ['--excl', r'.*\.py'], ['--loc', 'ins,del'], ['--cost', 'hour'], ['--cost', 'month'], ['--cost', 'month', '--excl', r'.*\.py'], ['-e'], ['-w'], ['-M'], ['-C'], ['-t']]: main(['-s'] + params) # test --manpath tmp = mkdtemp() man = path.join(tmp, "git-fame.1") assert not path.exists(man) try: main(['--manpath', tmp]) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) rmtree(tmp, True) # test multiple gitdirs main(['.', '.']) sys.argv, sys.stdout, sys.stderr = _SYS_AOE