3030
3131from mslib .msui .updater import Updater
3232from mslib .utils import Worker
33- from mslib import __version__
3433
3534
36- class SubprocessVersionMock :
37- def __init__ (self ):
38- self .stdout = "*mss 999.999.999\n "
35+ class SubprocessDifferentVersionMock :
36+ def __init__ (self , args = None , ** named_args ):
3937 self .returncode = 0
38+ self .args = args
39+ if args and "list" in args and "mss" in args :
40+ self .stdout = "*mss 0.0.0\n "
41+ else :
42+ self .stdout = "*mss 999.999.999\n "
4043
4144
4245class SubprocessSameMock :
43- def __init__ (self ):
44- self .stdout = f "*mss { __version__ } \n "
46+ def __init__ (self , args = None , ** named_args ):
47+ self .stdout = "*mss 999.999.999 \n "
4548 self .returncode = 0
49+ self .args = args
4650
4751
4852class SubprocessGitMock :
49- def __init__ (self ):
53+ def __init__ (self , args = None , ** named_args ):
5054 self .stdout = "true"
55+ self .args = args
5156
5257
5358def create_mock (function , on_success = None , on_failure = None , start = True ):
@@ -70,110 +75,54 @@ def teardown(self):
7075 self .application .quit ()
7176 QtWidgets .QApplication .processEvents ()
7277
73- @mock .patch ("subprocess.run" , return_value = SubprocessVersionMock ())
78+ @mock .patch ("subprocess.Popen" , new = SubprocessDifferentVersionMock )
79+ @mock .patch ("subprocess.run" , new = SubprocessDifferentVersionMock )
7480 @mock .patch ("mslib.utils.Worker.create" , create_mock )
75- def test_update_recognised (self , mock ):
81+ def test_update_recognised (self ):
7682 update_available = False
77- status = ""
78- progress = 0
79- finished = False
80-
81- def status_changed (s ):
82- nonlocal status
83- status = s
84-
85- def progress_changed (value ):
86- nonlocal progress
87- progress = value
8883
8984 def update_signal ():
9085 nonlocal update_available
9186 update_available = True
9287
93- def finished_signal ():
94- nonlocal finished
95- finished = True
96-
97- self .updater .on_status_update .connect (status_changed )
98- self .updater .on_update_finished .connect (finished_signal )
99- self .updater .on_progress_update .connect (progress_changed )
10088 self .updater .on_update_available .connect (update_signal )
10189 self .updater .run ()
10290
10391 assert self .updater .new_version == "999.999.999"
10492 assert update_available
93+ self .updater .new_version = "0.0.0"
10594
10695 self .updater .update_mss ()
10796 assert self .updater .base_path == "999.999.999"
10897 assert self .updater .current_path == "999.999.999"
109- assert progress == 100
110- assert finished
111- assert status == "Update finished. Please restart MSS."
98+ assert self .updater .progressBar .value () == 100
99+ assert self .updater .statusLabel .text () == "Update successful. Please restart MSS."
112100
113- @mock .patch ("subprocess.run" , return_value = SubprocessSameMock ())
101+ @mock .patch ("subprocess.Popen" , new = SubprocessSameMock )
102+ @mock .patch ("subprocess.run" , new = SubprocessSameMock )
114103 @mock .patch ("mslib.utils.Worker.create" , create_mock )
115- def test_no_update (self , mock ):
116- update_available = False
117- status = ""
118-
119- def update_signal ():
120- nonlocal update_available
121- update_available = True
122-
123- def status_changed (s ):
124- nonlocal status
125- status = s
126-
127- self .updater .on_update_available .connect (update_signal )
128- self .updater .on_status_update .connect (status_changed )
104+ def test_no_update (self ):
129105 self .updater .run ()
106+ assert self .updater .statusLabel .text () == "Your MSS is up to date."
130107
131- assert self .updater .new_version == __version__
132- assert not update_available
133- assert status == "Your MSS is up to date."
134-
135- @mock .patch ("subprocess.run" , return_value = SubprocessGitMock ())
108+ @mock .patch ("subprocess.Popen" , new = SubprocessGitMock )
109+ @mock .patch ("subprocess.run" , new = SubprocessGitMock )
136110 @mock .patch ("mslib.utils.Worker.create" , create_mock )
137- def test_no_update_on_git (self , mock ):
138- update_available = False
139-
140- def update_signal ():
141- nonlocal update_available
142- update_available = True
143-
144- self .updater .on_update_available .connect (update_signal )
111+ def test_no_update_on_git (self ):
145112 self .updater .run ()
146-
147113 assert self .updater .new_version is None
148- assert not update_available
114+ assert self . updater . statusLabel . text () == "Nothing to do"
149115
150- @mock .patch ("subprocess.run" , return_value = SubprocessVersionMock ())
116+ @mock .patch ("subprocess.Popen" , new = SubprocessDifferentVersionMock )
117+ @mock .patch ("subprocess.run" , new = SubprocessDifferentVersionMock )
151118 @mock .patch ("mslib.utils.Worker.create" , create_mock )
152- def test_update_failed (self , mock ):
119+ def test_update_failed (self ):
153120 update_available = False
154- status = ""
155- progress = 0
156- finished = False
157-
158- def status_changed (s ):
159- nonlocal status
160- status = s
161-
162- def progress_changed (value ):
163- nonlocal progress
164- progress = value
165121
166122 def update_signal ():
167123 nonlocal update_available
168124 update_available = True
169125
170- def finished_signal ():
171- nonlocal finished
172- finished = True
173-
174- self .updater .on_status_update .connect (status_changed )
175- self .updater .on_update_finished .connect (finished_signal )
176- self .updater .on_progress_update .connect (progress_changed )
177126 self .updater .on_update_available .connect (update_signal )
178127 self .updater .run ()
179128
@@ -182,14 +131,12 @@ def finished_signal():
182131 self .updater .new_version = "1000.1000.1000"
183132
184133 self .updater .update_mss ()
185- assert progress == 45
186- assert not finished
187- assert status == "Update failed, please do it manually."
188-
189- @mock .patch ("subprocess.run" , return_value = SubprocessVersionMock ())
190- @mock .patch ("shutil.copytree" , return_value = None )
191- @mock .patch ("shutil.rmtree" , return_value = None )
192- def test_environment_replace (self , subprocess_mock , shutil1 , shutil2 ):
193- self .updater .new_version = "999.999.999"
134+ assert self .updater .statusLabel .text () == "Update failed. Please try it manually or " \
135+ "try replacing the environment!"
136+
137+ @mock .patch ("subprocess.Popen" , new = SubprocessDifferentVersionMock )
138+ @mock .patch ("subprocess.run" , new = SubprocessDifferentVersionMock )
139+ def test_environment_replace (self ):
140+ self .updater .new_version = "0.0.0"
194141 self .updater ._set_base_env_path ()
195142 assert self .updater ._try_environment_replace ()
0 commit comments