Commit e40589b6 authored by Simon Welsh's avatar Simon Welsh

Add the framework for running a test

Follows a PHPT-esque style of test definition. At the moment, the only
supported parts are FILE (required) and PASS (1 or 0, if set). The FILE
part is written to a temp file, hh_server --check is run, then the file
is deleted.

PASS is currently checked based on the exit code of hh_server, with 0
being passed, 1 being failed to typecheck and anything else being a
failure.
parent 54eb4c30
stages:
- typecheck
- test
- deploy
before_script:
- "echo 'assume_php=false' > .hhconfig"
- composer install
......@@ -9,11 +14,19 @@ cache:
hack:
script:
- hh_server --check .
stage: typecheck
tags:
- hhvm
phpunit:
script:
- hhvm vendor/bin/phpunit --bootstrap tests/HHITest.php tests/
stage: test
tags:
- hhvm
packagist:
type: deploy
stage: deploy
only:
- tags
script:
......
<?hh // strict
namespace PHPUnitHHI;
abstract class HHITest extends \PHPUnit_Framework_TestCase {
protected function parseFile(string $testName): \ConstMap<string, string> {
$file = __DIR__.'/runs/'.$testName.'.hhit';
$parts = explode('---', file_get_contents($file));
$head = array_shift($parts);
$this->assertEmpty(
$head,
'File for test '.$testName.' has invalid start',
);
invariant(count($parts) % 2 == 0, 'Invalid file for test: '.$file);
$mapping = Map {};
for ($i = 0; $i < count($parts); $i += 2) {
$key = strtoupper(trim($parts[$i]));
$mapping[$key] = trim($parts[$i + 1]);
}
return $mapping->toImmMap();
}
protected function runTestFile(string $testName): void {
$parts = $this->parseFile($testName);
$this->assertTrue(
$parts->contains('FILE'),
'Test file for '.$testName.' is missing a FILE section',
);
$dumpPath = __DIR__.'/output/test-'.md5($parts['FILE']).'.php';
try {
file_put_contents($dumpPath, $parts['FILE']);
// We use hh_server --check instead of \HH\Client\typecheck so that we
// ignore timeouts and don't have to sleep between runs to ensure the
// mtime for the cache check hass passed.
$path = \escapeshellarg(dirname(__DIR__));
$cmd = \sprintf('hh_server --check %s 2>/dev/null', $path);
$ret = null;
$output_arr = null;
$output = \exec($cmd, $output_arr, $ret);
if ($parts->contains('PASS')) {
if ($parts['PASS'] == '1') {
$this->assertEquals(0, $ret, $testName.' did not pass: '.$output);
} else {
$this->assertEquals(1, $ret, $testName.' did pass: '.$output);
}
}
} finally {
unlink($dumpPath);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment