Skip to content

Commit

Permalink
refactor(test_feedstock_tokens): use pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbaHerrerias committed May 24, 2024
1 parent 934594f commit 67ee85b
Showing 1 changed file with 50 additions and 49 deletions.
99 changes: 50 additions & 49 deletions tests/test_feedstock_tokens.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
from pathlib import Path
from unittest import mock
import time

Expand Down Expand Up @@ -76,15 +77,15 @@ def test_feedstock_tokens_roundtrip(
project,
provider=ci,
)
token_json_pth = os.path.join(tmpdir, "tokens", "%s.json" % project)
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
token_json_pth = Path(tmpdir) / "tokens" / f"{project}.json"
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)

try:
generate_and_write_feedstock_token(user, project, provider=ci)
assert os.path.exists(pth)
assert Path(pth).exists()

register_feedstock_token(user, project, repo, provider=ci)
assert os.path.exists(token_json_pth)
assert token_json_pth.exists()

with open(token_json_pth) as fp:
token_data = json.load(fp)
Expand All @@ -102,10 +103,10 @@ def test_feedstock_tokens_roundtrip(
user, project, feedstock_token, repo, provider=ci
)
finally:
if os.path.exists(pth):
os.remove(pth)
if os.path.exists(token_json_pth):
os.remove(token_json_pth)
if Path(pth).exists():
Path(pth).unlink()
if token_json_pth.exists():
token_json_pth.unlink()

assert retval is (retval_ci and retval_time)

Expand Down Expand Up @@ -180,8 +181,8 @@ def test_is_valid_feedstock_token_badtoken(
user = "conda-forge"
feedstock_token = "akdjhfl"

token_pth = os.path.join(tmpdir, "tokens", "%s.json" % project)
os.makedirs(os.path.dirname(token_pth), exist_ok=True)
token_pth = Path(tmpdir) / "tokens" / f"{project}.json"
Path(token_pth.parent).mkdir(parents=True, exist_ok=True)
with open(token_pth, "w") as fp:
td = {"salt": b"adf".hex(), "hashed_token": b"fgh".hex()}
if provider is not None:
Expand All @@ -202,17 +203,17 @@ def test_generate_and_write_feedstock_token(ci):
repo = "foo"

if ci:
pth = os.path.expanduser("~/.conda-smithy/bar_foo_%s.token" % ci)
opth = os.path.expanduser("~/.conda-smithy/bar_foo.token")
pth = Path(f"~/.conda-smithy/bar_foo_{ci}.token").expanduser()
opth = Path("~/.conda-smithy/bar_foo.token").expanduser()
else:
pth = os.path.expanduser("~/.conda-smithy/bar_foo.token")
opth = os.path.expanduser("~/.conda-smithy/bar_foo_azure.token")
pth = Path("~/.conda-smithy/bar_foo.token").expanduser()
opth = Path("~/.conda-smithy/bar_foo_azure.token").expanduser()

try:
generate_and_write_feedstock_token(user, repo, provider=ci)

assert not os.path.exists(opth)
assert os.path.exists(pth)
assert not Path(opth).exists()
assert Path(pth).exists()

if ci is not None:
generate_and_write_feedstock_token(user, repo, provider=None)
Expand All @@ -223,20 +224,20 @@ def test_generate_and_write_feedstock_token(ci):
with pytest.raises(FeedstockTokenError):
generate_and_write_feedstock_token(user, repo, provider=ci)
finally:
if os.path.exists(pth):
os.remove(pth)
if os.path.exists(opth):
os.remove(opth)
if Path(pth).exists():
Path(pth).unlink()
if Path(opth).exists():
Path(opth).unlink()


@pytest.mark.parametrize("ci", [None, "azure"])
def test_read_feedstock_token(ci):
user = "bar"
repo = "foo"
if ci:
pth = os.path.expanduser("~/.conda-smithy/bar_foo_%s.token" % ci)
pth = Path(f"~/.conda-smithy/bar_foo_{ci}.token").expanduser()
else:
pth = os.path.expanduser("~/.conda-smithy/bar_foo.token")
pth = Path("~/.conda-smithy/bar_foo.token").expanduser()

# no token
token, err = read_feedstock_token(user, repo, provider=ci)
Expand All @@ -250,8 +251,8 @@ def test_read_feedstock_token(ci):
assert "Empty token found in" in err
assert token is None
finally:
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()

# token ok
try:
Expand All @@ -267,8 +268,8 @@ def test_read_feedstock_token(ci):
assert "No token found in" in err
assert token is None
finally:
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -324,10 +325,10 @@ def test_feedstock_token_exists(
)

user = "foo"
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)
if file_exists:
with open(
os.path.join(tmpdir, "tokens", "%s.json" % project), "w"
Path(tmpdir) / "tokens" / f"{project}.json", "w"
) as fp:
data = {"tokens": [{}]}
if provider is not None:
Expand Down Expand Up @@ -365,8 +366,8 @@ def test_feedstock_token_raises(

git_mock.Repo.clone_from.side_effect = ValueError("blarg")
user = "foo"
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
with open(os.path.join(tmpdir, "tokens", "%s.json" % project), "w") as fp:
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)
with open(Path(tmpdir) / "tokens" / f"{project}.json", "w") as fp:
fp.write("{}")

