Skip to content

Commit f90b682

Browse files
committed
Correctly handle lockfile git ignoring
1 parent adba29a commit f90b682

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Commands/AddCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private function addHook($hook, $contents)
110110
private function addLockFile()
111111
{
112112
if ($this->noLock) {
113-
$this->debug("Skipped creating a [{$this->lockFile}] file");
113+
$this->debug('Skipped creating a [' . Hook::LOCK_FILE . '] file');
114114
return;
115115
}
116116

@@ -125,15 +125,15 @@ private function ignoreLockFile()
125125
}
126126

127127
if (! $this->ignoreLock) {
128-
$this->debug("Skipped adding [{$this->lockFile}] to .gitignore");
128+
$this->debug('Skipped adding [' . Hook::LOCK_FILE . '] to .gitignore');
129129
return;
130130
}
131131

132132
$contents = file_get_contents('.gitignore');
133-
$return = strpos($contents, $this->lockFile);
133+
$return = strpos($contents, Hook::LOCK_FILE);
134134

135135
if ($return === false) {
136-
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL, FILE_APPEND);
136+
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL . PHP_EOL, FILE_APPEND);
137137
$this->debug(sprintf('Added [%s] to .gitignore', Hook::LOCK_FILE));
138138
}
139139
}

tests/AddCommandTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function it_does_not_create_the_hook_lock_file_if_the_no_lock_option_is_p
138138
$currentDir = realpath(getcwd());
139139
$this->commandTester->execute(['--no-lock' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
140140

141-
$this->assertContains('Skipped creating a ' . $currentDir . '/' . Hook::LOCK_FILE . ' file', $this->commandTester->getDisplay());
141+
$this->assertContains('Skipped creating a ' . Hook::LOCK_FILE . ' file', $this->commandTester->getDisplay());
142142
$this->assertFileNotExists(Hook::LOCK_FILE);
143143
}
144144

@@ -150,7 +150,7 @@ public function it_does_not_ignore_the_hook_lock_file()
150150
$currentDir = realpath(getcwd());
151151
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
152152

153-
$this->assertContains('Skipped adding '. $currentDir . '/' . Hook::LOCK_FILE . ' to .gitignore', $this->commandTester->getDisplay());
153+
$this->assertContains('Skipped adding ' . Hook::LOCK_FILE . ' to .gitignore', $this->commandTester->getDisplay());
154154
$this->assertFalse(strpos(file_get_contents('.gitignore'), Hook::LOCK_FILE));
155155
}
156156

@@ -165,6 +165,18 @@ public function it_ignores_the_hook_lock_file_if_the_ignore_lock_option_is_passe
165165
$this->assertTrue(strpos(file_get_contents('.gitignore'), Hook::LOCK_FILE) !== false);
166166
}
167167

168+
/**
169+
* @test
170+
*/
171+
public function it_does_not_ignore_the_hook_lock_file_if_it_is_already_ignored()
172+
{
173+
file_put_contents('.gitignore', Hook::LOCK_FILE . PHP_EOL, FILE_APPEND);
174+
$this->commandTester->execute(['--ignore-lock' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
175+
176+
$this->assertNotContains('Added ' . Hook::LOCK_FILE . ' to .gitignore', $this->commandTester->getDisplay());
177+
$this->assertTrue(strpos(file_get_contents('.gitignore'), Hook::LOCK_FILE) !== false);
178+
}
179+
168180
/**
169181
* @test
170182
*/

0 commit comments

Comments
 (0)