Skip to content

Commit

Permalink
add tests for Sentry revision code (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Feb 16, 2023
2 parents 6b5a30c + f5e2cb5 commit d1145ba
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.env
*.mo
*.tfbackend
*.tmp
benefits/core/migrations/0002_*.py
!benefits/core/migrations/0002_sample_data.py
static/
Expand Down
18 changes: 13 additions & 5 deletions benefits/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ def get_git_revision_hash():
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()


def get_sha_path():
def get_sha_file_path():
current_file = os.path.dirname(os.path.abspath(__file__))
return os.path.join(current_file, "..", "static", "sha.txt")


def get_sha_from_file():
sha_path = get_sha_file_path()
if os.path.isfile(sha_path):
with open(sha_path) as f:
return f.read().strip()
else:
return None


def get_release() -> str:
"""Returns the first available: the SHA from Git, the value from sha.txt, or the VERSION."""

if git_available() and is_git_directory():
return get_git_revision_hash()
else:
sha_path = get_sha_path()
if os.path.isfile(sha_path):
with open(sha_path) as f:
return f.read().strip()
sha = get_sha_from_file()
if sha:
return sha
else:
# one of the above *should* always be available, but including this just in case
return VERSION
Expand Down
47 changes: 47 additions & 0 deletions tests/pytest/test_sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from benefits import VERSION
from benefits.sentry import get_release
import os
from tempfile import NamedTemporaryFile


def test_git(mocker):
mocker.patch("benefits.sentry.git_available", return_value=True)
mocker.patch("benefits.sentry.is_git_directory", return_value=True)

fake_sha = "123"
mocker.patch("benefits.sentry.get_git_revision_hash", return_value=fake_sha)

assert get_release() == fake_sha


def test_sha_file(mocker):
mocker.patch("benefits.sentry.git_available", return_value=True)
mocker.patch("benefits.sentry.is_git_directory", return_value=False)

with NamedTemporaryFile() as fp:
fake_sha = "123"

fp.write(bytes(fake_sha, "utf-8"))
fp.seek(0)
mocker.patch("benefits.sentry.get_sha_file_path", return_value=fp.name)

assert get_release() == fake_sha


def test_no_git(mocker):
mocker.patch("benefits.sentry.git_available", return_value=False)

file_path = os.path.join("tmp", "nonexistent.txt")
mocker.patch("benefits.sentry.get_sha_file_path", return_value=file_path)

assert get_release() == VERSION


def test_git_no_repo(mocker):
mocker.patch("benefits.sentry.git_available", return_value=True)
mocker.patch("benefits.sentry.is_git_directory", return_value=False)

file_path = os.path.join("tmp", "nonexistent.txt")
mocker.patch("benefits.sentry.get_sha_file_path", return_value=file_path)

assert get_release() == VERSION

0 comments on commit d1145ba

Please sign in to comment.