From b899da6637a5152fc13be5b89688c5f70c85563d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 10 Apr 2023 15:18:49 -0700 Subject: [PATCH] fix(xml): preserve trailing newline if the xml file had one (#1911) * test: failing test for preserving newlines * fix(xml): preserve trailing newline if the xml file had one --- __snapshots__/pom-xml.js | 86 +++++++++++++++++++ src/updaters/base-xml.ts | 6 +- .../fixtures/pom-trailing-newline.xml | 81 +++++++++++++++++ test/updaters/pom-xml.ts | 10 +++ 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 test/updaters/fixtures/pom-trailing-newline.xml diff --git a/__snapshots__/pom-xml.js b/__snapshots__/pom-xml.js index ea0287e73..121b3770c 100644 --- a/__snapshots__/pom-xml.js +++ b/__snapshots__/pom-xml.js @@ -1,3 +1,88 @@ +exports['PomXml updateContent preserves trailing newlines 1'] = ` + + + 4.0.0 + + + com.google.auth + google-auth-library-parent + 0.16.2 + ../pom.xml + + + com.google.auth + google-auth-library-appengine + 2.3.4 + Google Auth Library for Java - Google App Engine + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + + + java + javatests + + + org.sonatype.plugins + nexus-staging-maven-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + + + + com.google.auth + google-auth-library-credentials + + + com.google.auth + google-auth-library-oauth2-http + + + com.google.http-client + google-http-client + + + com.google.http-client + google-http-client-jackson2 + + + com.google.appengine + appengine-api-1.0-sdk + provided + + + com.google.guava + guava + + + junit + junit + test + + + com.google.auth + google-auth-library-oauth2-http + test-jar + test + + + + +` + exports['PomXml updateContent updates dependencies 1'] = ` @@ -67,6 +152,7 @@ exports['PomXml updateContent updates project.parent.version 1'] = ` google-auth-library-appengine + ` exports['PomXml updateContent updates project.version 1'] = ` diff --git a/src/updaters/base-xml.ts b/src/updaters/base-xml.ts index f5fbad4e8..3930bbeec 100644 --- a/src/updaters/base-xml.ts +++ b/src/updaters/base-xml.ts @@ -29,7 +29,11 @@ export abstract class BaseXml implements Updater { const updated = this.updateDocument(document); if (updated) { - return new dom.XMLSerializer().serializeToString(document); + const newContent = new dom.XMLSerializer().serializeToString(document); + if (content.endsWith('\n') && !newContent.endsWith('\n')) { + return `${newContent}\n`; + } + return newContent; } else { return content; } diff --git a/test/updaters/fixtures/pom-trailing-newline.xml b/test/updaters/fixtures/pom-trailing-newline.xml new file mode 100644 index 000000000..e8b4118b0 --- /dev/null +++ b/test/updaters/fixtures/pom-trailing-newline.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + + com.google.auth + google-auth-library-parent + 0.16.2 + ../pom.xml + + + com.google.auth + google-auth-library-appengine + 0.16.2 + Google Auth Library for Java - Google App Engine + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + + + java + javatests + + + org.sonatype.plugins + nexus-staging-maven-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + + + + com.google.auth + google-auth-library-credentials + + + com.google.auth + google-auth-library-oauth2-http + + + com.google.http-client + google-http-client + + + com.google.http-client + google-http-client-jackson2 + + + com.google.appengine + appengine-api-1.0-sdk + provided + + + com.google.guava + guava + + + junit + junit + test + + + com.google.auth + google-auth-library-oauth2-http + test-jar + test + + + diff --git a/test/updaters/pom-xml.ts b/test/updaters/pom-xml.ts index e218dd856..32004f10c 100644 --- a/test/updaters/pom-xml.ts +++ b/test/updaters/pom-xml.ts @@ -59,5 +59,15 @@ describe('PomXml', () => { const newContent = updater.updateContent(oldContent); snapshot(newContent); }); + + it('preserves trailing newlines', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './pom-trailing-newline.xml'), + 'utf8' + ).replace(/\r\n/g, '\n'); + const updater = new PomXml(Version.parse('v2.3.4')); + const newContent = updater.updateContent(oldContent); + snapshot(newContent); + }); }); });