utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import sys
  2. from subprocess import Popen, PIPE
  3. def get_output_error_code(cmd):
  4. """Get stdout, stderr, and exit code from running a command"""
  5. p = Popen(cmd, stdout=PIPE, stderr=PIPE)
  6. out, err = p.communicate()
  7. out = out.decode('utf8', 'replace')
  8. err = err.decode('utf8', 'replace')
  9. return out, err, p.returncode
  10. def check_help_output(pkg, subcommand=None):
  11. """test that `python -m PKG [subcommand] -h` works"""
  12. cmd = [sys.executable, '-m', pkg]
  13. if subcommand:
  14. cmd.extend(subcommand)
  15. cmd.append('-h')
  16. out, err, rc = get_output_error_code(cmd)
  17. assert rc == 0, err
  18. assert "Traceback" not in err
  19. assert "Options" in out
  20. assert "--help-all" in out
  21. return out, err
  22. def check_help_all_output(pkg, subcommand=None):
  23. """test that `python -m PKG --help-all` works"""
  24. cmd = [sys.executable, '-m', pkg]
  25. if subcommand:
  26. cmd.extend(subcommand)
  27. cmd.append('--help-all')
  28. out, err, rc = get_output_error_code(cmd)
  29. assert rc == 0, err
  30. assert "Traceback" not in err
  31. assert "Options" in out
  32. assert "Class parameters" in out
  33. return out, err