From 63402a2984fbb064b4fb7639d963f2faca55d504 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 12 Feb 2021 12:29:37 +0000 Subject: [PATCH] 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 --- .../boot/build/bom/bomr/UpgradeApplicator.java | 11 ++++++++--- .../build/bom/bomr/UpgradeApplicatorTests.java | 14 ++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java index 8e1df88e379..2f47151f183 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeApplicator.java @@ -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"); * 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 modified = gradlePropertiesContents.replace(property + "=" + upgrade.getLibrary().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 { String modified = buildFileContents.replace( "library(\"" + upgrade.getLibrary().getName() + "\", \"" + upgrade.getLibrary().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); } } diff --git a/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java b/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java index 4a39c841097..aef6b32ff52 100644 --- a/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java +++ b/buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java @@ -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"); * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; /** * Tests for {@link UpgradeApplicator}. @@ -47,13 +48,14 @@ class UpgradeApplicatorTests { void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOException { File bom = new File(this.temp, "bom.gradle"); 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"); FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties); new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()) .apply(new Upgrade(new Library("ActiveMQ", DependencyVersion.parse("5.15.11"), null, null), DependencyVersion.parse("5.16"))); 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 @@ -62,14 +64,14 @@ class UpgradeApplicatorTests { FileCopyUtils.copy(new File("src/test/resources/bom.gradle"), bom); File gradleProperties = new File(this.temp, "gradle.properties"); FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties); - new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()) - .apply(new Upgrade(new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null), - DependencyVersion.parse("1.3.71"))); + new UpgradeApplicator(bom.toPath(), gradleProperties.toPath()).apply(new Upgrade( + new Library("Kotlin", DependencyVersion.parse("1.3.70"), null, null), DependencyVersion.parse("1.4"))); Properties properties = new Properties(); try (InputStream in = new FileInputStream(gradleProperties)) { 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")); } }