Skip to content

Commit e29e6f7

Browse files
authored
🔀 Merge pull request #90 from astariul/add_tests_on_PR
Add tests on PR
2 parents b21abdc + 9d22a13 commit e29e6f7

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

‎.github/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def update(pkg_name, version):
129129
# Extract the URL from the onclick attribute
130130
button = soup.find('button', id='repoHomepage')
131131
if button:
132-
link = button.get("onclick")[len("location.href='"):-1]
132+
link = button.get("onclick")[len("openLinkInNewTab('"):-2]
133133
else:
134134
raise Exception("Homepage URL not found")
135135

‎.github/tests.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import subprocess
2+
import os
3+
from importlib.metadata import PackageNotFoundError, version
4+
from contextlib import contextmanager
5+
6+
7+
@contextmanager
8+
def run_local_pypi_index():
9+
# WARNING : This requires the script to be run from the root of the repo
10+
p = subprocess.Popen(["python", "-m", "http.server"])
11+
try:
12+
yield
13+
finally:
14+
p.terminate()
15+
16+
17+
def exists(pkg_name: str) -> bool:
18+
try:
19+
version(pkg_name)
20+
except PackageNotFoundError:
21+
return False
22+
else:
23+
return True
24+
25+
26+
def pip_install(pkg_name: str, upgrade: bool = False, version: str = None):
27+
package_to_install = pkg_name if version is None else f"{pkg_name}=={version}"
28+
cmd = ["python", "-m", "pip", "install", package_to_install, "--upgrade" if upgrade else "", "--extra-index-url", "http://localhost:8000"]
29+
subprocess.run([c for c in cmd if c])
30+
31+
32+
def pip_uninstall(pkg_name: str):
33+
subprocess.run(["python", "-m", "pip", "uninstall", pkg_name, "-y"])
34+
35+
36+
def register(pkg_name: str, pkg_version: str, pkg_link: str):
37+
env = os.environ.copy()
38+
env["PKG_ACTION"] = "REGISTER"
39+
env["PKG_NAME"] = pkg_name
40+
env["PKG_VERSION"] = pkg_version
41+
env["PKG_AUTHOR"] = "Dummy author"
42+
env["PKG_SHORT_DESC"] = "Dummy Description"
43+
env["PKG_HOMEPAGE"] = pkg_link
44+
subprocess.run(["python", ".github/actions.py"], env=env)
45+
46+
47+
def update(pkg_name: str, pkg_version: str):
48+
env = os.environ.copy()
49+
env["PKG_ACTION"] = "UPDATE"
50+
env["PKG_NAME"] = pkg_name
51+
env["PKG_VERSION"] = pkg_version
52+
subprocess.run(["python", ".github/actions.py"], env=env)
53+
54+
55+
def delete(pkg_name: str):
56+
env = os.environ.copy()
57+
env["PKG_ACTION"] = "DELETE"
58+
env["PKG_NAME"] = pkg_name
59+
subprocess.run(["python", ".github/actions.py"], env=env)
60+
61+
62+
def run_tests():
63+
# This test is checking that the Github actions for registering, updating,
64+
# and deleting are working and the PyPi index is updated accordingly.
65+
# What we do is :
66+
# * Serve the HTML locally so we have a local PyPi index
67+
# * Run the actions for registering, updating, and deleting packages in
68+
# this local PyPi
69+
# * In-between, run some basic checks to see if the installation is
70+
# working properly
71+
with run_local_pypi_index():
72+
# First, make sure the package is not installed
73+
assert not exists("public-hello")
74+
75+
# The package `public-hello` is already registered in our local PyPi
76+
# ACTION : Let's remove it from our local PyPi
77+
delete("public-hello")
78+
79+
# Trying to install the package, only the version uploaded to PyPi (0.0.0)
80+
# will be detected and installed (the version in our local PyPi was
81+
# successfully deleted)
82+
pip_install("public-hello")
83+
assert exists("public-hello") and version("public-hello") == "0.0.0"
84+
85+
# ACTION : Register the package, to make it available again
86+
register("public-hello", "0.1", "https://github.com/astariul/public-hello")
87+
88+
# Now we can install it from the local PyPi
89+
pip_install("public-hello", upgrade=True)
90+
assert exists("public-hello") and version("public-hello") == "0.1"
91+
92+
# ACTION : Update the package with a new version
93+
update("public-hello", "0.2")
94+
95+
# We can update the package to the newest version
96+
pip_install("public-hello", upgrade=True)
97+
assert exists("public-hello") and version("public-hello") == "0.2"
98+
99+
# We should still be able to install the old version
100+
pip_install("public-hello", version="0.1")
101+
assert exists("public-hello") and version("public-hello") == "0.1"
102+
103+
# Uninstall the package (for consistency with the initial state)
104+
pip_uninstall("public-hello")
105+
assert not exists("public-hello")
106+
107+
108+
if __name__ == "__main__":
109+
run_tests()

‎.github/workflows/tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: tests
2+
3+
on: pull_request
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
steps:
12+
- uses: actions/checkout@v4
13+
with: # Use deploy key to ensure pushed change trigger checks as well : https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#workarounds-to-trigger-further-workflow-runs
14+
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: "3.8"
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install beautifulsoup4
23+
- name: Run unit-tests
24+
timeout-minutes: 5
25+
run: |
26+
# Enable pipefail option, so if the tests fail, the job will fail as well
27+
set -o pipefail
28+
python .github/tests.py

0 commit comments

Comments
 (0)