Skip to content

Commit

Permalink
Enable ParaTest on itself (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Jun 20, 2022
1 parent 109925f commit b5e108b
Show file tree
Hide file tree
Showing 24 changed files with 191 additions and 138 deletions.
26 changes: 18 additions & 8 deletions .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
push:
branches:
- "master"
- "*.x"

jobs:
composer-json-lint:
Expand Down Expand Up @@ -194,24 +194,34 @@ jobs:
run: "composer update --no-interaction --no-progress"

- name: "Run tests"
timeout-minutes: 3
run: "vendor/bin/phpunit --coverage-clover=coverage.xml --coverage-xml=coverage/coverage-xml --log-junit=coverage/junit.xml"
timeout-minutes: 5
run: "make test"

- name: "Send code coverage report to Codecov.io"
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
file: ./coverage/clover.xml
fail_ci_if_error: true

- name: "Infection"
- name: "Gather base branch for diff"
if: ${{ github.event_name == 'pull_request' }}
run: git fetch origin --no-tags --prune --depth=1 ${{ github.base_ref }} ${{ github.event.pull_request.base.sha }}

- name: "Infection on DIFF"
if: ${{ github.event_name == 'pull_request' }}
timeout-minutes: 10
run: "vendor/bin/infection --ansi --threads=$(nproc) --coverage=coverage --skip-initial-tests"
run: make code-coverage INFECTION_ARGS="--logger-github"
env:
BASE_BRANCH: origin/${{ github.base_ref }}
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}

- name: "Output infections"
run: "cat infections.log"
- name: "Infection on complete code base"
if: ${{ github.event_name != 'pull_request' }}
timeout-minutes: 10
run: "vendor/bin/infection --ansi --threads=$(nproc) --skip-initial-tests --coverage=coverage"
env:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}

coding-standards:
name: "Coding Standards"
Expand Down
44 changes: 36 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

SRCS := $(shell find ./src ./test -type f -not -path "*/tmp/*")

LOCAL_BASE_BRANCH ?= $(shell git show-branch | sed "s/].*//" | grep "\*" | grep -v "$$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed "s/^.*\[//")
ifeq ($(strip $(LOCAL_BASE_BRANCH)),)
LOCAL_BASE_BRANCH := HEAD^
endif
BASE_BRANCH ?= $(LOCAL_BASE_BRANCH)

all: csfix static-analysis code-coverage
@echo "Done."

Expand All @@ -12,19 +21,38 @@ csfix: vendor

.PHONY: static-analysis
static-analysis: vendor
vendor/bin/psalm
vendor/bin/psalm $(PSALM_ARGS)

.PHONY: test
test: vendor
coverage/junit.xml: vendor $(SRCS) Makefile
php -d zend.assertions=1 vendor/bin/phpunit \
--coverage-xml=coverage/coverage-xml \
--no-coverage \
--no-logging \
$(PHPUNIT_ARGS)
php -d zend.assertions=1 bin/paratest \
--no-coverage \
--processes=1 \
$(PARATEST_ARGS)
php -d zend.assertions=1 bin/paratest \
--coverage-clover=coverage/clover.xml \
--coverage-xml=coverage/xml \
--coverage-html=coverage/html \
--log-junit=coverage/junit.xml \
${arg}
--log-junit=$@ \
--processes=$(shell nproc) \
$(PARATEST_ARGS) \
|| (rm $@ && exit 1)

.PHONY: test
test: coverage/junit.xml

.PHONY: code-coverage
code-coverage: test
code-coverage: coverage/junit.xml
echo "Base branch: $(BASE_BRANCH)"
php -d zend.assertions=1 vendor/bin/infection \
--threads=$(shell nproc) \
--git-diff-lines \
--git-diff-base=$(BASE_BRANCH) \
--skip-initial-tests \
--coverage=coverage \
--skip-initial-tests
--show-mutations \
--min-msi=100 \
$(INFECTION_ARGS)
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"ext-pcre": "*",
"ext-reflection": "*",
"ext-simplexml": "*",
"jean85/pretty-package-versions": "^2.0",
"jean85/pretty-package-versions": "^2.0.5",
"phpunit/php-code-coverage": "^9.2.15",
"phpunit/php-file-iterator": "^3.0.6",
"phpunit/php-timer": "^5.0.3",
"phpunit/phpunit": "^9.5.20",
"phpunit/phpunit": "^9.5.21",
"sebastian/environment": "^5.1.4",
"symfony/console": "^5.4.9 || ^6.1.1",
"symfony/process": "^5.4.8 || ^6.1.0"
Expand All @@ -52,7 +52,7 @@
"doctrine/coding-standard": "^9.0.0",
"infection/infection": "^0.26.11",
"malukenho/mcbumpface": "^1.1.5",
"squizlabs/php_codesniffer": "^3.7.0",
"squizlabs/php_codesniffer": "^3.7.1",
"symfony/filesystem": "^5.4.9 || ^6.1.0",
"vimeo/psalm": "^4.23.0"
},
Expand Down
7 changes: 2 additions & 5 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
},
"timeout": 10,
"logs": {
"text": "infections.log",
"stryker": {
"report": "master"
"report": "/^\\d+\\.x$/"
}
},
"mutators": {
Expand All @@ -17,7 +16,5 @@
"assert\\(.+\\);",
"mkdir\\(\\$dir, 0777, true\\);"
]
},
"minMsi": 79,
"minCoveredMsi": 80
}
}
47 changes: 27 additions & 20 deletions test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,39 @@
use ParaTest\Runners\PHPUnit\Options;
use ParaTest\Runners\PHPUnit\Runner;
use ParaTest\Runners\PHPUnit\RunnerInterface;
use PHPUnit;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use SebastianBergmann\Environment\Runtime;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;

