Skip to content

#99: try to improve error messages when the json properties can't be read #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/pl/project13/core/util/GenericFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -61,6 +64,7 @@ public static Properties readProperties(
);
}

@Nonnull
public static Properties readProperties(
@Nullable LogInterface log,
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
Expand Down Expand Up @@ -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));
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/pl/project13/core/util/JsonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/pl/project13/core/util/YmlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ protected static Properties readYmlProperties(@Nonnull File xmlFile, Charset sou
loaderOptions.setProcessComments(false);
Yaml yaml = new Yaml(loaderOptions);
Map<String, Object> data = yaml.load(reader);
for (Map.Entry<String, Object> e: data.entrySet()) {
for (Map.Entry<String, Object> 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);
}
Expand Down
98 changes: 85 additions & 13 deletions src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

/**
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);


Expand All @@ -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);
}
}