Skip to content

Commit c81fee6

Browse files
Copilott0mdavid-m
andauthored
Fix integration test failures caused by sys.modules pollution and shutil.SameFileError (#349)
* Initial plan * Fix integration test failures: restore sys.modules mocks, handle SameFileError, update CI workflow Co-authored-by: t0mdavid-m <57191390+t0mdavid-m@users.noreply.github.com> * Remove unnecessary pyopenms mock from test_topp_workflow_parameter.py, simplify test_parameter_presets.py Co-authored-by: t0mdavid-m <57191390+t0mdavid-m@users.noreply.github.com> * Fix Windows build: correct site-packages path in cleanup step Co-authored-by: t0mdavid-m <57191390+t0mdavid-m@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: t0mdavid-m <57191390+t0mdavid-m@users.noreply.github.com>
1 parent c65d503 commit c81fee6

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

.github/workflows/build-windows-executable-app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ jobs:
248248
- name: Clean up unnecessary Python files
249249
shell: pwsh
250250
run: |
251-
$sitePkgs = "py/Lib/site-packages"
251+
$sitePkgs = "python-${{ env.PYTHON_VERSION }}/Lib/site-packages"
252252
253253
# Remove __pycache__ directories
254254
Get-ChildItem -Path $sitePkgs -Recurse -Directory -Filter "__pycache__" | Remove-Item -Recurse -Force

.github/workflows/ci.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest]
11-
# Requirements file generated with python=3.11
11+
# Requirements file generated with python=3.12; tested with python=3.11
1212
python-version: ["3.11"]
1313
steps:
1414
- uses: actions/checkout@v4
15-
- uses: conda-incubator/setup-miniconda@v3
15+
- uses: actions/setup-python@v4
1616
with:
17-
activate-environment: openms
1817
python-version: ${{ matrix.python-version }}
19-
channels: defaults,bioconda,conda-forge
2018

21-
- name: Install OpenMS
22-
run: |
23-
conda install openms -y
2419
- name: Install dependencies
2520
run: |
2621
python -m pip install --upgrade pip

test_gui.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def test_view_raw_ms_data(launch, example):
9393

9494
# Copy files from example-data/mzML to workspace mzML directory, add to selected files
9595
for f in Path("example-data", "mzML").glob("*.mzML"):
96-
shutil.copy(f, mzML_dir)
96+
try:
97+
shutil.copy(f, mzML_dir)
98+
except shutil.SameFileError:
99+
pass # File already exists as a symlink to the same source (on Linux)
97100
launch.run()
98101

99102
## TODO: Figure out a way to select a spectrum to be displayed
@@ -119,7 +122,10 @@ def test_run_workflow(launch, example):
119122

120123
# Copy files from example-data/mzML to workspace mzML directory, add to selected files
121124
for f in Path("example-data", "mzML").glob("*.mzML"):
122-
shutil.copy(f, mzML_dir)
125+
try:
126+
shutil.copy(f, mzML_dir)
127+
except shutil.SameFileError:
128+
pass # File already exists as a symlink to the same source (on Linux)
123129
launch.run()
124130

125131
## Select experiments to process

tests/test_parameter_presets.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,33 @@
1616
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1717
sys.path.append(PROJECT_ROOT)
1818

19-
# Create mock for streamlit before importing ParameterManager
19+
# Mock streamlit before importing ParameterManager so that the imported module
20+
# uses a controllable `st.session_state` (a plain dict) instead of the real one,
21+
# which requires a running Streamlit app context. This allows unit-testing
22+
# ParameterManager's preset logic (apply_preset, clear_parameter_session_state)
23+
# in isolation.
2024
mock_streamlit = MagicMock()
2125
mock_streamlit.session_state = {}
22-
sys.modules['streamlit'] = mock_streamlit
2326

24-
# Create mock for pyopenms
25-
mock_pyopenms = MagicMock()
26-
mock_pyopenms.__version__ = "2.9.1"
27-
sys.modules['pyopenms'] = mock_pyopenms
27+
# Temporarily replace streamlit in sys.modules so that ParameterManager's
28+
# `import streamlit as st` picks up the mock. Restore immediately after import
29+
# so other test files (e.g., test_gui.py AppTest) get the real streamlit.
30+
_original_streamlit = sys.modules.get('streamlit')
31+
sys.modules['streamlit'] = mock_streamlit
2832

29-
# Now import after mocks are set up
3033
from src.workflow.ParameterManager import ParameterManager
3134

35+
if _original_streamlit is not None:
36+
sys.modules['streamlit'] = _original_streamlit
37+
else:
38+
sys.modules.pop('streamlit', None)
39+
40+
# Remove cached src.workflow modules that were imported with mocked streamlit so
41+
# that AppTest (in test_gui.py) re-imports them fresh with the real package.
42+
for _key in list(sys.modules.keys()):
43+
if _key.startswith('src.workflow'):
44+
sys.modules.pop(_key, None)
45+
3246

3347
@pytest.fixture
3448
def temp_workflow_dir():

tests/test_topp_workflow_parameter.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1515
sys.path.append(PROJECT_ROOT)
1616

17-
# Create mock for pyopenms to avoid dependency on actual OpenMS installation
18-
mock_pyopenms = MagicMock()
19-
mock_pyopenms.__version__ = "2.9.1" # Mock version for testing
20-
sys.modules['pyopenms'] = mock_pyopenms
21-
2217
@pytest.fixture
2318
def mock_streamlit():
2419
"""Mock essential Streamlit components for testing parameter display."""
@@ -47,12 +42,6 @@ def mock_streamlit():
4742
}
4843

4944

50-
def test_mock_pyopenms():
51-
"""Verify that pyopenms mock is working correctly."""
52-
import pyopenms
53-
assert hasattr(pyopenms, '__version__')
54-
55-
5645
def test_topp_parameter_correctness():
5746
"""Test that TOPP parameters are displayed with correct values."""
5847
# Define expected parameters with values

0 commit comments

Comments
 (0)