Skip to content

Commit

Permalink
CI: unify and test all os (#115)
Browse files Browse the repository at this point in the history
* CI: unify and test all os

* fix package name

* fix location of environment file

* remove another mesmer artifact

* min-deps-check: fix filename

* remove all flag

* fix macosx backend check

* remove unused pytest-env

* add colors try not verbose

* not unbuffered

* remove pytest.ini

* get config

* move config to pyproject.toml

* undo show options

* try no options

* set options again

* cannot remove env var :-(

* fix tests

* fix expected_size

* next_try

* set dpi

* fix last test

* Apply suggestions from code review
  • Loading branch information
mathause authored May 30, 2024
1 parent 1e341b2 commit 73bca9d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 113 deletions.
55 changes: 1 addition & 54 deletions .github/workflows/ci-additional.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,6 @@ concurrency:
cancel-in-progress: true

jobs:
test:
name: py${{ matrix.python-version }}-${{ matrix.env }}
runs-on: "ubuntu-latest"
defaults:
run:
shell: bash -l {0}
strategy:
fail-fast: false
matrix:
include:
- env: "min-all-deps"
python-version: "3.9"
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags.

- name: Set environment variables
run: |
PY=${{ matrix.python-version }}
# replace "." in matrix.python-version
PY=${PY//./}
echo "CONDA_ENV_FILE=ci/requirements/py${PY}-${{ matrix.env }}.yml" >> $GITHUB_ENV
- name: Create conda environment
uses: mamba-org/setup-micromamba@v1
with:
environment-file: ${{ env.CONDA_ENV_FILE }}
cache-downloads: true
cache-downloads-key: "${{runner.os}}-${{runner.arch}}-py${{matrix.python-version}}"
create-args: >-
python=${{ matrix.python-version }}
- name: Install mplotutils
run: |
python -m pip install --no-deps -e .
- name: Import mplotutils
run: |
python -c "import mplotutils"
- name: Run tests
timeout-minutes: 15
run: python -u -m pytest
--cov=mplotutils
--cov-report=xml
$PYTEST_EXTRA_FLAGS

- name: Upload code coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
flags: unittests,${{ matrix.env }}
env_vars: RUNNER_OS
name: codecov-umbrella
fail_ci_if_error: false

min-version-policy:
name: Minimum Version Policy
runs-on: "ubuntu-latest"
Expand Down Expand Up @@ -94,4 +41,4 @@ jobs:
- name: minimum versions policy
run: |
python ci/min_deps_check.py ci/requirements/py39-min-all-deps.yml
python ci/min_deps_check.py ci/requirements/min-all-deps.yml
86 changes: 51 additions & 35 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,67 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
FORCE_COLOR: 3

jobs:
test:
name: py${{ matrix.python-version }}
name: py${{ matrix.python-version }} ${{ matrix.os }} ${{ matrix.env }}
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash -l {0}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
# Bookend python versions
python-version: ["3.9", "3.11", "3.12"]
env: [""]
include:
- env: "min-all-deps"
python-version: "3.9"
os: "ubuntu-latest"
- env: ""
python-version: "3.12"
os: "macos-latest"
defaults:
run:
shell: bash -l {0}

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags.
- name: Set environment variables
run: |
- name: Checkout repository
uses: actions/checkout@v4

- name: Set environment variables
run: |
if [[ "${{ matrix.env }}" == "" ]]; then
echo "CONDA_ENV_FILE=ci/requirements/environment.yml" >> $GITHUB_ENV
echo "PYTHON_VERSION=${{ matrix.python-version }}" >> $GITHUB_ENV
- name: Create conda environment
uses: mamba-org/setup-micromamba@v1
with:
environment-file: ci/requirements/environment.yml
cache-downloads: true
cache-downloads-key: "${{runner.os}}-${{runner.arch}}-py${{matrix.python-version}}"
create-args: >-
python=${{ matrix.python-version }}
- name: Install mplotutils
run: |
python -m pip install --no-deps -e .
- name: Import mplotutils
run: |
python -c "import mplotutils"
- name: Run tests
timeout-minutes: 5
run: python -u -m pytest
--cov=mplotutils
--cov-report=xml
--junitxml=test-results/${{ runner.os }}-${{ matrix.python-version }}.xml
else
echo "CONDA_ENV_FILE=ci/requirements/${{ matrix.env }}.yml" >> $GITHUB_ENV
fi
- name: Create conda environment
uses: mamba-org/setup-micromamba@v1
with:
environment-name: mplotutils-tests
cache-downloads: true
cache-downloads-key: "${{runner.os}}-${{runner.arch}}-py${{matrix.python-version}}-${{ env.CONDA_ENV_FILE }}"
micromamba-version: 'latest'
environment-file: ${{ env.CONDA_ENV_FILE }}
create-args: >-
python=${{ matrix.python-version }}
- name: Upload code coverage to Codecov
uses: codecov/codecov-action@v4
with:
- name: Install mplotutils
run: |
python -m pip install --no-deps -e .
- name: Run tests
run: python -m pytest
-ra -v
--cov=mplotutils
--cov-report=xml

- name: Upload code coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
flags: unittests
env_vars: RUNNER_OS,PYTHON_VERSION
Expand All @@ -67,7 +83,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.11"]
python-version: ["3.12"]

steps:
- uses: actions/checkout@v4
Expand Down
1 change: 0 additions & 1 deletion ci/requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ dependencies:
# for testing
- pytest
- pytest-cov
- pytest-env
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ dependencies:
# for testing
- pytest
- pytest-cov
- pytest-env
63 changes: 47 additions & 16 deletions mplotutils/tests/test_set_map_layout.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import matplotlib.pyplot as plt
import numpy as np
import pytest

from mplotutils import set_map_layout

from . import subplots_context
from . import figure_context, subplots_context


def get_rtol(f):
# macosx is only exact up to 1 / dpi

if plt.get_backend().lower() != "macosx":
rtol = 1e-07
else:
rtol = 1 / f.get_dpi()
return rtol


def test_set_map_layout_default_width():
with subplots_context() as (f, ax):
set_map_layout(ax)

assert f.get_size_inches()[0] * 2.54 == 17.0
width = f.get_size_inches()[0] * 2.54
np.testing.assert_allclose(width, 17.0, rtol=get_rtol(f))


@pytest.mark.parametrize(
Expand All @@ -25,7 +37,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
set_map_layout(ax, 10, **nrow_ncol)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 10))
np.testing.assert_allclose((width, height), (10, 10), rtol=get_rtol(f))

# width:height = 2:1
with subplots_context() as (f, ax):
Expand All @@ -35,7 +47,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
set_map_layout(ax, 10, **nrow_ncol)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 5))
np.testing.assert_allclose((width, height), (10, 5), rtol=get_rtol(f))

