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 tool to generate formatting fixtures #1936

Merged
merged 7 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ repos:
rev: v4.1.0
hooks:
- id: end-of-file-fixer
# ignore formatting-prettier to have an accurate prettier comparison
# also temporarily ignore formatting-after until #1927 is merged
exclude: >
(?x)^(
test/eco/.*.result|
test/fixtures/formatting-before/.*
test/fixtures/formatting-before/.*|
test/fixtures/formatting-prettier/.*|
test/fixtures/formatting-after/.*
)$
- id: trailing-whitespace
exclude: >
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ xfail_strict = true

markers =
eco: Tests effects on a set of 3rd party ansible repositories
formatting_fixtures: Test that regenerates and tests formatting fixtures (requires prettier on PATH)
39 changes: 38 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import os
from contextlib import contextmanager
from typing import Iterator
from typing import TYPE_CHECKING, Iterator

import pytest

if TYPE_CHECKING:
from typing import List # pylint: disable=ungrouped-imports

from _pytest import nodes
from _pytest.config import Config
from _pytest.config.argparsing import Parser


@contextmanager
Expand All @@ -12,3 +21,31 @@ def cwd(path: str) -> Iterator[None]:
yield
finally:
os.chdir(old_pwd)


def pytest_addoption(parser: "Parser") -> None:
"""Add --regenerate-formatting-fixtures option to pytest."""
parser.addoption(
"--regenerate-formatting-fixtures",
action="store_true",
default=False,
help="Regenerate formatting fixtures with prettier and internal formatter",
)


def pytest_collection_modifyitems(items: "List[nodes.Item]", config: "Config") -> None:
"""Skip tests based on --regenerate-formatting-fixtures option."""
do_regenerate = config.getoption("--regenerate-formatting-fixtures")
skip_other = pytest.mark.skip(
reason="not a formatting_fixture test and "
"--regenerate-formatting-fixtures was specified"
)
skip_formatting_fixture = pytest.mark.skip(
reason="specify --regenerate-formatting-fixtures to "
"only run formatting_fixtures test"
)
for item in items:
if do_regenerate and "formatting_fixtures" not in item.keywords:
item.add_marker(skip_other)
elif not do_regenerate and "formatting_fixtures" in item.keywords:
item.add_marker(skip_formatting_fixture)
1 change: 1 addition & 0 deletions test/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Fixtures used in tests."""
1 change: 1 addition & 0 deletions test/fixtures/formatting-after/fmt-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ overly-indented-vault-value: !vault |
123466303630313
# this file also has 3 newlines at end-of-file instead of one

19 changes: 19 additions & 0 deletions test/fixtures/formatting-after/fmt-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# preamble/header comment
---
# initial comment
- foo: bar

- baz: # over indented
- qwerty
- foobar
animals: # under indented
- crow
- pig
- giraffe

- nothing: null # null

- octal:
- 0o123 # YAML 1.2 octal
- 0123 # YAML 1.1 octal

19 changes: 19 additions & 0 deletions test/fixtures/formatting-before/fmt-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# preamble/header comment
---
# initial comment
- foo: bar

- baz: # over indented
- qwerty
- foobar
animals: # under indented
- crow
- pig
- giraffe

- nothing: null # null

- octal:
- 0o123 # YAML 1.2 octal
- 0123 # YAML 1.1 octal

47 changes: 47 additions & 0 deletions test/fixtures/formatting-prettier/fmt-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# ^ too many newlines before
foo: bar # This is a comment has extra spaces preceding it

fruits: # unindented sequence:
- apple
- orange
vegetables: # indented sequence:
- onion
- carrot

quoting:
- "that should have double quotes"
- "that should remain in single quotes"
- 'a string with " inside'
# next line has some undesired trailing spaces:
- "a string with ' inside"
- can't be sure!
# next line should be converted to use double quotes:
- ["foo", "bar"]

inline-dictionary:
- { foo: bar } # should add some spacing between curly braces and content

# YAML 1.1 Boolean-hell: https://yaml.org/type/bool.html
booleans-true:
preferred: true # YAML 1.2 compatible!
answer-1.1: YES
canonical-1.1: y
canonical-upper-1.1: Y
logical-1.1: True
option-1.1: on
booleans-false:
preferred: false # YAML 1.2 compatible!
answer-1.1: NO
canonical-1.1: n
canonical-upper-1.1: N
logical-1.1: False
option-1.1: off

# ^ double newline should be removed
overly-indented-vault-value: !vault |
$ANSIBLE_VAULT;1.1;AES256
123466303630313
# this file also has 3 newlines at end-of-file instead of one

19 changes: 19 additions & 0 deletions test/fixtures/formatting-prettier/fmt-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# preamble/header comment
---
# initial comment
- foo: bar

- baz: # over indented
- qwerty
- foobar
animals: # under indented
- crow
- pig
- giraffe

- nothing: null # null

- octal:
- 0o123 # YAML 1.2 octal
- 0123 # YAML 1.1 octal

39 changes: 39 additions & 0 deletions test/fixtures/test_regenerate_formatting_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Test that re-generates formatting fixtures."""
import shutil
import subprocess
from pathlib import Path

import pytest


@pytest.mark.formatting_fixtures()
def test_regenerate_formatting_fixtures() -> None:
"""Re-generate formatting fixtures with prettier and internal formatter.
Pass ``--regenerate-formatting-fixtures`` to run this and skip all other tests.
This is a "test" because once fixtures are regenerated,
we run prettier again to make sure it does not change files formatted
with our internal formatting code.
"""
print("Looking for prettier on PATH...")
subprocess.check_call(["which", "prettier"])

fixtures_dir = Path("test/fixtures/")
fixtures_dir_before = fixtures_dir / "formatting-before"
fixtures_dir_prettier = fixtures_dir / "formatting-prettier"
fixtures_dir_after = fixtures_dir / "formatting-after"

fixtures_dir_prettier.mkdir(exist_ok=True)
fixtures_dir_after.mkdir(exist_ok=True)

print("\nCopying before fixtures...")
for fixture in fixtures_dir_before.glob("fmt-[0-9].yml"):
shutil.copy(str(fixture), str(fixtures_dir_prettier / fixture.name))
shutil.copy(str(fixture), str(fixtures_dir_after / fixture.name))

print("\nWriting fixtures with prettier...")
subprocess.check_call(["prettier", "-w", str(fixtures_dir_prettier)])
# NB: pre-commit end-of-file-fixer can also modify files.

# prepare ruamel.yaml fixtures (diff in next PR will show how it compares).
subprocess.check_call(["prettier", "-w", str(fixtures_dir_after)])