GithubHelp home page GithubHelp logo

tox-ini-fmt's People

Contributors

dependabot[bot] avatar gaborbernat avatar hugovk avatar jugmac00 avatar julian avatar kurtmckee avatar pdecat avatar pre-commit-ci[bot] avatar wwuck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tox-ini-fmt's Issues

[feature-request] Support for "file|" syntax in set_env

From https://tox.wiki/en/latest/config.html:

image

With my tox.ini as:

[tox]
env_list = py311

[testenv:py311]
allowlist_externals = bash
set_env = 
    file|.env
commands =
    bash -c 'env | grep "FOO"'

and .env:

FOO=BAR

I get

$ tox
py311: commands[0]> bash -c "env | grep \"FOO\""
FOO=BAR
  py311: OK (1.23=setup[0.14]+cmd[1.09] seconds)
  congratulations :) (1.55 seconds)

Which is tottaly expected. However:

$ pre-commit run tox-ini-fmt
tox-ini-fmt..............................................................Failed
- hook id: tox-ini-fmt
- exit code: 1

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Scripts\tox-ini-fmt.EXE\__main__.py", line 7, in <module>
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Lib\site-packages\tox_ini_fmt\__main__.py", line 47, in run
    formatted = format_tox_ini(before, opts)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Lib\site-packages\tox_ini_fmt\formatter\__init__.py", line 34, in format_tox_ini
    format_test_env(parser, section_name)
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Lib\site-packages\tox_ini_fmt\formatter\test_env.py", line 74, in format_test_env
    fix_and_reorder(parser, name, tox_section_cfg, upgrade)
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Lib\site-packages\tox_ini_fmt\formatter\util.py", line 51, in fix_and_reorder
    section[key] = fix(section[key])
                   ^^^^^^^^^^^^^^^^^
  File "C:\Users\xxx\.cache\pre-commit\reporcnyrb_x\py_env-python3\Lib\site-packages\tox_ini_fmt\formatter\test_env.py", line 107, in to_set_env
    raise RuntimeError(msg)
RuntimeError: invalid line file|.env in setenv

One-line sections become multiline

(Again not sure if this is intentional, feel free to close if so!)

git clone https://github.com/Julian/regret && cd regret && tox-ini-fmt tox.ini

turns

[testenv:build]
deps = build
commands = {envpython} -m build {toxinidir} --outdir {envtmpdir}/dist

into


[testenv:build]
deps =
    build
commands =
    {envpython} -m build {toxinidir} --outdir {envtmpdir}/dist

The README says "values that contain a list are split one value per line (PR/merge friendly)" which makes a lot of sense of course, but for some envs they'll never have more than one dep or command by their nature. If not by default, maybe at least an option to keep len == 1 things on one line?

envlist order is not respected

In the documentation we say:

envlist - multi-line, start with py envs in decreasing python order, then same with pypy, then everything else

This is not true yet, we actually do something else...

comments get deleted

 [testenv:mypy]
 deps =
     mypy
 commands =
-    # do not lint tests yet; waiting for pytest 6.0 release
     mypy --strict src {posargs}

and

 [pytest]
-# only collect intended test classes, e.g. TestSaving
-# do not collect e.g. TestingFileStorage
 python_classes = Test[A-Z]*

whereas the first "bug" is pretty nice, as pytest 6.0 got released already some time ago :-)

logic for application of line breaks?

Hi,

