Skip to content

[Feature] Allow rewriting of {$STAGE_FILES} #128

@robertfausk

Description

@robertfausk

...to improve compatibility with a docker setup which is based on subdirectories.

Problem description

Following is an example concerning behat.

...
{
  "config": {
    "run-mode": "docker",
    "run-exec": "docker-compose exec --user=docker -T web"
  },
...
{
  "action": "vendor/bin/behat --colors --format=progress {$STAGED_FILES|of-type:feature}",
  "options": [],
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
      "args": [
        "feature"
      ]
    }
  ]
}
...

This would work as long as the composer directory is the same as the git directory.
But when the composer directory is in a subpath (e.g. web) then behat will fail cause it is gettting the wrong paths.
Wrong one: vendor/bin/behat web/tests/Integration/features/associations_management.feature
Needed one: vendor/bin/behat tests/Integration/features/associations_management.feature

Following picture is to get an idea about the project structure:
image

Workaround

My current workaround is with a custom action:

{
  "action": "\\App\\GitHook\\BehatStagedFiles",
  "options": [],
  "conditions": []
}
<?php
declare(strict_types=1);

namespace App\GitHook;

use CaptainHook\App\Config;
use CaptainHook\App\Console\IO;
use CaptainHook\App\Exception\ActionFailed;
use CaptainHook\App\Hook\Action;
use SebastianFeldmann\Cli\Command\Result;
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
use SebastianFeldmann\Git\Repository;

class BehatStagedFiles implements Action
{
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
    {
        $changedFeatureFiles = $repository->getIndexOperator()->getStagedFilesOfType('feature');

        $changedFeatureFilesAsString = \implode(($options['separated-by'] ?? ','), $changedFeatureFiles);

        $paths = \str_replace('web/', '', $changedFeatureFilesAsString);

        if ('' === \trim($paths)) {
            $io->write('<info>No behat tests to execute</info>');

            return;
        }

        $result = $this->runBehat($paths);
        if (!$result->isSuccessful()) {
            throw new ActionFailed(
                \sprintf(
                    "<error>Behat failed</error>%sRerun with: %s%sOutput is:%s%s",
                    PHP_EOL,
                    $result->getCmd(),
                    PHP_EOL,
                    PHP_EOL,
                    $result->getStdOut()
                )
            );
        }

        $io->write(
            \sprintf(
                "<info>All behat features passed</info>%sRerun with: %s%sOutput is:%s%s",
                PHP_EOL,
                $result->getCmd(),
                PHP_EOL,
                PHP_EOL,
                $result->getStdOut()
            )
        );
    }

    protected function runBehat(string $paths): Result
    {
        return (new Processor())->run('vendor/bin/behat  --colors --format=progress '.escapeshellarg($paths));
    }
}

Suggestion

I would like to use something like:

{
  "action": "bin/behat --colors --format=progress {$STAGED_FILES|of-type:feature|replace-path:web\tests|replace-path-by:tests}",
  "options": [],
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
      "args": [
        "feature"
      ]
    }
  ]
}

I am not quite sure about the syntax of replace-path:<search> respective replace-path-by:<replace> and I also do not know if something like replace-path:<search>:<replace> would be possible.

@sebastianfeldmann WDYT? Maybe you have also another good solution or proposal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions