Skip to content

Commit

Permalink
fix(xml): preserve trailing newline if the xml file had one (#1911)
Browse files Browse the repository at this point in the history
* test: failing test for preserving newlines

* fix(xml): preserve trailing newline if the xml file had one
  • Loading branch information
chingor13 committed Apr 10, 2023
1 parent 6f92f55 commit b899da6
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
86 changes: 86 additions & 0 deletions __snapshots__/pom-xml.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
exports['PomXml updateContent preserves trailing newlines 1'] = `
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-parent</artifactId>
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-appengine</artifactId>
<version>2.3.4</version><!-- {x-version-update:google-auth-library-parent:current} -->
<name>Google Auth Library for Java - Google App Engine</name>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<sourceDirectory>java</sourceDirectory>
<testSourceDirectory>javatests</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
`

exports['PomXml updateContent updates dependencies 1'] = `
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
Expand Down Expand Up @@ -67,6 +152,7 @@ exports['PomXml updateContent updates project.parent.version 1'] = `
<artifactId>google-auth-library-appengine</artifactId>
</project>
`

exports['PomXml updateContent updates project.version 1'] = `
Expand Down
6 changes: 5 additions & 1 deletion src/updaters/base-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
81 changes: 81 additions & 0 deletions test/updaters/fixtures/pom-trailing-newline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-parent</artifactId>
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-appengine</artifactId>
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
<name>Google Auth Library for Java - Google App Engine</name>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

<build>
<sourceDirectory>java</sourceDirectory>
<testSourceDirectory>javatests</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
10 changes: 10 additions & 0 deletions test/updaters/pom-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit b899da6

Please sign in to comment.