use function file_exists;
use function glob;
use function getenv;
use function putenv;
use function sprintf;

abstract class TestBase extends PHPUnit\Framework\TestCase
abstract class TestBase extends TestCase
{
/** @var class-string<RunnerInterface> */
protected $runnerClass = Runner::class;
/** @var array<string, string|bool|int|null> */
protected $bareOptions = [];
/** @var string */
protected $tmpDir;

final protected function setUp(): void
{
$this->clearTmpDir();
$this->tmpDir = (new TmpDirCreator())->create();

$this->setUpTest();
}

final protected function tearDown(): void
{
$this->clearTmpDir();
}

protected function setUpTest(): void
{
}

private function clearTmpDir(): void
{
$glob = glob(TMP_DIR . DS . '*');
static::assertNotFalse($glob);

(new Filesystem())->remove($glob);
}

/**
* @param array<string, string|bool|int|null> $argv
*/
Expand All @@ -65,7 +54,7 @@ final protected function createOptionsFromArgv(array $argv, ?string $cwd = null)
}

if (! isset($argv['--tmp-dir'])) {
$argv['--tmp-dir'] = TMP_DIR;
$argv['--tmp-dir'] = $this->tmpDir;
}

$input = new ArrayInput($argv, $inputDefinition);
Expand All @@ -78,8 +67,26 @@ final protected function runRunner(?string $cwd = null): RunnerResult
$output = new BufferedOutput();
$runnerClass = $this->runnerClass;

$runner = new $runnerClass($this->createOptionsFromArgv($this->bareOptions, $cwd), $output);
$options = $this->createOptionsFromArgv($this->bareOptions, $cwd);
$shouldPutEnvForParatestTestingItSelf = $options->noTestTokens();
$runner = new $runnerClass($options, $output);
if ($shouldPutEnvForParatestTestingItSelf) {
$prevToken = getenv(Options::ENV_KEY_TOKEN);
$prevUniqueToken = getenv(Options::ENV_KEY_UNIQUE_TOKEN);

putenv(Options::ENV_KEY_TOKEN);
putenv(Options::ENV_KEY_UNIQUE_TOKEN);
unset($_SERVER[Options::ENV_KEY_TOKEN]);
unset($_SERVER[Options::ENV_KEY_UNIQUE_TOKEN]);
}

$runner->run();
if ($shouldPutEnvForParatestTestingItSelf) {
putenv(Options::ENV_KEY_TOKEN . '=' . $prevToken);
putenv(Options::ENV_KEY_UNIQUE_TOKEN . '=' . $prevUniqueToken);
$_SERVER[Options::ENV_KEY_TOKEN] = $prevToken;
$_SERVER[Options::ENV_KEY_UNIQUE_TOKEN] = $prevUniqueToken;
}

return new RunnerResult($runner->getExitCode(), $output->fetch());
}
Expand Down
29 changes: 29 additions & 0 deletions test/TmpDirCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests;

use PHPUnit\Framework\Assert;
use Symfony\Component\Filesystem\Filesystem;

use function getenv;
use function glob;

use const DIRECTORY_SEPARATOR;

final class TmpDirCreator
{
public function create(): string
{
$tmpDir = TEST_DIR . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'token_' . (string) getenv('TEST_TOKEN');

$glob = glob($tmpDir . DIRECTORY_SEPARATOR . '*');
Assert::assertNotFalse($glob);

(new Filesystem())->remove($glob);
(new Filesystem())->mkdir($tmpDir);

return $tmpDir;
}
}
6 changes: 3 additions & 3 deletions test/Unit/Console/Commands/ParaTestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ final class ParaTestCommandTest extends TestBase

