Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- `script_runner` is now a session-scoped fixture.
[#89](https://github.com/kvas-it/pytest-console-scripts/issues/89)

## [1.4.1] - 2023-05-29

### Removed
Expand Down
32 changes: 18 additions & 14 deletions pytest_console_scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import traceback
import warnings
from pathlib import Path
from typing import Any, Callable, Iterator, Sequence, Union
from typing import Any, Callable, Final, Iterator, Sequence, Union
from unittest import mock

import pytest
Expand Down Expand Up @@ -111,10 +111,16 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
mode = mark_mode or option_mode or config_mode or 'inprocess'

if mode in {'inprocess', 'subprocess'}:
metafunc.parametrize('script_launch_mode', [mode], indirect=True)
metafunc.parametrize(
'script_launch_mode', [mode], indirect=True, scope="session"
)
elif mode == 'both':
metafunc.parametrize('script_launch_mode', ['inprocess', 'subprocess'],
indirect=True)
metafunc.parametrize(
'script_launch_mode',
['inprocess', 'subprocess'],
indirect=True,
scope="session",
)
else:
raise ValueError(f'Invalid script launch mode: {mode}')

Expand Down Expand Up @@ -234,9 +240,9 @@ def __init__(
print_result: bool = True
) -> None:
assert launch_mode in {'inprocess', 'subprocess'}
self.launch_mode = launch_mode
self.print_result = print_result
self.rootdir = rootdir
self.launch_mode: Final = launch_mode
self.print_result: Final = print_result
self.rootdir: Final = rootdir

def __repr__(self) -> str:
return f'<ScriptRunner {self.launch_mode}>'
Expand Down Expand Up @@ -455,19 +461,17 @@ def run_subprocess(
return RunResult(cp.returncode, cp.stdout, cp.stderr, print_result)


@pytest.fixture
@pytest.fixture(scope="session")
def script_launch_mode(request: pytest.FixtureRequest) -> str:
return str(request.param)


@pytest.fixture
def script_cwd(tmp_path: Path) -> Path:
work_dir = tmp_path / 'script-cwd'
work_dir.mkdir()
return work_dir
@pytest.fixture(scope="session")
def script_cwd(tmp_path_factory: pytest.TempPathFactory) -> Path:
return tmp_path_factory.mktemp('script-cwd')


@pytest.fixture
@pytest.fixture(scope="session")
def script_runner(
request: pytest.FixtureRequest, script_cwd: Path, script_launch_mode: str
) -> ScriptRunner:
Expand Down