test_commands.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import io
  2. from testpath import assert_isfile, assert_not_path_exists
  3. from zipfile import ZipFile
  4. from nsist import commands
  5. def test_prepare_bin_dir(tmp_path):
  6. cmds = {
  7. 'acommand': {
  8. 'entry_point': 'somemod:somefunc',
  9. 'extra_preamble': io.StringIO(u'import extra')
  10. }
  11. }
  12. commands.prepare_bin_directory(tmp_path, cmds)
  13. exe_file = str(tmp_path / 'acommand.exe')
  14. assert_isfile(exe_file)
  15. with open(commands.find_exe(console=True), 'rb') as lf:
  16. b_launcher = lf.read()
  17. assert b_launcher[:2] == b'MZ' # Sanity check
  18. with open(exe_file, 'rb') as ef:
  19. b_exe = ef.read()
  20. assert b_exe[:len(b_launcher)] == b_launcher
  21. assert b_exe[len(b_launcher):].startswith(b"#!<launcher_dir>\\..\\Python\\python.exe\r\n")
  22. with ZipFile(exe_file) as zf:
  23. assert zf.testzip() is None
  24. script_contents = zf.read('__main__.py').decode('utf-8')
  25. assert 'import extra' in script_contents
  26. assert 'somefunc()' in script_contents
  27. def test_prepare_bin_dir_noconsole(tmp_path):
  28. cmds = {
  29. 'acommand': {
  30. 'entry_point': 'somemod:somefunc',
  31. 'console': False
  32. }
  33. }
  34. commands.prepare_bin_directory(tmp_path, cmds)
  35. exe_file = str(tmp_path / 'acommand.exe')
  36. assert_isfile(exe_file)
  37. with open(commands.find_exe(console=False), 'rb') as lf:
  38. b_launcher = lf.read()
  39. assert b_launcher[:2] == b'MZ' # Sanity check
  40. with open(exe_file, 'rb') as ef:
  41. b_exe = ef.read()
  42. assert b_exe[:len(b_launcher)] == b_launcher
  43. assert b_exe[len(b_launcher):].startswith(b"#!<launcher_dir>\\..\\Python\\pythonw.exe\r\n")
  44. with ZipFile(exe_file) as zf:
  45. assert zf.testzip() is None
  46. script_contents = zf.read('__main__.py').decode('utf-8')
  47. assert 'somefunc()' in script_contents