Skip to content

Commit 475a9b8

Browse files
authored
Merge pull request #1830 from oesteban/fix/1777
FIX: Error during version check trying to access read-only file systems
2 parents 8353080 + d57022c commit 475a9b8

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ jobs:
226226
no_output_timeout: 2h
227227
command: |
228228
docker run -ti --rm=false \
229+
-e TEST_READONLY_FILESYSTEM=1 -v $HOME:/home/readonly:ro \
229230
--entrypoint="pytest" poldracklab/fmriprep:latest \
230231
/src/fmriprep/fmriprep -svx --doctest-modules
231232

fmriprep/cli/tests/test_version.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test version checks."""
2+
from os import getenv
23
from datetime import datetime
34
from pathlib import Path
45
from packaging.version import Version
@@ -161,3 +162,21 @@ def mock_get(*args, **kwargs):
161162
assert reason == test_reason
162163
else:
163164
assert reason is None
165+
166+
167+
def test_readonly(tmp_path, monkeypatch):
168+
"""Test behavior when $HOME/.cache/fmriprep/latest can't be written out."""
169+
home_path = Path('/home/readonly') if getenv('TEST_READONLY_FILESYSTEM') \
170+
else tmp_path
171+
monkeypatch.setenv('HOME', str(home_path))
172+
cachedir = home_path / '.cache'
173+
174+
if getenv('TEST_READONLY_FILESYSTEM') is None:
175+
cachedir.mkdir(mode=0o555, exist_ok=True)
176+
177+
# Make sure creating the folder will raise the exception.
178+
with pytest.raises(OSError):
179+
(cachedir / 'fmriprep').mkdir(parents=True)
180+
181+
# Should not raise
182+
check_latest()

fmriprep/cli/version.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ def check_latest():
1919
date = None
2020
outdated = None
2121
cachefile = Path.home() / '.cache' / 'fmriprep' / 'latest'
22-
cachefile.parent.mkdir(parents=True, exist_ok=True)
23-
2422
try:
25-
latest, date = cachefile.read_text().split('|')
26-
except Exception:
27-
pass
28-
else:
23+
cachefile.parent.mkdir(parents=True, exist_ok=True)
24+
except OSError:
25+
cachefile = None
26+
27+
if cachefile and cachefile.exists():
2928
try:
30-
latest = Version(latest)
31-
date = datetime.strptime(date, DATE_FMT)
32-
except (InvalidVersion, ValueError):
33-
latest = None
29+
latest, date = cachefile.read_text().split('|')
30+
except Exception:
31+
pass
3432
else:
35-
if abs((datetime.now() - date).days) > RELEASE_EXPIRY_DAYS:
36-
outdated = True
33+
try:
34+
latest = Version(latest)
35+
date = datetime.strptime(date, DATE_FMT)
36+
except (InvalidVersion, ValueError):
37+
latest = None
38+
else:
39+
if abs((datetime.now() - date).days) > RELEASE_EXPIRY_DAYS:
40+
outdated = True
3741

3842
if latest is None or outdated is True:
3943
try:
@@ -49,7 +53,7 @@ def check_latest():
4953
else:
5054
latest = None
5155

52-
if latest is not None:
56+
if cachefile is not None and latest is not None:
5357
try:
5458
cachefile.write_text('|'.join(('%s' % latest, datetime.now().strftime(DATE_FMT))))
5559
except Exception:

0 commit comments

Comments
 (0)