test_main.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import io
  2. import json
  3. from pathlib import Path
  4. import pytest
  5. import re
  6. import responses
  7. from shutil import copytree
  8. from subprocess import run, PIPE
  9. import sys
  10. from testpath import MockCommand, assert_isfile, assert_isdir
  11. from zipfile import ZipFile
  12. from nsist import main
  13. from nsist.util import CACHE_ENV_VAR
  14. from .utils import test_dir, only_on_windows
  15. example_dir = Path(test_dir, 'console_example')
  16. @pytest.fixture()
  17. def console_eg_copy(tmp_path):
  18. dst = tmp_path / 'example'
  19. copytree(str(example_dir), str(dst))
  20. return dst
  21. def respond_python_zip(req):
  22. buf = io.BytesIO()
  23. with ZipFile(buf, 'w') as zf:
  24. zf.writestr('python.exe', b'')
  25. return 200, {}, buf.getvalue()
  26. @responses.activate
  27. def test_console_example(tmp_path, console_eg_copy, monkeypatch):
  28. responses.add_callback('GET', re.compile(r'https://www.python.org/ftp/.*'),
  29. callback=respond_python_zip, content_type='application/zip',
  30. )
  31. monkeypatch.chdir(console_eg_copy)
  32. monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / 'cache'))
  33. with MockCommand('makensis') as makensis:
  34. ec = main(['installer.cfg'])
  35. assert ec == 0
  36. assert makensis.get_calls()[0]['argv'][1].endswith('installer.nsi')
  37. build_dir = console_eg_copy / 'build' / 'nsis'
  38. assert_isdir(build_dir)
  39. assert_isfile(build_dir / 'Python' / 'python.exe')
  40. assert_isfile(build_dir / 'pkgs' / 'sample_printer' / '__init__.py')
  41. assert_isfile(build_dir / 'Sample_printer.launch.py')
  42. # To exclude tests requiring network on an unplugged machine, use: pytest -m "not network"
  43. @only_on_windows
  44. @pytest.mark.network
  45. def test_installer(console_eg_copy, tmp_path):
  46. # Create installer
  47. run(
  48. [sys.executable, '-m', 'nsist', 'installer.cfg'],
  49. cwd=str(console_eg_copy),
  50. check=True,
  51. )
  52. installer = console_eg_copy / 'build' / 'nsis' / 'Guess_the_Number_1.0.exe'
  53. inst_dir = tmp_path / 'inst'
  54. # Run installer
  55. run([str(installer), '/S', '/D={}'.format(inst_dir)], check=True)
  56. inst_python = inst_dir / 'Python' / 'python.exe'
  57. inst_launch_script = inst_dir / 'Sample_printer.launch.py'
  58. assert_isfile(inst_python)
  59. assert_isfile(inst_launch_script)
  60. assert_isfile(inst_dir / 'pkgs' / 'sample_printer' / '__init__.py')
  61. # Run installed program
  62. res = run([str(inst_python), str(inst_launch_script)],
  63. check=True, stdout=PIPE)
  64. json_res = json.loads(res.stdout)
  65. assert json_res['py_executable'] == str(inst_python)
  66. assert json_res['py_version'].startswith('3.6.3') # Set in installer.cfg
  67. assert json_res['data_file_path'].endswith('data.txt')
  68. assert json_res['data_file_content'] == 'newt'