public function setUpTest(): void
{
$application = ParaTestCommand::applicationFactory(TMP_DIR);
$application = ParaTestCommand::applicationFactory($this->tmpDir);
$application->add(new HelpCommand());

$this->commandTester = new CommandTester($application->find(ParaTestCommand::COMMAND_NAME));
}

public function testApplicationFactory(): void
{
$application = ParaTestCommand::applicationFactory(TMP_DIR);
$application = ParaTestCommand::applicationFactory($this->tmpDir);
$commands = $application->all();

static::assertArrayHasKey(ParaTestCommand::COMMAND_NAME, $commands);
Expand All @@ -49,7 +49,7 @@ public function testApplicationFactory(): void
public function testMessagePrintedWhenInvalidConfigFileSupplied(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage(sprintf('Could not read "%s%snope.xml"', TMP_DIR, DS));
static::expectExceptionMessage(sprintf('Could not read "%s%snope.xml"', $this->tmpDir, DS));

$this->commandTester->execute(['--configuration' => 'nope.xml']);
}
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Coverage/CoverageMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function testMerge(int $testLimit): void
'Test2'
);

$target1 = TMP_DIR . DS . 'coverage1.php';
$target2 = TMP_DIR . DS . 'coverage2.php';
$target1 = $this->tmpDir . DS . 'coverage1.php';
$target2 = $this->tmpDir . DS . 'coverage2.php';
$phpReport = new PHP();
$phpReport->process($coverage1, $target1);
$phpReport->process($coverage2, $target2);
Expand Down Expand Up @@ -108,7 +108,7 @@ public function provideTestLimit(): array

public function testCoverageFileIsEmpty(): void
{
$filename = TMP_DIR . DS . 'coverage.php';
$filename = $this->tmpDir . DS . 'coverage.php';
touch($filename);
$coverageMerger = new CoverageMerger(0);

Expand Down
12 changes: 6 additions & 6 deletions test/Unit/Coverage/CoverageReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private function createCoverageReporter(string $fixtureFile): void

public function testGenerateClover(): void
{
$target = TMP_DIR . DS . 'clover';
$target = $this->tmpDir . DS . 'clover';

$this->coverageReporter->clover($target);

Expand All @@ -60,7 +60,7 @@ public function testGenerateClover(): void

public function testGenerateCobertura(): void
{
$target = TMP_DIR . DS . 'cobertura';
$target = $this->tmpDir . DS . 'cobertura';

$this->coverageReporter->cobertura($target);

Expand All @@ -72,7 +72,7 @@ public function testGenerateCobertura(): void

public function testGenerateCrap4j(): void
{
$target = TMP_DIR . DS . 'crap4j';
$target = $this->tmpDir . DS . 'crap4j';

$this->coverageReporter->crap4j($target);

Expand All @@ -87,7 +87,7 @@ public function testGenerateCrap4j(): void

public function testGenerateHtml(): void
{
$target = TMP_DIR . DS . 'html';
$target = $this->tmpDir . DS . 'html';

$this->coverageReporter->html($target);

Expand All @@ -97,7 +97,7 @@ public function testGenerateHtml(): void

public function testGeneratePhp(): void
{
$target = TMP_DIR . DS . 'php';
$target = $this->tmpDir . DS . 'php';

$this->coverageReporter->php($target);

Expand Down Expand Up @@ -192,7 +192,7 @@ public function generateTextCoveredFilesProvider(): array

public function testGenerateXml(): void
{
$target = TMP_DIR . DS . 'xml';
$target = $this->tmpDir . DS . 'xml';

$this->coverageReporter->xml($target);

Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Logging/JUnit/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testDataProviderWithSpecialCharacters(): void

public function testWriteToFile(): void
{
$output = TMP_DIR . DS . 'passing.xml';
$output = $this->tmpDir . DS . 'passing.xml';
$this->addPassingReader();
$this->writer->write($output);
static::assertXmlFileEqualsXmlFile($this->passing, $output);
Expand All @@ -85,7 +85,7 @@ public function testWriteToFile(): void

public function testWriteToFileInNonExistentDir(): void
{
$output = TMP_DIR . DS . 'logs' . DS . 'new' . DS . 'dir' . DS . 'passing.xml';
$output = $this->tmpDir . DS . 'logs' . DS . 'new' . DS . 'dir' . DS . 'passing.xml';
$this->addPassingReader();
$this->writer->write($output);
static::assertXmlFileEqualsXmlFile($this->passing, $output);
Expand Down
Loading

0 comments on commit b5e108b

Please sign in to comment.