diff --git a/src/Dependencies/Updater.php b/src/Dependencies/Updater.php index 40eacc0..e2d0f3b 100644 --- a/src/Dependencies/Updater.php +++ b/src/Dependencies/Updater.php @@ -64,13 +64,15 @@ private function loadWhippetFiles() $this->lockFile = $result->unwrap(); } + $this->newLockFile = $this->factory->newInstance('\\Dxw\\Whippet\\Files\\WhippetLock', []); + return \Result\Result::ok(); } private function updateHash() { $jsonHash = sha1(file_get_contents($this->dir.'/whippet.json')); - $this->lockFile->setHash($jsonHash); + $this->newLockFile->setHash($jsonHash); } private function loadGitignore() diff --git a/tests/dependencies/updater_test.php b/tests/dependencies/updater_test.php index d1803c1..c4eb886 100644 --- a/tests/dependencies/updater_test.php +++ b/tests/dependencies/updater_test.php @@ -39,9 +39,14 @@ private function getWhippetLockWritable(array $addDependency, /* string */ $hash $addDependency ); - $whippetLock->expects($this->exactly(1)) - ->method('setHash') - ->with($hash); + if ($hash !== null) { + $whippetLock->expects($this->exactly(1)) + ->method('setHash') + ->with($hash); + } else { + $whippetLock->expects($this->exactly(0)) + ->method('setHash'); + } $whippetLock->expects($this->exactly($path === null ? 0 : 1)) ->method('saveToPath') @@ -92,11 +97,13 @@ public function testUpdate() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ['plugins', 'my-plugin', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'd961c3d'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); @@ -146,10 +153,12 @@ public function testUpdateWithExistingGitignore() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); @@ -199,10 +208,12 @@ public function testUpdateWithExistingGitignoreNoDuplication() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); @@ -243,8 +254,10 @@ public function testUpdateFailedGitCommand() ], false, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([], sha1('foobar'), null, []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $oldWhippetLock = $this->getWhippetLockWritable([], null, null, []); + $newWhippetLock = $this->getWhippetLockWritable([], sha1('foobar'), null, []); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::err('oh no')); @@ -287,10 +300,12 @@ public function testUpdateWithExplicitSrc() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'foobar', '27ba906'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'foobar', 'v1.4', \Result\Result::ok('27ba906')); @@ -330,10 +345,12 @@ public function testUpdateWithoutRef() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'master', \Result\Result::ok('27ba906')); @@ -362,8 +379,10 @@ public function testUpdateBlankJsonfile() $gitignore = $this->getGitignore([], [], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([], sha1('foobar'), $dir.'/whippet.lock', []); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $dependencies = new \Dxw\Whippet\Dependencies\Updater( $this->getFactory(), @@ -410,11 +429,13 @@ public function testUpdateNoGitignore() ], true, true); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $newWhippetLock = $this->getWhippetLockWritable([ ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ['plugins', 'my-plugin', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'd961c3d'], ], sha1('foobar'), $dir.'/whippet.lock', []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); @@ -462,15 +483,17 @@ public function testUpdateRemoveFromGitignore() ], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([ - ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], - ], sha1('foobar'), $dir.'/whippet.lock', [ + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', [ ['themes', []], ['plugins', [ ['name' => 'removed-plugin'], ]], ]); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $newWhippetLock = $this->getWhippetLockWritable([ + ['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], + ], sha1('foobar'), $dir.'/whippet.lock', []); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); @@ -594,8 +617,10 @@ public function testUpdateWithBrokenJson() ], false, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); - $whippetLock = $this->getWhippetLockWritable([], sha1('foobar'), null, []); - $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($whippetLock)); + $oldWhippetLock = $this->getWhippetLockWritable([], null, null, []); + $newWhippetLock = $this->getWhippetLockWritable([], sha1('foobar'), null, []); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); @@ -613,4 +638,50 @@ public function testUpdateWithBrokenJson() $this->assertEquals('missing sources', $result->getErr()); $this->assertEquals("[Updating themes/my-theme]\n", $output); } + + public function testUpdateRemovingDeps() + { + $dir = $this->getDir(); + + $whippetJson = $this->getWhippetJson([ + 'src' => [ + 'plugins' => 'git@git.dxw.net:wordpress-plugins/', + ], + 'plugins' => [ + ['name' => 'my-plugin', 'ref' => 'v1.6'], + ], + ]); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetJson', 'fromFile', $dir.'/whippet.json', \Result\Result::ok($whippetJson)); + + file_put_contents($dir.'/whippet.json', 'foobar'); + + $gitignore = $this->getGitignore([], [ + "/wp-content/plugins/my-plugin\n", + ], true, false); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); + + $oldWhippetLock = $this->getWhippetLockWritable([], null, $dir.'/whippet.lock', []); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir.'/whippet.lock', \Result\Result::ok($oldWhippetLock)); + + $newWhippetLock = $this->getWhippetLockWritable([ + ['plugins', 'my-plugin', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'd961c3d'], + ], sha1('foobar'), $dir.'/whippet.lock', []); + $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $newWhippetLock); + + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); + $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); + + $dependencies = new \Dxw\Whippet\Dependencies\Updater( + $this->getFactory(), + $this->getProjectDirectory($dir) + ); + + ob_start(); + $result = $dependencies->update(); + $output = ob_get_clean(); + + $this->assertFalse($result->isErr()); + // $this->assertEquals("[Updating plugins/my-plugin]\n[Removing plugins/my-other-plugin]\n", $output); + $this->assertEquals("[Updating plugins/my-plugin]\n", $output); + } }