From f2112fe3e80c7b9508c07b4ca9bcb5f71e2ec72a Mon Sep 17 00:00:00 2001 From: TheSnoozer <6849390+TheSnoozer@users.noreply.github.com> Date: Tue, 19 Mar 2024 21:01:50 +0100 Subject: [PATCH] https://github.com/git-commit-id/git-commit-id-plugin-core/issues/99: try to improve error messages when the json properties can't be read --- .../core/util/GenericFileManager.java | 8 +- .../pl/project13/core/util/JsonManager.java | 3 + .../pl/project13/core/util/YmlManager.java | 5 +- .../core/PropertiesFileGeneratorTest.java | 98 ++++++++++++++++--- 4 files changed, 99 insertions(+), 15 deletions(-) diff --git a/src/main/java/pl/project13/core/util/GenericFileManager.java b/src/main/java/pl/project13/core/util/GenericFileManager.java index 0185d7f..0ac8709 100644 --- a/src/main/java/pl/project13/core/util/GenericFileManager.java +++ b/src/main/java/pl/project13/core/util/GenericFileManager.java @@ -33,7 +33,10 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; public class GenericFileManager { public static Properties readPropertiesAsUtf8( @@ -61,6 +64,7 @@ public static Properties readProperties( ); } + @Nonnull public static Properties readProperties( @Nullable LogInterface log, @Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat, @@ -139,7 +143,9 @@ public static void dumpProperties( } } } catch (final IOException ex) { - throw new GitCommitIdExecutionException("Cannot create custom git properties file: " + gitPropsFile, ex); + throw new GitCommitIdExecutionException( + String.format("Failed to write %s file [%s] (for project %s)...", + propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName)); } } } diff --git a/src/main/java/pl/project13/core/util/JsonManager.java b/src/main/java/pl/project13/core/util/JsonManager.java index 5fd9a04..87776df 100644 --- a/src/main/java/pl/project13/core/util/JsonManager.java +++ b/src/main/java/pl/project13/core/util/JsonManager.java @@ -64,6 +64,9 @@ protected static Properties readJsonProperties(@Nonnull File jsonFile, Charset s }); } } + } catch (jakarta.json.stream.JsonParsingException e) { + // We are likely trying to read a properties that that was not encoded with json-syntax + throw new CannotReadFileException(e); } catch (IOException e) { throw new CannotReadFileException(e); } diff --git a/src/main/java/pl/project13/core/util/YmlManager.java b/src/main/java/pl/project13/core/util/YmlManager.java index 74537aa..a215766 100644 --- a/src/main/java/pl/project13/core/util/YmlManager.java +++ b/src/main/java/pl/project13/core/util/YmlManager.java @@ -67,10 +67,13 @@ protected static Properties readYmlProperties(@Nonnull File xmlFile, Charset sou loaderOptions.setProcessComments(false); Yaml yaml = new Yaml(loaderOptions); Map data = yaml.load(reader); - for (Map.Entry e: data.entrySet()) { + for (Map.Entry e : data.entrySet()) { retVal.put(e.getKey(), e.getValue()); } } + } catch (ClassCastException e) { + // We are likely trying to read a properties that that was not encoded with yml-syntax + throw new CannotReadFileException(e); } catch (IOException e) { throw new CannotReadFileException(e); } diff --git a/src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java b/src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java index a5aa2ef..24f7ca5 100644 --- a/src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java +++ b/src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java @@ -17,38 +17,63 @@ package pl.project13.core; -import org.junit.Before; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; import pl.project13.core.log.LogInterface; import pl.project13.core.util.BuildFileChangeListener; +import pl.project13.core.util.GenericFileManager; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; import java.util.Properties; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; +@RunWith(JUnitParamsRunner.class) public class PropertiesFileGeneratorTest { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); - private PropertiesFileGenerator propertiesFileGenerator; - - @Before - public void setUp() { - LogInterface logInterface = mock(LogInterface.class); + private LogInterface getLogInterface() { + return mock(LogInterface.class); + } + + private BuildFileChangeListener getBuildFileChangeListener() { BuildFileChangeListener buildFileChangeListener = file -> { // Ignore }; + return buildFileChangeListener; + } - propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, CommitIdPropertiesOutputFormat.PROPERTIES, "", "test"); + private PropertiesFileGenerator getPropertiesFileGenerator() { + return getPropertiesFileGenerator(CommitIdPropertiesOutputFormat.PROPERTIES); + } + + private PropertiesFileGenerator getPropertiesFileGenerator(CommitIdPropertiesOutputFormat propertiesOutputFormat) { + return new PropertiesFileGenerator( + getLogInterface(), + getBuildFileChangeListener(), + CommitIdPropertiesOutputFormat.PROPERTIES, + "", + "test" + ); } /** @@ -66,7 +91,7 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文"); Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties"); - propertiesFileGenerator.maybeGeneratePropertiesFile( + getPropertiesFileGenerator().maybeGeneratePropertiesFile( properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, false); String actualContent = Files.readString(propertiesPath, UTF_8); @@ -85,7 +110,7 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文"); Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties"); - propertiesFileGenerator.maybeGeneratePropertiesFile( + getPropertiesFileGenerator().maybeGeneratePropertiesFile( properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true); String actualContent = Files.readString(propertiesPath, UTF_8); @@ -103,7 +128,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI properties.put(GitCommitPropertyConstant.BRANCH, "develop"); Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties"); - propertiesFileGenerator.maybeGeneratePropertiesFile( + getPropertiesFileGenerator().maybeGeneratePropertiesFile( properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true); String actualContent = Files.readString(propertiesPath, UTF_8); @@ -114,11 +139,12 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI } @Test - public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException, IOException { + public void reReadGeneratedPropertiesFile() throws GitCommitIdExecutionException, IOException { Properties properties = new Properties(); properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29"); properties.put(GitCommitPropertyConstant.BRANCH, "develop"); - + + PropertiesFileGenerator propertiesFileGenerator = getPropertiesFileGenerator(); Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties"); propertiesFileGenerator.maybeGeneratePropertiesFile( properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true); @@ -140,7 +166,7 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29"); Path relativePath = new File("src/blah/blub/git.properties").toPath(); - propertiesFileGenerator.maybeGeneratePropertiesFile( + getPropertiesFileGenerator().maybeGeneratePropertiesFile( properties, temporaryFolder.getRoot(), relativePath.toFile(), UTF_8, false); @@ -151,4 +177,50 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException + "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"); assertEquals(expectedContent, actualContent); } + + public Collection dumpAndReadFormats() { + Collection collection = Arrays.stream(CommitIdPropertiesOutputFormat.values()).flatMap(f1 -> + Arrays.stream(CommitIdPropertiesOutputFormat.values()).map(f2 -> { + if (f1.equals(f2)) { + return Optional.empty(); + } else { + return Optional.of(new Object[]{f1, f2}); + } + }).filter(o -> o.isPresent()).map(o -> o.get()) + ).collect(Collectors.toSet()); + return collection; + } + + + @Test + @Parameters(method = "dumpAndReadFormats") + @Ignore("Read and write is not consistent...") + // https://github.com/git-commit-id/git-commit-id-plugin-core/issues/99 + public void reReadGeneratedPropertiesFileWithDifferentFormats( + CommitIdPropertiesOutputFormat dumpFormat, + CommitIdPropertiesOutputFormat readFormat + ) throws GitCommitIdExecutionException, IOException { + Properties dumpedProperties = new Properties(); + dumpedProperties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29"); + dumpedProperties.put(GitCommitPropertyConstant.BRANCH, "develop"); + + Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.json"); + GenericFileManager.dumpProperties( + getLogInterface(), + dumpFormat, + propertiesPath.toFile(), + UTF_8, + true, + "test", + dumpedProperties + ); + Properties readProperties = GenericFileManager.readProperties( + getLogInterface(), + readFormat, + propertiesPath.toFile(), + UTF_8, + "test" + ); + Assert.assertEquals(dumpedProperties, readProperties); + } }