Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ jobs:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
# TODO: remove once gitlib 2.0 is released.
# The main branch of gitlib is installed as 2.0.0, here and in the projects of the E2E tests.
- name: Use the main branch of gitlib as 2.0.0
if: matrix.composer-deps == 'highest' && (matrix.php-versions == '8.4' || matrix.php-versions == '8.5')
run: |
git clone --depth=1 https://github.com/gitonomy/gitlib.git "$RUNNER_TEMP/gitlib"
composer config --global repositories.gitlib '{"type": "path", "url": "'"$RUNNER_TEMP/gitlib"'", "options": {"symlink": false, "versions": {"gitonomy/gitlib": "2.0.0"}}}'
- name: Install dependencies (highest)
if: matrix.composer-deps == 'highest'
run: |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"amphp/amp": "^3.0",
"amphp/parallel": "^2.1",
"doctrine/collections": "^1.6.8 || ^2.0 || ^3.0",
"gitonomy/gitlib": "^1.6",
"gitonomy/gitlib": "^1.6 || ^2.0",
"laravel/serializable-closure": "^2.0",
"monolog/monolog": "^2.0 || ^3.0",
"ondram/ci-detector": "^4.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ services:
GrumPHP\Git\GitRepository:
arguments:
- '@GrumPHP\Locator\GitRepositoryLocator'
-
logger: '@grumphp.logger'
inherit_environment_variables: true
- '@grumphp.logger'

GrumPHP\IO\IOFactory:
arguments:
Expand Down
31 changes: 22 additions & 9 deletions spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace spec\GrumPHP\Event\Subscriber;