with pytest.raises(FeedstockTokenError) as e:
Expand Down Expand Up @@ -409,22 +410,22 @@ def test_register_feedstock_token_works(

user = "foo"
project = "bar"
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)
pth = feedstock_token_local_path(
user,
project,
provider=ci,
)
token_json_pth = os.path.join(tmpdir, "tokens", "%s.json" % project)
token_json_pth = Path(tmpdir) / "tokens" / f"{project}.json"

try:
generate_and_write_feedstock_token(user, project, provider=ci)

register_feedstock_token(user, project, repo, provider=ci)

finally:
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()

git_mock.Repo.clone_from.assert_called_once_with(
"abc123",
Expand All @@ -433,7 +434,7 @@ def test_register_feedstock_token_works(
)

repo = git_mock.Repo.clone_from.return_value
repo.index.add.assert_called_once_with(token_json_pth)
repo.index.add.assert_called_once_with(str(token_json_pth))
repo.index.commit.assert_called_once_with(
"[ci skip] [skip ci] [cf admin skip] ***NO_CI*** added token for %s/%s on provider%s"
% (user, project, "" if ci is None else " " + ci)
Expand Down Expand Up @@ -481,20 +482,20 @@ def test_register_feedstock_token_notoken(

user = "foo"
project = "bar"
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)
pth = feedstock_token_local_path(
user,
project,
provider=ci,
)
token_json_pth = os.path.join(tmpdir, "tokens", "bar.json")
token_json_pth = Path(tmpdir) / "tokens" / "bar.json"

try:
with pytest.raises(FeedstockTokenError) as e:
register_feedstock_token(user, project, repo, provider=ci)
finally:
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()

git_mock.Repo.clone_from.assert_not_called()

Expand All @@ -504,7 +505,7 @@ def test_register_feedstock_token_notoken(
repo.remote.return_value.pull.assert_not_called()
repo.remote.return_value.push.assert_not_called()

assert not os.path.exists(token_json_pth)
assert not token_json_pth.exists()

assert "No token found in" in str(e.value)

Expand Down Expand Up @@ -537,22 +538,22 @@ def test_register_feedstock_token_append(

user = "foo"
project = "bar"
os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
Path(tmpdir, "tokens").mkdir(parents=True, exist_ok=True)
pth = feedstock_token_local_path(
user,
project,
provider=ci,
)
token_json_pth = os.path.join(tmpdir, "tokens", "bar.json")
token_json_pth = Path(tmpdir) / "tokens" / "bar.json"
with open(token_json_pth, "w") as fp:
fp.write('{"tokens": [1]}')

try:
generate_and_write_feedstock_token(user, project, provider=ci)
register_feedstock_token(user, project, repo, provider=ci)
finally:
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()

git_mock.Repo.clone_from.assert_called_once_with(
"abc123",
Expand All @@ -561,7 +562,7 @@ def test_register_feedstock_token_append(
)

repo = git_mock.Repo.clone_from.return_value
repo.index.add.assert_called_once_with(token_json_pth)
repo.index.add.assert_called_once_with(str(token_json_pth))
repo.index.commit.assert_called_once_with(
"[ci skip] [skip ci] [cf admin skip] ***NO_CI*** added token for %s/%s on provider%s"
% (user, project, "" if ci is None else " " + ci)
Expand Down Expand Up @@ -715,8 +716,8 @@ def test_register_feedstock_token_with_providers(
finally:
for provider in providers:
pth = feedstock_token_local_path(user, project, provider=provider)
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()


@pytest.mark.parametrize("unique_token_per_provider", [False, True])
Expand Down Expand Up @@ -833,5 +834,5 @@ def test_register_feedstock_token_with_providers_error(
finally:
for _provider in providers:
pth = feedstock_token_local_path(user, project, provider=_provider)
if os.path.exists(pth):
os.remove(pth)
if Path(pth).exists():
Path(pth).unlink()

0 comments on commit 67ee85b

Please sign in to comment.