Add ConfigProperty Validation

This commit is contained in:
2023-02-18 23:53:26 +01:00
parent d0c4b07f52
commit f69fbedee0
4 changed files with 31 additions and 20 deletions

View File

@@ -2,18 +2,24 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.AllArgsConstructor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@AllArgsConstructor
public enum ConfigProperty {
CONFIG_PATH("config-path", "c", "Path to config file"),
CONFIG_PATH("config-path", "p", "Path to config file"),
LIBRARY("library", "l", "Path to library"),
SAFE_MODE("safe-mode", "s", "Test run (no files will be changes)"),
WINDOWS("windows", null, "Is operating system windows"),
THREADS("threads", "t", "thread count (default: 2)"),
INCLUDE_PATTERN("include-pattern", "p", "Include files matching pattern"),
INCLUDE_PATTERN("include-pattern", "i", "Include files matching pattern"),
MKV_TOOL_NIX("mkvtoolnix", "m", "Path to mkv tool nix installation"),
FORCED_KEYWORDS("forcedKeywords", "fk", "Additional keywords to identify forced tracks"),
COMMENTARY_KEYWORDS("commentary-keywords", "ck", "Additional keywords to identify commentary tracks"),
EXCLUDE_DIRECTORY("exclude-directories", "e", "Directories to be excluded, combines with config file"),
COHERENT("coherent", "c", "Try to match whole series with same config"),
HELP("help", "h", "\"for help this is\" - Yoda"),
VERSION("version", "v", "Display version"),
ARGUMENTS("arguments", null, "List of arguments"),
@@ -34,4 +40,19 @@ public enum ConfigProperty {
public String desc() {
return description;
}
/*
* Verify at startup that there are no duplicated shortParameters.
*/
static {
Set<String> shortParameters = new HashSet<>();
for (String param: Arrays.stream(ConfigProperty.values()).map(ConfigProperty::abrv).collect(Collectors.toList())) {
if (shortParameters.contains(param)) {
throw new IllegalStateException("It is not allowed to have multiple properties with the same abbreviation!");
}
if (param != null) {
shortParameters.add(param);
}
}
}
}

View File

@@ -3,7 +3,6 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import org.apache.commons.cli.CommandLineParser;
@@ -16,8 +15,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
@@ -25,14 +22,9 @@ import java.util.stream.Stream;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.argumentsOf;
import static org.junit.jupiter.api.Assertions.*;
class AttributeConfigValidatorTest {
private static final String TEST_DIR = "src/test/resources/test-dir";
private static final String TEST_FILE = "src/test/resources/test-dir/test-file.mkv";
private static final String TEST_CONFIG = "src/test/resources/test-dir/test-config.yml";
private static CommandLineParser parser;
private static Options options;
@@ -71,13 +63,13 @@ class AttributeConfigValidatorTest {
}
private static String attrConfYaml(String... languages) {
String yaml = "attribute-config: ";
StringBuilder yaml = new StringBuilder("attribute-config: ");
int counter = 0;
for (int i = 0; i < languages.length; i += 2) {
counter++;
yaml += String.format("\n %s:\n audio: %s\n subtitle: %s", counter, languages[0], languages[1]);
yaml.append(String.format("\n %s:\n audio: %s\n subtitle: %s", counter, languages[0], languages[1]));
}
return yaml;
return yaml.toString();
}
private static List<AttributeConfig> attrConf(String... languages) {

View File

@@ -20,13 +20,11 @@ import java.util.stream.Stream;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.*;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ValidationResult.INVALID;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.CONFIG_PATH;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.LIBRARY;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.TestUtil.argumentsOf;
import static org.junit.jupiter.api.Assertions.*;
class ConfigPathValidatorTest {
private static final String TEST_DIR = "src/test/resources/test-dir";
private static final String TEST_FILE = "src/test/resources/test-dir/test-file.mkv";
private static final String TEST_CONFIG = "src/test/resources/test-dir/test-config.yml";
@@ -42,11 +40,11 @@ class ConfigPathValidatorTest {
private static Stream<Arguments> provideTestCases() {
return Stream.of(
argumentsOf(CONFIG_PATH, true, null, "", new String[]{"-c", TEST_CONFIG}, VALID),
argumentsOf(CONFIG_PATH, true, null, "", new String[]{"-p", TEST_CONFIG}, VALID),
argumentsOf(CONFIG_PATH, true, null, "config-path: " + TEST_CONFIG, new String[]{}, MISSING),
argumentsOf(CONFIG_PATH, false, null, "config-path: " + TEST_CONFIG, new String[]{}, NOT_PRESENT),
argumentsOf(CONFIG_PATH, false, Path.of(TEST_CONFIG).toFile(), "", new String[]{}, DEFAULT),
argumentsOf(CONFIG_PATH, true, null, "", new String[]{"-c", TEST_FILE}, INVALID)
argumentsOf(CONFIG_PATH, true, null, "", new String[]{"-p", TEST_FILE}, INVALID)
);
}

View File

@@ -37,7 +37,7 @@ class PatternValidatorTest {
private static Stream<Arguments> provideTestCases() {
return Stream.of(
argumentsOf(INCLUDE_PATTERN, false, null, "include-pattern: \"[abd]?.*\"", new String[]{}, VALID),
argumentsOf(INCLUDE_PATTERN, true, null, "", new String[]{"-p", "[abd]?.*"}, VALID),
argumentsOf(INCLUDE_PATTERN, true, null, "", new String[]{"-i", "[abd]?.*"}, VALID),
argumentsOf(INCLUDE_PATTERN, false, Pattern.compile(".*"), "", new String[]{}, DEFAULT),
argumentsOf(INCLUDE_PATTERN, true, Pattern.compile(".*"), "", new String[]{}, DEFAULT),
@@ -45,9 +45,9 @@ class PatternValidatorTest {
argumentsOf(INCLUDE_PATTERN, true, null, "", new String[]{}, MISSING),
argumentsOf(INCLUDE_PATTERN, false, null, "", new String[]{}, NOT_PRESENT),
argumentsOf(INCLUDE_PATTERN, true, null, "", new String[]{"-p", "?."}, INVALID),
argumentsOf(INCLUDE_PATTERN, true, null, "", new String[]{"-i", "?."}, INVALID),
argumentsOf(INCLUDE_PATTERN, false, null, "include-pattern: \"[arst*\"", new String[]{}, INVALID),
argumentsOf(INCLUDE_PATTERN, true, Pattern.compile(".?"), "", new String[]{"-p", "?."}, INVALID)
argumentsOf(INCLUDE_PATTERN, true, Pattern.compile(".?"), "", new String[]{"-i", "?."}, INVALID)
);
}