Add tests for reading, rewriting and pushing the README
This commit is contained in:
1 parent
ab905577b8
commit
cea85baade
1 file changed
+86
@@ -0,0 +1,86 @@
|
||||
"""Reading, rewriting and pushing the .profile README."""
|
||||
|
||||
import base64
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from language_stats import (
|
||||
fetch_readme,
|
||||
get_session,
|
||||
replace_between_markers,
|
||||
update_readme,
|
||||
)
|
||||
|
||||
README_URL = "https://gitea.test/api/v1/repos/alice/.profile/contents/README.md"
|
||||
|
||||
|
||||
def test_fetch_readme_decodes_the_content_and_returns_its_sha(gitea):
|
||||
encoded = base64.b64encode("# Hi 👋\n".encode("utf-8")).decode("ascii")
|
||||
gitea.add("GET", README_URL, {"content": encoded, "sha": "abc123"})
|
||||
assert fetch_readme(get_session()) == ("# Hi 👋\n", "abc123")
|
||||
|
||||
|
||||
def test_fetch_readme_raises_when_the_readme_is_missing(gitea):
|
||||
gitea.add("GET", README_URL, {"message": "not found"}, status=404)
|
||||
with pytest.raises(requests.HTTPError):
|
||||
fetch_readme(get_session())
|
||||
|
||||
|
||||
def test_text_between_the_markers_is_replaced_and_the_rest_is_kept():
|
||||
readme = (
|
||||
"# Hi\n"
|
||||
"<!--LANG_STATS_START-->\nold stats\n<!--LANG_STATS_END-->\n"
|
||||
"Thanks for visiting\n"
|
||||
)
|
||||
assert replace_between_markers(readme, "new stats") == (
|
||||
"# Hi\n"
|
||||
"<!--LANG_STATS_START-->\nnew stats\n<!--LANG_STATS_END-->\n"
|
||||
"Thanks for visiting\n"
|
||||
)
|
||||
|
||||
|
||||
def test_backslash_line_breaks_in_the_stats_survive_replacement():
|
||||
# re.sub gives backslashes in the replacement a special meaning, and the
|
||||
# chart uses a backslash at the end of each line as a markdown line break.
|
||||
stats = "## 📊 Languages\n██ Python 75.0%\\\n█ Lua 25.0%"
|
||||
readme = "<!--LANG_STATS_START--><!--LANG_STATS_END-->"
|
||||
assert replace_between_markers(readme, stats) == (
|
||||
"<!--LANG_STATS_START-->\n"
|
||||
"## 📊 Languages\n██ Python 75.0%\\\n█ Lua 25.0%\n"
|
||||
"<!--LANG_STATS_END-->"
|
||||
)
|
||||
|
||||
|
||||
def test_replacing_with_the_same_stats_leaves_the_readme_unchanged():
|
||||
# main() relies on this to skip pushing an identical README.
|
||||
readme = "<!--LANG_STATS_START-->\nsame stats\n<!--LANG_STATS_END-->\n"
|
||||
assert replace_between_markers(readme, "same stats") == (
|
||||
"<!--LANG_STATS_START-->\nsame stats\n<!--LANG_STATS_END-->\n"
|
||||
)
|
||||
|
||||
|
||||
def test_readme_without_markers_raises():
|
||||
with pytest.raises(ValueError, match="not found in README"):
|
||||
replace_between_markers("# Hi\nno markers here\n", "stats")
|
||||
|
||||
|
||||
def test_readme_with_only_the_start_marker_raises():
|
||||
with pytest.raises(ValueError, match="not found in README"):
|
||||
replace_between_markers("<!--LANG_STATS_START-->\nold stats\n", "stats")
|
||||
|
||||
|
||||
def test_update_readme_sends_base64_content_with_the_sha(gitea):
|
||||
gitea.add("PUT", README_URL, {})
|
||||
update_readme(get_session(), "# Hi 👋\n", "abc123")
|
||||
body = gitea.put_bodies[0]
|
||||
assert base64.b64decode(body["content"]).decode("utf-8") == "# Hi 👋\n"
|
||||
assert body["sha"] == "abc123"
|
||||
assert body["message"] == "Update language statistics"
|
||||
|
||||
|
||||
def test_update_readme_raises_when_gitea_rejects_the_push(gitea):
|
||||
# Gitea answers 409 when the sha is stale (README changed since we read it).
|
||||
gitea.add("PUT", README_URL, {"message": "sha does not match"}, status=409)
|
||||
with pytest.raises(requests.HTTPError):
|
||||
update_readme(get_session(), "# Hi\n", "old-sha")
|
||||
Reference in new issue
Block a user