def cloneable(tempdir_factory): repo_path = tempdir_factory.get() with cwd(repo_path): subprocess.check_call(('git', 'init', '.')) subprocess.check_call(('git', 'commit', '-m', 'foo', '--allow-empty')) yield repo_path
def cloneable_with_commits(cloneable): commits = [] def append_commit(): output = cmd_output("git", "show", COMMIT_FORMAT) sha, date = output.splitlines()[:2] commits.append(Commit(sha, int(date))) def make_commit(filename, contents): # Make the graph tests more deterministic # import time; time.sleep(2) with io.open(filename, "w") as file_obj: file_obj.write(contents) subprocess.check_call(["git", "add", filename]) subprocess.check_call(["git", "commit", "-m", "Add {0}".format(filename)]) append_commit() with cwd(cloneable): # Append a commit for the inital commit append_commit() make_commit("bar.py", "") make_commit("baz.py", "") make_commit("test.py", "import foo\nimport bar\n") make_commit("foo.tmpl", "#import foo\n#import bar\n") yield auto_namedtuple(path=cloneable, commits=commits)
def cloneable_with_commits(cloneable): commits = [] def append_commit(): output = cmd_output('git', 'show', COMMIT_FORMAT) sha, date = output.splitlines()[:2] commits.append(Commit(sha, int(date))) def make_commit(filename, contents): # Make the graph tests more deterministic # import time; time.sleep(2) with io.open(filename, 'w') as file_obj: file_obj.write(contents) subprocess.check_call(('git', 'add', filename)) subprocess.check_call(( 'git', 'commit', '-m', 'Add {0}'.format(filename), )) append_commit() with cwd(cloneable): # Append a commit for the inital commit append_commit() make_commit('bar.py', '') make_commit('baz.py', '') make_commit('test.py', 'import foo\nimport bar\n') make_commit('foo.tmpl', '#import foo\n#import bar\n') yield auto_namedtuple(path=cloneable, commits=commits)
def cloneable(tmpdir_factory): repo_path = tmpdir_factory.get() with cwd(repo_path): subprocess.check_call(['git', 'init', '.']) subprocess.check_call(['git', 'commit', '-m', 'foo', '--allow-empty']) yield repo_path
def hg_cloneable_with_commits(hg_cloneable): commits = [] def append_commit(): output = cmd_output( 'hg', 'log', '--template={node}\n{word(0, date, \".\")}\n', '--rev', '.', ) sha, date = output.splitlines()[:2] commits.append(Commit(sha, int(date))) def make_commit(filename, contents): with open(filename, 'w') as file_obj: file_obj.write(contents) subprocess.check_call(('hg', 'add', filename)) subprocess.check_call(('hg', 'ci', '-m', f'Add {filename}')) append_commit() with cwd(hg_cloneable): append_commit() make_commit('bar.py', '') make_commit('baz.py', '') make_commit('test.py', 'import foo\nimport bar\n') make_commit('foo.tmpl', '#import foo\n#import bar\n') yield auto_namedtuple(path=hg_cloneable, commits=commits)
def cloneable_with_commits(cloneable): commits = [] def append_commit(): output = cmd_output('git', 'show', COMMIT_FORMAT) sha, date = output.splitlines()[:2] commits.append(Commit(sha, int(date))) def make_commit(filename, contents): # Make the graph tests more deterministic # import time; time.sleep(2) with io.open(filename, 'w') as file_obj: file_obj.write(contents) subprocess.check_call(['git', 'add', filename]) subprocess.check_call([ 'git', 'commit', '-m', 'Add {0}'.format(filename), ]) append_commit() with cwd(cloneable): # Append a commit for the inital commit append_commit() make_commit('bar.py', '') make_commit('baz.py', '') make_commit('test.py', 'import foo\nimport bar\n') make_commit('foo.tmpl', '#import foo\n#import bar\n') yield auto_namedtuple(path=cloneable, commits=commits)
def test_regression_for_issue_10(sandbox, cloneable): # Create a commit, then create another commit at a previous time with cwd(cloneable): cmd_output( 'git', 'commit', '--allow-empty', '-m', 'c1', env=dict( os.environ, GIT_COMMITTER_DATE='Wed, Feb 16 14:00 2011 +0100', ), ) cmd_output( 'git', 'commit', '--allow-empty', '-m', 'c2', env=dict( os.environ, GIT_COMMITTER_DATE='Tue, Feb 15 14:00 2011 +0100', ), ) cfg = sandbox.gen_config(repo=cloneable) main(('-C', cfg)) data_count_before = get_metric_data_count(sandbox) # Used to raise IntegrityError main(('-C', cfg)) data_count_after = get_metric_data_count(sandbox) assert data_count_before == data_count_after
def cloneable(tmpdir_factory): repo_path = tmpdir_factory.get() with cwd(repo_path): subprocess.check_call(["git", "init", "."]) subprocess.check_call(["git", "commit", "-m", "foo", "--allow-empty"]) yield repo_path
def test_generate_new_data_created(sandbox, cloneable): main([cloneable, sandbox.db_path]) before_data_count = get_metric_data_count(sandbox) # Add some commits with cwd(cloneable): cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') main([cloneable, sandbox.db_path]) after_data_count = get_metric_data_count(sandbox) assert after_data_count > before_data_count
def test_generate_integration_config_file(sandbox, cloneable, tempdir_factory): tmpdir = tempdir_factory.get() config_filename = os.path.join(tmpdir, 'generate_config.yaml') with io.open(config_filename, 'w') as config_file: yaml.dump( {'repo': cloneable, 'database': sandbox.db_path}, stream=config_file, ) with cwd(tmpdir): main([])
def test_moves_handled_properly(sandbox, cloneable): with cwd(cloneable): with io.open('f', 'w') as f: f.write('foo\nbar\nbaz\n') cmd_output('git', 'add', 'f') cmd_output('git', 'commit', '-m', 'add f') cmd_output('git', 'mv', 'f', 'g') cmd_output('git', 'commit', '-m', 'move f to g') # Used to raise AssertionError assert main((cloneable, sandbox.db_path)) is None
def test_moves_handled_properly(sandbox, cloneable): with cwd(cloneable): with io.open('f', 'w') as f: f.write('foo\nbar\nbaz\n') cmd_output('git', 'add', 'f') cmd_output('git', 'commit', '-m', 'add f') cmd_output('git', 'mv', 'f', 'g') cmd_output('git', 'commit', '-m', 'move f to g') # Used to raise AssertionError assert not main(('-C', sandbox.gen_config(repo=cloneable)))
def test_create_metric_config_if_not_exists_existing(tmpdir): with cwd(tmpdir.strpath): with io.open( 'metric_config.yaml', 'w', ) as metric_config_file: # pragma: no cover (PY26 derps on `with`) metric_config_file.write('Groups: []\nColorOverrides: []\n') create_metric_config_if_not_exists() after_contents = io.open('metric_config.yaml').read() assert after_contents == 'Groups: []\nColorOverrides: []\n'
def hg_cloneable(tempdir_factory): repo_path = tempdir_factory.get() with cwd(repo_path): subprocess.check_call(('hg', 'init', '.')) # hg doesn't allow empty commits so we create an empty file and # commit that with open('readme.md', 'w') as file_obj: file_obj.write('') subprocess.check_call(('hg', 'add', 'readme.md')) subprocess.check_call(('hg', 'commit', '-m', 'add testing branch')) yield repo_path
def test_generate_new_data_created(sandbox, cloneable_with_commits): cfg = sandbox.gen_config(repo=cloneable_with_commits.path) main(('-C', cfg)) before_data_count = get_metric_data_count(sandbox) # Add some commits with cwd(cloneable_with_commits.path): with open('new_file.py', 'w') as f: f.write('# test\n') cmd_output('git', 'add', 'new_file.py') cmd_output('git', 'commit', '-m', 'bar') main(('-C', cfg)) after_data_count = get_metric_data_count(sandbox) assert after_data_count > before_data_count
def test_internal_zero_populated(sandbox, cloneable): with cwd(cloneable): with io.open('f.py', 'w') as f: f.write('# hello world\n') cmd_output('git', 'add', 'f.py') cmd_output('git', 'commit', '-m', 'add f') cmd_output('git', 'rm', 'f.py') cmd_output('git', 'commit', '-m', 'remove f') cmd_output('git', 'revert', 'HEAD', '--no-edit') assert not main(('-C', sandbox.gen_config(repo=cloneable))) with sandbox.db_logic() as db_logic: query = ('SELECT running_value\n' 'FROM metric_data\n' 'INNER JOIN metric_names ON\n' ' metric_data.metric_id == metric_names.id\n' 'WHERE name = "TotalLinesOfCode_python"\n') vals = [x for x, in db_logic._fetch_all(query)] assert vals == [1, 0, 1]
def test_get_options_from_config_create_config(tempdir_factory): tmpdir = tempdir_factory.get() with cwd(tmpdir): ret = get_options_from_config([ '--create-config', '.', 'database.db', ]) assert os.path.exists('generate_config.yaml') assert yaml.load(io.open('generate_config.yaml').read()) == { 'repo': '.', 'database': 'database.db', } assert ret == GenerateOptions( skip_default_metrics=False, metric_package_names=[], repo='.', database='database.db', )
def test_internal_zero_populated(sandbox, cloneable): with cwd(cloneable): with io.open('f.py', 'w') as f: f.write('# hello world\n') cmd_output('git', 'add', 'f.py') cmd_output('git', 'commit', '-m', 'add f') cmd_output('git', 'rm', 'f.py') cmd_output('git', 'commit', '-m', 'remove f') cmd_output('git', 'revert', 'HEAD', '--no-edit') assert not main(('-C', sandbox.gen_config(repo=cloneable))) with sandbox.db() as db: query = ( 'SELECT running_value\n' 'FROM metric_data\n' 'INNER JOIN metric_names ON\n' ' metric_data.metric_id == metric_names.id\n' 'WHERE name = "TotalLinesOfCode_python"\n' ) vals = [x for x, in db.execute(query).fetchall()] assert vals == [1, 0, 1]
def test_regression_for_issue_10(sandbox, cloneable): # Create a commit, then create another commit at a previous time with cwd(cloneable): cmd_output( 'git', 'commit', '--allow-empty', '-m', 'c1', env=dict( os.environ, GIT_COMMITTER_DATE='Wed, Feb 16 14:00 2011 +0100', ), ) cmd_output( 'git', 'commit', '--allow-empty', '-m', 'c2', env=dict( os.environ, GIT_COMMITTER_DATE='Tue, Feb 15 14:00 2011 +0100', ), ) main([cloneable, sandbox.db_path]) data_count_before = get_metric_data_count(sandbox) # Used to raise IntegrityError main([cloneable, sandbox.db_path]) data_count_after = get_metric_data_count(sandbox) assert data_count_before == data_count_after
def test_create_metric_config_if_not_exists_not_existing(tmpdir): with cwd(tmpdir.strpath): create_metric_config_if_not_exists() assert os.path.exists('metric_config.yaml')