3
3
import sys
4
4
from contextlib import contextmanager
5
5
6
+
6
7
import freezegun
7
8
import pretend
8
9
import pytest
@@ -37,6 +38,7 @@ def _options():
37
38
return pretend .stub (
38
39
find_links = False , extra_index_urls = [], index_url = 'default_url' ,
39
40
pre = False , trusted_hosts = False , process_dependency_links = False ,
41
+ cache_dir = '' ,
40
42
)
41
43
42
44
@@ -72,16 +74,17 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
72
74
save = pretend .call_recorder (lambda v , t : None ),
73
75
)
74
76
monkeypatch .setattr (
75
- outdated , 'load_selfcheck_statefile ' , lambda : fake_state
77
+ outdated , 'SelfCheckState ' , lambda ** kw : fake_state
76
78
)
77
79
78
80
with freezegun .freeze_time (
79
- "1970-01-09 10:00:00" ,
80
- ignore = [
81
- "six.moves" ,
82
- "pip._vendor.six.moves" ,
83
- "pip._vendor.requests.packages.urllib3.packages.six.moves" ,
84
- ]):
81
+ "1970-01-09 10:00:00" ,
82
+ ignore = [
83
+ "six.moves" ,
84
+ "pip._vendor.six.moves" ,
85
+ "pip._vendor.requests.packages.urllib3.packages.six.moves" ,
86
+ ]
87
+ ):
85
88
latest_pypi_version = outdated .pip_version_check (None , _options ())
86
89
87
90
# See we return None if not installed_version
@@ -105,41 +108,7 @@ def test_pip_version_check(monkeypatch, stored_time, installed_ver, new_ver,
105
108
assert len (outdated .logger .warning .calls ) == 0
106
109
107
110
108
- def test_virtualenv_state (monkeypatch ):
109
- CONTENT = '{"last_check": "1970-01-02T11:00:00Z", "pypi_version": "1.0"}'
110
- fake_file = pretend .stub (
111
- read = pretend .call_recorder (lambda : CONTENT ),
112
- write = pretend .call_recorder (lambda s : None ),
113
- )
114
-
115
- @pretend .call_recorder
116
- @contextmanager
117
- def fake_open (filename , mode = 'r' ):
118
- yield fake_file
119
-
120
- monkeypatch .setattr (outdated , 'open' , fake_open , raising = False )
121
-
122
- monkeypatch .setattr (outdated , 'running_under_virtualenv' ,
123
- pretend .call_recorder (lambda : True ))
124
-
125
- monkeypatch .setattr (sys , 'prefix' , 'virtually_env' )
126
-
127
- state = outdated .load_selfcheck_statefile ()
128
- state .save ('2.0' , datetime .datetime .utcnow ())
129
-
130
- assert len (outdated .running_under_virtualenv .calls ) == 1
131
-
132
- expected_path = os .path .join ('virtually_env' , 'pip-selfcheck.json' )
133
- assert fake_open .calls == [
134
- pretend .call (expected_path ),
135
- pretend .call (expected_path , 'w' ),
136
- ]
137
-
138
- # json.dumps will call this a number of times
139
- assert len (fake_file .write .calls )
140
-
141
-
142
- def test_global_state (monkeypatch , tmpdir ):
111
+ def test_self_check_state (monkeypatch , tmpdir ):
143
112
CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z",
144
113
"pypi_version": "1.0"}}'''
145
114
fake_file = pretend .stub (
@@ -164,18 +133,12 @@ def fake_lock(filename):
164
133
monkeypatch .setattr (lockfile , 'LockFile' , fake_lock )
165
134
monkeypatch .setattr (os .path , "exists" , lambda p : True )
166
135
167
- monkeypatch .setattr (outdated , 'running_under_virtualenv' ,
168
- pretend .call_recorder (lambda : False ))
169
-
170
136
cache_dir = tmpdir / 'cache_dir'
171
- monkeypatch .setattr (outdated , 'USER_CACHE_DIR' , cache_dir )
172
137
monkeypatch .setattr (sys , 'prefix' , tmpdir / 'pip_prefix' )
173
138
174
- state = outdated .load_selfcheck_statefile ( )
139
+ state = outdated .SelfCheckState ( cache_dir = cache_dir )
175
140
state .save ('2.0' , datetime .datetime .utcnow ())
176
141
177
- assert len (outdated .running_under_virtualenv .calls ) == 1
178
-
179
142
expected_path = cache_dir / 'selfcheck.json'
180
143
assert fake_lock .calls == [pretend .call (expected_path )]
181
144
0 commit comments