Skip to content

Commit

Permalink
Add smoketesting of running main function
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Oct 5, 2018
1 parent 0fb1d8f commit b83a5aa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
31 changes: 18 additions & 13 deletions nsist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,20 @@ def run_nsis(self):
Returns the exit code.
"""
try:
if os.name == 'nt':
makensis = shutil.which('makensis')
if (makensis is None) and os.name == 'nt':
try:
makensis = find_makensis_win()
else:
makensis = 'makensis'
logger.info('\n~~~ Running makensis ~~~')
return call([makensis, self.nsi_file])
except OSError as e:
# This should catch either the registry key or makensis being absent
if e.errno == errno.ENOENT:
print("makensis was not found. Install NSIS and try again.")
print("http://nsis.sourceforge.net/Download")
return 1
except OSError:
pass

if makensis is None:
print("makensis was not found. Install NSIS and try again.")
print("http://nsis.sourceforge.net/Download")
return 1

logger.info('\n~~~ Running makensis ~~~')
return call([makensis, self.nsi_file])

def run(self, makensis=True):
"""Run all the steps to build an installer.
Expand Down Expand Up @@ -480,6 +481,8 @@ def run(self, makensis=True):

if not exitcode:
logger.info('Installer written to %s', pjoin(self.build_dir, self.installer_name))
return exitcode
return 0

def main(argv=None):
"""Make an installer from the command line.
Expand Down Expand Up @@ -513,8 +516,10 @@ def main(argv=None):
args = get_installer_builder_args(cfg)

try:
InstallerBuilder(**args).run(makensis=(not options.no_makensis))
ec = InstallerBuilder(**args).run(makensis=(not options.no_makensis))
except InputError as e:
logger.error("Error in config values:")
logger.error(str(e))
sys.exit(1)

return ec
28 changes: 28 additions & 0 deletions nsist/tests/console_example/guessnumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""A fun number guessing game!"""

import random

def main():
number = random.randint(1, 100)
guesses = 0

print("I'm thinking of a number, between 1 and 100. Can you guess what it is?")
while True:
guesses += 1
guess = input('= ')
try:
guess = int(guess)
except ValueError:
print("Base 10 integers only, please.")
continue

if guess > number:
print("Too high!")
elif guess < number:
print("Too low!")
else:
print("That's right, {}. You got it in {} guesses.".format(number, guesses))
break

print()
input("Press enter to quit.")
19 changes: 19 additions & 0 deletions nsist/tests/console_example/installer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Application]
name=Guess the Number
version=1.0
entry_point=guessnumber:main
# We need to set this to get a console:
console=true

[Python]
version=3.6.3
bitness=64
format=bundled

[Include]
packages=guessnumber

# This optional section adds a command which can be run from the Windows
# command prompt.
[Command guessnumber]
entry_point=guessnumber:main
42 changes: 42 additions & 0 deletions nsist/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import io
from pathlib import Path
import re
import responses
from shutil import copy
from testpath import MockCommand, assert_isfile, assert_isdir
from testpath.tempdir import TemporaryWorkingDirectory
from zipfile import ZipFile

from nsist import main
from .utils import test_dir

example_dir = Path(test_dir, 'console_example')

def respond_python_zip():
buf = io.BytesIO()
with ZipFile(buf, 'w') as zf:
zf.writestr('python.exe', b'')
return 200, {}, buf

@responses.activate
def test_console_example():
responses.add_callback('GET', re.compile(r'https://www.python.org/ftp/.*'),
callback=respond_python_zip, content_type='application/zip',
)

with TemporaryWorkingDirectory() as td:
for src in example_dir.iterdir():
copy(str(src), td)


with MockCommand('makensis') as makensis:
ec = main(['installer.cfg'])

assert ec == 0
assert makensis.get_calls()[0]['argv'][1].endswith('installer.nsi')

build_dir = Path(td, 'build', 'nsis')
assert_isdir(build_dir)
assert_isfile(build_dir / 'Python' / 'python.exe')
assert_isfile(build_dir / 'pkgs' / 'guessnumber.py')
assert_isfile(build_dir / 'Guess_the_Number.launch.py')

0 comments on commit b83a5aa

Please sign in to comment.