@@ -82,7 +82,13 @@ public function testGetReplayLog()
8282 chdir ($ cwd );
8383
8484 @shell_exec ('rm -rf ' . escapeshellarg ($ directory ) . ' 2>&1 ' );
85- file_exists ($ directory ) && @shell_exec ('rmdir /S /Q ' . escapeshellarg ($ directory ) . ' 2>&1 ' );
85+
86+ // @codeCoverageIgnoreStart
87+ if (file_exists ($ directory )) {
88+ @shell_exec ('rmdir /S /Q ' . escapeshellarg ($ directory ) . ' 2>&1 ' );
89+ }
90+ // @codeCoverageIgnoreEnd
91+
8692 @FileSystem::delete ($ directory );
8793
8894 $ this ->assertSame ([
@@ -133,10 +139,11 @@ public function testSetGitCommitter()
133139 $ output
134140 );
135141 }
142+
136143 /**
137144 * @covers ::distributePackage
138145 */
139- public function testRun ()
146+ public function testWithUpToDateDirectory ()
140147 {
141148 $ cwd = getcwd ();
142149 $ cli = new Split ();
@@ -231,6 +238,116 @@ public function testRun()
231238 $ this ->assertSame ('F ' , $ contentF );
232239 }
233240
241+ /**
242+ * @covers ::distributePackage
243+ */
244+ public function testWithTouchedDirectory ()
245+ {
246+ $ cwd = getcwd ();
247+ $ cli = new Split ();
248+ $ cli ->setEscapeCharacter ('# ' );
249+ $ dist = new Update ();
250+
251+ $ directory1 = sys_get_temp_dir ().'/split-test- ' .mt_rand (0 , 9999999 );
252+ FileSystem::createDir ($ directory1 );
253+ FileSystem::createDir ("$ directory1/sub-package " );
254+ FileSystem::createDir ("$ directory1/api/vendor " );
255+ $ directory1 = realpath ($ directory1 );
256+ $ subPackageDirectory = realpath ("$ directory1/sub-package " );
257+
258+ $ directory2 = sys_get_temp_dir ().'/split-test- ' .mt_rand (0 , 9999999 );
259+ FileSystem::createDir ("$ directory2/tests " );
260+ $ directory2 = realpath ($ directory2 );
261+
262+ chdir ($ directory1 );
263+ file_put_contents ('composer.json ' , json_encode (['name ' => 'vendor/package ' ]));
264+ file_put_contents ('sub-package/composer.json ' , json_encode (['name ' => 'vendor/sub-package ' ]));
265+ file_put_contents ('sub-package/d.txt ' , "D1 \n" );
266+ file_put_contents ('sub-package/e.txt ' , 'E ' );
267+ file_put_contents ('sub-package/f.txt ' , 'F ' );
268+ file_put_contents ('a.txt ' , 'A ' );
269+ file_put_contents ('b.txt ' , 'B ' );
270+ file_put_contents ('c.txt ' , 'C ' );
271+ shell_exec ('git init 2>&1 ' );
272+ shell_exec ('git add --all 2>&1 ' );
273+ shell_exec ('git commit --message=Init 2>&1 ' );
274+ preg_match ('/^commit (\S+)/ ' , shell_exec ('git log -n 1 ' ), $ match );
275+ $ hash1 = $ match [1 ];
276+ file_put_contents ('sub-package/d.txt ' , "D1 \nD2 \n" );
277+ shell_exec ('git add sub-package/d.txt 2>&1 ' );
278+ shell_exec ('git commit --message=ModifyD 2>&1 ' );
279+
280+ file_put_contents ('api/vendor/sub-package.json ' , json_encode ([
281+ 'packages ' => [
282+ 'vendor/sub-package ' => [
283+ 'dev-master ' => [
284+ 'source ' => [
285+ 'type ' => 'git ' ,
286+ 'url ' => $ directory2 ,
287+ ],
288+ ],
289+ ],
290+ ],
291+ ]));
292+
293+ chdir ($ directory2 );
294+ file_put_contents ('composer.json ' , json_encode (['name ' => 'vendor/sub-package ' ]));
295+ file_put_contents ('d.txt ' , 'D ' );
296+ file_put_contents ('e.txt ' , 'E ' );
297+ file_put_contents ('f.txt ' , 'F ' );
298+ shell_exec ('git init 2>&1 ' );
299+ shell_exec ('git add --all 2>&1 ' );
300+ file_put_contents ('message.txt ' , "ABC \n\nsplit: $ hash1 " );
301+ shell_exec ('git commit --file=message.txt 2>&1 ' );
302+
303+ chdir ($ directory1 );
304+
305+ ob_start ();
306+ $ dist ->api = 'api/%s.json ' ;
307+ $ return = $ dist ->run ($ cli );
308+ $ output = ob_get_contents ();
309+ ob_end_clean ();
310+
311+ $ outputDirectory = realpath ("$ directory1/dist " );
312+ $ contentD = @file_get_contents ("$ outputDirectory/vendor/sub-package/d.txt " );
313+ $ contentE = @file_get_contents ("$ outputDirectory/vendor/sub-package/e.txt " );
314+ $ contentF = @file_get_contents ("$ outputDirectory/vendor/sub-package/f.txt " );
315+
316+ chdir ($ cwd );
317+
318+ foreach ([$ directory1 , $ directory2 ] as $ directory ) {
319+ @shell_exec ('rm -rf ' . escapeshellarg ($ directory ) . ' 2>&1 ' );
320+ file_exists ($ directory ) && @shell_exec ('rmdir /S /Q ' . escapeshellarg ($ directory ) . ' 2>&1 ' );
321+ @FileSystem::delete ($ directory );
322+ }
323+
324+ $ path = $ directory1 .DIRECTORY_SEPARATOR ;
325+ $ output = preg_replace ('/remote: error: [\s\S]+\n\n/ ' , "@@ERROR@@ \n" , $ output );
326+ $ expected = implode ("\n" , array_merge ([
327+ 'vendor/package ' ,
328+ '#[1;35mBuild vendor/sub-package ' ,
329+ "#[0m#[1;32mgit clone $ directory2 {$ path }dist/vendor/sub-package " ,
330+ "#[0m#[1;30mCloning into ' {$ path }dist/vendor/sub-package'... " ,
331+ 'done. ' ,
332+ "#[0m#[1;34mcd {$ path }dist/vendor/sub-package " ,
333+ '#[0m#[1;32mgit checkout master ' ,
334+ "#[0m#[1;34mcd $ subPackageDirectory " ,
335+ "#[0m#[1;34mcd {$ path }dist/vendor/sub-package " ,
336+ '#[0m#[0;31mPushing vendor/sub-package ' ,
337+ '@@ERROR@@ ' ,
338+ "#[0m#[1;34mcd $ subPackageDirectory " ,
339+ "#[0m#[1;35mBuild distributed in {$ path }dist " ,
340+ '#[0m ' ,
341+ ]));
342+ $ expected = str_replace ('\\' , '/ ' , $ expected );
343+ $ output = str_replace ('\\' , '/ ' , $ output );
344+ $ this ->assertSame ($ expected , $ output );
345+ $ this ->assertTrue ($ return );
346+ $ this ->assertSame ("D1 \nD2 \n" , $ contentD );
347+ $ this ->assertSame ('E ' , $ contentE );
348+ $ this ->assertSame ('F ' , $ contentF );
349+ }
350+
234351 /**
235352 * @covers ::distributePackage
236353 */
0 commit comments