Skip to content

Commit b899da6

Browse files
authored
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
1 parent 6f92f55 commit b899da6

4 files changed

Lines changed: 182 additions & 1 deletion

File tree

__snapshots__/pom-xml.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,88 @@
1+
exports['PomXml updateContent preserves trailing newlines 1'] = `
2+
<?xml version="1.0" encoding="UTF-8"?>
3+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.google.auth</groupId>
8+
<artifactId>google-auth-library-parent</artifactId>
9+
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<groupId>com.google.auth</groupId>
14+
<artifactId>google-auth-library-appengine</artifactId>
15+
<version>2.3.4</version><!-- {x-version-update:google-auth-library-parent:current} -->
16+
<name>Google Auth Library for Java - Google App Engine</name>
17+
18+
<distributionManagement>
19+
<snapshotRepository>
20+
<id>ossrh</id>
21+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
22+
</snapshotRepository>
23+
</distributionManagement>
24+
25+
<build>
26+
<sourceDirectory>java</sourceDirectory>
27+
<testSourceDirectory>javatests</testSourceDirectory>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.sonatype.plugins</groupId>
31+
<artifactId>nexus-staging-maven-plugin</artifactId>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-source-plugin</artifactId>
36+
</plugin>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-javadoc-plugin</artifactId>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>com.google.auth</groupId>
47+
<artifactId>google-auth-library-credentials</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.google.auth</groupId>
51+
<artifactId>google-auth-library-oauth2-http</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.http-client</groupId>
55+
<artifactId>google-http-client</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.http-client</groupId>
59+
<artifactId>google-http-client-jackson2</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.appengine</groupId>
63+
<artifactId>appengine-api-1.0-sdk</artifactId>
64+
<scope>provided</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.guava</groupId>
68+
<artifactId>guava</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.google.auth</groupId>
77+
<artifactId>google-auth-library-oauth2-http</artifactId>
78+
<type>test-jar</type>
79+
<scope>test</scope>
80+
</dependency>
81+
</dependencies>
82+
</project>
83+
84+
`
85+
186
exports['PomXml updateContent updates dependencies 1'] = `
287
<?xml version="1.0" encoding="UTF-8"?>
388
<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">
@@ -67,6 +152,7 @@ exports['PomXml updateContent updates project.parent.version 1'] = `
67152
68153
<artifactId>google-auth-library-appengine</artifactId>
69154
</project>
155+
70156
`
71157

72158
exports['PomXml updateContent updates project.version 1'] = `

src/updaters/base-xml.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export abstract class BaseXml implements Updater {
2929
const updated = this.updateDocument(document);
3030

3131
if (updated) {
32-
return new dom.XMLSerializer().serializeToString(document);
32+
const newContent = new dom.XMLSerializer().serializeToString(document);
33+
if (content.endsWith('\n') && !newContent.endsWith('\n')) {
34+
return `${newContent}\n`;
35+
}
36+
return newContent;
3337
} else {
3438
return content;
3539
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.google.auth</groupId>
7+
<artifactId>google-auth-library-parent</artifactId>
8+
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<groupId>com.google.auth</groupId>
13+
<artifactId>google-auth-library-appengine</artifactId>
14+
<version>0.16.2</version><!-- {x-version-update:google-auth-library-parent:current} -->
15+
<name>Google Auth Library for Java - Google App Engine</name>
16+
17+
<distributionManagement>
18+
<snapshotRepository>
19+
<id>ossrh</id>
20+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
21+
</snapshotRepository>
22+
</distributionManagement>
23+
24+
<build>
25+
<sourceDirectory>java</sourceDirectory>
26+
<testSourceDirectory>javatests</testSourceDirectory>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.sonatype.plugins</groupId>
30+
<artifactId>nexus-staging-maven-plugin</artifactId>
31+
</plugin>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-source-plugin</artifactId>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-javadoc-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.google.auth</groupId>
46+
<artifactId>google-auth-library-credentials</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.google.auth</groupId>
50+
<artifactId>google-auth-library-oauth2-http</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.http-client</groupId>
54+
<artifactId>google-http-client</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.http-client</groupId>
58+
<artifactId>google-http-client-jackson2</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.appengine</groupId>
62+
<artifactId>appengine-api-1.0-sdk</artifactId>
63+
<scope>provided</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.google.guava</groupId>
67+
<artifactId>guava</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.google.auth</groupId>
76+
<artifactId>google-auth-library-oauth2-http</artifactId>
77+
<type>test-jar</type>
78+
<scope>test</scope>
79+
</dependency>
80+
</dependencies>
81+
</project>

test/updaters/pom-xml.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,15 @@ describe('PomXml', () => {
5959
const newContent = updater.updateContent(oldContent);
6060
snapshot(newContent);
6161
});
62+
63+
it('preserves trailing newlines', async () => {
64+
const oldContent = readFileSync(
65+
resolve(fixturesPath, './pom-trailing-newline.xml'),
66+
'utf8'
67+
).replace(/\r\n/g, '\n');
68+
const updater = new PomXml(Version.parse('v2.3.4'));
69+
const newContent = updater.updateContent(oldContent);
70+
snapshot(newContent);
71+
});
6272
});
6373
});

0 commit comments

Comments
 (0)