Skip to content

Commit 0ef470b

Browse files
authored
feat: testing and linting (#155)
Assorted testing and linting improvements: * Makefile with test and lint targets * Long lines reformated with black * Sort imports with isort And in addition: * gh_refs_path changed to get_github_refs_path Instead of only returning heads or tags, and appending it to a GitHub url, the function now accepts an owner and a name and return the full refs path.
1 parent 3930d65 commit 0ef470b

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
lines changed

.github/workflows/test.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Run tests
2+
3+
on:
4+
pull_request:
5+
branches: [master]
6+
7+
jobs:
8+
tests:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Set up Python 3.8
13+
uses: actions/setup-python@v2
14+
with:
15+
python-version: 3.8
16+
- name: Upgrade pip
17+
run: python -m pip install --upgrade pip setuptools
18+
- name: Install dependencies
19+
run: |
20+
pip install black==23.7.0 \
21+
isort==5.12.0 \
22+
mypy==1.5.1 \
23+
pylint==2.17.5 \
24+
pytest==7.4.2 \
25+
tutor==16.1.2 \
26+
types-docutils==0.20.0.3 \
27+
types-pyyaml==6.0.12.11 \
28+
types-setuptools==68.1.0.0
29+
- name: Test lint, types, and format
30+
run: make test

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.DEFAULT_GOAL := help
2+
.PHONY: docs
3+
SRC_DIRS = ./tutormfe
4+
BLACK_OPTS = --exclude templates ${SRC_DIRS}
5+
6+
# Warning: These checks are run on every PR.
7+
test: test-lint test-types test-format # Run some static checks.
8+
9+
test-format: ## Run code formatting tests.
10+
black --check --diff $(BLACK_OPTS)
11+
12+
test-lint: ## Run code linting tests
13+
pylint --errors-only --enable=unused-import,unused-argument --ignore=templates --ignore=docs/_ext ${SRC_DIRS}
14+
15+
test-types: ## Run type checks.
16+
mypy --exclude=templates --ignore-missing-imports --implicit-reexport --strict ${SRC_DIRS}
17+
18+
format: ## Format code automatically.
19+
black $(BLACK_OPTS)
20+
21+
isort: ## Sort imports. This target is not mandatory because the output may be incompatible with black formatting. Provided for convenience purposes.
22+
isort --skip=templates ${SRC_DIRS}
23+
24+
changelog-entry: ## Create a new changelog entry.
25+
scriv create
26+
27+
changelog: ## Collect changelog entries in the CHANGELOG.md file.
28+
scriv collect
29+
30+
ESCAPE = 
31+
help: ## Print this help.
32+
@grep -E '^([a-zA-Z_-]+:.*?## .*|######* .+)$$' Makefile \
33+
| sed 's/######* \(.*\)/@ $(ESCAPE)[1;31m\1$(ESCAPE)[0m/g' | tr '@' '\n' \
34+
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [Improvement] Added Makefile and test action to repository and formatted code with Black and isort. (by @CodeWithEmad)

tutormfe/hooks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
them.
55
"""
66
from __future__ import annotations
7+
78
import typing as t
89

910
from tutor.core.hooks import Filter
1011

11-
12-
MFE_ATTRS_TYPE = t.Dict[t.Literal["repository", "port"], t.Union["str", int]]
12+
MFE_ATTRS_TYPE = t.Dict[t.Literal["repository", "refs", "port"], t.Union["str", int]]
1313

1414
MFE_APPS: Filter[dict[str, MFE_ATTRS_TYPE], []] = Filter()

tutormfe/plugin.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
from __future__ import annotations
22

3-
from glob import glob
43
import os
54
import typing as t
5+
from glob import glob
66

77
import pkg_resources
8-
98
from tutor import fmt
109
from tutor import hooks as tutor_hooks
1110
from tutor.hooks import priorities
1211
from tutor.types import Config, get_typed
12+
1313
from .__about__ import __version__, __version_suffix__
14-
from .hooks import MFE_ATTRS_TYPE, MFE_APPS
14+
from .hooks import MFE_APPS, MFE_ATTRS_TYPE
1515

1616
config = {
1717
"defaults": {
@@ -24,55 +24,64 @@
2424
}
2525

2626

27-
# If the package version suffix is set (for instance, in the nightly branch) use the "heads" Github refs API endpoint by default.
28-
def gh_refs_path() -> str:
29-
return "heads" if __version_suffix__ else "tags"
27+
def get_github_refs_path(name: str) -> str:
28+
"""
29+
Generate a URL to access refs in heads (nightly) or tags (stable) via Github API.
30+
Args:
31+
name (str): Consisted of the repository owner and the repository name, as a string in 'owner/repo' format.
32+
33+
Returns:
34+
str: A string URL to the Github API, pointing to heads if version_suffix is set, tags otherwise.
35+
36+
"""
37+
38+
return f"https://api.github.com/repos/{name}/git/refs/{'heads' if __version_suffix__ else 'tags'}"
3039

3140

3241
CORE_MFE_APPS: dict[str, MFE_ATTRS_TYPE] = {
3342
"authn": {
3443
"repository": "https://github.com/openedx/frontend-app-authn",
35-
"refs": "https://api.github.com/repos/openedx/frontend-app-authn/git/refs/" + gh_refs_path(),
44+
"refs": get_github_refs_path("openedx/frontend-app-authn"),
3645
"port": 1999,
3746
},
3847
"account": {
3948
"repository": "https://github.com/openedx/frontend-app-account",
40-
"refs": "https://api.github.com/repos/openedx/frontend-app-account/git/refs/" + gh_refs_path(),
49+
"refs": get_github_refs_path("openedx/frontend-app-account"),
4150
"port": 1997,
4251
},
4352
"communications": {
4453
"repository": "https://github.com/openedx/frontend-app-communications",
45-
"refs": "https://api.github.com/repos/openedx/frontend-app-communications/git/refs/" + gh_refs_path(),
54+
"refs": get_github_refs_path("openedx/frontend-app-communications"),
4655
"port": 1984,
4756
},
4857
"course-authoring": {
4958
"repository": "https://github.com/openedx/frontend-app-course-authoring",
50-
"refs": "https://api.github.com/repos/openedx/frontend-app-course-authoring/git/refs/" + gh_refs_path(),
59+
"refs": get_github_refs_path("openedx/frontend-app-course-authoring"),
5160
"port": 2001,
5261
},
5362
"discussions": {
5463
"repository": "https://github.com/openedx/frontend-app-discussions",
55-
"refs": "https://api.github.com/repos/openedx/frontend-app-discussions/git/refs/" + gh_refs_path(),
64+
"refs": get_github_refs_path("openedx/frontend-app-discussions"),
5665
"port": 2002,
5766
},
5867
"gradebook": {
5968
"repository": "https://github.com/openedx/frontend-app-gradebook",
60-
"refs": "https://api.github.com/repos/openedx/frontend-app-gradebook/git/refs/" + gh_refs_path(),
69+
"refs": get_github_refs_path("openedx/frontend-app-gradebook"),
6170
"port": 1994,
6271
},
6372
"learning": {
6473
"repository": "https://github.com/openedx/frontend-app-learning",
65-
"refs": "https://api.github.com/repos/openedx/frontend-app-learning/git/refs/" + gh_refs_path(),
74+
"refs": get_github_refs_path("openedx/frontend-app-learning"),
6675
"port": 2000,
6776
},
6877
"ora-grading": {
6978
"repository": "https://github.com/openedx/frontend-app-ora-grading",
70-
"refs": "https://api.github.com/repos/openedx/frontend-app-ora-grading/git/refs/" + gh_refs_path(),
79+
"refs": get_github_refs_path("openedx/frontend-app-ora-grading"),
7180
"port": 1993,
7281
},
7382
"profile": {
7483
"repository": "https://github.com/openedx/frontend-app-profile",
75-
"refs": "https://api.github.com/repos/openedx/frontend-app-profile/git/refs/" + gh_refs_path(),
84+
"refs": get_github_refs_path("openedx/frontend-app-profile"),
7685
"port": 1995,
7786
},
7887
}
@@ -161,7 +170,9 @@ def is_mfe_enabled(mfe_name: str) -> bool:
161170

162171

163172
@tutor_hooks.Filters.COMPOSE_MOUNTS.add()
164-
def _mount_frontend_apps(volumes, path_basename):
173+
def _mount_frontend_apps(
174+
volumes: list[tuple[str, str]], path_basename: str
175+
) -> list[tuple[str, str]]:
165176
"""
166177
If the user mounts any repo named frontend-app-APPNAME, then make sure
167178
it's available in the APPNAME service container. This is only applicable

0 commit comments

Comments
 (0)