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

Add more typing to the codebase and minor fixes. #286

Merged
merged 1 commit into from
Feb 5, 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
8 changes: 6 additions & 2 deletions green/cmdline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""The green command line entry point."""

from __future__ import annotations


Expand Down Expand Up @@ -53,7 +55,8 @@ def _main(argv: Sequence[str] | None, testing: bool) -> int:

# Add debug logging for stuff that happened before this point here
if config.files_loaded:
debug("Loaded config file(s): {}".format(", ".join(config.files_loaded)))
loaded_files = ", ".join(str(path) for path in config.files_loaded)
debug(f"Loaded config file(s): {loaded_files}")

# Discover/Load the test suite
if testing:
Expand Down Expand Up @@ -81,7 +84,7 @@ def _main(argv: Sequence[str] | None, testing: bool) -> int:
return int(not result.wasSuccessful())


def main(argv: Sequence[str] | None = None, testing: bool = False):
def main(argv: Sequence[str] | None = None, testing: bool = False) -> int:
# create the temp dir only once (i.e., not while in the recursed call)
if not os.environ.get("TMPDIR"): # pragma: nocover
try:
Expand All @@ -97,6 +100,7 @@ def main(argv: Sequence[str] | None = None, testing: bool = False):
if os_error.errno == 39:
# "Directory not empty" when trying to delete the temp dir can just be a warning
print(f"warning: {os_error.strerror}")
return 0
else:
raise os_error
else:
Expand Down
41 changes: 29 additions & 12 deletions green/command.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,63 @@
"""Registers the green command with setuptools."""

from __future__ import annotations

import functools
import sys
from typing import TYPE_CHECKING

from setuptools import Command

from green.config import parseArguments
from green.cmdline import main

if TYPE_CHECKING:
from argparse import Action


def get_user_options():
def get_user_options() -> list[tuple[str, str | None, str | None]]:
# When running "python setup.py --help-commands", setup.py will call this
# function -- but green isn't actually being called.
if "--help-commands" in sys.argv:
return []

r = parseArguments()
options = []
args = parseArguments()
options: list[tuple[str, str | None, str | None]] = []

for action in r.store_opt.actions:
names = [str(name.lstrip("-")) for name in action.option_strings]
action: Action
for action in args.store_opt.actions:
names = [name.lstrip("-") for name in action.option_strings]
short_name: str | None
if len(names) == 1:
names.insert(0, None)
full_name = names[0]
short_name = None
else:
# TODO: We might want to pick the longer of the two for full_name.
full_name = names[1]
short_name = names[0]
if not action.const:
names[1] += "="
options.append((names[1], names[0], action.help))
full_name += "="
options.append((full_name, short_name, action.help))

return options


class green(Command):
command_name = "green"
description = "Run unit tests using green"
user_options = get_user_options()

def initialize_options(self):
@functools.cached_property
def user_options(self) -> list[tuple[str, str | None, str | None]]:
return get_user_options()

def initialize_options(self) -> None:
for name, _, _ in self.user_options:
setattr(self, name.replace("-", "_").rstrip("="), None)

def finalize_options(self):
def finalize_options(self) -> None:
pass

def run(self):
def run(self) -> None:
self.ensure_finalized()

if self.distribution.install_requires:
Expand Down
Loading
Loading