Skip to content

Commit

Permalink
dev(hansbug): add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Jul 20, 2024
1 parent 03850d6 commit 24e9a2d
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,18 @@ jobs:
with:
fetch-depth: 20
submodules: 'recursive'
- name: Set up system dependences on Linux
- name: Set up system dependencies on Linux
if: ${{ env.OS_NAME == 'Linux' }}
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y tree cloc wget curl make zip
- name: Set up system dependences on Windows
- name: Set up system dependencies on Windows
if: ${{ env.OS_NAME == 'Windows' }}
shell: bash
run: |
choco install tree cloc wget curl make zip
- name: Set up system dependences on MacOS
- name: Set up system dependencies on MacOS
if: ${{ env.OS_NAME == 'MacOS' }}
run: |
brew install tree cloc wget curl make zip
Expand Down
6 changes: 2 additions & 4 deletions hbutils/system/git/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@lru_cache()
def _raw_check_git(git_path: str):
git_info = {}
if git_path:
if git_path and os.path.exists(git_path):
git_info['exec'] = git_path
git_info['installed'] = True
try:
Expand Down Expand Up @@ -44,13 +44,11 @@ def _raw_check_git(git_path: str):
git_lfs_info['version'] = None
except subprocess.CalledProcessError:
git_lfs_info['installed'] = False
git_lfs_info['version'] = None
git_lfs_info['version_info'] = None

return git_info


def check_git(git_path: Optional[str] = None):
def git_info(git_path: Optional[str] = None):
git_path = git_path or shutil.which('git') or None
if git_path:
git_path = os.path.normcase(os.path.normpath(git_path))
Expand Down
1 change: 1 addition & 0 deletions hbutils/testing/requires/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
"""
from .cmd import *
from .expr import *
from .git import *
40 changes: 40 additions & 0 deletions hbutils/testing/requires/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Optional

from .version import VersionInfo
from ...system.git.info import git_info

__all__ = [
'is_git_installed',
'git_version',
'is_git_lfs_installed',
'git_lfs_version',
]


def is_git_installed(git_path: Optional[str] = None) -> bool:
return git_info(git_path=git_path)['installed']


def git_version(git_path: Optional[str] = None) -> Optional[VersionInfo]:
# note that return value of this function is not guaranteed to be non-None when git is installed
# when git --version output unrecognizable value, this func can also return None
info = git_info(git_path=git_path)
if info['installed'] and info['version']:
return VersionInfo(info['version'])
else:
return None


def is_git_lfs_installed(git_path: Optional[str] = None) -> bool:
info = git_info(git_path=git_path)
return bool(info['installed'] and info['lfs']['installed'])


def git_lfs_version(git_path: Optional[str] = None) -> Optional[VersionInfo]:
# note that return value of this function is not guaranteed to be non-None when git lfs is installed
# when git lfs version output unrecognizable value, this func can also return None
info = git_info(git_path=git_path)
if info['installed'] and info['lfs']['installed'] and info['lfs']['version']:
return VersionInfo(info['lfs']['version'])
else:
return None
Empty file added test/system/git/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions test/system/git/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import shutil

_GIT_LFS = shutil.which('git_lfs', path='dist')
_GIT_RAW = shutil.which('git_raw', path='dist')
55 changes: 55 additions & 0 deletions test/system/git/test_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import shutil
from unittest import skipUnless

import pytest

from hbutils.system.git.info import git_info
from hbutils.testing import isolated_directory
from .conftest import _GIT_LFS, _GIT_RAW


@pytest.mark.unittest
class TestSystemGitInfo:
@skipUnless(shutil.which('git'), 'Git required.')
def test_git_installed(self):
assert git_info()['installed']

@skipUnless(not shutil.which('git'), 'No git required.')
def test_git_not_installed(self):
assert not git_info()['installed']

def test_git_not_installed_tmp(self):
with isolated_directory():
assert git_info('git') == {
'exec': None,
'installed': False,
}


@pytest.mark.unittest
@skipUnless(_GIT_LFS, 'Pre-compiled git_lfs required')
class TestSystemGitInfoLFS:
def test_git_info(self):
assert git_info(_GIT_LFS) == {
'exec': _GIT_LFS,
'installed': True,
'lfs': {
'installed': True,
'version': '2.13.3',
'version_info': 'git-lfs/2.13.3 (GitHub; linux amd64; go 1.16.2)'},
'version': '2.30.0',
'version_info': 'git version 2.30.0'
}


@pytest.mark.unittest
@skipUnless(_GIT_RAW, 'Pre-compiled git_lfs required')
class TestSystemGitInfoLFS:
def test_git_info(self):
assert git_info(_GIT_RAW) == {
'exec': _GIT_RAW,
'installed': True,
'lfs': {'installed': False},
'version': '2.28.0',
'version_info': 'git version 2.28.0'
}
4 changes: 4 additions & 0 deletions test/testing/requires/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import shutil

_GIT_LFS = shutil.which('git_lfs', path='dist')
_GIT_RAW = shutil.which('git_raw', path='dist')
58 changes: 58 additions & 0 deletions test/testing/requires/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import shutil
from unittest import skipUnless

import pytest

from hbutils.testing import isolated_directory
from hbutils.testing.requires import is_git_installed, is_git_lfs_installed, git_version, git_lfs_version
from .conftest import _GIT_LFS, _GIT_RAW


@pytest.mark.unittest
class TestTestingRequiresGitNative:
@skipUnless(shutil.which('git'), 'Git required.')
def test_is_git_installed(self):
assert is_git_installed()

@skipUnless(not shutil.which('git'), 'No git required.')
def test_is_git_installed_not_installed(self):
assert not is_git_installed()

def test_is_git_installed_negative(self):
with isolated_directory():
assert not is_git_installed('git')
assert git_version('git') is None
assert not is_git_lfs_installed('git')
assert git_lfs_version('git') is None


@pytest.mark.unittest
@skipUnless(_GIT_LFS, 'Pre-compiled git_lfs required')
class TestTestingRequiresGitLFS:
def test_is_git_installed(self):
assert is_git_installed(_GIT_LFS)

def test_git_version(self):
assert git_version(_GIT_LFS) == '2.30.0'

def test_is_git_lfs_installed(self):
assert is_git_lfs_installed(_GIT_LFS)

def test_git_lfs_version(self):
assert git_lfs_version(_GIT_LFS) == '2.13.3'


@pytest.mark.unittest
@skipUnless(_GIT_RAW, 'Pre-compiled git_raw required')
class TestTestingRequiresGitRAW:
def test_is_git_installed(self):
assert is_git_installed(_GIT_RAW)

def test_git_version(self):
assert git_version(_GIT_RAW) == '2.28.0'

def test_is_git_lfs_installed(self):
assert not is_git_lfs_installed(_GIT_RAW)

def test_git_lfs_version(self):
assert git_lfs_version(_GIT_RAW) is None

0 comments on commit 24e9a2d

Please sign in to comment.