Skip to content

Commit 4f27474

Browse files
committed
test: update vcs tests to take git 'init.defaultBranch' config into account
I recently updated my local `init.defaultBranch` setting in git from `master` -> `main`, and this broke a bunch of Taskgraph tests. Let's make them a bit smarter.
1 parent cf4d630 commit 4f27474

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

test/fixtures/vcs.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ def git_repo(tmpdir_factory):
5757
yield repo_dir
5858

5959

60+
@pytest.fixture(scope="package")
61+
def default_git_branch():
62+
proc = subprocess.run(
63+
["git", "config", "init.defaultBranch"],
64+
capture_output=True,
65+
check=False,
66+
text=True,
67+
)
68+
return proc.stdout.strip() or "master"
69+
70+
6071
def _build_env_with_git_date_env_vars(date_time_string):
6172
env = os.environ.copy()
6273
env.update({env_var: date_time_string for env_var in _GIT_DATE_ENV_VARS})
@@ -90,30 +101,37 @@ def repo(request, hg_repo, git_repo, monkeypatch, tmpdir):
90101
return get_repository(repodir)
91102

92103

93-
def create_remote_repo(tmpdir, repo, remote_name, remote_path):
94-
if repo.tool == "hg":
95-
repo.run("phase", "--public", ".")
104+
@pytest.fixture
105+
def create_remote_repo(default_git_branch):
96106

97-
shutil.copytree(repo.path, str(tmpdir / remote_path))
107+
def inner(tmpdir, repo, remote_name, remote_path):
108+
if repo.tool == "hg":
109+
repo.run("phase", "--public", ".")
98110

99-
if repo.tool == "git":
100-
repo.run("remote", "add", remote_name, f"{tmpdir}/{remote_path}")
101-
repo.run("fetch", remote_name)
102-
repo.run("branch", "--set-upstream-to", f"{remote_name}/master")
103-
if repo.tool == "hg":
104-
with open(os.path.join(repo.path, ".hg/hgrc"), "a") as f:
105-
f.write(f"[paths]\n{remote_name} = {tmpdir}/{remote_path}\n")
111+
shutil.copytree(repo.path, str(tmpdir / remote_path))
112+
113+
if repo.tool == "git":
114+
repo.run("remote", "add", remote_name, f"{tmpdir}/{remote_path}")
115+
repo.run("fetch", remote_name)
116+
repo.run(
117+
"branch", "--set-upstream-to", f"{remote_name}/{default_git_branch}"
118+
)
119+
if repo.tool == "hg":
120+
with open(os.path.join(repo.path, ".hg/hgrc"), "a") as f:
121+
f.write(f"[paths]\n{remote_name} = {tmpdir}/{remote_path}\n")
122+
123+
return inner
106124

107125

108126
@pytest.fixture
109-
def repo_with_remote(tmpdir, repo):
127+
def repo_with_remote(tmpdir, create_remote_repo, repo):
110128
remote_name = "upstream"
111129
create_remote_repo(tmpdir, repo, remote_name, "remote_repo")
112130
return repo, remote_name
113131

114132

115133
@pytest.fixture
116-
def repo_with_upstream(tmpdir, repo):
134+
def repo_with_upstream(tmpdir, repo, default_git_branch):
117135
with open(os.path.join(repo.path, "second_file"), "w") as f:
118136
f.write("some data for the second file")
119137

@@ -127,7 +145,7 @@ def repo_with_upstream(tmpdir, repo):
127145
upstream_location = None
128146

129147
if repo.tool == "git":
130-
upstream_location = "upstream/master"
148+
upstream_location = f"upstream/{default_git_branch}"
131149
repo.run("remote", "add", "upstream", f"{tmpdir}/remoterepo")
132150
repo.run("fetch", "upstream")
133151
repo.run("branch", "--set-upstream-to", upstream_location)

test/test_util_vcs.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
from taskgraph.util.vcs import HgRepository, Repository, get_repository
1212

13-
from .fixtures.vcs import create_remote_repo
14-
1513

1614
def test_get_repository(repo):
1715
r = get_repository(repo.path)
@@ -159,14 +157,14 @@ def test_remote_name(repo_with_remote):
159157
assert repo.remote_name == remote_name
160158

161159

162-
def test_all_remote_names(tmpdir, repo_with_remote):
160+
def test_all_remote_names(tmpdir, create_remote_repo, repo_with_remote):
163161
repo, remote_name = repo_with_remote
164162
assert repo.all_remote_names == [remote_name]
165163
create_remote_repo(tmpdir, repo, "upstream2", "remote_path2")
166164
assert repo.all_remote_names == [remote_name, "upstream2"]
167165

168166

169-
def test_remote_name_many_remotes(tmpdir, repo_with_remote):
167+
def test_remote_name_many_remotes(tmpdir, create_remote_repo, repo_with_remote):
170168
repo, _ = repo_with_remote
171169
create_remote_repo(tmpdir, repo, "upstream2", "remote_path2")
172170

@@ -177,7 +175,7 @@ def test_remote_name_many_remotes(tmpdir, repo_with_remote):
177175
assert repo.remote_name == "upstream"
178176

179177

180-
def test_remote_name_default_and_origin(tmpdir, repo_with_remote):
178+
def test_remote_name_default_and_origin(tmpdir, create_remote_repo, repo_with_remote):
181179
repo, _ = repo_with_remote
182180
remote_name = "origin" if repo.tool == "git" else "default"
183181
create_remote_repo(tmpdir, repo, remote_name, "remote_path2")
@@ -188,28 +186,28 @@ def test_remote_name_default_and_origin(tmpdir, repo_with_remote):
188186
assert repo.remote_name == remote_name
189187

190188

191-
def test_default_branch_guess(repo):
189+
def test_default_branch_guess(default_git_branch, repo):
192190
if repo.tool == "git":
193-
assert repo.default_branch == "refs/heads/master"
191+
assert repo.default_branch == f"refs/heads/{default_git_branch}"
194192
else:
195193
assert repo.default_branch == "default"
196194

197195

198-
def test_default_branch_remote_query(repo_with_remote):
196+
def test_default_branch_remote_query(default_git_branch, repo_with_remote):
199197
repo, _ = repo_with_remote
200198
if repo.tool == "git":
201-
assert repo.default_branch == "upstream/master"
199+
assert repo.default_branch == f"upstream/{default_git_branch}"
202200
else:
203201
assert repo.default_branch == "default"
204202

205203

206-
def test_default_branch_cloned_metadata(tmpdir, repo):
204+
def test_default_branch_cloned_metadata(tmpdir, default_git_branch, repo):
207205
if repo.tool == "git":
208206
clone_repo_path = tmpdir / "cloned_repo"
209207
command = ("git", "clone", repo.path, clone_repo_path)
210208
subprocess.check_output(command, cwd=tmpdir)
211209
cloned_repo = get_repository(clone_repo_path)
212-
assert cloned_repo.default_branch == "origin/master"
210+
assert cloned_repo.default_branch == f"origin/{default_git_branch}"
213211

214212

215213
def assert_files(actual, expected):

0 commit comments

Comments
 (0)