Skip to content

Commit

Permalink
[App] Fix app unit tests (#18262)
Browse files Browse the repository at this point in the history
(cherry picked from commit e33816c)
  • Loading branch information
ethanwharris authored and lexierule committed Aug 14, 2023
1 parent c52e78c commit cbf53ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
8 changes: 2 additions & 6 deletions src/lightning/app/utilities/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def check_github_repository(cwd=None) -> bool:


def get_git_relative_path(file: Union[str, Path]) -> str:
"""Finds the relative path of the file to the git root."""
if not check_github_repository():
raise ValueError("Not a GitHub repository.")
""" Finds the relative path of the file to the git root. """
abs_path = Path(file).absolute()
repository_path = execute_git_command(["rev-parse", "--show-toplevel"])
return str(abs_path.relative_to(repository_path))
Expand All @@ -71,11 +71,7 @@ def check_if_remote_head_is_different() -> Union[bool, None]:
if any("fatal" in f for f in (local_sha, remote_sha, base_sha)):
return None

is_different = True
if local_sha in (remote_sha, base_sha):
is_different = False

return is_different
return local_sha not in (remote_sha, base_sha)


def has_uncommitted_files() -> bool:
Expand Down
5 changes: 5 additions & 0 deletions tests/tests_app/utilities/packaging/test_lightning_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lightning_utilities.core.imports import module_available

from lightning.app.testing.helpers import _RunIf
from lightning.app.utilities.git import check_github_repository, get_dir_name
from lightning.app.utilities.packaging import lightning_utils
from lightning.app.utilities.packaging.lightning_utils import (
_prepare_lightning_wheels_and_requirements,
Expand All @@ -21,6 +22,10 @@ def test_prepare_lightning_wheels_and_requirement(tmpdir):
if not get_dist_path_if_editable_install(package_name):
pytest.skip("Requires --editable install")

git_dir_name = get_dir_name() if check_github_repository() else None
if git_dir_name != package_name:
pytest.skip("Needs to be run from within the repo")

cleanup_handle = _prepare_lightning_wheels_and_requirements(tmpdir, package_name=package_name)
assert len(os.listdir(tmpdir)) == 1
assert len(glob.glob(str(tmpdir / "lightning-*.tar.gz"))) == 1
Expand Down
35 changes: 25 additions & 10 deletions tests/tests_app/utilities/test_git.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import os
import sys

import pytest
from unittest.mock import patch

from lightning.app.utilities.git import (
check_github_repository,
check_if_remote_head_is_different,
execute_git_command,
get_dir_name,
get_git_relative_path,
has_uncommitted_files,
)


@pytest.mark.skipif(sys.platform == "win32", reason="Don't run on windows")
def test_execute_git_command():
res = execute_git_command(["pull"])
assert res
def mock_execute_git_command(args, cwd=None) -> str:
if args == ["config", "--get", "remote.origin.url"]:
return "https://github.com/Lightning-AI/lightning.git"

if args == ["rev-parse", "--show-toplevel"]:
return os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

if args == ["update-index", "--refresh"]:
return ""

if args == ["rev-parse", "@"]:
return "local-sha"

if args == ["rev-parse", r"@{u}"] or args == ["merge-base", "@", r"@{u}"]:
return "remote-sha"

return "Error: Unexpected call"


@patch("lightning.app.utilities.git.execute_git_command", mock_execute_git_command)
def test_execute_git_command():
assert get_dir_name() == "lightning"

assert check_github_repository()
Expand All @@ -26,6 +41,6 @@ def test_execute_git_command():
else:
assert get_git_relative_path(__file__) == "tests/tests_app/utilities/test_git.py"

# this commands can be either True or False based on dev.
check_if_remote_head_is_different()
has_uncommitted_files()
assert check_if_remote_head_is_different()

assert not has_uncommitted_files()

0 comments on commit cbf53ca

Please sign in to comment.