Skip to content

Improve error message for multiple formatters using STDOUT #744

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class FormatterFactory {
put("rerun", RerunFormatter.class);
}};
private static final Pattern FORMATTER_WITH_FILE_PATTERN = Pattern.compile("([^:]+):(.*)");
private String defaultOutFormatter = null;
private Appendable defaultOut = new PrintStream(System.out) {
@Override
public void close() {
Expand Down Expand Up @@ -81,7 +82,7 @@ private Formatter instantiate(String formatterString, Class<? extends Formatter>
for (Class ctorArgClass : CTOR_ARGS) {
Constructor<? extends Formatter> constructor = findConstructor(formatterClass, ctorArgClass);
if (constructor != null) {
Object ctorArg = convertOrNull(pathOrUrl, ctorArgClass);
Object ctorArg = convertOrNull(pathOrUrl, ctorArgClass, formatterString);
try {
if (ctorArgClass == null) {
return constructor.newInstance();
Expand All @@ -103,7 +104,7 @@ private Formatter instantiate(String formatterString, Class<? extends Formatter>
throw new CucumberException(String.format("%s must have a constructor that is either empty or a single arg of one of: %s", formatterClass, asList(CTOR_ARGS)));
}

private Object convertOrNull(String pathOrUrl, Class ctorArgClass) throws IOException, URISyntaxException {
private Object convertOrNull(String pathOrUrl, Class ctorArgClass, String formatterString) throws IOException, URISyntaxException {
if (ctorArgClass == null) {
return null;
}
Expand All @@ -126,7 +127,7 @@ private Object convertOrNull(String pathOrUrl, Class ctorArgClass) throws IOExce
if (pathOrUrl != null) {
return new UTF8OutputStreamWriter(new URLOutputStream(toURL(pathOrUrl)));
} else {
return defaultOutOrFailIfAlreadyUsed();
return defaultOutOrFailIfAlreadyUsed(formatterString);
}
}
return null;
Expand Down Expand Up @@ -161,12 +162,15 @@ private Class<? extends Formatter> loadClass(String className) {
}
}

private Appendable defaultOutOrFailIfAlreadyUsed() {
private Appendable defaultOutOrFailIfAlreadyUsed(String formatterString) {
try {
if (defaultOut != null) {
defaultOutFormatter = formatterString;
return defaultOut;
} else {
throw new CucumberException("Only one formatter can use STDOUT. If you use more than one formatter you must specify output path with FORMAT:PATH_OR_URL");
throw new CucumberException("Only one formatter can use STDOUT, now both " +
defaultOutFormatter + " and " + formatterString + " use it. " +
"If you use more than one formatter you must specify output path with FORMAT:PATH_OR_URL");
}
} finally {
defaultOut = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public void instantiates_single_custom_appendable_formatter_with_stdout() {
fc.create("cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable");
fail();
} catch (CucumberException expected) {
assertEquals("Only one formatter can use STDOUT. If you use more than one formatter you must specify output path with FORMAT:PATH_OR_URL", expected.getMessage());
assertEquals("Only one formatter can use STDOUT, now both cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable " +
"and cucumber.runtime.formatter.FormatterFactoryTest$WantsAppendable use it. " +
"If you use more than one formatter you must specify output path with FORMAT:PATH_OR_URL", expected.getMessage());
}
}

Expand Down