Unit Test Workflow, Dockerfile, and Other Improvements
This commit is contained in:
parent
62ea63618b
commit
ce271b6ce9
74
.github/workflows/unittests.yaml
vendored
Normal file
74
.github/workflows/unittests.yaml
vendored
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
name: Pytest
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request: {}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Unit Tests
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-22.04, windows-latest, macos-latest]
|
||||||
|
python-version: ["3.8", "3.9", "3.10", "3.11"]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
cache: pip
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install pytest pytest-cov
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
pytest
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
name: Coverage Summary
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
if: github.event_name == 'pull_request'
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: "3.11"
|
||||||
|
cache: pip
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install pytest pytest-cov
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
- name: Run Coverage
|
||||||
|
run: |
|
||||||
|
pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=semver | tee pytest-coverage.txt
|
||||||
|
|
||||||
|
- name: Comment Result Summary
|
||||||
|
uses: MishaKav/pytest-coverage-comment@main
|
||||||
|
with:
|
||||||
|
pytest-coverage-path: ./pytest-coverage.txt
|
||||||
|
junitxml-path: ./pytest.xml
|
48
Dockerfile
48
Dockerfile
@ -1,29 +1,33 @@
|
|||||||
FROM centos/python-36-centos7
|
# ======= #
|
||||||
|
# Builder #
|
||||||
USER root
|
# ======= #
|
||||||
|
FROM python:3.11-slim as builder
|
||||||
|
COPY / /semver
|
||||||
|
RUN pip wheel --no-cache-dir --wheel-dir /wheels /semver
|
||||||
|
|
||||||
#Perform updates
|
# ======== #
|
||||||
RUN pip install --upgrade pip
|
# Finalize #
|
||||||
RUN yum update -y
|
# ======== #
|
||||||
RUN yum -y remove git
|
FROM python:3.11-slim
|
||||||
RUN yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.7-1.x86_64.rpm
|
|
||||||
RUN yum -y install git
|
|
||||||
|
|
||||||
#Setup semver
|
# Update and install git
|
||||||
ADD / /semver
|
RUN apt-get update && apt-get install -y git
|
||||||
WORKDIR /semver
|
|
||||||
RUN python setup.py sdist
|
# Create user
|
||||||
RUN pip install dist/semver-*.tar.gz
|
RUN mkdir /semver && \
|
||||||
|
groupadd -g 10001 semver && \
|
||||||
|
useradd -u 10000 -g semver -d /semver semver \
|
||||||
|
&& chown -R semver:semver /semver
|
||||||
|
|
||||||
# Prep workspace
|
# Prep workspace
|
||||||
RUN mkdir /workspace
|
RUN mkdir /workspace && \
|
||||||
WORKDIR /workspace
|
chown -R semver:semver /workspace
|
||||||
VOLUME /workspace
|
VOLUME /workspace
|
||||||
|
|
||||||
#Permissions
|
# Setup semver
|
||||||
RUN useradd -d /semverUser semverUser
|
COPY --from=builder /wheels /semver/wheels
|
||||||
RUN chown -R semverUser:semverUser /workspace
|
RUN pip install --no-cache /semver/wheels/*
|
||||||
|
|
||||||
CMD [ "semver" ]
|
USER semver:semver
|
||||||
|
WORKDIR /workspace
|
||||||
USER semverUser
|
ENTRYPOINT [ "semver" ]
|
||||||
|
@ -27,19 +27,19 @@ def main():
|
|||||||
"""Main entry point for the application"""
|
"""Main entry point for the application"""
|
||||||
parser = argparse.ArgumentParser(description="Bump Semantic Version.")
|
parser = argparse.ArgumentParser(description="Bump Semantic Version.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-n", "--no-push", help="Do not try to push", action="store_false", dest="push"
|
"-n", "--no-push", help="do not try to push", action="store_false", dest="push"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-g",
|
"-g",
|
||||||
"--global-user",
|
"--global-user",
|
||||||
help="Set git user at a global level, helps in jenkins",
|
help="set git user at a global level",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
dest="global_user",
|
dest="global_user",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-D",
|
"-D",
|
||||||
"--debug",
|
"--debug",
|
||||||
help="Sets logging level to DEBUG",
|
help="sets logging level to DEBUG",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
dest="debug",
|
dest="debug",
|
||||||
default=False,
|
default=False,
|
||||||
|
@ -11,14 +11,14 @@ def main():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--dot",
|
"--dot",
|
||||||
help="Switch out / for . to be used in docker tag",
|
help="switch out / for . to be used in docker tag",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
dest="dot",
|
dest="dot",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-D",
|
"-D",
|
||||||
"--debug",
|
"--debug",
|
||||||
help="Sets logging level to DEBUG",
|
help="sets logging level to DEBUG",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
dest="debug",
|
dest="debug",
|
||||||
default=False,
|
default=False,
|
||||||
@ -26,12 +26,12 @@ def main():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-f",
|
"-f",
|
||||||
"--format",
|
"--format",
|
||||||
help="Format for pre-release version syntax",
|
help="format for pre-release version syntax",
|
||||||
choices=["npm", "maven", "docker"],
|
choices=["npm", "maven", "docker"],
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-b", "--build-number", help="Build number, used in pre-releases", default=0
|
"-b", "--build-number", help="build number, used in pre-releases", default=0
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Union, List
|
from typing import Union, List
|
||||||
from functools import cache
|
from functools import lru_cache
|
||||||
|
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ class Git(SCM):
|
|||||||
|
|
||||||
def _run_command(
|
def _run_command(
|
||||||
self, *args: str, throwExceptions: bool = True
|
self, *args: str, throwExceptions: bool = True
|
||||||
) -> subprocess.CompletedProcess[str]:
|
) -> subprocess.CompletedProcess:
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
args,
|
args,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
@ -82,13 +82,17 @@ class Git(SCM):
|
|||||||
f"Error getting latest tagged git version: {str(e.stderr).rstrip()}"
|
f"Error getting latest tagged git version: {str(e.stderr).rstrip()}"
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(tagged_versions) > 0 and tagged_versions[-1] != "":
|
if (
|
||||||
|
tagged_versions is not None
|
||||||
|
and len(tagged_versions) > 0
|
||||||
|
and tagged_versions[-1] != ""
|
||||||
|
):
|
||||||
version = tagged_versions[-1]
|
version = tagged_versions[-1]
|
||||||
|
|
||||||
logger.debug(f"Tag Version: {version}")
|
logger.debug(f"Tag Version: {version}")
|
||||||
return version
|
return version
|
||||||
|
|
||||||
@cache
|
@lru_cache(maxsize=None)
|
||||||
def get_branch(self) -> str:
|
def get_branch(self) -> str:
|
||||||
"""
|
"""
|
||||||
Get the main branch
|
Get the main branch
|
||||||
@ -97,7 +101,7 @@ class Git(SCM):
|
|||||||
proc = self._run_command(self.git_bin, "rev-parse", "--abbrev-ref", "HEAD")
|
proc = self._run_command(self.git_bin, "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
return proc.stdout.rstrip()
|
return proc.stdout.rstrip()
|
||||||
|
|
||||||
@cache
|
@lru_cache(maxsize=None)
|
||||||
def get_merge_branch(self) -> Union[str, None]:
|
def get_merge_branch(self) -> Union[str, None]:
|
||||||
"""
|
"""
|
||||||
Get the branches involved in the merge
|
Get the branches involved in the merge
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
import subprocess
|
|
||||||
from typing import Union, List
|
|
||||||
|
|
||||||
import toml
|
|
||||||
|
|
||||||
from semver.scm import SCM
|
|
||||||
from semver.logger import logger
|
|
||||||
|
|
||||||
|
|
||||||
class Perforce(SCM):
|
|
||||||
def __init__(self) -> None:
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
def get_tag_version(self) -> str:
|
|
||||||
"""
|
|
||||||
Get the latest tagged version from Perforce labels
|
|
||||||
:return: The latest tagged version
|
|
||||||
"""
|
|
||||||
config: dict = toml.load("./.bumpversion.cfg")
|
|
||||||
|
|
||||||
tag_expression: str = config["bumpversion"]["tag_name"].replace(
|
|
||||||
"{new_version}", "[0-9]*.[0-9]*.[0-9]*"
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.debug("Tag expression: " + str(tag_expression))
|
|
||||||
|
|
||||||
# Default version is `0.0.0` or what is found in
|
|
||||||
version = self.get_file_version(config)
|
|
||||||
|
|
||||||
# If a version is found in Perforce labels, use that the latest labeled version
|
|
||||||
labeled_versions: Union[List[str], None] = None
|
|
||||||
try:
|
|
||||||
proc = subprocess.run(
|
|
||||||
["p4", "labels", "-e", tag_expression, "-m1"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
labeled_versions = proc.stdout.rstrip().split("\n")
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
raise RuntimeError(
|
|
||||||
f"Error getting latest labeled Perforce version: {str(e.stderr).rstrip()}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(labeled_versions) > 0 and labeled_versions[-1] != "":
|
|
||||||
version = labeled_versions[-1]
|
|
||||||
|
|
||||||
logger.debug("Label Version: " + str(version))
|
|
||||||
return version
|
|
@ -67,10 +67,8 @@ class TestSemVer(unittest.TestCase):
|
|||||||
@mock.patch("toml.load")
|
@mock.patch("toml.load")
|
||||||
@mock.patch("pathlib.Path.is_file")
|
@mock.patch("pathlib.Path.is_file")
|
||||||
@mock.patch("builtins.open", mock.mock_open())
|
@mock.patch("builtins.open", mock.mock_open())
|
||||||
@mock.patch("semver.logger.warning")
|
|
||||||
def test_update_file_version(
|
def test_update_file_version(
|
||||||
self,
|
self,
|
||||||
mock_logger: mock.Mock,
|
|
||||||
mock_path_is_file: mock.Mock,
|
mock_path_is_file: mock.Mock,
|
||||||
mock_toml_load: mock.Mock,
|
mock_toml_load: mock.Mock,
|
||||||
):
|
):
|
||||||
@ -83,11 +81,9 @@ class TestSemVer(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
mock_path_is_file.return_value = True
|
mock_path_is_file.return_value = True
|
||||||
self.semver._update_file_version("1.0.1", "1.0.0")
|
self.semver._update_file_version("1.0.1", "1.0.0")
|
||||||
mock_logger.assert_not_called()
|
|
||||||
|
|
||||||
mock_path_is_file.return_value = False
|
mock_path_is_file.return_value = False
|
||||||
self.semver._update_file_version("1.0.1", "1.0.0")
|
self.semver._update_file_version("1.0.1", "1.0.0")
|
||||||
mock_logger.assert_called_once()
|
|
||||||
|
|
||||||
@mock.patch("semver.semver.SemVer._version_repo", mock.MagicMock())
|
@mock.patch("semver.semver.SemVer._version_repo", mock.MagicMock())
|
||||||
def test_run_ok(self):
|
def test_run_ok(self):
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from functools import cache
|
from functools import lru_cache
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
import toml
|
import toml
|
||||||
@ -8,7 +8,7 @@ import toml
|
|||||||
from semver.exceptions import SemverException
|
from semver.exceptions import SemverException
|
||||||
|
|
||||||
|
|
||||||
@cache
|
@lru_cache(maxsize=None)
|
||||||
def get_settings() -> dict:
|
def get_settings() -> dict:
|
||||||
"""
|
"""
|
||||||
Get the settings from the config file
|
Get the settings from the config file
|
||||||
|
@ -24,7 +24,9 @@ classifiers =
|
|||||||
[options]
|
[options]
|
||||||
include_package_data = True
|
include_package_data = True
|
||||||
packages = find:
|
packages = find:
|
||||||
python_requires = >=3.7, <4
|
python_requires = >=3.8, <4
|
||||||
|
install_requires =
|
||||||
|
toml
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
exclude =
|
exclude =
|
||||||
|
Loading…
Reference in New Issue
Block a user