Repository navigation
Expand file tree
/
Copy pathtest_workflow_update.py
More file actions
162 lines (121 loc) · 4.23 KB
/
Copy pathtest_workflow_update.py
File metadata and controls
162 lines (121 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2016 Dean Jackson <deanishe@deanishe.net>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2016-02-27
#
from __future__ import print_function
from contextlib import contextmanager
import time
import pytest
import pytest_localserver
from workflow import Workflow
from workflow import update
from util import WorkflowMock, create_info_plist, delete_info_plist
from test_update import fakeresponse, DATA_JSON, HTTP_HEADERS_JSON
UPDATE_SETTINGS = {
'github_slug': 'deanishe/alfred-workflow-dummy',
'version': 'v2.0',
'frequency': 1,
}
@contextmanager
def dummy(*args, **kwargs):
yield None
@contextmanager
def ctx(args=None, update_settings=None, clear=True):
update_settings = update_settings or UPDATE_SETTINGS
args = args or []
c = dummy()
if args:
# Add placeholder for ARGV[0]
args = ['script'] + args
create_info_plist()
if args:
c = WorkflowMock(args)
with c:
wf = Workflow(update_settings=update_settings)
yield wf, c
if clear:
wf.reset()
delete_info_plist()
def test_auto_update():
"""Auto-update toggle active"""
def fake(wf):
return
with ctx(['workflow:autoupdate']) as (wf, c):
print('wf={0!r}, c={1!r}'.format(wf, c))
wf.args
print('wf.args={0!r}'.format(wf.args))
wf.run(fake)
assert wf.settings.get('__workflow_autoupdate') is True
with ctx(['workflow:noautoupdate']) as (wf, c):
wf.args
wf.run(fake)
assert wf.settings.get('__workflow_autoupdate') is False
print('update_available={0!r}'.format(wf.update_available))
def test_update(httpserver):
"""Auto-update installs update"""
def fake(wf):
return
# Mock subprocess.call etc. so the script doesn't try to
# update the workflow in Alfred
with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
with ctx(['workflow:update'], clear=False) as (wf, c):
wf.run(fake)
wf.args
print('Magic update command : {0!r}'.format(c.cmd))
assert c.cmd[0] == '/usr/bin/python'
assert c.cmd[2] == '__workflow_update_install'
update_settings = UPDATE_SETTINGS.copy()
update_settings['version'] = 'v6.0'
with ctx(['workflow:update'], update_settings) as (wf, c):
wf.run(fake)
wf.args
# Update command wasn't called
assert c.cmd == ()
def test_update_with_prereleases(httpserver):
"""Auto-update installs update with pre-releases enabled"""
def fake(wf):
return
# Mock subprocess.call etc. so the script doesn't try to
# update the workflow in Alfred
with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
update_settings = UPDATE_SETTINGS.copy()
update_settings['prereleases'] = True
with ctx(['workflow:update'], update_settings, clear=False) as (wf, c):
wf.run(fake)
wf.args
print('Magic update command : {0!r}'.format(c.cmd))
assert c.cmd[0] == '/usr/bin/python'
assert c.cmd[2] == '__workflow_update_install'
update_settings = UPDATE_SETTINGS.copy()
update_settings['version'] = 'v7.1-beta'
update_settings['prereleases'] = True
with ctx(['workflow:update'], update_settings) as (wf, c):
wf.run(fake)
wf.args
# Update command wasn't called
assert c.cmd == ()
def test_update_available(httpserver):
"""update_available property works"""
slug = UPDATE_SETTINGS['github_slug']
v = UPDATE_SETTINGS['version']
with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
with ctx() as (wf, c):
assert wf.update_available is False
update.check_update(slug, v)
assert wf.update_available is True
def test_update_turned_off():
"""Auto-update turned off"""
# Check update isn't performed if user has turned off
# auto-update
def fake(wf):
return
with ctx() as (wf, c):
wf.settings['__workflow_autoupdate'] = False
assert wf.check_update() is None
if __name__ == '__main__':
pytest.main([__file__])