use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\WorkingCopy;
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\TaskResultCollection;
use GrumPHP\Collection\TasksCollection;
Expand All @@ -15,23 +13,38 @@
use GrumPHP\IO\IOInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHPTest\Helpers\TemporaryGitRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class StashUnstagedChangesSubscriberSpec extends ObjectBehavior
{
function let(GitStashConfig $config, GitRepository $repository, IOInterface $io, WorkingCopy $workingCopy, Diff $unstaged)
/**
* @var TemporaryGitRepository
*/
private $gitRepository;

function let(GitStashConfig $config, GitRepository $repository, IOInterface $io)
{
$this->gitRepository = TemporaryGitRepository::create();
$this->gitRepository->dumpFile('file1.php', 'committed');
$this->gitRepository->git('add', '--all');
$this->gitRepository->git('commit', '--quiet', '--message=Initial commit');
$this->gitRepository->dumpFile('file1.php', 'unstaged');
$this->gitRepository->dumpFile('untracked.php');

$config->ignoreUnstagedChanges()->willReturn(true);
$repository->getWorkingCopy()->willReturn($workingCopy);
$workingCopy->getDiffPending()->willReturn($unstaged);
$unstaged->getFiles()->willReturn(['file1.php']);
$workingCopy->getUntrackedFiles()->willReturn(['untracked.php']);
$repository->getWorkingCopy()->willReturn($this->gitRepository->getRepository()->getWorkingCopy());

$this->beConstructedWith($config, $repository, $io);
}

function letGo()
{
$this->gitRepository->remove();
}

function it_is_initializable()
{
$this->shouldHaveType(StashUnstagedChangesSubscriber::class);
Expand Down Expand Up @@ -68,10 +81,10 @@ function it_should_not_run_in_invalid_context(GitRepository $repository)
$repository->run(Argument::cetera())->shouldNotBeCalled();
}

function it_should_not_run_when_there_are_no_unstaged_changes(GitRepository $repository, Diff $unstaged)
function it_should_not_run_when_there_are_no_unstaged_changes(GitRepository $repository)
{
$event = new RunnerEvent(new TasksCollection(), new RunContext(new FilesCollection()), new TaskResultCollection());
$unstaged->getFiles()->willReturn([]);
$this->gitRepository->git('checkout', '--', 'file1.php');

$this->saveStash($event);
$this->popStash($event);
Expand Down
51 changes: 30 additions & 21 deletions spec/Git/GitRepositorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,56 @@
use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Diff\File;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Repository;
use Gitonomy\Git\WorkingCopy;
use GrumPHP\Locator\GitRepositoryLocator;
use GrumPHPTest\Helpers\TemporaryGitRepository;
use PhpSpec\ObjectBehavior;
use GrumPHP\Git\GitRepository;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

class GitRepositorySpec extends ObjectBehavior
{
public function let(GitRepositoryLocator $locator, Repository $repository): void
/**
* @var TemporaryGitRepository
*/
private $gitRepository;

public function let(GitRepositoryLocator $locator, LoggerInterface $logger): void
{
$this->gitRepository = TemporaryGitRepository::create();
$this->beConstructedWith($locator, $logger);
$locator->locate($logger)->willReturn($this->gitRepository->getRepository());
}

public function letGo(): void
{
$this->beConstructedWith($locator, []);
$locator->locate([])->willReturn($repository);
$this->gitRepository->remove();
}

function it_is_initializable()
{
$this->shouldHaveType(GitRepository::class);
}

public function it_is_lazy(GitRepositoryLocator $locator, Repository $repository): void
public function it_is_lazy(GitRepositoryLocator $locator, LoggerInterface $logger): void
{
$repository->run(Argument::cetera())->willReturn('ok');
$this->run('command', []);
$this->run('command', []);
$this->run('rev-parse', ['--git-dir']);
$this->run('rev-parse', ['--git-dir']);

$locator->locate([])->shouldBeCalledTimes(1);
$locator->locate($logger)->shouldBeCalledTimes(1);
}

public function it_can_run_commands(Repository $repository): void
public function it_can_run_commands(): void
{
$repository->run('command', [])->willReturn('ok');
$this->run('command', [])->shouldBe('ok');
$this->run('rev-parse', ['--is-bare-repository'])->shouldBe("false\n");
}

public function it_can_deal_with_errornous_throwing_runs(Process $process): void
{
$process->getCommandLine()->willReturn('');
$process->getErrorOutput()->willReturn('');
$process->getExitCode()->willReturn(1);
$process->getOutput()->willReturn('');

$this->tryToRunWithFallback(
Expand Down Expand Up @@ -75,31 +85,30 @@ function () {
)->shouldBe('ok');
}

public function it_can_fetch_working_copy(Repository $repository, WorkingCopy $workingCopy): void
public function it_can_fetch_working_copy(): void
{
$repository->getWorkingCopy()->willReturn($workingCopy);
$this->getWorkingCopy()->shouldBe($workingCopy);
$this->getWorkingCopy()->shouldBeAnInstanceOf(WorkingCopy::class);
}

public function it_can_parse_raw_diff(Repository $repository): void
public function it_can_parse_raw_diff(): void
{
$rawDiff = ':100644 100644 c09a0d49 77e5a35c M file.txt
$rawDiff = ":000000 100644 0000000 d95f3ad A\tfile.txt

diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000000000000000000000000000000000000..9766475a4185a151dc9d56d614ffb9aaea3bfd42
index 0000000000000000000000000000000000000000..d95f3ad14dee633a758d2e331151e950dd13e4ed
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+content
';
";
$diff = $this->createRawDiff($rawDiff);
$diff->shouldBeAnInstanceOf(Diff::class);
/** @var File $file */
$file = $diff->getFiles()[0];
$file->isCreation()->shouldBe(true);
$file->getName()->shouldBe('file.txt');
$file->getRepository()->shouldBe($repository);
$file->getRepository()->shouldBe($this->gitRepository->getRepository());

}
}
60 changes: 30 additions & 30 deletions spec/Locator/ChangedFilesSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
namespace spec\GrumPHP\Locator;

use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Diff\File;
use Gitonomy\Git\WorkingCopy;
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Git\GitRepository;
use GrumPHP\Locator\ChangedFiles;
use GrumPHP\Util\Filesystem;
use GrumPHP\Util\Paths;
use GrumPHPTest\Helpers\TemporaryGitRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Prophet;

class ChangedFilesSpec extends ObjectBehavior
{
/**
* @var TemporaryGitRepository
*/
private $gitRepository;

function let(GitRepository $repository, Filesystem $filesystem, Paths $paths)
{
$this->gitRepository = TemporaryGitRepository::create();
$repository->getWorkingCopy()->willReturn($this->gitRepository->getRepository()->getWorkingCopy());

$this->beConstructedWith($repository, $filesystem, $paths);
$paths->makePathRelativeToProjectDir(Argument::type('string'))->will(
function (array $arguments): string {
Expand All @@ -26,51 +32,45 @@ function (array $arguments): string {
);
}

function it_is_initializable()
function letGo()
{
$this->shouldHaveType(ChangedFiles::class);
$this->gitRepository->remove();
}

protected function mockFile($name, $isRename = false, $isDelete = false)
function it_is_initializable()
{
$prophet = new Prophet();
$file = $prophet->prophesize(File::class);
$file->getName()->willReturn($name);
$file->getNewName()->willReturn($name);
$file->isRename()->willReturn($isRename);
$file->isDeletion()->willReturn($isDelete);
return $file->reveal();
$this->shouldHaveType(ChangedFiles::class);
}

function it_will_list_all_diffed_files(GitRepository $repository, Filesystem $filesystem, Diff $diff, WorkingCopy $workingCopy)
function it_will_list_all_diffed_files(Filesystem $filesystem)
{
$changedFile = $this->mockFile('file1.txt');
$movedFile = $this->mockFile('file2.txt', true);
$deletedFile = $this->mockFile('file3.txt', false, true);
$this->gitRepository->dumpFile('file1.txt', 'changed');
$this->gitRepository->dumpFile('moved.txt', 'moved');
$this->gitRepository->dumpFile('file3.txt', 'deleted');
$this->gitRepository->git('add', '--all');
$this->gitRepository->git('commit', '--quiet', '--message=Initial commit');
$this->gitRepository->dumpFile('file1.txt', 'changed again');
$this->gitRepository->git('mv', 'moved.txt', 'file2.txt');
$this->gitRepository->git('rm', '--quiet', 'file3.txt');
$this->gitRepository->git('add', '--all');

$filesystem->exists('file1.txt')->willReturn(true);
$filesystem->exists('file2.txt')->willReturn(true);
$filesystem->exists('file3.txt')->willReturn(false);

$repository->getWorkingCopy()->willReturn($workingCopy);
$workingCopy->getDiffStaged()->willReturn($diff);
$diff->getFiles()->willReturn([$changedFile, $movedFile, $deletedFile]);

$result = $this->locateFromGitRepository();
$result->shouldBeAnInstanceOf(FilesCollection::class);
$result[0]->getPathname()->shouldBe('file1.txt');
$result[1]->getPathname()->shouldBe('file2.txt');
$result->getIterator()->count()->shouldBe(2);
}

function it_will_not_list_non_existing_files(GitRepository $repository, Filesystem $filesystem, Diff $diff, WorkingCopy $workingCopy)
function it_will_not_list_non_existing_files(Filesystem $filesystem)
{
$changedFile = $this->mockFile('file1.txt');
$filesystem->exists('file1.txt')->willReturn(false);
$this->gitRepository->dumpFile('file1.txt', 'changed');
$this->gitRepository->git('add', '--all');

$repository->getWorkingCopy()->willReturn($workingCopy);
$workingCopy->getDiffStaged()->willReturn($diff);
$diff->getFiles()->willReturn([$changedFile]);
$filesystem->exists('file1.txt')->willReturn(false);

$result = $this->locateFromGitRepository();
$result->shouldBeAnInstanceOf(FilesCollection::class);
Expand All @@ -79,16 +79,16 @@ function it_will_not_list_non_existing_files(GitRepository $repository, Filesyst

function it_will_list_all_diffed_files_from_raw_diff_input(GitRepository $repository, Filesystem $filesystem)
{
$rawDiff = ':100644 100644 c09a0d49 77e5a35c M file.txt
$rawDiff = ":000000 100644 0000000 d95f3ad A\tfile.txt

diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000000000000000000000000000000000000..9766475a4185a151dc9d56d614ffb9aaea3bfd42
index 0000000000000000000000000000000000000000..d95f3ad14dee633a758d2e331151e950dd13e4ed
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+content
';
";

$repository->createRawDiff($rawDiff)->willReturn(Diff::parse($rawDiff));
$filesystem->exists('file.txt')->willReturn(true);
Expand Down
Loading