Skip to content

Commit 63402a2

Browse files
committed
Truncate files before writing new content to them
Previously, UpgradeApplicator would open build.gradle using open options that left the fields existing content intact. It would then write the new content at the beginning of the file. If the new content was n bytes shorter than the existing content, this would leave n bytes of the existing content at the end of the file. This commit updates UpgradeApplicator to truncate the existing file when it opens it. This ensures that the existing content is completely replaced by the new content, irrespective of their lengths. Closes gh-25256
1 parent 4132565 commit 63402a2

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,14 +65,19 @@ private void updateGradleProperties(Upgrade upgrade, String version) throws IOEx
6565
String gradlePropertiesContents = new String(Files.readAllBytes(this.gradleProperties), StandardCharsets.UTF_8);
6666
String modified = gradlePropertiesContents.replace(property + "=" + upgrade.getLibrary().getVersion(),
6767
property + "=" + upgrade.getVersion());
68-
Files.write(this.gradleProperties, modified.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
68+
overwrite(this.gradleProperties, modified);
6969
}
7070

7171
private void updateBuildFile(Upgrade upgrade, String buildFileContents) throws IOException {
7272
String modified = buildFileContents.replace(
7373
"library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getLibrary().getVersion() + "\")",
7474
"library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getVersion() + "\")");
75-
Files.write(this.buildFile, modified.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
75+
overwrite(this.buildFile, modified);
76+
}
77+
78+
private void overwrite(Path target, String content) throws IOException {
79+
Files.write(target, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE,
80+
StandardOpenOption.TRUNCATE_EXISTING);
7681
}
7782

7883
}

buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.util.FileCopyUtils;
3333

3434
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.entry;
3536

3637
/**
3738
* Tests for {@link UpgradeApplicator}.
@@ -47,13 +48,14 @@ class UpgradeApplicatorTests {
4748
void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOException {
4849
File bom = new File(this.temp, "bom.gradle");
4950
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
51+
String originalContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
5052
File gradleProperties = new File(this.temp, "gradle.properties");
5153
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
5254
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath())
5355
.apply(new Upgrade(new Library("ActiveMQ", DependencyVersion.parse("5.15.11"), null, null),
5456
DependencyVersion.parse("5.16")));
5557
String bomContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
56-
assertThat(bomContents).contains("library(\"ActiveMQ\", \"5.16\")");
58+
assertThat(bomContents.length()).isEqualTo(originalContents.length() - 3);
5759
}
5860

5961
@Test
@@ -62,14 +64,14 @@ void whenUpgradeIsAppliedToLibraryWithVersionPropertyThenGradlePropertiesIsUpdat
6264
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
6365
File gradleProperties = new File(this.temp, "gradle.properties");
6466
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
65-
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath())
66-
.apply(new Upgrade(new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null),
67-
DependencyVersion.parse("1.3.71")));
67+
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()).apply(new Upgrade(
68+
new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null), DependencyVersion.parse("1.4")));
6869
Properties properties = new Properties();
6970
try (InputStream in = new FileInputStream(gradleProperties)) {
7071
properties.load(in);
7172
}
72-
assertThat(properties).containsEntry("kotlinVersion", "1.3.71");
73+
assertThat(properties).containsOnly(entry("a", "alpha"), entry("b", "bravo"), entry("kotlinVersion", "1.4"),
74+
entry("t", "tango"));
7375
}
7476

7577
}

0 commit comments

Comments
 (0)