Skip to content

Commit

Permalink
Allow users to set color options in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
adityatrivedi committed Mar 4, 2016
1 parent db9b01c commit 0ea9d61
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/sleekbyte/tailor/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,9 @@ public class Messages {
public static final String FORMAT_SHORT_OPT = "f";
public static final String FORMAT_LONG_OPT = "format";
public static final String INVALID_OPTION_VALUE = "Invalid value provided for option ";

// Config options
public static final String INVERT = "invert";
public static final String DISABLE = "disable";

}
10 changes: 10 additions & 0 deletions src/main/java/com/sleekbyte/tailor/common/YamlConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class YamlConfiguration {
private Set<String> except = new HashSet<>();
private String format = "";
private boolean debug = false;
private String color = null;

public String getFormat() {
return format;
Expand Down Expand Up @@ -49,6 +50,10 @@ public boolean isDebug() {
return debug;
}

public String getColor() {
return color;
}

public void setFileLocation(String fileLocation) {
this.fileLocation = Optional.ofNullable(fileLocation);
}
Expand Down Expand Up @@ -76,4 +81,9 @@ public void setFormat(String format) {
public void setDebug(boolean debug) {
this.debug = debug;
}

public void setColor(String color) {
this.color = color;
}

}
38 changes: 36 additions & 2 deletions src/main/java/com/sleekbyte/tailor/utils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,38 @@ public boolean shouldListFiles() {
return cliArgumentParser.shouldListFiles();
}

/**
* Determine if the output should be colorized.
*
* @return whether the output should be colorized
*/
public boolean shouldColorOutput() {
return cliArgumentParser.shouldColorOutput();
boolean shouldColorOutput = cliArgumentParser.shouldColorOutput();
if (shouldColorOutput && yamlConfiguration.isPresent() && yamlConfiguration.get().getColor() != null) {
String option = yamlConfiguration.get().getColor();
validateColorOption(option);
if (option.equals(Messages.DISABLE)) {
shouldColorOutput = false;
}
}
return shouldColorOutput;
}

/**
* Determine if the output color should be inverted.
*
* @return whether the output color should be inverted
*/
public boolean shouldInvertColorOutput() {
return cliArgumentParser.shouldInvertColorOutput();
boolean shouldInvertColorOutput = cliArgumentParser.shouldInvertColorOutput();
if (!shouldInvertColorOutput && yamlConfiguration.isPresent() && yamlConfiguration.get().getColor() != null) {
String option = yamlConfiguration.get().getColor();
validateColorOption(option);
if (option.equals(Messages.INVERT)) {
shouldInvertColorOutput = true;
}
}
return shouldInvertColorOutput;
}

/**
Expand Down Expand Up @@ -281,4 +307,12 @@ private Set<Rules> getRulesFilteredByExcept(Set<String> parsedRules) throws CliA
return enabledRules.stream().filter(rule -> !parsedRules.contains(rule.getName())).collect(Collectors.toSet());
}

private void validateColorOption(String colorOption) throws YAMLException {
Set<String> colorOptions = new HashSet<>(Arrays.asList(Messages.DISABLE, Messages.INVERT));
if (!colorOptions.contains(colorOption)) {
throw new YAMLException("The color option was not recognized. Options are <" + Messages.DISABLE + "|"
+ Messages.INVERT + ">.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public void testExceptOption() throws IOException {
// Create config file that will only analyze YAML_TEST_2.swift for violations other than upper-camel-case
File configurationFile = exceptOptionConfig(".tailor.yml");
String[] command = new String[] {
"--config", configurationFile.getAbsolutePath(),
"--no-color"
"--config", configurationFile.getAbsolutePath()
};
runTest(command);
}
Expand Down Expand Up @@ -213,6 +212,26 @@ public void testOnlyExceptOptionPrecedence() throws IOException {
runTest(command);
}

@Test
public void testNoColorOption() throws IOException {
// Add expected output
addExpectedMsg(3, 7,
Rules.UPPER_CAMEL_CASE,
Messages.CLASS + Messages.NAMES + Messages.UPPER_CAMEL_CASE,
YAML_TEST_1);

addExpectedMsg(7, 7,
Rules.UPPER_CAMEL_CASE,
Messages.CLASS + Messages.NAMES + Messages.UPPER_CAMEL_CASE,
YAML_TEST_1);

File configurationFile = noColorConfig(".tailor.yml");
String[] command = new String[] {
"--config", configurationFile.getAbsolutePath(),
};
runTest(command);
}

@Test
public void testCliAndConfigFilePrecedence() throws IOException {

Expand Down Expand Up @@ -337,10 +356,27 @@ private File onlyAndExceptPrecedenceConfig(String fileName) throws IOException {
return configFile;
}

private File noColorConfig(String fileName) throws IOException {
File configFile = folder.newFile(fileName);
Writer streamWriter = new OutputStreamWriter(new FileOutputStream(configFile), Charset.forName("UTF-8"));
PrintWriter printWriter = new PrintWriter(streamWriter);
printWriter.println("include:");
printWriter.println(" - '**/" + YAML_TEST_1 + "'");
printWriter.println("only:");
printWriter.println(" - upper-camel-case");
printWriter.println("except:");
printWriter.println(" - terminating-semicolon");
printWriter.println("color: disable");
streamWriter.close();
printWriter.close();
return configFile;
}

private File exceptOptionConfig(String fileName) throws IOException {
File configFile = folder.newFile(fileName);
Writer streamWriter = new OutputStreamWriter(new FileOutputStream(configFile), Charset.forName("UTF-8"));
PrintWriter printWriter = new PrintWriter(streamWriter);
printWriter.println("color: disable");
printWriter.println("include:");
printWriter.println(" - '**/" + YAML_TEST_2 + "'");
printWriter.println("except:");
Expand Down

0 comments on commit 0ea9d61

Please sign in to comment.