def test_self_check_state_key_uses_sys_prefix(monkeypatch): key = "helloworld" monkeypatch.setattr(sys, "prefix", key) state = outdated.SelfCheckState("") assert state.key == key
def test_self_check_state(monkeypatch, tmpdir): CONTENT = '''{"pip_prefix": {"last_check": "1970-01-02T11:00:00Z", "pypi_version": "1.0"}}''' fake_file = pretend.stub( read=pretend.call_recorder(lambda: CONTENT), write=pretend.call_recorder(lambda s: None), ) @pretend.call_recorder @contextmanager def fake_open(filename, mode='r'): yield fake_file monkeypatch.setattr(outdated, 'open', fake_open, raising=False) @pretend.call_recorder @contextmanager def fake_lock(filename): yield monkeypatch.setattr(outdated, "check_path_owner", lambda p: True) monkeypatch.setattr(lockfile, 'LockFile', fake_lock) monkeypatch.setattr(os.path, "exists", lambda p: True) cache_dir = tmpdir / 'cache_dir' monkeypatch.setattr(sys, 'prefix', tmpdir / 'pip_prefix') state = outdated.SelfCheckState(cache_dir=cache_dir) state.save('2.0', datetime.datetime.utcnow()) expected_path = cache_dir / 'selfcheck.json' assert fake_lock.calls == [pretend.call(expected_path)] assert fake_open.calls == [ pretend.call(expected_path), pretend.call(expected_path), pretend.call(expected_path, 'w'), ] # json.dumps will call this a number of times assert len(fake_file.write.calls)
def test_self_check_state_writes_expected_statefile(monkeypatch, tmpdir): cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() key = "helloworld" statefile_path = _get_statefile_path(str(cache_dir), key) last_check = datetime.datetime.strptime("1970-01-02T11:00:00Z", outdated.SELFCHECK_DATE_FMT) pypi_version = "1.0" monkeypatch.setattr(sys, "prefix", key) state = outdated.SelfCheckState(str(cache_dir)) state.save(pypi_version, last_check) with open(statefile_path) as f: saved = json.load(f) expected = { "key": key, "last_check": last_check.strftime(outdated.SELFCHECK_DATE_FMT), "pypi_version": pypi_version, } assert expected == saved
def test_self_check_state_reads_expected_statefile(monkeypatch, tmpdir): cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() key = "helloworld" statefile_path = _get_statefile_path(str(cache_dir), key) last_check = "1970-01-02T11:00:00Z" pypi_version = "1.0" content = { "key": key, "last_check": last_check, "pypi_version": pypi_version, } Path(statefile_path).parent.mkdir() with open(statefile_path, "w") as f: json.dump(content, f) monkeypatch.setattr(sys, "prefix", key) state = outdated.SelfCheckState(str(cache_dir)) assert state.state["last_check"] == last_check assert state.state["pypi_version"] == pypi_version
def test_self_check_state_no_cache_dir(): state = outdated.SelfCheckState(cache_dir=False) assert state.state == {} assert state.statefile_path is None