def test_git_repo(git_repo): paths = [ # Both full ... os.path.join(git_repo, "foo"), # ... and relative paths work. "bar", # So do paths in subdirectories. os.path.join(git_repo, "subdir/baz") ] runner = GitRunner(git_repo) branch = current_branch(runner) tracer = VCSTracer() with chpwd(git_repo): dists = list( tracer.identify_distributions(paths + [COMMON_SYSTEM_PATH])) assert_distributions(dists, expected_length=1, expected_unknown={COMMON_SYSTEM_PATH}, expected_subset={ "name": "git", "packages": [{ "files": [op.relpath(p) for p in paths], "path": git_repo, "branch": branch }] }) assert dists[0][0].packages[0].hexsha assert dists[0][0].packages[0].root_hexsha hexshas, _ = runner(["git", "rev-list", branch]) root_hexsha = hexshas.strip('\n').split('\n')[-1] repo = dists[0][0].packages[0] assert repo.root_hexsha == root_hexsha assert repo._diff_cmp_id == (repo.root_hexsha, ) assert repo.commit == repo.hexsha # Above we identify a subdirectory file, but we should not # identify the subdirectory itself because in principle Git is # not tracking directories. subdir = os.path.join(git_repo, "subdir") assert not list(tracer.identify_distributions([subdir]))
def test_git_repo_empty(git_repo_empty): tracer = VCSTracer() # Should not crash when given path to empty repo. assert_distributions( tracer.identify_distributions([git_repo_empty]), expected_length=1, expected_unknown=set(), expected_subset={ "name": "git", "packages": [{ "path": git_repo_empty, "branch": "master", # We do not include repo path itself. "files": [] }] })
def test_svn(svn_repo): (svn_repo_root, checked_out_dir) = svn_repo svn_file = os.path.join(checked_out_dir, 'foo') uuid_file = os.path.join(svn_repo_root, 'db', 'uuid') uuid = open(uuid_file).readlines()[0].strip() tracer = VCSTracer() assert_distributions(tracer.identify_distributions([svn_file]), expected_length=1, expected_subset={'name': 'svn'}) svn_repo = list(tracer.identify_distributions([svn_file ]))[0][0].packages[0] assert svn_repo.files == ['foo'] assert svn_repo.uuid == uuid assert svn_repo.root_url == 'file://' + svn_repo_root assert svn_repo.revision == 1 assert svn_repo._diff_cmp_id == (svn_repo.uuid, ) assert svn_repo.commit == svn_repo.revision
def traced_repo(git_repo_pair_module): """Return a Git repo pair and the traced GitDistribution for the local repo. """ repo_local, repo_remote = git_repo_pair_module runner = GitRunner(cwd=repo_local) runner(["git", "remote", "add", "dummy-remote", "nowhere"]) tracer = VCSTracer() dists = list(tracer.identify_distributions([op.join(repo_local, "foo")])) git_dist = dists[0][0] assert len(git_dist.packages) == 1 return { "repo_local": repo_local, "repo_remote": repo_remote, "git_dist": git_dist }
def install(git_dist, dest, check=False): """Helper to install GitDistribution `git_dist` to `dest`. If `check`, trace the installed repository and run a basic comparison in the GitRepo objects. """ tracer = VCSTracer() git_dist.install_packages() if check: dists_installed = list( tracer.identify_distributions([op.join(dest, "foo")])) git_dist_installed = dists_installed[0][0] assert len(git_dist_installed.packages) == 1 git_pkg = git_dist.packages[0] git_pkg_installed = git_dist_installed.packages[0] for att in ["hexsha", "root_hexsha", "tracked_remote", "remotes"]: assert getattr(git_pkg, att) == getattr(git_pkg_installed, att)
def test_git_repo_detached(git_repo): runner = GitRunner(git_repo) branch = current_branch(runner) # If we are in a detached state, we still don't identify the # repository itself. runner(["git", "checkout", branch + "^{}", "--"], expect_stderr=True) hexsha_branch, _ = runner(["git", "rev-parse", branch]) hexsha_branch = hexsha_branch.strip() tracer = VCSTracer() dists = list(tracer.identify_distributions([git_repo])) pkg = dists[0][0].packages[0] # We do not include repository path itself. assert pkg.files == [] assert pkg.hexsha == hexsha_branch assert not pkg.branch assert not pkg.remotes
def test_empty_svn(svn_repo_empty): (svn_repo_root, checked_out_dir) = svn_repo_empty tracer = VCSTracer() distributions = list(tracer.identify_distributions([checked_out_dir])) svn_repo = distributions[0][0].packages[0] assert svn_repo.revision is None
def test_git_repo_remotes(git_repo_pair): repo_local, repo_remote = git_repo_pair runner = GitRunner() tracer = VCSTracer() # Set remote.pushdefault to a value we know doesn't exist. # Otherwise, the test machine may have remote.pushdefault globally # configured to point to "origin". runner(["git", "config", "remote.pushdefault", "notexisting"], cwd=repo_local) # Add another remote that doesn't contain the current commit (in # fact doesn't actually exist), so that we test the "listed as # remote but doesn't contain" case. runner(["git", "remote", "add", "fakeremote", "fakepath"], cwd=repo_local) paths = [os.path.join(repo_local, "foo")] dists_nopush = list(tracer.identify_distributions(paths)) assert_distributions(dists_nopush, expected_length=1, expected_subset={ "name": "git", "packages": [{ "files": [op.relpath(p, repo_local) for p in paths], "path": repo_local, "branch": "master", "tracked_remote": "origin", "remotes": { "origin": { "url": repo_remote, "contains": True }, "fakeremote": { "url": "fakepath" } } }] }) pkg_nopush = dists_nopush[0][0].packages[0] assert set(pkg_nopush.remotes.keys()) == {"origin", "fakeremote"} # fakeremote, which doesn't contain the current commit, doesn't # have contains=True. assert "contains" in pkg_nopush.remotes["origin"] assert "contains" not in pkg_nopush.remotes["fakeremote"] # pushurl is not included in the output above because it is not # set. assert "pushurl" not in list(pkg_nopush.remotes.values()) # If we set the pushurl and retrace, it is included. runner(["git", "config", "remote.origin.pushurl", repo_remote], cwd=repo_local) dists_push = list(tracer.identify_distributions(paths)) pkg_push = dists_push[0][0].packages[0] assert pkg_push.remotes["origin"]["pushurl"] == repo_remote # If we are at a commit that none of the remotes are known to # contain, there are no listed remotes. with chpwd(repo_local): runner(["git", "commit", "--allow-empty", "-m", "empty commit"]) dists_nocontain = list(tracer.identify_distributions(paths)) assert not dists_nocontain[0][0].packages[0].remotes.values() # The remote repository, however, doesn't have a remote, so there # are not listed remotes. paths = [os.path.join(repo_remote, "foo")] dists_remote = list(tracer.identify_distributions(paths)) assert not dists_remote[0][0].packages[0].remotes.values()