Skip to content

Commit

Permalink
Update pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoor committed Nov 12, 2024
1 parent 32a4e14 commit abd75e5
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 112 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ repos:
hooks:
- id: yesqa
- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
rev: v3.19.0
hooks:
- id: pyupgrade
args: ['--py39-plus']
- repo: https://github.com/ambv/black
rev: 24.4.2
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 7.1.0
rev: 7.1.1
hooks:
- id: flake8
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-docstring-first
Expand All @@ -42,11 +42,11 @@ repos:
- id: codespell
exclude_types: [json]
- repo: https://github.com/marco-c/taskcluster_yml_validator
rev: v0.0.11
rev: v0.0.12
hooks:
- id: taskcluster_yml
- repo: https://github.com/MozillaSecurity/orion-ci
rev: v0.0.9
rev: v0.0.10
hooks:
- id: orion_ci
- repo: meta
Expand Down
3 changes: 1 addition & 2 deletions src/lithium/docs/examples/arithmetic/product_divides.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"""This tests Lithium's main "minimize" algorithm."""

import sys
from typing import List


def interesting(args: List[str], _temp_prefix: str) -> bool:
def interesting(args: list[str], _temp_prefix: str) -> bool:
"""Interesting if the product of the numbers in the file divides the argument.
Args:
Expand Down
6 changes: 3 additions & 3 deletions src/lithium/interestingness/crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
Example:
python -m lithium crashes --timeout=9 <binary> --fuzzing-safe <testcase>
"""
from __future__ import annotations

import logging
import sys
from typing import List, Optional

from .timed_run import BaseParser, ExitStatus, timed_run

LOG = logging.getLogger(__name__)


def interesting(
cli_args: Optional[List[str]] = None,
temp_prefix: Optional[str] = None,
cli_args: list[str] | None = None,
temp_prefix: str | None = None,
) -> bool:
"""Interesting if the binary causes a crash.
Expand Down
13 changes: 7 additions & 6 deletions src/lithium/interestingness/diff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
-b "--fuzzing-safe --wasm-always-baseline" \
<binary> <testcase>
"""
from __future__ import annotations

import argparse
import filecmp
import logging
import sys
from typing import List, Optional, Union

from .timed_run import BaseParser, ExitStatus, timed_run

LOG = logging.getLogger(__name__)


