Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions framework/py/flwr/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import typer
from typer.main import get_command

from flwr.supercore.update_check import warn_if_flwr_update_available
from flwr.supercore.version import package_version

from .app_cmd import publish as app_publish
Expand Down Expand Up @@ -147,6 +148,8 @@ def main(
),
) -> None:
"""Flower CLI."""
warn_if_flwr_update_available(process_name="flwr")

if version:
typer.secho(f"Flower version: {package_version}", fg="blue")
raise typer.Exit()
Expand Down
55 changes: 43 additions & 12 deletions framework/py/flwr/cli/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,40 @@
"""Tests for the CLI."""


from typing import Any
from unittest.mock import patch

import pytest
from typer.testing import CliRunner

from flwr.supercore.version import package_version

from . import app as app_module
from .app import app

runner = CliRunner()


def _invoke_flwr(args: list[str]) -> Any:
with patch("flwr.cli.app.warn_if_flwr_update_available"):
return runner.invoke(app, args)


def test_version_args() -> None:
"""Test the --version flag."""
result = runner.invoke(app, ["--version"])
result = _invoke_flwr(["--version"])
assert result.exit_code == 0
assert f"Flower version: {package_version}\n" in result.output

# Test the -V flag
result = runner.invoke(app, ["-V"])
result = _invoke_flwr(["-V"])
assert result.exit_code == 0
assert f"Flower version: {package_version}\n" in result.output


def test_help_command() -> None:
"""Test the -h flag."""
result = runner.invoke(app, ["-h"])
result = _invoke_flwr(["-h"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "Options " in result.output
Expand All @@ -47,71 +57,92 @@ def test_help_command() -> None:

def test_new_command() -> None:
"""Add appropriate assertions for the new command."""
result = runner.invoke(app, ["new", "--help"])
result = _invoke_flwr(["new", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "new" in result.output


def test_run_command() -> None:
"""Add appropriate assertions for the run command."""
result = runner.invoke(app, ["run", "--help"])
result = _invoke_flwr(["run", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "run" in result.output


def test_build_command() -> None:
"""Add appropriate assertions for the build command."""
result = runner.invoke(app, ["build", "--help"])
result = _invoke_flwr(["build", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "build" in result.output


def test_install_command() -> None:
"""Add appropriate assertions for the install command."""
result = runner.invoke(app, ["install", "--help"])
result = _invoke_flwr(["install", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "install" in result.output


def test_log_command() -> None:
"""Add appropriate assertions for the log command."""
result = runner.invoke(app, ["log", "--help"])
result = _invoke_flwr(["log", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "log" in result.output


def test_ls_command() -> None:
"""Add appropriate assertions for the ls command."""
result = runner.invoke(app, ["ls", "--help"])
result = _invoke_flwr(["ls", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "ls" in result.output


def test_stop_command() -> None:
"""Add appropriate assertions for the stop command."""
result = runner.invoke(app, ["stop", "--help"])
result = _invoke_flwr(["stop", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "stop" in result.output


def test_login_command() -> None:
"""Add appropriate assertions for the login command."""
result = runner.invoke(app, ["login", "--help"])
result = _invoke_flwr(["login", "--help"])
assert result.exit_code == 0
assert "Usage:" in result.output
assert "login" in result.output


def test_flwr_callback_checks_for_update(monkeypatch: pytest.MonkeyPatch) -> None:
"""The top-level flwr callback should perform the startup update check."""

class _SentinelError(Exception):
pass

captured: dict[str, str] = {}

def _raise_sentinel(process_name: str | None = None) -> None:
if process_name is not None:
captured["process_name"] = process_name
raise _SentinelError()

monkeypatch.setattr(app_module, "warn_if_flwr_update_available", _raise_sentinel)

with pytest.raises(_SentinelError):
app_module.main(version=False)

assert captured == {"process_name": "flwr"}


def test_invalid_command() -> None:
"""Test CLI behavior with invalid commands and arguments."""
# Test unknown command
result = runner.invoke(app, ["nonexistent-command"])
result = _invoke_flwr(["nonexistent-command"])
assert result.exit_code != 0
assert "No such command" in result.output
7 changes: 7 additions & 0 deletions framework/py/flwr/cli/local_superlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

import os
import subprocess
import time
from pathlib import Path
Expand All @@ -27,6 +28,7 @@

from flwr.common.constant import ISOLATION_MODE_SUBPROCESS
from flwr.common.grpc import create_channel
from flwr.supercore.constant import FLWR_DISABLE_UPDATE_CHECK
from flwr.supercore.utils import get_flwr_home

from .constant import (
Expand Down Expand Up @@ -126,8 +128,13 @@ def _start_local_superlink() -> None:

# Keep process detached and rely on SuperLink's file logging/rotation.
try:
env = os.environ.copy()
# NOTE: `flwr` already performs the startup update check before spawning this
# managed child process, so we disable the child-side check to avoid duplicates.
env[FLWR_DISABLE_UPDATE_CHECK] = "1"
process = subprocess.Popen( # pylint: disable=consider-using-with
command,
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
Expand Down
2 changes: 2 additions & 0 deletions framework/py/flwr/cli/local_superlink_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from unittest.mock import MagicMock, patch

from flwr.cli.typing import SuperLinkConnection, SuperLinkSimulationOptions
from flwr.supercore.constant import FLWR_DISABLE_UPDATE_CHECK

from .local_superlink import _start_local_superlink, ensure_local_superlink

Expand Down Expand Up @@ -111,3 +112,4 @@ def test_start_local_superlink_uses_builtin_log_rotation(tmp_path: Path) -> None
assert "7" in cmd
assert popen.call_args.kwargs["stdout"] is subprocess.DEVNULL
assert popen.call_args.kwargs["stderr"] is subprocess.DEVNULL
assert popen.call_args.kwargs["env"][FLWR_DISABLE_UPDATE_CHECK] == "1"
Loading