Skip to content

Commit df4a826

Browse files
authored
Add packaging tests, support blocks defined in __main__ (#252)
Add local packaging run to workflow tests, with an abbreviated (blinky-only) unittest and top-level test. Resolves #251, where blocks defined in `__main__` cause an error when the HDL server tries to resolve those blocks, because the HDL server is a separate Python instance and thinks itself is `__main__` (instead of the original file). The fix is to detect when a library's module is `__main__` and effectively guess the correct original module name by using the filename. Might be a better way to do it, but this works, and is largely what a websearch turned out for finding the original module name for `__main__`. Bumps the package version since this is a significant bugfix.
1 parent e22fb40 commit df4a826

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

.github/workflows/pr-python.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ jobs:
4343
pip install mypy mypy-protobuf types-protobuf types-Deprecated
4444
mypy --version
4545
- name: mypy
46-
run: |
47-
mypy --install-types .
46+
run: mypy --install-types .
4847

4948
unittest_latest_3_11:
5049
needs: pre_job
@@ -57,11 +56,29 @@ jobs:
5756
python-version: '3.11'
5857

5958
- name: install dependencies
60-
run: |
61-
pip install -r requirements.txt
59+
run: pip install -r requirements.txt
6260
- name: unittest
6361
run: python -m unittest discover
6462

63+
packagetest_latest_3_11:
64+
needs: pre_job
65+
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
66+
runs-on: ubuntu-latest
67+
steps:
68+
- uses: actions/checkout@v1
69+
- uses: actions/setup-python@v1
70+
with:
71+
python-version: '3.11'
72+
73+
- name: install dependencies
74+
run: pip install -r requirements.txt
75+
- name: package
76+
run: python -m pip install .
77+
- name: unittest
78+
run: cd examples && python -m unittest test_blinky
79+
- name: toptest
80+
run: cd examples && python test_blinky.py
81+
6582
mypy_latest_3_9:
6683
needs: pre_job
6784
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
@@ -77,8 +94,7 @@ jobs:
7794
pip install mypy mypy-protobuf types-protobuf types-Deprecated
7895
mypy --version
7996
- name: mypy
80-
run: |
81-
mypy --install-types .
97+
run: mypy --install-types .
8298

8399
unittest_latest_3_9:
84100
needs: pre_job
@@ -91,7 +107,6 @@ jobs:
91107
python-version: '3.9'
92108

93109
- name: install dependencies
94-
run: |
95-
pip install -r requirements.txt
110+
run: pip install -r requirements.txt
96111
- name: unittest
97112
run: python -m unittest discover

developing.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ If you have not modified any of the .scala files, you do not need to recompile t
5050
1. [Download and install sbt](https://www.scala-sbt.org/download.html), a build tool or Scala.
5151
2. In the `compiler/` folder, run `sbt assembly` to compile the compiler JAR file.
5252
The system will automatically prefer the locally built JAR file over the pre-compiled JAR and indicate its use through the console.
53-
3. Optionally, to commit a new pre-compiled JAR, move the newly compiled JAR from `compiler/target/scala-*/edg-compiler-assembly-*-SNAPSHOT.jar` to `compiler/edg-compiler-precompiled.jar`.
53+
3. Optionally, to commit a new pre-compiled JAR, move the newly compiled JAR from `compiler/target/scala-*/edg-compiler-assembly-*-SNAPSHOT.jar` to `edg_core/resources/edg-compiler-precompiled.jar`.
54+
55+
### Packaging
56+
Largely based on [this tutorial](https://realpython.com/pypi-publish-python-package/).
57+
58+
Local:
59+
- Install in editable mode (package refers to this source directory, instead of making a copy): `python -m pip install -e .`
60+
61+
Upload:
62+
- These dependencies are necessary: `python -m pip install build twine`
63+
- Build the package (creates a `dist` folder with a `.whl` (zipped) file): `python -m build`
64+
- Optionally, upload to TestPyPI: `python -m twine upload -r testpypi dist/*`
65+
- Upload to PyPI: `python -m twine upload dist/*`
5466

5567

5668
## Frontend Architecture

edg_core/Core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,16 @@ def _name_from(self, base: LibraryElement) -> str:
227227
def _static_def_name(cls) -> str:
228228
"""If this library element is defined by class (all instances have an equivalent library definition),
229229
returns the definition name. Otherwise, should crash."""
230-
return cls.__module__ + "." + cls.__name__
230+
if cls.__module__ == "__main__":
231+
# when the top-level design is run as main, the module name is __main__ which is meaningless
232+
# and breaks when the HDL server tries to resolve the __main__ reference (to itself),
233+
# so this needs to resolve the correct name
234+
import inspect
235+
import os
236+
module = os.path.splitext(os.path.basename(inspect.getfile(cls)))[0]
237+
else:
238+
module = cls.__module__
239+
return module + "." + cls.__name__
231240

232241
def _get_def_name(self) -> str:
233242
"""Returns the definition name"""

edg_core/Util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ def edg_to_dict(obj: Union[BasePort, BaseBlock]) -> Dict[str, Any]:
2828
}
2929
else:
3030
raise ValueError
31-

examples/test_blinky.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,13 @@ def test_design_array(self) -> None:
445445

446446
def test_design_packed(self) -> None:
447447
compile_board_inplace(TestBlinkyPacked, False)
448+
449+
450+
if __name__ == "__main__":
451+
# this unit test can also be run as __main__ to test a non-unit-test environment
452+
compile_board_inplace(TestBlinkyEmpty, False)
453+
compile_board_inplace(TestBlinkyComplete, False)
454+
compile_board_inplace(TestBlinkyWithLibrary, False)
455+
compile_board_inplace(TestBlinkyWithLibraryExport, False)
456+
compile_board_inplace(TestBlinkyArray, False)
457+
compile_board_inplace(TestBlinkyPacked, False)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "edg"
7-
version = "0.0.0"
7+
version = "0.0.1"
88
description = "Hardware description language for circuit boards"
99
readme = "README.md"
1010
authors = [{ name = "Ducky", email = "[email protected]" }]

0 commit comments

Comments
 (0)