Skip to content

Commit

Permalink
Fix issues with freezing and merging stubs
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
  • Loading branch information
Josverl committed Nov 5, 2023
1 parent 9f0a605 commit 322f96c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/stubber/freeze/freeze_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def freeze_folders(stub_folder: str, mpy_folder: str, lib_folder: str, version:
- 'ports/<port>/modules/*.py'
- 'ports/<port>/boards/<board>/modules/*.py'
"""
match_lib_with_mpy(version_tag=version, lib_path=Path(lib_folder))
match_lib_with_mpy(version_tag=version, mpy_path=Path(mpy_folder),lib_path=Path(lib_folder))

targets = []
scripts = glob.glob(mpy_folder + "/ports/**/modules/*.py", recursive=True)
Expand Down
9 changes: 4 additions & 5 deletions src/stubber/publish/merge_docstubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

import shutil
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing import List, Optional, Union

from loguru import logger as log

from stubber.codemod.enrich import enrich_folder
from stubber.publish.candidates import board_candidates, filter_list
from stubber.publish.defaults import GENERIC, GENERIC_L, default_board
from stubber.publish.missing_class_methods import add_machine_pin_call
from stubber.publish.package import GENERIC, GENERIC_L
from stubber.publish.defaults import DEFAULT_BOARDS
from stubber.publish.pathnames import get_base, get_board_path, get_merged_path
from stubber.publish.pathnames import get_base, get_board_path, get_merged_path
from stubber.utils.config import CONFIG


Expand Down Expand Up @@ -50,7 +49,7 @@ def merge_all_docstubs(
for candidate in candidates:
# use the default board for the port
if candidate["board"] in GENERIC:
candidate["board"] = DEFAULT_BOARDS[candidate["port"]][0]
candidate["board"] = default_board(candidate["port"])
# check if we have board stubs of this version and port
doc_path = CONFIG.stub_path / f"{get_base(candidate)}-docstubs"
# src and dest paths
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/utils/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def switch(tag: str, *, mpy_path: Path, mpy_lib_path: Path):
git.switch_branch(repo=mpy_path, branch="master")
else:
git.checkout_tag(repo=mpy_path, tag=tag)
match_lib_with_mpy(version_tag=tag, lib_path=mpy_lib_path)
match_lib_with_mpy(version_tag=tag, mpy_path=mpy_path, lib_path=mpy_lib_path)


def read_micropython_lib_commits(filename: str = "data/micropython_tags.csv"):
Expand Down
64 changes: 48 additions & 16 deletions tests/freeze/freezer_mpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def test_get_portboard(path: str, port: str, board: str):
assert _port == port


def test_manifest_uasync(tmp_path: Path, testrepo_micropython: Path, testrepo_micropython_lib: Path):
def test_manifest_uasync(
tmp_path: Path, testrepo_micropython: Path, testrepo_micropython_lib: Path
):
"test if task.py is included with the uasyncio frozen module"
mpy_version = "v1.18"
mpy_folder = testrepo_micropython.absolute()
Expand All @@ -78,7 +80,9 @@ def test_manifest_uasync(tmp_path: Path, testrepo_micropython: Path, testrepo_mi
manifest = mpy_folder / "ports/esp32/boards/manifest.py"
freeze_one_manifest_2(manifest, stub_folder, mpy_folder, lib_folder, mpy_version)

assert (tmp_path / "esp32" / GENERIC_U / "uasyncio/task.py").exists(), "task.py must be included in uasyncio"
assert (
tmp_path / "esp32" / GENERIC_U / "uasyncio/task.py"
).exists(), "task.py must be included in uasyncio"


#######################################################################################################################
Expand Down Expand Up @@ -198,10 +202,12 @@ def test_freeze_one_manifest_v2(
[
"v1.12",
"v1.16",
"v1.17",
# "v1.17",
"v1.18",
"v1.19",
# "v1.19",
"v1.19.1",
"v1.20.0",
"v1.21.0",
"latest",
],
)
Expand All @@ -215,7 +221,12 @@ def test_freeze_any(
# print(f"Testing {mpy_version} in {tmp_path}")
switch(mpy_version, mpy_path=testrepo_micropython, mpy_lib_path=testrepo_micropython_lib)

freeze_any(tmp_path, version=mpy_version, mpy_path=testrepo_micropython, mpy_lib_path=testrepo_micropython_lib)
freeze_any(
tmp_path,
version=mpy_version,
mpy_path=testrepo_micropython,
mpy_lib_path=testrepo_micropython_lib,
)
scripts = list(tmp_path.rglob("*.py"))

assert scripts is not None, "can freeze scripts from manifest"
Expand All @@ -232,12 +243,15 @@ def test_freeze_any(
"mpy_version",
[
"master",
"v1.19",
"v1.18",
"v1.17",
"v1.16",
"v1.12",
"v1.10",
"v1.16",
# "v1.17",
"v1.18",
# "v1.19",
"v1.19.1",
"v1.20.0",
"v1.21.0",
"latest",
],
)
@pytest.mark.mocked
Expand All @@ -250,10 +264,19 @@ def test_freeze_any_mocked(
):
"mocked test if we can freeze source using manifest.py files"

m_freeze_folders: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_folders", autospec=True, return_value=[1])
m_freeze_folders: MagicMock = mocker.patch(
"stubber.freeze.get_frozen.freeze_folders", autospec=True, return_value=[1]
)
# m_freeze_one_manifest_1: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_one_manifest_1", autospec=True, return_value=1)
m_freeze_one_manifest_2: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_one_manifest_2", autospec=True, return_value=1)
x = freeze_any(tmp_path, version=mpy_version, mpy_path=testrepo_micropython, mpy_lib_path=testrepo_micropython_lib)
m_freeze_one_manifest_2: MagicMock = mocker.patch(
"stubber.freeze.get_frozen.freeze_one_manifest_2", autospec=True, return_value=1
)
x = freeze_any(
tmp_path,
version=mpy_version,
mpy_path=testrepo_micropython,
mpy_lib_path=testrepo_micropython_lib,
)
# calls = m_freeze_folders.call_count + m_freeze_one_manifest_1.call_count + m_freeze_one_manifest_2.call_count
calls = m_freeze_folders.call_count + m_freeze_one_manifest_2.call_count
print(f" m_freeze_folders.call_count {m_freeze_folders.call_count}")
Expand All @@ -274,12 +297,21 @@ def test_freeze_manifest2_error_mocked(
):
"mocked test if we can freeze source using manifest.py files"

m_freeze_folders: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_folders", autospec=True, return_value=[1])
m_freeze_folders: MagicMock = mocker.patch(
"stubber.freeze.get_frozen.freeze_folders", autospec=True, return_value=[1]
)
# m_freeze_one_manifest_1: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_one_manifest_1", autospec=True, return_value=1)
m_freeze_one_manifest_2: MagicMock = mocker.patch("stubber.freeze.get_frozen.freeze_one_manifest_2", autospec=True, return_value=1)
m_freeze_one_manifest_2: MagicMock = mocker.patch(
"stubber.freeze.get_frozen.freeze_one_manifest_2", autospec=True, return_value=1
)
# get the correct version to test
switch(mpy_version, mpy_path=testrepo_micropython, mpy_lib_path=testrepo_micropython_lib)
x = freeze_any(tmp_path, version=mpy_version, mpy_path=testrepo_micropython, mpy_lib_path=testrepo_micropython_lib)
x = freeze_any(
tmp_path,
version=mpy_version,
mpy_path=testrepo_micropython,
mpy_lib_path=testrepo_micropython_lib,
)
assert x >= 1, "expect >= 1 stubs"
assert m_freeze_folders.call_count == 0, "expect no calls to freeze_folders"
assert m_freeze_one_manifest_2.call_count == 34, "34 calls to freeze_one_manifest_2"
Expand Down
6 changes: 5 additions & 1 deletion tests/publish/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def test_merge_all_docstubs_mocked(mocker, tmp_path, pytestconfig):
],
)
m_copy_and_merge_docstubs: MagicMock = mocker.patch("stubber.publish.merge_docstubs.copy_and_merge_docstubs", autospec=True)
m_add_machine_pin_call: MagicMock = mocker.patch("stubber.publish.merge_docstubs.add_machine_pin_call", autospec=True)

result = merge_all_docstubs(["v1.18", "v1.19"])
# mock pathlib.Path.exists to return True so there is no dependency of folders existing on the test system
mocker.patch("stubber.publish.merge_docstubs.Path.exists", autospec=True, return_value=True)

result = merge_all_docstubs(["v1.18", "v1.19"])
assert result == 2
assert m_board_candidates.call_count == 1
assert m_copy_and_merge_docstubs.call_count == 2

Expand Down

0 comments on commit 322f96c

Please sign in to comment.