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 Support for pyproject.toml configuration. #301

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ Quick Start
-----------

```bash
pip3 install green # To upgrade: "pip3 install --upgrade green"
pip3 install green
# To upgrade:
pip3 install --upgrade green
# To add pyproject.toml support in python < 3.11:
pip3 install 'green[toml]'
```

Now run green...
Expand Down Expand Up @@ -99,9 +103,10 @@ in the resolution chain overwriting earlier settings (last setting wins).
1) `$HOME/.green`
2) A config file specified by the environment variable `$GREEN_CONFIG`
3) `setup.cfg` in the current working directory of test run
4) `.green` in the current working directory of the test run
5) A config file specified by the command-line argument `--config FILE`
6) [Command-line arguments](https://github.com/CleanCut/green/blob/main/cli-options.txt)
4) `pyproject.toml` in the current working directory of test run
5) `.green` in the current working directory of the test run
6) A config file specified by the command-line argument `--config FILE`
7) [Command-line arguments](https://github.com/CleanCut/green/blob/main/cli-options.txt)

Any arguments specified in more than one place will be overwritten by the
value of the LAST place the setting is seen. So, for example, if a setting
Expand Down Expand Up @@ -463,9 +468,9 @@ To run the unittests, we would change to the parent directory of the project

$ green proj
....

Ran 4 tests in 0.125s using 8 processes

OK (passes=4)

Okay, so that's the classic short-form output for unit tests. Green really
Expand All @@ -475,17 +480,17 @@ shines when you start getting more verbose:

$ green -vvv proj
Green 4.1.0, Coverage 7.4.1, Python 3.12.2

test_foo
TestAnswer
. answer() returns 42
. answer() returns an integer
TestSchool
. test_age
. test_food

Ran 4 tests in 0.123s using 8 processes

OK (passes=4)

Notes:
Expand Down Expand Up @@ -624,5 +629,5 @@ Wait! What about the other test runners?
- **the ones I missed** -- Er, haven't heard of them yet either.

I'd love to hear **your** feedback regarding Green. Like it? Hate it? Have
some awesome suggestions? Whatever the case, go
some awesome suggestions? Whatever the case, go
[open a discussion](https://github.com/CleanCut/green/discussions)
18 changes: 17 additions & 1 deletion green/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

import coverage # pragma: no cover

try:
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

supports_tomllib = True
except ImportError:
supports_tomllib = False

coverage_version = f"Coverage {coverage.__version__}" # pragma: no cover

# Used for debugging output in cmdline, since we can't do debug output here.
Expand Down Expand Up @@ -628,7 +638,10 @@ def getConfig( # pragma: no cover

cwd = pathlib.Path.cwd()
# Medium priority
for cfg_file in ("setup.cfg", ".green"):
config_files = ["pyproject.toml", "setup.cfg", ".green"]
if not supports_tomllib:
config_files.remove("pyproject.toml")
for cfg_file in config_files + ["setup.cfg", ".green"]:
config_path = cwd / cfg_file
if config_path.is_file():
filepaths.append(config_path)
Expand All @@ -647,6 +660,9 @@ def getConfig( # pragma: no cover
# only if they use setup.cfg
if config_path.name == "setup.cfg":
parser.read(config_path)
elif config_path.name == "pyproject.toml":
data = tomllib.load(config_path.open("rb"))["tool"]
parser.read_dict(data, source="green")
else:
parser.read_file(ConfigFile(config_path))

Expand Down
Loading
Loading