CommitMessageGitHookTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Tests the commit message validation shell script used by the git hook and travis
  5. */
  6. class CommitMessageGitHookTest extends \PHPUnit_Framework_TestCase {
  7. protected $scriptsDir;
  8. protected $filesDir;
  9. protected $validateScript;
  10. public function setUp() {
  11. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  12. $this->markTestSkipped('Can only test in *nix envs.');
  13. }
  14. $this->scriptsDir = dirname(dirname(dirname(dirname(__DIR__)))) . '/.scripts/';
  15. $this->filesDir = dirname(__DIR__) . '/test_files/commit_messages/';
  16. $this->validateScript = "php {$this->scriptsDir}validate_commit_msg.php";
  17. parent::setUp();
  18. }
  19. /**
  20. * Test failures for missing input
  21. */
  22. public function testRejectsEmptyStringInput() {
  23. // have to pass an empty arg because it looks for stdin
  24. $cmd = "$this->validateScript ''";
  25. $result = $this->runCmd($cmd, $output);
  26. $this->assertFalse($result, $output);
  27. }
  28. public function testRejectsEmptyFileInput() {
  29. $cmd = "$this->validateScript /dev/null";
  30. $result = $this->runCmd($cmd, $output);
  31. $this->assertFalse($result, $output);
  32. }
  33. public function testRejectsEmptyPipeInput() {
  34. $cmd = "echo '' | $this->validateScript";
  35. $result = $this->runCmd($cmd, $output);
  36. $this->assertFalse($result, $output);
  37. }
  38. public function testRejectsInvalidFileInput() {
  39. $cmd = "$this->validateScript {$this->filesDir}invalid_format.txt";
  40. $result = $this->runCmd($cmd, $output);
  41. $this->assertFalse($result, $output);
  42. }
  43. public function testAcceptsValidFileInput() {
  44. $cmd = "$this->validateScript {$this->filesDir}valid.txt";
  45. $result = $this->runCmd($cmd, $output);
  46. $this->assertTrue($result, $output);
  47. }
  48. public function testAcceptsValidPipeInput() {
  49. $msg = escapeshellarg(file_get_contents("{$this->filesDir}valid.txt"));
  50. $cmd = "echo $msg | $this->validateScript";
  51. $result = $this->runCmd($cmd, $output);
  52. $this->assertTrue($result, $output);
  53. }
  54. public function testAcceptsValidStringInput() {
  55. $msg = escapeshellarg(file_get_contents("{$this->filesDir}valid.txt"));
  56. $cmd = "$this->validateScript $msg";
  57. $result = $this->runCmd($cmd, $output);
  58. $this->assertTrue($result, $output);
  59. }
  60. /**
  61. * Executes a command and returns true if the cmd
  62. * exited with 0.
  63. *
  64. * @param string $cmd Shell command to execute
  65. * @param string $output Output from stdout and stderr will be written to this variable
  66. * @param array $env Array of environment variables to be passed to sub-process
  67. * @return bool Result depending on process exit code.
  68. */
  69. protected function runCmd($cmd, &$output, array $env = array()) {
  70. $descriptorspec = array(
  71. 0 => array("pipe", "r"),// stdin
  72. 1 => array("pipe", "w"),// stdout
  73. 2 => array("pipe", "w"),// stderr
  74. );
  75. $defaultEnv = array(
  76. 'PATH' => getenv('PATH'),// we need to copy PATH variable to run php without specifying absolute path
  77. );
  78. $env = array_merge($defaultEnv, $env);
  79. $process = proc_open($cmd, $descriptorspec, $pipes, null, $env);
  80. $this->assertTrue(is_resource($process));
  81. // unfortunately we separate errors from output, but it should be good enough for current usage
  82. $output = stream_get_contents($pipes[1]) . stream_get_contents($pipes[2]);
  83. $exit = proc_close($process);
  84. return $exit > 0 ? false : true;
  85. }
  86. }