Skip to content

Commit

Permalink
refactor: Replace git tag fetching with micropython_versions function
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Dec 9, 2024
1 parent b67d4d8 commit 44f4af4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/stubber/commands/switch_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import rich_click as click

import mpflash.basicgit as git
from stubber.utils.config import CONFIG
from stubber.utils.repos import fetch_repos, repo_paths
from mpflash.versions import SET_PREVIEW, V_PREVIEW
from mpflash.versions import SET_PREVIEW, V_PREVIEW, micropython_versions

from .cli import stubber_cli

Expand All @@ -22,7 +21,7 @@
# get version list from Git tags in the repo that is provided on the command line

try:
VERSION_LIST = git.get_tags("micropython/micropython", minver="v1.9.3") + [
VERSION_LIST = micropython_versions( minver="v1.9.3") + [
V_PREVIEW,
"latest",
"stable",
Expand Down
6 changes: 2 additions & 4 deletions src/stubber/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from typing import List

from mpflash.logger import log
from mpflash.versions import V_PREVIEW, micropython_versions
from typedconfig.config import Config, key, section
from typedconfig.source import EnvironmentConfigSource

import mpflash.basicgit as git
from mpflash.versions import V_PREVIEW

from .typed_config_toml import TomlConfigSource


Expand Down Expand Up @@ -94,7 +92,7 @@ def post_read_hook(self) -> dict:
# read the versions from the git tags
all_versions = []
try:
all_versions = git.get_tags("micropython/micropython", minver="v1.17")
all_versions = micropython_versions(minver="v1.17")
except Exception as e:
log.warning(f"Could not read micropython versions from git: {e}")
all_versions = ["1.19", "1.19.1", "1.20.0", "1.21.0"]
Expand Down
74 changes: 52 additions & 22 deletions tests/commandline/stubber_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from typing import List

import pytest
from click.testing import CliRunner
from pytest_mock import MockerFixture

# module under test :
import stubber.stubber as stubber
from click.testing import CliRunner
from pytest_mock import MockerFixture
from stubber.commands.switch_cmd import VERSION_LIST

# mark all tests
Expand Down Expand Up @@ -99,11 +99,19 @@ def test_cmd_switch(mocker: MockerFixture, params: List[str]):
mocker.patch("stubber.commands.clone_cmd.git.clone", autospec=True, return_value=0)
m_fetch = mocker.patch("stubber.commands.clone_cmd.git.fetch", autospec=True, return_value=0)

m_switch_branch = mocker.patch("stubber.commands.clone_cmd.git.switch_branch", autospec=True, return_value=0)
m_switch_tag = mocker.patch("stubber.commands.clone_cmd.git.switch_tag", autospec=True, return_value=0)
mocker.patch("stubber.commands.clone_cmd.git.get_local_tag", autospec=True, return_value="v1.42")
m_switch_branch = mocker.patch(
"stubber.commands.clone_cmd.git.switch_branch", autospec=True, return_value=0
)
m_switch_tag = mocker.patch(
"stubber.commands.clone_cmd.git.switch_tag", autospec=True, return_value=0
)
mocker.patch(
"stubber.commands.clone_cmd.git.get_local_tag", autospec=True, return_value="v1.42"
)

m_match = mocker.patch("stubber.utils.repos.match_lib_with_mpy", autospec=True) # Moved to other module
m_match = mocker.patch(
"stubber.utils.repos.match_lib_with_mpy", autospec=True
) # Moved to other module

mocker.patch("stubber.commands.clone_cmd.Path.exists", return_value=True)
result = runner.invoke(stubber.stubber_cli, params)
Expand All @@ -118,12 +126,14 @@ def test_cmd_switch(mocker: MockerFixture, params: List[str]):
# core
m_match.assert_called_once()

if "latest" in params or "preview" in params:
m_switch_branch.assert_called_once()
m_switch_tag.assert_not_called()
else:
m_switch_branch.assert_not_called()
m_switch_tag.assert_called_once()
assert m_switch_branch.call_count + m_switch_tag.call_count == 1

# if "latest" in params or "preview" in params:
# m_switch_branch.assert_called_once()
# m_switch_tag.assert_not_called()
# else:
# m_switch_branch.assert_not_called()
# m_switch_tag.assert_called_once()


@pytest.mark.parametrize("version", VERSION_LIST)
Expand All @@ -134,9 +144,15 @@ def test_cmd_switch_version(mocker: MockerFixture, version: str):
m_clone = mocker.patch("stubber.commands.clone_cmd.git.clone", autospec=True, return_value=0)
m_fetch = mocker.patch("stubber.commands.clone_cmd.git.fetch", autospec=True, return_value=0)

m_switch = mocker.patch("stubber.commands.clone_cmd.git.switch_branch", autospec=True, return_value=0)
m_checkout = mocker.patch("stubber.commands.clone_cmd.git.checkout_tag", autospec=True, return_value=0)
m_get_l_tag = mocker.patch("stubber.commands.clone_cmd.git.get_local_tag", autospec=True, return_value="v1.42")
m_switch = mocker.patch(
"stubber.commands.clone_cmd.git.switch_branch", autospec=True, return_value=0
)
m_checkout = mocker.patch(
"stubber.commands.clone_cmd.git.checkout_tag", autospec=True, return_value=0
)
m_get_l_tag = mocker.patch(
"stubber.commands.clone_cmd.git.get_local_tag", autospec=True, return_value="v1.42"
)

m_match = mocker.patch("stubber.utils.repos.match_lib_with_mpy", autospec=True)

Expand Down Expand Up @@ -177,7 +193,9 @@ def test_cmd_stub(mocker: MockerFixture):

m_generate.assert_called_once_with(Path("."))
m_postprocessing.assert_called_once()
m_postprocessing.assert_called_once_with([Path(".")], stubgen=False, black=True, autoflake=False)
m_postprocessing.assert_called_once_with(
[Path(".")], stubgen=False, black=True, autoflake=False
)
assert result.exit_code == 0


Expand All @@ -189,13 +207,17 @@ def test_cmd_get_frozen(mocker: MockerFixture, tmp_path: Path):
# check basic command line sanity check
runner = CliRunner()

m_get_local_tag = mocker.patch("mpflash.basicgit.get_local_tag", autospec=True, return_value="v1.42")
m_get_local_tag = mocker.patch(
"mpflash.basicgit.get_local_tag", autospec=True, return_value="v1.42"
)

m_freeze_any = mocker.patch("stubber.commands.get_frozen_cmd.freeze_any", autospec=True)
m_post = mocker.patch("stubber.utils.do_post_processing", autospec=True)

# fake run - need to ensure that there is a destination folder
result = runner.invoke(stubber.stubber_cli, ["get-frozen", "--stub-folder", tmp_path.as_posix()])
result = runner.invoke(
stubber.stubber_cli, ["get-frozen", "--stub-folder", tmp_path.as_posix()]
)
assert result.exit_code == 0
# FIXME : test fails in CI
m_freeze_any.assert_called_once()
Expand Down Expand Up @@ -249,13 +271,17 @@ def test_cmd_get_docstubs(mocker: MockerFixture, tmp_path: Path):
# check basic command line sanity check
runner = CliRunner()

m_get_l_tag = mocker.patch("mpflash.basicgit.get_local_tag", autospec=True, return_value="v1.42")
m_get_l_tag = mocker.patch(
"mpflash.basicgit.get_local_tag", autospec=True, return_value="v1.42"
)

# from stubber.commands.get_docstubs import generate_from_rst
m_generate = mocker.patch("stubber.commands.get_docstubs_cmd.generate_from_rst", autospec=True)

# fake run
result = runner.invoke(stubber.stubber_cli, ["get-docstubs", "--stub-folder", tmp_path.as_posix()])
result = runner.invoke(
stubber.stubber_cli, ["get-docstubs", "--stub-folder", tmp_path.as_posix()]
)
assert result.exit_code == 0
# process is called
assert m_generate.call_count == 1
Expand All @@ -277,7 +303,9 @@ def test_cmd_get_docstubs(mocker: MockerFixture, tmp_path: Path):
def test_cmd_merge(mocker: MockerFixture, cmdline: List[str]):
runner = CliRunner()
# from stubber.commands.clone import git
m_merge_docstubs = mocker.patch("stubber.commands.merge_cmd.merge_all_docstubs", autospec=True, return_value={})
m_merge_docstubs = mocker.patch(
"stubber.commands.merge_cmd.merge_all_docstubs", autospec=True, return_value={}
)
result = runner.invoke(stubber.stubber_cli, cmdline)
assert result.exit_code == 0
m_merge_docstubs.assert_called_once()
Expand All @@ -297,7 +325,9 @@ def test_cmd_merge(mocker: MockerFixture, cmdline: List[str]):
def test_cmd_publish(mocker: MockerFixture, cmdline: List[str]):
runner = CliRunner()
# from stubber.commands.clone import git
m_publish_multiple = mocker.patch("stubber.commands.publish_cmd.publish_multiple", autospec=True, return_value={})
m_publish_multiple = mocker.patch(
"stubber.commands.publish_cmd.publish_multiple", autospec=True, return_value={}
)
result = runner.invoke(stubber.stubber_cli, cmdline)
assert result.exit_code == 0
m_publish_multiple.assert_called_once()
Binary file added tests/publish/data/all_packages_test.db
Binary file not shown.

0 comments on commit 44f4af4

Please sign in to comment.