def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
"""Parse args
Args:
Expand Down Expand Up @@ -58,8 +59,8 @@ def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:


def interesting(
cli_args: Optional[List[str]] = None,
temp_prefix: Optional[str] = None,
cli_args: list[str] | None = None,
temp_prefix: str | None = None,
) -> bool:
"""Check if there's a difference in output or return code with different args.
Expand Down Expand Up @@ -98,8 +99,8 @@ def interesting(

# Compare outputs
def cmp_out(
a_data: Union[str, bytes],
b_run: Union[str, bytes],
a_data: str | bytes,
b_run: str | bytes,
is_file: bool = False,
) -> bool:
if is_file:
Expand Down
6 changes: 3 additions & 3 deletions src/lithium/interestingness/hangs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
Example:
python -m lithium hangs --timeout=3 <binary> --fuzzing-safe <testcase>
"""
from __future__ import annotations

import logging
import sys
from typing import List, Optional

from .timed_run import BaseParser, ExitStatus, timed_run

LOG = logging.getLogger(__name__)


def interesting(
cli_args: Optional[List[str]] = None,
temp_prefix: Optional[str] = None,
cli_args: list[str] | None = None,
temp_prefix: str | None = None,
) -> bool:
"""Interesting if the binary causes a hang.
Expand Down
8 changes: 4 additions & 4 deletions src/lithium/interestingness/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
Example:
python -m lithium outputs --timeout=9 FOO <binary> --fuzzing-safe <testcase>
"""
from __future__ import annotations

import logging
import re
import sys
from pathlib import Path
from typing import List, Optional, Union

from . import utils
from .timed_run import BaseParser, timed_run

LOG = logging.getLogger(__name__)


def file_contains(path: Union[Path, str], is_regex: bool, search: str) -> bool:
def file_contains(path: Path | str, is_regex: bool, search: str) -> bool:
"""Determine if string is present in a file.
Args:
Expand All @@ -38,8 +38,8 @@ def file_contains(path: Union[Path, str], is_regex: bool, search: str) -> bool:


def interesting(
cli_args: Optional[List[str]] = None,
temp_prefix: Optional[str] = None,
cli_args: list[str] | None = None,
temp_prefix: str | None = None,
) -> bool:
"""Interesting if the binary causes an intended message to show up. (e.g. on
stdout/stderr)
Expand Down
4 changes: 2 additions & 2 deletions src/lithium/interestingness/repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

import argparse
import logging
from typing import Any, List, cast
from typing import Any, cast

from .utils import rel_or_abs_import


def interesting(cli_args: List[str], temp_prefix: str) -> bool:
def interesting(cli_args: list[str], temp_prefix: str) -> bool:
"""Interesting if the desired interestingness test that is run together with
"repeat" also reports "interesting".
Expand Down
26 changes: 14 additions & 12 deletions src/lithium/interestingness/timed_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""Run a subprocess with timeout"""
from __future__ import annotations

import argparse
import enum
import logging
Expand All @@ -12,7 +14,7 @@
import sys
import time
from pathlib import Path
from typing import BinaryIO, Callable, Dict, List, Optional, Union
from typing import BinaryIO, Callable

from ffpuppet import SanitizerOptions

Expand Down Expand Up @@ -53,11 +55,11 @@ def __init__(
self,
pid: int,
status: ExitStatus,
return_code: Union[int, None],
return_code: int | None,
message: str,
elapsed: float,
out: Union[bytes, str],
err: Union[bytes, str],
out: bytes | str,
err: bytes | str,
):
self.pid = pid
self.status = status
Expand All @@ -68,7 +70,7 @@ def __init__(
self.err = err


def _configure_sanitizers(orig_env: Dict[str, str]) -> Dict[str, str]:
def _configure_sanitizers(orig_env: dict[str, str]) -> dict[str, str]:
"""Copy environment and update default values in *SAN_OPTIONS entries.
Args:
Expand All @@ -77,7 +79,7 @@ def _configure_sanitizers(orig_env: Dict[str, str]) -> Dict[str, str]:
Returns:
Environment with *SAN_OPTIONS defaults set.
"""
env: Dict[str, str] = dict(orig_env)
env: dict[str, str] = dict(orig_env)
# https://github.com/google/sanitizers/wiki/SanitizerCommonFlags
common_flags = [
("abort_on_error", "false"),
Expand Down Expand Up @@ -126,12 +128,12 @@ def _get_signal_name(signum: int, default: str = "Unknown signal") -> str:


def timed_run(
cmd_with_args: List[str],
cmd_with_args: list[str],
timeout: int,
log_prefix: Optional[str] = None,
env: Optional[Dict[str, str]] = None,
log_prefix: str | None = None,
env: dict[str, str] | None = None,
inp: str = "",
preexec_fn: Optional[Callable[[], None]] = None,
preexec_fn: Callable[[], None] | None = None,
) -> RunData:
"""If log_prefix is None, uses pipes instead of files for all output.
Expand Down Expand Up @@ -162,8 +164,8 @@ def timed_run(

status = None
env = _configure_sanitizers(os.environ.copy() if env is None else env)
child_stderr: Union[BinaryIO, int] = subprocess.PIPE
child_stdout: Union[BinaryIO, int] = subprocess.PIPE
child_stderr: BinaryIO | int = subprocess.PIPE
child_stdout: BinaryIO | int = subprocess.PIPE
if log_prefix is not None:
# pylint: disable=consider-using-with
child_stdout = open(f"{log_prefix}-out.txt", "wb")
Expand Down
4 changes: 2 additions & 2 deletions src/lithium/interestingness/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
from pathlib import Path
from types import ModuleType
from typing import Tuple, Union
from typing import Union


def file_contains_str(
Expand Down Expand Up @@ -52,7 +52,7 @@ def file_contains_regex(
input_file: Union[Path, str],
regex: bytes,
verbose: bool = True,
) -> Tuple[bool, bytes]:
) -> tuple[bool, bytes]:
"""e.g. python -m lithium crashesat --timeout=30 \
--regex '^#0\\s*0x.* in\\s*.*(?:\\n|\\r\\n?)#1\\s*' \
./js --fuzzing-safe --no-threads --ion-eager testcase.js
Expand Down
26 changes: 14 additions & 12 deletions src/lithium/reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""lithium reducer"""
from __future__ import annotations

import argparse
import logging
import os
import sys
from collections.abc import Iterator
from pathlib import Path
from types import ModuleType
from typing import Any, Dict, Iterator, List, Optional, Type, cast
from typing import Any, cast

from .interestingness.utils import rel_or_abs_import
from .strategies import DEFAULT as DEFAULT_STRATEGY
Expand Down Expand Up @@ -43,22 +45,22 @@ class Lithium:
"""Lithium reduction object."""

def __init__(self) -> None:
self.strategy: Optional[Strategy] = None
self.strategy: Strategy | None = None

self.condition_script: Optional[ModuleType] = None
self.condition_args: Optional[List[str]] = None
self.condition_script: ModuleType | None = None
self.condition_args: list[str] | None = None

self.test_count = 0
self.test_total = 0

self.temp_dir: Optional[Path] = None
self.temp_dir: Path | None = None

self.testcase: Optional[Testcase] = None
self.last_interesting: Optional[Testcase] = None
self.testcase: Testcase | None = None
self.last_interesting: Testcase | None = None

self.temp_file_count = 1

def main(self, argv: Optional[List[str]] = None) -> int:
def main(self, argv: list[str] | None = None) -> int:
"""Main entrypoint (parse args and call `run()`)
Args:
Expand Down Expand Up @@ -112,7 +114,7 @@ def run(self) -> int:
if self.last_interesting is not None:
self.last_interesting.dump()

def process_args(self, argv: Optional[List[str]] = None) -> None:
def process_args(self, argv: list[str] | None = None) -> None:
"""Parse command-line args and initialize self.
Args:
Expand All @@ -124,7 +126,7 @@ class _ArgParseTry(argparse.ArgumentParser):
# pylint: disable=arguments-differ,no-self-argument

def exit( # type: ignore[override]
self, status: int = 0, message: Optional[str] = None
self, status: int = 0, message: str | None = None
) -> None:
pass

Expand All @@ -141,8 +143,8 @@ def error(self, message: str) -> None: # type: ignore[override]
grp_opt = parser.add_argument_group(description="Lithium options")
grp_atoms = grp_opt.add_mutually_exclusive_group()

strategies: Dict[str, Type[Strategy]] = {}
testcase_types: Dict[str, Type[Testcase]] = {}
strategies: dict[str, type[Strategy]] = {}
testcase_types: dict[str, type[Testcase]] = {}
for entry_point in iter_entry_points("lithium_strategies"):
try:
strategy_cls = entry_point.load()
Expand Down
Loading

0 comments on commit abd75e5

Please sign in to comment.