Skip to content

Commit 4552dc1

Browse files
committed
tests: simplify run_command
1 parent 885719d commit 4552dc1

File tree

3 files changed

+35
-54
lines changed

3 files changed

+35
-54
lines changed

tests/conftest.py

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -99,43 +99,35 @@ def run_in_venv(
9999
raise
100100

101101

102-
@pytest.fixture(scope="function")
103-
def run_command():
102+
def run_command(
103+
cmd: str, check: bool = True, capture_stderr: bool = True
104+
) -> subprocess.CompletedProcess:
104105
"""
105106
Execute shell commands and return output.
106107
107-
This fixture is kept for backward compatibility with tests using isolated_solc_data.
108+
This function is kept for backward compatibility with tests using isolated_solc_data.
108109
For tests using isolated_python_env, use run_in_venv instead.
109-
"""
110110
111-
def _run(
112-
cmd: str, check: bool = True, capture_stderr: bool = True
113-
) -> subprocess.CompletedProcess:
114-
"""
115-
Run a shell command and return the result.
116-
117-
Args:
118-
cmd: Command to run
119-
check: Whether to raise on non-zero exit code
120-
capture_stderr: Whether to capture stderr
121-
122-
Returns:
123-
CompletedProcess instance with stdout, stderr, and returncode
124-
"""
125-
stderr_setting = subprocess.STDOUT if capture_stderr else subprocess.PIPE
126-
127-
result = subprocess.run(
128-
cmd,
129-
shell=True,
130-
capture_output=False,
131-
stdout=subprocess.PIPE,
132-
stderr=stderr_setting,
133-
text=True,
134-
check=check,
135-
)
136-
return result
111+
Args:
112+
cmd: Command to run
113+
check: Whether to raise on non-zero exit code
114+
capture_stderr: Whether to capture stderr
137115
138-
return _run
116+
Returns:
117+
CompletedProcess instance with stdout, stderr, and returncode
118+
"""
119+
stderr_setting = subprocess.STDOUT if capture_stderr else subprocess.PIPE
120+
121+
result = subprocess.run(
122+
cmd,
123+
shell=True,
124+
capture_output=False,
125+
stdout=subprocess.PIPE,
126+
stderr=stderr_setting,
127+
text=True,
128+
check=check,
129+
)
130+
return result
139131

140132

141133
@pytest.fixture(scope="session")
@@ -144,21 +136,6 @@ def test_contracts_dir() -> Path:
144136
return Path(__file__).parent.parent / "scripts" / "solidity_tests"
145137

146138

147-
@pytest.fixture(scope="session", autouse=True)
148-
def ensure_solc_select_installed():
149-
"""
150-
Ensure solc-select is installed in development mode.
151-
152-
This runs once per test session to ensure the current
153-
development version is being tested.
154-
"""
155-
# Check if solc-select is available
156-
result = subprocess.run(["solc-select", "--help"], capture_output=True, text=True, check=False)
157-
158-
if result.returncode != 0:
159-
pytest.exit("solc-select is not installed. Please run: pip install -e .")
160-
161-
162139
# Platform markers for conditional test execution
163140
def pytest_configure(config):
164141
"""Register custom markers."""

tests/test_compiler_versions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
exactly mirroring the behavior of the original test_solc.sh script.
66
"""
77

8+
from .conftest import run_command
9+
810

911
class TestCompilerVersions:
1012
"""Test compilation with different Solidity compiler versions."""
1113

12-
def test_solc_045(self, run_command, test_contracts_dir, isolated_solc_data):
14+
def test_solc_045(self, test_contracts_dir, isolated_solc_data):
1315
"""Test Solidity 0.4.5 compilation behavior."""
1416
# Switch to 0.4.5
1517
result = run_command("solc-select use 0.4.5 --always-install", check=False)
@@ -26,7 +28,7 @@ def test_solc_045(self, run_command, test_contracts_dir, isolated_solc_data):
2628
f"solc045_fail_compile did not fail as expected. Output: {result.stdout}"
2729
)
2830

