use of com.google.copybara.util.console.Console in project copybara by google.
the class TestingConsoleTest method testThrowsExceptionIfNoMoreResponses.
@Test
public void testThrowsExceptionIfNoMoreResponses() throws Exception {
Console console = new TestingConsole().respondNo();
assertThat(console.promptConfirmation("Proceed?")).isFalse();
IllegalStateException expected = assertThrows(IllegalStateException.class, () -> console.promptConfirmation("Proceed?"));
assertThat(expected).hasMessageThat().contains("No more programmed responses");
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class Copybara method validate.
/**
* Validates that the configuration is correct and that there is a valid migration specified by
* {@code migrationName}.
*
* <p>Note that, besides validating the specific migration, all the configuration will be
* validated syntactically.
*/
public boolean validate(Options options, ConfigLoader<?> configLoader, String migrationName) throws IOException {
Console console = options.get(GeneralOptions.class).console();
ArrayList<Message> messages = new ArrayList<>();
try {
Config config = configLoader.loadConfig(options, console);
messages.addAll(validateConfig(config, migrationName));
} catch (ValidationException e) {
// The validate subcommand should not throw Validation exceptions but log a result
StringBuilder error = new StringBuilder(e.getMessage()).append("\n");
Throwable cause = e.getCause();
while (cause != null) {
error.append(" CAUSED BY: ").append(cause.getMessage()).append("\n");
cause = cause.getCause();
}
messages.add(Message.error(error.toString()));
}
messages.forEach(message -> message.printTo(console));
boolean hasNoErrors = messages.stream().noneMatch(message -> message.getType() == MessageType.ERROR);
if (hasNoErrors) {
console.info(String.format("Configuration '%s' is valid.", configLoader.location()));
} else {
console.error(String.format("Configuration '%s' is invalid.", configLoader.location()));
}
return hasNoErrors;
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class Copybara method info.
/**
* Retrieves the {@link Info} of the {@code migrationName} and prints it to the console.
*/
public void info(Options options, Config config, String migrationName) throws ValidationException, RepoException {
@SuppressWarnings("unchecked") Info<? extends Revision> info = getInfo(migrationName, config);
Console console = options.get(GeneralOptions.class).console();
int outputSize = 0;
for (MigrationReference<? extends Revision> migrationRef : info.migrationReferences()) {
console.info(String.format("'%s': last_migrated %s - last_available %s.", migrationRef.getLabel(), migrationRef.getLastMigrated() != null ? migrationRef.getLastMigrated().asString() : "None", migrationRef.getLastAvailableToMigrate() != null ? migrationRef.getLastAvailableToMigrate().asString() : "None"));
ImmutableList<? extends Change<? extends Revision>> availableToMigrate = migrationRef.getAvailableToMigrate();
int outputLimit = options.get(GeneralOptions.class).getOutputLimit();
if (!availableToMigrate.isEmpty()) {
console.infoFmt("Available changes%s:", availableToMigrate.size() <= outputLimit ? "" : String.format(" (showing only first %d out of %d)", outputLimit, availableToMigrate.size()));
int changeNumber = 1;
for (Change<? extends Revision> change : Iterables.limit(availableToMigrate, outputLimit)) {
outputSize++;
console.info(String.format("%d - %s %s by %s", changeNumber++, change.getRevision().asString(), change.firstLineMessage(), change.getAuthor()));
}
}
// TODO(danielromero): Check flag usage on 2018-06 and decide if we keep it
if (outputSize > 100) {
console.infoFmt("Use %s to limit the output of the command.", GeneralOptions.OUTPUT_LIMIT_FLAG);
}
}
options.get(GeneralOptions.class).eventMonitor().onInfoFinished(new InfoFinishedEvent(info));
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class Copybara method loadConfig.
protected Config loadConfig(Options options, ConfigLoader<?> configLoader, String migrationName) throws IOException, ValidationException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Console console = generalOptions.console();
Config config = configLoader.loadConfig(options, console);
console.progress("Validating configuration");
List<Message> validationMessages = validateConfig(config, migrationName);
List<Message> errors = validationMessages.stream().filter(message -> message.getType() == MessageType.ERROR).collect(Collectors.toList());
if (errors.isEmpty()) {
return config;
}
errors.forEach(error -> error.printTo(console));
console.error("Configuration is invalid.");
throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class TestingConsoleTest method throwsExceptionIfNoMoreResponses.
@Test
public void throwsExceptionIfNoMoreResponses() throws Exception {
Console console = new TestingConsole().respondNo();
console.promptConfirmation("Proceed?");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("No more programmed responses");
console.promptConfirmation("Proceed?");
}
Aggregations