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
pull/25265/head
Andy Wilkinson 4 years ago
parent 413256501f
commit 63402a2984

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,14 +65,19 @@ class UpgradeApplicator {
String gradlePropertiesContents = new String(Files.readAllBytes(this.gradleProperties), StandardCharsets.UTF_8); String gradlePropertiesContents = new String(Files.readAllBytes(this.gradleProperties), StandardCharsets.UTF_8);
String modified = gradlePropertiesContents.replace(property + "=" + upgrade.getLibrary().getVersion(), String modified = gradlePropertiesContents.replace(property + "=" + upgrade.getLibrary().getVersion(),
property + "=" + upgrade.getVersion()); property + "=" + upgrade.getVersion());
Files.write(this.gradleProperties, modified.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); overwrite(this.gradleProperties, modified);
} }
private void updateBuildFile(Upgrade upgrade, String buildFileContents) throws IOException { private void updateBuildFile(Upgrade upgrade, String buildFileContents) throws IOException {
String modified = buildFileContents.replace( String modified = buildFileContents.replace(
"library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getLibrary().getVersion() + "\")", "library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getLibrary().getVersion() + "\")",
"library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getVersion() + "\")"); "library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getVersion() + "\")");
Files.write(this.buildFile, modified.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); overwrite(this.buildFile, modified);
}
private void overwrite(Path target, String content) throws IOException {
Files.write(target, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2020-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
/** /**
* Tests for {@link UpgradeApplicator}. * Tests for {@link UpgradeApplicator}.
@ -47,13 +48,14 @@ class UpgradeApplicatorTests {
void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOException { void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOException {
File bom = new File(this.temp, "bom.gradle"); File bom = new File(this.temp, "bom.gradle");
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom); FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
String originalContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
File gradleProperties = new File(this.temp, "gradle.properties"); File gradleProperties = new File(this.temp, "gradle.properties");
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties); FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()) new UpgradeApplicator(bom.toPath(), gradleProperties.toPath())
.apply(new Upgrade(new Library("ActiveMQ", DependencyVersion.parse("5.15.11"), null, null), .apply(new Upgrade(new Library("ActiveMQ", DependencyVersion.parse("5.15.11"), null, null),
DependencyVersion.parse("5.16"))); DependencyVersion.parse("5.16")));
String bomContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8); String bomContents = new String(Files.readAllBytes(bom.toPath()), StandardCharsets.UTF_8);
assertThat(bomContents).contains("library(\"ActiveMQ\", \"5.16\")"); assertThat(bomContents.length()).isEqualTo(originalContents.length() - 3);
} }
@Test @Test
@ -62,14 +64,14 @@ class UpgradeApplicatorTests {
FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom); FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom);
File gradleProperties = new File(this.temp, "gradle.properties"); File gradleProperties = new File(this.temp, "gradle.properties");
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties); FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()) new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()).apply(new Upgrade(
.apply(new Upgrade(new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null), new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null), DependencyVersion.parse("1.4")));
DependencyVersion.parse("1.3.71")));
Properties properties = new Properties(); Properties properties = new Properties();
try (InputStream in = new FileInputStream(gradleProperties)) { try (InputStream in = new FileInputStream(gradleProperties)) {
properties.load(in); properties.load(in);
} }
assertThat(properties).containsEntry("kotlinVersion", "1.3.71"); assertThat(properties).containsOnly(entry("a", "alpha"), entry("b", "bravo"), entry("kotlinVersion", "1.4"),
entry("t", "tango"));
} }
} }

Loading…
Cancel
Save