29-
def test_solc_050(self, run_command, test_contracts_dir, isolated_solc_data):
31+
def test_solc_050(self, test_contracts_dir, isolated_solc_data):
3032
"""Test Solidity 0.5.0 compilation behavior."""
3133
# Switch to 0.5.0
3234
result = run_command("solc-select use 0.5.0 --always-install", check=False)
@@ -44,7 +46,7 @@ def test_solc_050(self, run_command, test_contracts_dir, isolated_solc_data):
4446
in result.stdout
4547
), f"solc050_fail_compile did not fail as expected. Output: {result.stdout}"
4648

47-
def test_solc_060(self, run_command, test_contracts_dir, isolated_solc_data):
49+
def test_solc_060(self, test_contracts_dir, isolated_solc_data):
4850
"""Test Solidity 0.6.0 compilation behavior."""
4951
# Switch to 0.6.0
5052
result = run_command("solc-select use 0.6.0 --always-install", check=False)
@@ -58,7 +60,7 @@ def test_solc_060(self, run_command, test_contracts_dir, isolated_solc_data):
5860
result = run_command(f"solc {test_contracts_dir}/solc060_success_receive.sol", check=False)
5961
assert result.returncode == 0, f"solc060_success_receive failed with: {result.stdout}"
6062

61-
def test_solc_070(self, run_command, test_contracts_dir, isolated_solc_data):
63+
def test_solc_070(self, test_contracts_dir, isolated_solc_data):
6264
"""Test Solidity 0.7.0 compilation behavior."""
6365
# Switch to 0.7.0
6466
result = run_command("solc-select use 0.7.0 --always-install", check=False)
@@ -75,7 +77,7 @@ def test_solc_070(self, run_command, test_contracts_dir, isolated_solc_data):
7577
result = run_command(f"solc {test_contracts_dir}/solc070_success.sol", check=False)
7678
assert result.returncode == 0, f"solc070_success failed with: {result.stdout}"
7779

78-
def test_solc_080(self, run_command, test_contracts_dir, isolated_solc_data):
80+
def test_solc_080(self, test_contracts_dir, isolated_solc_data):
7981
"""Test Solidity 0.8.0 compilation behavior."""
8082
# Switch to 0.8.0
8183
result = run_command("solc-select use 0.8.0 --always-install", check=False)
@@ -104,7 +106,7 @@ def test_solc_080(self, run_command, test_contracts_dir, isolated_solc_data):
104106
class TestVersionSwitching:
105107
"""Test version switching functionality."""
106108

107-
def test_always_install_flag(self, run_command, isolated_solc_data):
109+
def test_always_install_flag(self, isolated_solc_data):
108110
"""Test --always-install flag functionality."""
109111
# In isolated environment, 0.8.9 won't be installed initially
110112
# No need for complex path validation or manual cleanup
@@ -116,7 +118,7 @@ def test_always_install_flag(self, run_command, isolated_solc_data):
116118
f"Failed to switch with --always-install. Output: {result.stdout}"
117119
)
118120

119-
def test_use_without_install(self, run_command, isolated_solc_data):
121+
def test_use_without_install(self, isolated_solc_data):
120122
"""Test that 'use' fails when version is not installed."""
121123
# In isolated environment, 0.8.1 won't be installed initially
122124
# No need for complex cleanup logic

tests/test_platform_specific.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import pytest
1111

12+
from .conftest import run_command
13+
1214
# Platform configuration matrix
1315
PLATFORM_CONFIGS = {
1416
"linux": {
@@ -42,7 +44,7 @@ class TestPlatformSpecific: # pylint: disable=too-few-public-methods
4244
),
4345
],
4446
)
45-
def test_version_boundaries(self, platform, config, run_command, isolated_solc_data):
47+
def test_version_boundaries(self, platform, config, isolated_solc_data):
4648
"""Test version boundaries and constraints for all platforms."""
4749

4850
min_version = config["min_version"]

0 commit comments

Comments
 (0)