|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Integration; |
|
6
|
|
|
|
|
7
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; |
|
8
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName; |
|
9
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser\internal\GitCliWrapper; |
|
10
|
|
|
use PHPUnit\Framework\Assert; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
class GitCliWrapperTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
use TestDirectoryTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Filesystem |
|
20
|
|
|
*/ |
|
21
|
|
|
private $fileSystem; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var GitCliWrapper |
|
25
|
|
|
*/ |
|
26
|
|
|
private $gitWrapper; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ProjectRoot |
|
29
|
|
|
*/ |
|
30
|
|
|
private $projectRoot; |
|
31
|
|
|
|
|
32
|
|
|
protected function setUp(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->fileSystem = new Filesystem(); |
|
35
|
|
|
$this->gitWrapper = new GitCliWrapper(); |
|
36
|
|
|
$this->createTestDirectory(); |
|
37
|
|
|
$this->gitWrapper->init($this->projectRoot); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testNoChanges(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertClean(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testUntrackedFile(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$relativeFileName = new RelativeFileName('untracked.txt'); |
|
48
|
|
|
$absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName); |
|
49
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked'); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertNotClean(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testUpdatedFilesCommitted(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$relativeFileName = new RelativeFileName('committed.txt'); |
|
57
|
|
|
$absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName); |
|
58
|
|
|
|
|
59
|
|
|
// Add and commit file to git |
|
60
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'committed'); |
|
61
|
|
|
$this->gitWrapper->addAndCommit('Add file', $this->projectRoot); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertClean(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testUnstaged(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$relativeFileName = new RelativeFileName('unstaged.txt'); |
|
69
|
|
|
$absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName); |
|
70
|
|
|
|
|
71
|
|
|
// Add and commit file to git |
|
72
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked'); |
|
73
|
|
|
$this->gitWrapper->addAndCommit('Add file', $this->projectRoot); |
|
74
|
|
|
|
|
75
|
|
|
// Update |
|
76
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'modified but not staged'); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertNotClean(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testStaged(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$relativeFileName = new RelativeFileName('staged.txt'); |
|
84
|
|
|
$absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName); |
|
85
|
|
|
|
|
86
|
|
|
// Add and commit file to git |
|
87
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked'); |
|
88
|
|
|
$this->gitWrapper->addAndCommit('Add file', $this->projectRoot); |
|
89
|
|
|
|
|
90
|
|
|
// Update |
|
91
|
|
|
$this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'staged'); |
|
92
|
|
|
$this->gitWrapper->addAll($this->projectRoot); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertNotClean(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function assertNotClean(): void |
|
98
|
|
|
{ |
|
99
|
|
|
Assert::assertFalse($this->gitWrapper->isClean($this->projectRoot)); |
|
100
|
|
|
$this->removeTestDirectory(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function assertClean(): void |
|
104
|
|
|
{ |
|
105
|
|
|
Assert::assertTrue($this->gitWrapper->isClean($this->projectRoot)); |
|
106
|
|
|
$this->removeTestDirectory(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|