Browse Source

Merge branch '1.5'

Jordi Boggiano 7 years ago
parent
commit
dae575c197

+ 5 - 0
src/Composer/Json/JsonManipulator.php

@@ -430,6 +430,11 @@ class JsonManipulator
                 return false;
             }
 
+            // check that we are not leaving a dangling comma on the previous line if the last line was removed
+            if (preg_match('#,\s*$#', $matches['start']) && preg_match('#^\}$#', $matches['end'])) {
+                $matches['start'] = rtrim(preg_replace('#,(\s*)$#', '$1', $matches['start']), $this->indent);
+            }
+
             $this->contents = $matches['start'] . $matches['end'];
             if (preg_match('#^\{\s*\}\s*$#', $this->contents)) {
                 $this->contents = "{\n}";

+ 30 - 0
tests/Composer/Test/Json/JsonManipulatorTest.php

@@ -2331,4 +2331,34 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
 }
 ', $manipulator->getContents());
     }
+
+    public function testRemoveMainKeyAtEndOfFile()
+    {
+        $manipulator = new JsonManipulator('{
+    "require": {
+        "package/a": "*"
+    }
+}
+');
+        $this->assertTrue($manipulator->addMainKey('homepage', 'http...'));
+        $this->assertTrue($manipulator->addMainKey('license', 'mit'));
+        $this->assertEquals('{
+    "require": {
+        "package/a": "*"
+    },
+    "homepage": "http...",
+    "license": "mit"
+}
+', $manipulator->getContents());
+
+        $this->assertTrue($manipulator->removeMainKey('homepage'));
+        $this->assertTrue($manipulator->removeMainKey('license'));
+        $this->assertEquals('{
+    "require": {
+        "package/a": "*"
+    }
+}
+', $manipulator->getContents());
+
+    }
 }