Skip to content

Commit a75ce5e

Browse files
author
Alexandre Salomé
committed
Merge pull request #21 from gitonomy/test-repositories
refactor tests to use foobar test repository
2 parents 420c9f7 + 173676e commit a75ce5e

21 files changed

+568
-401
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/test-sandbox
21
/vendor
32
/composer.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: php
22
php:
33
- 5.3
4-
script: ./test.sh
4+
script: phpunit
55
before_script:
66
- composer install --dev --prefer-source

src/Gitonomy/Git/Admin.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ public static function init($path, $bare = true, LoggerInterface $logger = null)
5151

5252
return new Repository($path, null, $logger);
5353
}
54+
55+
public static function cloneTo($path, $url, $bare = true, LoggerInterface $logger = null)
56+
{
57+
$builder = ProcessBuilder::create(array('git', 'clone', '-q'));
58+
59+
if ($bare) {
60+
$builder->add('--bare');
61+
}
62+
63+
$builder->add($url);
64+
$builder->add($path);
65+
66+
$process = $builder->getProcess();
67+
$process->run();
68+
69+
if (!$process->isSuccessFul()) {
70+
throw new \RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
71+
}
72+
73+
return new Repository($path, null, $logger);
74+
}
5475
}

src/Gitonomy/Git/Hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,6 @@ public function remove($name)
120120

121121
protected function getPath($name)
122122
{
123-
return $this->repository->getPath().'/hooks/'.$name;
123+
return $this->repository->getGitDir().'/hooks/'.$name;
124124
}
125125
}

src/Gitonomy/Git/ReferenceBag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function update(Reference $reference)
102102
{
103103
$fullname = $reference->getFullname();
104104

105+
$this->initialize();
105106
$this->repository->run('update-ref', array($fullname, $reference->getCommitHash()));
106107

107108
$this->references[$fullname] = $reference;

src/Gitonomy/Git/Repository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ public function setLogger(LoggerInterface $logger)
419419
return $this;
420420
}
421421

422+
public function cloneTo($path, $bare = true)
423+
{
424+
return Admin::cloneTo($path, $this->gitDir, $bare);
425+
}
426+
422427
/**
423428
* @see self::run
424429
*/

test.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/Gitonomy/Git/Tests/AbstractTest.php

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,78 @@
1717

1818
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
1919
{
20-
private static $libRepo;
21-
private static $testRepo;
20+
const REPOSITORY_URL = 'git://github.com/gitonomy/foobar.git';
21+
22+
const LONGFILE_COMMIT = '4f17752acc9b7c54ba679291bf24cb7d354f0f4f';
23+
const BEFORE_LONGFILE_COMMIT = 'e0ec50e2af75fa35485513f60b2e658e245227e9';
24+
const INITIAL_COMMIT = '74acd054c8ec873ae6be044041d3a85a4f890ba5';
25+
26+
/**
27+
* Local clone of remote URL. Avoids network call on each test.
28+
*/
29+
private static $localRepository;
30+
31+
/**
32+
* Creates an empty git repository and returns instance.
33+
*
34+
* @return Repository
35+
*/
36+
public static function createEmptyRepository($bare = true)
37+
{
38+
$dir = self::createTempDir();
39+
$repository = Admin::init($dir, $bare);
40+
self::registerDeletion($repository);
41+
42+
return $repository;
43+
}
2244

23-
// Initial commit is the first commit of the repository
24-
const INITIAL_COMMIT = '1040d331549232a7d64907ec75d71d31da2e43f4';
25-
const INITIAL_TREE = '8bfd3135e80ee17c0d12d4b0f0f2297469aafdb7';
45+
/**
46+
* Can be used as data provider to get bare/not-bare repositories.
47+
*/
48+
public static function provideFoobar()
49+
{
50+
return array(
51+
array(self::createFoobarRepository()),
52+
array(self::createFoobarRepository(false))
53+
);
54+
}
2655

27-
// Travis commit is the commit integrating Travis-CI to the project
28-
const TRAVIS_COMMIT = '6964dfd6bdc1b4449f8de2d687e4609f08219cf2';
29-
const TRAVIS_PARENT_COMMIT = '922b7419044ddab753f66e163bbdd8c236f4d21e';
56+
/**
57+
* Creates a fixture test repository.
58+
*
59+
* @return Repository
60+
*/
61+
public static function createFoobarRepository($bare = true)
62+
{
63+
if (null === self::$localRepository) {
64+
self::$localRepository = Admin::cloneTo(self::createTempDir(), self::REPOSITORY_URL);
65+
self::registerDeletion(self::$localRepository);
66+
}
3067

31-
// Doc & Test commit (new file, deleted file, modified files)
32-
const DOC_COMMIT = 'f3c32f1e23d46391380c84a8cb388d1b86de9dfc';
68+
$repository = self::$localRepository->cloneTo(self::createTempDir(), $bare);
69+
self::registerDeletion($repository);
3370

34-
// References a blob in project: the README file
35-
const README_BLOB = 'e43530af24200d2ba946db7e6a069899287ec772';
36-
const README_FRAGMENT = 'methods to access Git repository';
71+
return $repository;
72+
}
3773

38-
public function createTempDir()
74+
public static function registerDeletion(Repository $repository)
75+
{
76+
register_shutdown_function(function () use ($repository) {
77+
if ($repository->getWorkingDir()) {
78+
$dir = $repository->getWorkingDir();
79+
} else {
80+
$dir = $repository->getGitDir();
81+
}
82+
AbstractTest::deleteDir($dir);
83+
});
84+
}
85+
86+
/**
87+
* Created an empty directory and return path to it.
88+
*
89+
* @return string a fullpath
90+
*/
91+
public static function createTempDir()
3992
{
4093
$tmpDir = tempnam(sys_get_temp_dir(), 'gitlib_');
4194
unlink($tmpDir);
@@ -44,12 +97,12 @@ public function createTempDir()
4497
return $tmpDir;
4598
}
4699

47-
public function createEmptyRepository()
48-
{
49-
return Admin::init($this->createTempDir());
50-
}
51-
52-
public function deleteDir($dir)
100+
/**
101+
* Deletes a directory recursively.
102+
*
103+
* @param string $dir directory to delete
104+
*/
105+
public static function deleteDir($dir)
53106
{
54107
$iterator = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS);
55108
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
@@ -63,41 +116,4 @@ public function deleteDir($dir)
63116

64117
rmdir($dir);
65118
}
66-
67-
public function getLibRepository()
68-
{
69-
if (null === self::$libRepo) {
70-
self::$libRepo = $this->createRepositoryInstance($this->getLibDirectory());
71-
}
72-
73-
return self::$libRepo;
74-
}
75-
76-
public function getLibDirectory()
77-
{
78-
return __DIR__.'/../../../../test-sandbox';
79-
}
80-
81-
public function getTestRepository()
82-
{
83-
if (null === self::$testRepo) {
84-
self::$testRepo = $this->createRepositoryInstance($this->getTestDirectory());
85-
}
86-
87-
return self::$testRepo;
88-
}
89-
90-
public function getTestDirectory()
91-
{
92-
return __DIR__.'/../../../..';
93-
}
94-
95-
protected function createRepositoryInstance($dir)
96-
{
97-
if (!is_dir($dir)) {
98-
$this->markTestSkipped("Test sandbox folder not present");
99-
}
100-
101-
return new Repository($dir);
102-
}
103119
}