# width:height = 1:2
with subplots_context() as (f, ax):
Expand All @@ -45,7 +57,7 @@ def test_set_map_layout_no_borders(nrow_ncol):
set_map_layout(ax, 10, **nrow_ncol)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 20))
np.testing.assert_allclose((width, height), (10, 20), rtol=get_rtol(f))


@pytest.mark.parametrize("ax_to_arr", (lambda ax: [ax], lambda ax: np.array(ax)))
Expand All @@ -58,7 +70,7 @@ def test_set_map_layout_ax_arr(ax_to_arr):
set_map_layout(ax_to_arr(ax), 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 10))
np.testing.assert_allclose((width, height), (10, 10), rtol=get_rtol(f))


def test_set_map_layout_vert_borders():
Expand All @@ -72,7 +84,7 @@ def test_set_map_layout_vert_borders():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 20))
np.testing.assert_allclose((width, height), (10, 20), rtol=get_rtol(f))

# width:height = 1:1
with subplots_context() as (f, ax):
Expand All @@ -84,7 +96,7 @@ def test_set_map_layout_vert_borders():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 12.5))
np.testing.assert_allclose((width, height), (10, 12.5), rtol=get_rtol(f))

# width:height = 1:1
with subplots_context() as (f, ax):
Expand All @@ -96,7 +108,7 @@ def test_set_map_layout_vert_borders():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 40))
np.testing.assert_allclose((width, height), (10, 40), rtol=get_rtol(f))


