1
0
Эх сурвалжийг харах

Fix bug with preamble on Python 2

A check for a string was too strict, excluding a unicode string that
should have been accepted.
Thomas Kluyver 10 жил өмнө
parent
commit
d30f215349

+ 2 - 2
nsist/__init__.py

@@ -23,7 +23,7 @@ else:
 
 from .copymodules import copy_modules
 from .nsiswriter import NSISFileWriter
-from .util import download
+from .util import download, text_types
 
 __version__ = '1.0'
 
@@ -203,7 +203,7 @@ if __name__ == '__main__':
                                                 + ('' if sc['console'] else 'w')
 
                     specified_preamble = sc.get('extra_preamble', None)
-                    if isinstance(specified_preamble, str):
+                    if isinstance(specified_preamble, text_types):
                         # Filename
                         extra_preamble = io.open(specified_preamble, encoding='utf-8')
                     elif specified_preamble is None:

+ 1 - 0
nsist/tests/sample_preamble.py

@@ -0,0 +1 @@
+os.environ['NSIST_TEST_PREAMBLE'] = '1'

+ 3 - 3
nsist/tests/test_copymodules.py

@@ -6,15 +6,15 @@ import unittest
 
 pjoin = os.path.join
 
+from .utils import assert_is_file, assert_is_dir, test_dir
+
 running_python = '.'.join(str(x) for x in sys.version_info[:3])
-test_dir = os.path.dirname(__file__)
+
 sample_path = [pjoin(test_dir, 'sample_pkgs'),
                pjoin(test_dir, 'sample_zip.egg'),
                pjoin(test_dir, 'sample_zip.egg/rootdir'),
               ]
 
-from .utils import assert_is_file, assert_is_dir
-
 from nsist.copymodules import copy_modules, ExtensionModuleMismatch
 
 

+ 41 - 0
nsist/tests/test_installerbuilder.py

@@ -0,0 +1,41 @@
+import io
+from os.path import join as pjoin
+import shutil
+import tempfile
+import unittest
+
+from .utils import assert_is_file, test_dir
+from nsist import InstallerBuilder, DEFAULT_ICON
+
+sample_preamble = pjoin(test_dir, u'sample_preamble.py')
+
+class TestInstallerBuilder(unittest.TestCase):
+    def setUp(self):
+        self.target = tempfile.mkdtemp()
+    
+    def tearDown(self):
+        shutil.rmtree(self.target)
+    
+    def test_prepare_shortcuts(self):
+        shortcuts = {'sc1': {'entry_point': 'norwegian.blue:parrot',
+                             'icon': DEFAULT_ICON,
+                             'console': False,
+                             'extra_preamble': sample_preamble,
+                             }
+                    }
+        ib = InstallerBuilder("Test App", "1.0", shortcuts, build_dir=self.target)
+        ib.prepare_shortcuts()
+        
+        scfile = pjoin(self.target, 'sc1.launch.pyw')
+        assert_is_file(scfile)
+        
+        with io.open(scfile, 'r', encoding='utf-8') as f:
+            contents = f.read()
+        
+        last2lines = [l.strip() for l in contents.rstrip().splitlines()[-2:]]
+        assert last2lines == ['from norwegian.blue import parrot', 'parrot()']
+        
+        with io.open(sample_preamble, 'r', encoding='utf-8') as f:
+            preamble_contents = f.read().strip()
+        
+        assert preamble_contents in contents

+ 3 - 1
nsist/tests/utils.py

@@ -1,4 +1,6 @@
-from os.path import isfile, isdir, exists
+from os.path import isfile, isdir, exists, dirname
+
+test_dir = dirname(__file__)
 
 def assert_is_file(path):
     assert exists(path), "%s does not exist"

+ 9 - 0
nsist/util.py

@@ -1,4 +1,13 @@
 import requests
+import sys
+
+PY3 = sys.version_info[0] >= 3
+
+if PY3:
+    text_types = (str,)
+else:
+    text_types = (str, unicode) # analysis:ignore
+
 
 def download(url, target):
     """Download a file using requests.