Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting and formatting with Ruff #56

Merged
merged 7 commits into from
Sep 11, 2024
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
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[run]
source = cotainr
omit = cotainr/tests/*
branch = True
branch = True
9 changes: 9 additions & 0 deletions .github/workflows/CI_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ on:
schedule:
- cron: '15 2 * * TUE' # Also run the full test suite for PRs on the main branch every Tuesday night
jobs:
lint-and-format:
name: Lint and check formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- uses: pre-commit/[email protected]
Chroxvi marked this conversation as resolved.
Show resolved Hide resolved
run-full-test-suite:
name: Run full test suite
runs-on: ${{ matrix.os }}
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/CI_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ on:
tags-ignore:
- "**"
jobs:
lint-and-format:
name: Lint and check formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- uses: pre-commit/[email protected]
run-unittests:
name: Run unittests
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ dmypy.json
# Sphinx
_build/

systems.json
systems.json
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.4
hooks:
- id: ruff
args:
- --fix
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,4 @@ the rights granted in Article 2 of this Licence and protect the covered Source
Code from exclusive appropriation.

All other changes or additions to this Appendix require the production of a new
EUPL version.
EUPL version.
2 changes: 1 addition & 1 deletion cotainr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
class CotainrSubcommand(ABC):
"""Abstract base class for `CotainrCLI` subcommands."""

@classmethod
@classmethod # noqa: B027
akx marked this conversation as resolved.
Show resolved Hide resolved
def add_arguments(cls, *, parser):
"""
Add command line arguments to arguments `parser`.
Expand Down
4 changes: 1 addition & 3 deletions cotainr/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ def _download_miniforge_installer(self, *, installer_path):
# Make up to 3 attempts at downloading the installer
for retry in range(3):
try:
with urllib.request.urlopen(
miniforge_installer_url
) as url: # nosec B310
with urllib.request.urlopen(miniforge_installer_url) as url: # nosec B310
installer_path.write_bytes(url.read())

break
Expand Down
8 changes: 3 additions & 5 deletions cotainr/tests/cli/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_specifying_both_system_and_base_image(self, capsys):
Build.add_arguments(parser=parser)

with pytest.raises(SystemExit):
args = parser.parse_args(
parser.parse_args(
args=shlex.split(
f"{image_path} --system {system} --base-image {base_image}"
)
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_specifying_both_verbose_and_quiet(self, capsys):
base_image = "some_base_image_6021"

with pytest.raises(SystemExit):
args = parser.parse_args(
parser.parse_args(
args=shlex.split(
f"{image_path} --base-image={base_image} --verbose --quiet"
)
Expand Down Expand Up @@ -377,9 +377,7 @@ def test_include_conda_env(
conda_env_create_cmd,
conda_clean_cmd,
sandbox_build_cmd,
) = (
capsys.readouterr().out.strip().split("\n")
)
) = capsys.readouterr().out.strip().split("\n")
assert sandbox_create_cmd.startswith("PATCH: Ran command in sandbox:")
assert all(
s in sandbox_create_cmd
Expand Down
5 changes: 4 additions & 1 deletion cotainr/tests/cli/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ def test_description_whitespace_strip(self):
Parameters
----------
some_arg : str
Much leading and trailing space
Much leading and trailing space
"""
# Doctor the docstring a little here, so editors etc. which eagerly
# strip whitespace don't mess up the test.
docstring = docstring.replace("space", "space" + " " * 10)
help_msg = _extract_help_from_docstring(arg="some_arg", docstring=docstring)
assert help_msg == "much leading and trailing space"

Expand Down
16 changes: 12 additions & 4 deletions cotainr/tests/cli/test_cotainr_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import itertools
import logging
import re
Expand Down Expand Up @@ -138,14 +139,17 @@ def test_missing_subcommand(self, argparse_options_line, capsys):
argparse_options_line=argparse_options_line
)


class TestVersionMessage:
def test_main_version(self, capsys):
from cotainr import __version__ as _cotainr_version

with pytest.raises(SystemExit):
CotainrCLI(args=["--version"])
stdout = capsys.readouterr().out
assert stdout == f"cotainr {_cotainr_version}\n"


class Test_SetupCotainrCLILogging:
@pytest.mark.parametrize("verbosity", [-1, -2, -3, -5, -1000])
def test_cotainr_critical_logging(
Expand Down Expand Up @@ -184,7 +188,8 @@ def test_cotainr_critical_logging(
assert isinstance(handler, logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down Expand Up @@ -225,7 +230,8 @@ def test_cotainr_debug_logging(
assert isinstance(handler, logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
actual_stdout_msgs = stdout.rstrip("\n").split("\n")
actual_stderr_msgs = stderr.rstrip("\n").split("\n")
assert len(actual_stdout_msgs) == len(expected_stdout_msgs)
Expand Down Expand Up @@ -275,7 +281,8 @@ def test_cotainr_info_logging(
assert isinstance(handler, logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down Expand Up @@ -361,6 +368,7 @@ def test_no_color_on_console(
assert isinstance(handler, logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs
2 changes: 1 addition & 1 deletion cotainr/tests/container/test_singularity_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_setup_log_dispatcher(self):

class TestContext:
def test_add_verbosity_arg(self, capsys, patch_disable_stream_subprocess):
with SingularitySandbox(base_image="my_base_image_6021") as sandbox:
with SingularitySandbox(base_image="my_base_image_6021"):
pass
stdout_lines = capsys.readouterr().out.rstrip("\n").split("\n")
assert "args=['singularity', '-q', " in stdout_lines[0]
Expand Down
6 changes: 4 additions & 2 deletions cotainr/tests/pack/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@


class StubLicensePopen(ABC):
def __init__(self, args, stdin=None, stdout=None, text=None):
license_text: str

def __init__(self, args, stdin=None, stdout=None, text=None): # noqa: B027
pass

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(self, exc_type, exc_value, traceback): # noqa: B027
pass

def communicate(self, input=None):
Expand Down
6 changes: 2 additions & 4 deletions cotainr/tests/pack/test_conda_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def test_beforehand_license_acceptance(
miniforge_license_accept_cmd,
_conda_bootstrap_cmd,
_conda_bootstrap_clean_cmd,
) = (
capsys.readouterr().out.strip().split("\n")
)
) = capsys.readouterr().out.strip().split("\n")
assert miniforge_license_accept_cmd == (
"You have accepted the Miniforge installer license via the command line option "
"'--accept-licenses'."
Expand Down Expand Up @@ -395,7 +393,7 @@ def test_miniforge_still_showing_license(
):
monkeypatch.setattr("builtins.input", factory_mock_input("yes"))
with SingularitySandbox(base_image="my_base_image_6021") as sandbox:
conda_install = CondaInstall(sandbox=sandbox)
CondaInstall(sandbox=sandbox)

stdout = capsys.readouterr().out.strip()

Expand Down
1 change: 1 addition & 0 deletions cotainr/tests/tracing/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import contextlib

import pytest
Expand Down
1 change: 0 additions & 1 deletion cotainr/tests/tracing/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

"""


import contextlib


Expand Down
1 change: 1 addition & 0 deletions cotainr/tests/tracing/test_coloured_output_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import logging

import pytest
Expand Down
1 change: 1 addition & 0 deletions cotainr/tests/tracing/test_console_spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import concurrent.futures
import sys

Expand Down
21 changes: 10 additions & 11 deletions cotainr/tests/tracing/test_log_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def test_log_dispatcher_critical_logging(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand All @@ -108,7 +107,8 @@ def test_log_dispatcher_critical_logging(
assert isinstance(logger.handlers[0], logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down Expand Up @@ -139,7 +139,6 @@ def test_log_dispatcher_debug_logging(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand All @@ -153,7 +152,8 @@ def test_log_dispatcher_debug_logging(
assert isinstance(logger.handlers[0], logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
actual_stdout_msgs = stdout.rstrip("\n").split("\n")
actual_stderr_msgs = stderr.rstrip("\n").split("\n")
assert len(actual_stdout_msgs) == len(expected_stdout_msgs)
Expand Down Expand Up @@ -197,7 +197,6 @@ def test_log_dispatcher_info_logging(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand All @@ -211,7 +210,8 @@ def test_log_dispatcher_info_logging(
assert isinstance(logger.handlers[0], logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down Expand Up @@ -241,7 +241,6 @@ def test_log_dispatcher_warning_logging(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand All @@ -255,7 +254,8 @@ def test_log_dispatcher_warning_logging(
assert isinstance(logger.handlers[0], logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down Expand Up @@ -287,7 +287,6 @@ def test_log_to_file(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand Down Expand Up @@ -334,7 +333,6 @@ def test_no_color_on_console(

# Log test messages to log dispatcher loggers
for logger in [log_dispatcher.logger_stdout, log_dispatcher.logger_stderr]:

# Log messages
for level, msg in log_level_msgs.items():
logger.log(level=level, msg=msg)
Expand All @@ -348,7 +346,8 @@ def test_no_color_on_console(
assert isinstance(logger.handlers[0], logging.StreamHandler)

# Check correct logging, incl. message format, coloring, log level, and output stream
stdout, stderr = capsys.readouterr() # readouterr clears its content when returning
# readouterr clears its content when returning
stdout, stderr = capsys.readouterr()
assert stdout.rstrip("\n").split("\n") == stdout_msgs
assert stderr.rstrip("\n").split("\n") == stderr_msgs

Expand Down
1 change: 1 addition & 0 deletions cotainr/tests/tracing/test_message_spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import io
import shutil
import time
Expand Down
3 changes: 2 additions & 1 deletion cotainr/tests/tracing/test_stream_write_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- see the LICENSE file for details.

"""

import io

import pytest
Expand Down Expand Up @@ -46,7 +47,7 @@ def test_failing_attribute_lookup_delegation(self):
AttributeError,
match=r"StringIO' object has no attribute 'some_nonsense_attribute_6021'$",
):
stream_write_proxy.some_nonsense_attribute_6021
_ = stream_write_proxy.some_nonsense_attribute_6021

def test_successful_attribute_lookup_delegation(self):
stream = io.StringIO("some text 6021")
Expand Down
Loading
Loading