def test_set_map_layout_horz_borders():
Expand All @@ -110,7 +122,7 @@ def test_set_map_layout_horz_borders():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 5))
np.testing.assert_allclose((width, height), (10, 5), rtol=get_rtol(f))

# width:height = 1:1
with subplots_context() as (f, ax):
Expand All @@ -122,7 +134,7 @@ def test_set_map_layout_horz_borders():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 5))
np.testing.assert_allclose((width, height), (10, 5), rtol=get_rtol(f))


def test_set_map_layout_two_axes_vert():
Expand All @@ -137,7 +149,7 @@ def test_set_map_layout_two_axes_vert():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 20))
np.testing.assert_allclose((width, height), (10, 20), rtol=get_rtol(f))

# width:height = 1:1
with subplots_context(2, 1) as (f, axs):
Expand All @@ -150,7 +162,7 @@ def test_set_map_layout_two_axes_vert():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 30))
np.testing.assert_allclose((width, height), (10, 30), rtol=get_rtol(f))


def test_set_map_layout_two_axes_horz():
Expand All @@ -165,7 +177,7 @@ def test_set_map_layout_two_axes_horz():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 5))
np.testing.assert_allclose((width, height), (10, 5), rtol=get_rtol(f))

# width:height = 1:1
with subplots_context(1, 2) as (f, axs):
Expand All @@ -178,7 +190,7 @@ def test_set_map_layout_two_axes_horz():
set_map_layout(ax, 10)

width, height = f.get_size_inches() * 2.54
assert np.allclose((width, height), (10, 10 / 3))
np.testing.assert_allclose((width, height), (10, 10 / 3), rtol=get_rtol(f))


def test_set_map_layout_nrow_ncol_only_one_raises():
Expand All @@ -201,4 +213,23 @@ def test_set_map_layout_cartopy_2_2():
result = f.get_size_inches() * 2.54
expected = (17, 8.5)

np.testing.assert_allclose(result, expected)
np.testing.assert_allclose(result, expected, rtol=get_rtol(f))


@pytest.mark.skipif(plt.get_backend().lower() != "macosx", reason="only for macosx")
@pytest.mark.parametrize("dpi", (100, 1000))
@pytest.mark.parametrize("size", ([17, 6], [10, 5]))
def test_set_size_inches_macosx(dpi, size):

with figure_context() as f:

f.set_dpi(dpi)

size = np.array(size)

f.set_size_inches(size / 2.54)

result = f.get_size_inches() * 2.54

expected_size = np.floor(size / 2.54 * dpi) / dpi * 2.54
np.testing.assert_allclose(result, expected_size)
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ build-backend = "setuptools.build_meta"
[tool.setuptools_scm]
fallback_version = "999"
version_scheme = "no-guess-dev"

[tool.pytest.ini_options]

log_cli_level = "INFO"

filterwarnings = [
"ignore:numpy.ufunc size changed, may indicate binary incompatibility.:RuntimeWarning"
]
2 changes: 0 additions & 2 deletions pytest.ini

This file was deleted.

4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ install_requires =
numpy >=1.22
xarray >=2022.12

[tool:pytest]
filterwarnings =
ignore:numpy.ufunc size changed, may indicate binary incompatibility.:RuntimeWarning

[flake8]
ignore=
# E203: whitespace before ':' - doesn't work well with black
Expand Down

0 comments on commit 73bca9d

Please sign in to comment.