I just tried this tool ( https://github.com/jugmac00/hibpcli/pull/51/files ) and I do not see a rule yet, when line breaks are applied and when not.

e.g. after develop = there is a line break and true is on the next line, but there is skip_install = true. There are also line breaks after deps = and commands.

Also, the configuration sections for flake8 and isort were not touched at all.

Update
Ok, I stepped through the code and now I know why the flake8 and isort sections did not get touched ๐Ÿ‘

def format_tox_ini(tox_ini: Path) -> str:
parser = ConfigParser()
with tox_ini.open("rt"):
parser.read([tox_ini])
order_sections(parser)
format_tox_section(parser)
for section_name in parser.sections():
if section_name == "testenv" or section_name.startswith("testenv:"):
format_test_env(parser, section_name)
return _generate_tox_ini(parser)

You do not generically sort and format all sections, but currently only the tox and testenv(s) sections.

Update 2
develop is a typo (in my config) and should read usedevelop ๐Ÿ˜ฌ I start to see the pattern ๐Ÿ˜

Update 3
I think it would be a good idea to update the readme that other sections besides tox and testenv won't be touched.

ValueError on formatting a valid tox.ini

Hi! (I don't know if this isn't yet intended to be used or not, if so apologies) --

Just tried to run it and got a ValueError with no message.

git clone https://github.com/Julian/regret && cd regret && tox-ini-fmt tox.ini should reproduce (I didn't look carefully at what's happening, so apologies if it's something simple):

โŠ™  git clone https://github.com/Julian/regret && cd regret && tox-ini-fmt tox.ini                                                                                                                 julian@Air
Cloning into 'regret'...
remote: Enumerating objects: 81, done.
remote: Counting objects: 100% (81/81), done.
remote: Compressing objects: 100% (56/56), done.
remote: Total 1007 (delta 47), reused 50 (delta 25), pack-reused 926
Receiving objects: 100% (1007/1007), 155.09 KiB | 5.35 MiB/s, done.
Resolving deltas: 100% (593/593), done.
Traceback (most recent call last):
  File "/Users/julian/.local/bin/tox-ini-fmt", line 8, in <module>
    sys.exit(run())
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/__main__.py", line 12, in run
    formatted = format_tox_ini(opts.tox_ini)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/__init__.py", line 20, in format_tox_ini
    format_test_env(parser, section_name)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/test_env.py", line 22, in format_test_env
    fix_and_reorder(parser, name, tox_section_cfg)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/util.py", line 13, in fix_and_reorder
    section[key] = fix(section[key])
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/test_env.py", line 30, in to_deps
    return fmt_list(requires(value))
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/requires.py", line 13, in requires
    normalized = sorted(values, key=lambda req: (";" in req, _req_base(req), req))
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/requires.py", line 12, in <genexpr>
    values = (_normalize_req(req) for req in require_group)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/requires.py", line 19, in _normalize_req
    normalized = _normalize_lib(lib)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/requires.py", line 27, in _normalize_lib
    base = _req_base(lib)
  File "/Users/julian/.dotfiles/.local/share/virtualenvs/tox/lib/python3.8/site-packages/tox_ini_fmt/formatter/requires.py", line 41, in _req_base
    raise ValueError(lib)
ValueError

Conditional dependencies are mangled

git clone https://github.com/Julian/regret && cd regret && tox-ini-fmt tox.ini turns (subset of tox.ini shown):

deps =
    -r{toxinidir}/test-requirements.txt
    virtue

    codecov,coverage: coverage
    codecov: codecov

into

deps =
  -r{toxinidir}/test-requirements.txt
  codecov,coverage:
  codecov:
  virtue

(where the conditional dependencies are gone)

Replaces '-r docs/requirements.txt' with '-r'

tox-ini-fmt==0.5.0

Input

[tox]
envlist =
    py{py3, 39, 38, 37, 36}

[testenv]
extras =
    tests
commands =
    {envpython} -m pytest --cov humanize --cov tests --cov-report xml {posargs}

[testenv:docs]
deps =
    -r docs/requirements.txt
commands =
    mkdocs build

[testenv:lint]
passenv =
    PRE_COMMIT_COLOR
skip_install = true
deps =
    pre-commit
commands =
    pre-commit run --all-files --show-diff-on-failure

[pytest]
addopts = --doctest-modules

Diff

--- tox.ini

+++ tox.ini

@@ -10,7 +10,7 @@


 [testenv:docs]
 deps =
-    -r docs/requirements.txt
+    -r
 commands =
     mkdocs build

Workaround

-    -r docs/requirements.txt
+    -rdocs/requirements.txt

Ordering of envvars

(I forget whether the below is true, if not obviously feel free to close)

git clone https://github.com/Julian/regret && cd regret && tox-ini-fmt tox.ini

turns

setenv =
    {[testenv]setenv}
    COVERAGE_DEBUG_FILE={envtmpdir}/coverage-debug
    COVERAGE_FILE={envtmpdir}/coverage-data

into

setenv =
  COVERAGE_DEBUG_FILE = {envtmpdir}/coverage-debug
  COVERAGE_FILE = {envtmpdir}/coverage-data
  {[testenv]setenv = {[testenv]setenv}

is that a safe change?

What if the parent setenv contained those envvars? Presumably those are processed in order right, and the original way, the overridden values win, whereas after sorting, the parent values will win?

(Again if that's false feel free to close :)

Trailing zeros are stripped from versions

Versions in e.g. dependencies like 1.2.0 are emitted as 1.2.

This is theoretically equivalent when using "=", but not when using other specifiers. E.g.:

โŠ™  tox-ini-fmt =(echo '                                                                                                                                                                         julian@Air โ—
[testenv]
deps =
  foo~=1.0.0
')

emits

--- /tmp/zshXDAZNW

+++ /tmp/zshXDAZNW

@@ -1,5 +1,3 @@

-
 [testenv]
 deps =
-  foo~=1.0.0
-
+  foo~~=1

where that version specification now has different meaning. (Also it looks like the ~ itself was mangled).

Error when formatting pytest --log-format argument

Running tox-ini-fmt 0.5.0 on the following tox.ini file gives an error:

[testenv:pytest]
commands = pytest \
           --numprocesses=auto \
           --open-files \
           --cov=my_project \
           --cov-report=xml \
           --junitxml=junit-{envname}.xml \
           --log-format='%(asctime)s %(levelname)s %(message)s' \
           --log-date-format='%Y-%m-%d %H:%M:%S' \
           --log-cli-level='INFO' \
           {posargs}
# Install testenv requirements
deps = pytest
       pytest-antilru
       pytest-cov
       pytest-openfiles
       pytest-randomly
       pytest-xdist[psutil]
$ tox-ini-fmt tox.ini
Traceback (most recent call last):
  File "/home/devuan/.pyenv/versions/venv/bin/tox-ini-fmt", line 8, in <module>
    sys.exit(run())
  File "/home/devuan/.pyenv/versions/3.9.5/envs/venv/lib/python3.9/site-packages/tox_ini_fmt/__main__.py", line 26, in run
    formatted = format_tox_ini(opts.tox_ini, opts)
  File "/home/devuan/.pyenv/versions/3.9.5/envs/venv/lib/python3.9/site-packages/tox_ini_fmt/formatter/__init__.py", line 22, in format_tox_ini
    order_sections(parser, opts.pin_toxenvs)
  File "/home/devuan/.pyenv/versions/3.9.5/envs/venv/lib/python3.9/site-packages/tox_ini_fmt/formatter/section_order.py", line 20, in order_sections
    sections[section] = dict(parser[section])
  File "/home/devuan/.pyenv/versions/3.9.5/lib/python3.9/configparser.py", line 1255, in __getitem__
    return self._parser.get(self._name, key)
  File "/home/devuan/.pyenv/versions/3.9.5/lib/python3.9/configparser.py", line 799, in get
    return self._interpolation.before_get(self, section, option, value,
  File "/home/devuan/.pyenv/versions/3.9.5/lib/python3.9/configparser.py", line 395, in before_get
    self._interpolate_some(parser, option, L, value, section, defaults, 1)
  File "/home/devuan/.pyenv/versions/3.9.5/lib/python3.9/configparser.py", line 434, in _interpolate_some
    raise InterpolationMissingOptionError(
configparser.InterpolationMissingOptionError: Bad value substitution: option 'commands' in section 'testenv:pytest' contains an interpolation key 'asctime' which is not a valid option name. Raw value: "pytest \\\n--numprocesses=auto \\\n--open-files \\\n--cov=my_project \\\n--cov-report=xml \\\n--junitxml=junit-{envname}.xml \\\n--log-format='%(asctime)s %(levelname)s %(message)s' \\\n--log-date-format='%Y-%m-%d %H:%M:%S' \\\n--log-cli-level='INFO' \\\n{posargs}"

Name of the `envlist` / `env_list` section

According to https://tox.wiki/en/latest/config.html#env_list both envlist and env_list are possible.

When running the latest version of tox-ini-fmt within pre-commit, it rejects envlist and suggests env_list. This conflicts with check-python-versions that cannot read env_list and expects envlist. As result, I cannot write single name of the section to satisfy both plugins.

To be fair, I am writing this issue to both projects (mgedmin/check-python-versions#42), but it would be nice if both alternatives were accepted. Would you like a PR?

Number of spaces for indentation

Hi,

I just tried this new tool, and it works great, and I like the output a lot.

I am curious why you decided to go with 2 spaces for indentation.

Pretty much all of my own projects, the projects I contribute to (zope, plone, ...) or I use e.g. tox, pytest, flask, ... use 4 spaces for indentation in config files, especially for tox.ini.

As a Python developer, this is something we are used to, but imho the better reason is, that the ini file then is way easier to scan, as there is a bigger distinction between keys and values ( This is similar to when you have a text and you make the heading not just one font size bigger, but some more).

Do you plan to make the number of spaces configurable?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.