test_commands.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import io
  2. from testpath import assert_isfile, assert_not_path_exists
  3. from zipfile import ZipFile
  4. from nsist import commands, _assemble_launchers
  5. def test_prepare_bin_dir(tmpdir):
  6. cmds = {
  7. 'acommand': {
  8. 'entry_point': 'somemod:somefunc',
  9. 'extra_preamble': io.StringIO(u'import extra')
  10. }
  11. }
  12. commands.prepare_bin_directory(tmpdir, cmds)
  13. launcher_file = str(tmpdir / 'launcher_exe.dat')
  14. launcher_noconsole_file = str(tmpdir / 'launcher_noconsole_exe.dat')
  15. zip_file = str(tmpdir / 'acommand-append.zip')
  16. zip_file_invalid = str(tmpdir / 'acommand-append-noconsole.zip')
  17. exe_file = str(tmpdir / 'acommand.exe')
  18. assert_isfile(launcher_file)
  19. assert_isfile(launcher_noconsole_file)
  20. assert_isfile(zip_file)
  21. assert_not_path_exists(zip_file_invalid)
  22. assert_not_path_exists(exe_file)
  23. with open(launcher_file, 'rb') as lf:
  24. b_launcher = lf.read()
  25. assert b_launcher[:2] == b'MZ'
  26. with open(launcher_noconsole_file, 'rb') as lf:
  27. assert lf.read(2) == b'MZ'
  28. with ZipFile(zip_file) as zf:
  29. assert zf.testzip() is None
  30. script_contents = zf.read('__main__.py').decode('utf-8')
  31. assert 'import extra' in script_contents
  32. assert 'somefunc()' in script_contents
  33. _assemble_launchers.main(['_assemble_launchers.py', 'C:\\path\\to\\python', str(tmpdir)])
  34. assert_isfile(exe_file)
  35. with open(exe_file, 'rb') as ef, open(zip_file, 'rb') as zf:
  36. b_exe = ef.read()
  37. b_zip = zf.read()
  38. assert b_exe[:len(b_launcher)] == b_launcher
  39. assert b_exe[len(b_launcher):-len(b_zip)].decode('utf-8') == '#!"C:\\path\\to\\python.exe"\r\n'
  40. assert b_exe[-len(b_zip):] == b_zip
  41. with ZipFile(exe_file) as zf:
  42. assert zf.testzip() is None
  43. assert zf.read('__main__.py').decode('utf-8') == script_contents
  44. def test_prepare_bin_dir_noconsole(tmpdir):
  45. cmds = {
  46. 'acommand': {
  47. 'entry_point': 'somemod:somefunc',
  48. 'console': False
  49. }
  50. }
  51. commands.prepare_bin_directory(tmpdir, cmds)
  52. launcher_file = str(tmpdir / 'launcher_exe.dat')
  53. launcher_noconsole_file = str(tmpdir / 'launcher_noconsole_exe.dat')
  54. zip_file = str(tmpdir / 'acommand-append-noconsole.zip')
  55. zip_file_invalid = str(tmpdir / 'acommand-append.zip')
  56. exe_file = str(tmpdir / 'acommand.exe')
  57. assert_isfile(launcher_file)
  58. assert_isfile(launcher_noconsole_file)
  59. assert_isfile(zip_file)
  60. assert_not_path_exists(zip_file_invalid)
  61. assert_not_path_exists(exe_file)
  62. with open(launcher_file, 'rb') as lf:
  63. assert lf.read(2) == b'MZ'
  64. with open(launcher_noconsole_file, 'rb') as lf:
  65. b_launcher = lf.read()
  66. assert b_launcher[:2] == b'MZ'
  67. with ZipFile(zip_file) as zf:
  68. assert zf.testzip() is None
  69. script_contents = zf.read('__main__.py').decode('utf-8')
  70. assert 'import extra' not in script_contents
  71. assert 'somefunc()' in script_contents
  72. _assemble_launchers.main(['_assemble_launchers.py', 'C:\\custom\\python.exe', str(tmpdir)])
  73. assert_isfile(exe_file)
  74. with open(exe_file, 'rb') as ef, open(zip_file, 'rb') as zf:
  75. b_exe = ef.read()
  76. b_zip = zf.read()
  77. assert b_exe[:len(b_launcher)] == b_launcher
  78. assert b_exe[len(b_launcher):-len(b_zip)].decode('utf-8') == '#!"C:\\custom\\pythonw.exe"\r\n'
  79. assert b_exe[-len(b_zip):] == b_zip
  80. with ZipFile(exe_file) as zf:
  81. assert zf.testzip() is None
  82. assert zf.read('__main__.py').decode('utf-8') == script_contents