Jelajahi Sumber

Add smoketesting of running main function

Thomas Kluyver 6 tahun lalu
induk
melakukan
b83a5aacf1

+ 18 - 13
nsist/__init__.py

@@ -433,19 +433,20 @@ if __name__ == '__main__':
 
         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.
@@ -480,6 +481,8 @@ if __name__ == '__main__':
 
             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.
@@ -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 - 0
nsist/tests/console_example/guessnumber.py

@@ -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 - 0
nsist/tests/console_example/installer.cfg

@@ -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 - 0
nsist/tests/test_main.py

@@ -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')