Skip to content

Commit

Permalink
Add --no-output-structure flag to pytest 🐍 (ethereum#162)
Browse files Browse the repository at this point in the history
* src/pytest_plugins/test_filler: Add --no-output-structure flag.

* src/pytest_plugins/test_filler: create top-level output dir

* src/pytest_plugins/test_filler: Change to --flat-output flag.

---------

Co-authored-by: danceratopz <[email protected]>
  • Loading branch information
spencer-tb and danceratopz committed Jun 27, 2023
1 parent 16461db commit 8636cf6
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/pytest_plugins/test_filler/test_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def pytest_addoption(parser):
default="./fixtures/",
help="Directory to store the generated test fixtures. Can be deleted.",
)
test_group.addoption(
"--flat-output",
action="store_true",
dest="flat_output",
default=False,
help="Output each test case in the directory without the folder structure.",
)


@pytest.hookimpl(tryfirst=True)
Expand Down Expand Up @@ -172,10 +179,12 @@ class FixtureCollector:

all_fixtures: Dict[str, List[Tuple[str, Any]]]
output_dir: str
flat_output: bool

def __init__(self, output_dir: str) -> None:
def __init__(self, output_dir: str, flat_output: bool) -> None:
self.all_fixtures = {}
self.output_dir = output_dir
self.flat_output = flat_output

def add_fixture(self, item, fixture: Fixture) -> None:
"""
Expand All @@ -196,9 +205,13 @@ def get_module_dir(item) -> str:
)
return module_dir

module_dir = os.path.join(
get_module_dir(item),
strip_test_prefix(item.originalname),
module_dir = (
strip_test_prefix(item.originalname)
if self.flat_output
else os.path.join(
get_module_dir(item),
strip_test_prefix(item.originalname),
)
)
if module_dir not in self.all_fixtures:
self.all_fixtures[module_dir] = []
Expand All @@ -215,14 +228,16 @@ def dump_fixtures(self) -> None:
"""
Dumps all collected fixtures to their respective files.
"""
os.makedirs(self.output_dir, exist_ok=True)
for module_file, fixtures in self.all_fixtures.items():
output_json = {}
for index, name_fixture in enumerate(fixtures):
name, fixture = name_fixture
name = str(index).zfill(3) + "-" + name
output_json[name] = fixture
file_path = os.path.join(self.output_dir, module_file + ".json")
os.makedirs(os.path.dirname(file_path), exist_ok=True)
if not self.flat_output:
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "w") as f:
json.dump(output_json, f, indent=4)

Expand All @@ -233,7 +248,10 @@ def fixture_collector(request):
Returns the configured fixture collector instance used for all tests
in one test module.
"""
fixture_collector = FixtureCollector(output_dir=request.config.getoption("output"))
fixture_collector = FixtureCollector(
output_dir=request.config.getoption("output"),
flat_output=request.config.getoption("flat_output"),
)
yield fixture_collector
fixture_collector.dump_fixtures()

Expand Down

0 comments on commit 8636cf6

Please sign in to comment.