tests/Gitonomy/Git/Tests/AdminTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class AdminTest extends AbstractTest
2121

2222
public function setUp()
2323
{
24-
$this->tmpDir = $this->createTempDir();
24+
$this->tmpDir = self::createTempDir();
2525
}
2626

2727
public function tearDown()
2828
{
29-
$this->deleteDir($this->createTempDir());
29+
$this->deleteDir(self::createTempDir());
3030
}
3131

3232
public function testBare()

tests/Gitonomy/Git/Tests/BlameTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,30 @@
1414

1515
class BlameTest extends AbstractTest
1616
{
17-
public function testBlame()
17+
/**
18+
* @dataProvider provideFoobar
19+
*/
20+
public function testBlame($repository)
1821
{
19-
$blame = $this->getLibRepository()->getBlame(self::DOC_COMMIT, 'README.md');
22+
$blame = $repository->getBlame(self::LONGFILE_COMMIT, 'README.md');
2023

21-
$this->assertCount(15, $blame);
24+
$this->assertCount(7, $blame);
2225

23-
$this->assertEquals('alexandresalome', $blame->getLine(1)->getCommit()->getAuthorName());
26+
$this->assertEquals('alice', $blame->getLine(1)->getCommit()->getAuthorName());
2427
$this->assertEquals(self::INITIAL_COMMIT, $blame->getLine(1)->getCommit()->getHash());
2528

26-
$this->assertEquals('alexandresalome', $blame->getLine(5)->getCommit()->getAuthorName());
29+
$this->assertEquals('alice', $blame->getLine(5)->getCommit()->getAuthorName());
2730
$this->assertNotEquals(self::INITIAL_COMMIT, $blame->getLine(5)->getCommit()->getHash());
2831
}
2932

30-
public function testGroupedBlame()
33+
/**
34+
* @dataProvider provideFoobar
35+
*/
36+
public function testGroupedBlame($repository)
3137
{
32-
$blame = $this->getLibRepository()->getBlame(self::DOC_COMMIT, 'README.md')->getGroupedLines();
38+
$blame = $repository->getBlame(self::LONGFILE_COMMIT, 'README.md')->getGroupedLines();
3339

34-
$this->assertCount(4, $blame);
40+
$this->assertCount(3, $blame);
3541

3642
$this->assertEquals(self::INITIAL_COMMIT, $blame[0][0]->getHash());
3743
}

0 commit comments

Comments
 (0)