Skip to content

Commit 48cc908

Browse files
committed
Make command_line.py more testing friendly and add download tests
1 parent d3ad662 commit 48cc908

File tree

6 files changed

+57
-25
lines changed

6 files changed

+57
-25
lines changed

command_line.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ def get_settings(self):
105105

106106
contents = codecs.open(config_file, encoding='utf-8').read()
107107

108-
contents = contents.replace('{DEFAULT_DOWNLOAD_PATH}',
109-
config.DEFAULT_DOWNLOAD_PATH)
110-
111108
config_io = StringIO(contents)
112109
config_obj = ConfigObj(config_io, unrepr=True).dict()
113110

@@ -362,7 +359,8 @@ def download_file_with_error_handling(self):
362359
in the GUI.
363360
"""
364361
setting = self.files_to_download.pop()
365-
location = self.get_setting('download_dir').value
362+
location = (self.get_setting('download_dir').value or
363+
config.download_path())
366364
version = self.selected_version()
367365
path = setting.url.format(version, version)
368366

@@ -583,7 +581,8 @@ def selected_version(self):
583581
def extract_files(self):
584582
"""Extract nw.js files to the specific version path"""
585583
self.extract_error = None
586-
location = self.get_setting('download_dir').value
584+
location = (self.get_setting('download_dir').value or
585+
config.download_path())
587586

588587
sdk_build_setting = self.get_setting('sdk_build')
589588
sdk_build = sdk_build_setting.value
@@ -1387,7 +1386,8 @@ def download_file(self, path, setting):
13871386
"""Download a file from the path and setting"""
13881387
self.logger.info('Downloading file {}.'.format(path))
13891388

1390-
location = self.get_setting('download_dir').value
1389+
location = (self.get_setting('download_dir').value or
1390+
config.download_path())
13911391

13921392
sdk_build_setting = self.get_setting('sdk_build')
13931393
sdk_build = sdk_build_setting.value
@@ -1496,7 +1496,7 @@ def error(self, message):
14961496
sys.stderr.write('error: {}\n'.format(message))
14971497
sys.exit(2)
14981498

1499-
def get_arguments(command_base):
1499+
def get_arguments(command_base, args=None):
15001500
"""Retrieves arguments from the command line"""
15011501

15021502
parser = ArgParser(description=('Command line interface '
@@ -1535,6 +1535,9 @@ def get_arguments(command_base):
15351535
help=('Choose at least one system '
15361536
'to export to.'))
15371537

1538+
if args:
1539+
return parse.parse_args(args)
1540+
15381541
return parser.parse_args()
15391542

15401543
def generate_setting_args(command_base, parser):
@@ -1678,12 +1681,12 @@ def initialize_setting_values(args, command_base):
16781681
if setting is not None:
16791682
setting.value = val
16801683

1681-
def main():
1684+
def main(args=None):
16821685
"""Main setup and argument parsing"""
16831686
command_base = CommandBase()
16841687
command_base.init()
16851688

1686-
args = get_arguments(command_base)
1689+
args = get_arguments(command_base, args)
16871690

16881691
setup_logging(args, command_base)
16891692
setup_directories(args, command_base)

config.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
MAX_RECENT = 10
2020

2121
DEBUG = False
22+
TESTING = False
2223

2324
SSL_CONTEXT = ssl._create_unverified_context()
2425

@@ -66,7 +67,6 @@ def is_installed():
6667
W2E_VER_FILE = 'files/version.txt'
6768

6869
TEMP_DIR = utils.get_temp_dir()
69-
DEFAULT_DOWNLOAD_PATH = utils.get_data_path('files/downloads')
7070

7171
ERROR_LOG_FILE = 'files/error.log'
7272
VER_FILE = 'files/nw-versions.txt'
@@ -91,6 +91,7 @@ def is_installed():
9191
## Logger setup ----------------------------------------------
9292

9393
LOG_FILENAME = utils.get_data_file_path(ERROR_LOG_FILE)
94+
9495
if __name__ != '__main__':
9596
logging.basicConfig(
9697
filename=LOG_FILENAME,
@@ -104,15 +105,18 @@ def is_installed():
104105
## Custom except hook to log all errors ----------------------
105106

106107
def my_excepthook(type_, value, tback):
107-
output_err = ''.join([x for x in traceback.format_exception(type_, value, tback)])
108+
output_err = ''.join([x for x in traceback.format_exception(type_, value,
109+
tback)])
108110
logger.error('{}'.format(output_err))
109111
sys.__excepthook__(type_, value, tback)
110112

111113
sys.excepthook = my_excepthook
112114

113-
114-
# Ensure that the default download path exists
115-
try:
116-
os.makedirs(DEFAULT_DOWNLOAD_PATH)
117-
except:
118-
pass
115+
def download_path(path=None):
116+
# Ensure that the default download path exists
117+
path = path or utils.get_data_path('files/downloads')
118+
try:
119+
os.makedirs(path)
120+
except:
121+
pass
122+
return path

main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import logging
2828
import platform
2929

30+
import config
31+
3032
import utils
3133
from utils import log, open_folder_in_explorer
3234

33-
import config
3435
from config import get_file
3536
from config import __version__ as __gui_version__
3637

@@ -730,7 +731,7 @@ def download_file(self, path, setting):
730731
sdk_build_setting = self.get_setting('sdk_build')
731732
sdk_build = sdk_build_setting.value
732733

733-
location = self.get_setting('download_dir').value
734+
location = self.get_setting('download_dir').value or config.download_path()
734735

735736
if sdk_build:
736737
# Switch the download URL if an sdk build is selected

tests/test_command_line.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
from command_line import CommandBase
2-
import utils
1+
import os
32
import config
3+
import utils
44
import pytest
55

6+
from command_line import CommandBase
7+
68
api = pytest.mark.skipif(
79
not pytest.config.getoption("--runapi"),
810
reason="need --runapi option to run"
911
)
1012

1113
@pytest.fixture(scope='module')
1214
def command_base():
15+
config.TESTING = True
16+
dpath = utils.get_data_path('')
17+
18+
if os.path.exists(dpath):
19+
utils.rmtree(dpath)
20+
1321
base = CommandBase()
1422
base._project_name = 'Test'
1523
return base
@@ -71,7 +79,6 @@ def test_get_default_nwjs_branch(command_base):
7179
assert match != None
7280

7381
def test_get_versions(command_base):
74-
import os
7582
path = utils.get_data_file_path(config.VER_FILE)
7683

7784
if os.path.exists(path):
@@ -83,4 +90,16 @@ def test_get_versions(command_base):
8390
data = ver_file.read()
8491
assert len(data) > 0
8592

93+
def test_download_nwjs(command_base):
94+
command_base.get_setting('nw_version').value = '0.19.0'
95+
command_base.get_setting('windows-x64').value = True
96+
command_base.init()
97+
command_base.get_files_to_download()
98+
99+
command_base.download_file_with_error_handling()
100+
101+
base, _ = os.path.split(__file__)
86102

103+
assert os.path.exists(utils.path_join(base, 'test_data', 'files',
104+
'downloads',
105+
'nwjs-v0.19.0-win-x64.zip'))

util_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def save_file_path(self, version, location=None, sdk_build=False):
429429
if location:
430430
self.save_path = location
431431
else:
432-
self.save_path = self.save_path or config.DEFAULT_DOWNLOAD_PATH
432+
self.save_path = location or config.download_path()
433433

434434

435435
self.get_file_information_from_url()

utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import validators
1818
import traceback
1919
import logging
20+
import config
2021

2122
from PySide import QtCore
2223

@@ -136,9 +137,13 @@ def path_join(base, *rest):
136137
return rpath
137138

138139
def get_data_path(dir_path):
140+
139141
parts = dir_path.split('/')
140-
dirs = AppDirs('Web2Executable', 'Web2Executable')
141-
data_path = path_join(dirs.user_data_dir, *parts)
142+
if config.TESTING:
143+
data_path = path_join(config.CWD, 'tests', 'test_data', *parts)
144+
else:
145+
dirs = AppDirs('Web2Executable', 'Web2Executable')
146+
data_path = path_join(dirs.user_data_dir, *parts)
142147

143148
if is_windows():
144149
data_path = data_path.replace('\\', '/')

0 commit comments

Comments
 (0)