Search in sources :

Example 21 with Console

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");
}
Also used : Console(com.google.copybara.util.console.Console) Test(org.junit.Test)

Example 22 with Console

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;
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Message(com.google.copybara.util.console.Message) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console) ArrayList(java.util.ArrayList)

Example 23 with Console

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));
}
Also used : InfoFinishedEvent(com.google.copybara.monitor.EventMonitor.InfoFinishedEvent) Console(com.google.copybara.util.console.Console)

Example 24 with Console

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.");
}
Also used : MigrationReference(com.google.copybara.Info.MigrationReference) ConfigValidator(com.google.copybara.config.ConfigValidator) Iterables(com.google.common.collect.Iterables) Migration(com.google.copybara.config.Migration) InfoFinishedEvent(com.google.copybara.monitor.EventMonitor.InfoFinishedEvent) RepoException(com.google.copybara.exception.RepoException) ValidationException(com.google.copybara.exception.ValidationException) MessageType(com.google.copybara.util.console.Message.MessageType) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Message(com.google.copybara.util.console.Message) Config(com.google.copybara.config.Config) Preconditions(com.google.common.base.Preconditions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ValidationException(com.google.copybara.exception.ValidationException) Message(com.google.copybara.util.console.Message) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console)

Example 25 with Console

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?");
}
Also used : Console(com.google.copybara.util.console.Console) Test(org.junit.Test)

Aggregations

Console (com.google.copybara.util.console.Console)38 Test (org.junit.Test)19 ValidationException (com.google.copybara.exception.ValidationException)14 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)11 IOException (java.io.IOException)11 ImmutableList (com.google.common.collect.ImmutableList)10 Config (com.google.copybara.config.Config)8 Path (java.nio.file.Path)7 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)6 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 Before (org.junit.Before)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 Migration (com.google.copybara.config.Migration)5 Glob (com.google.copybara.util.Glob)5 RunWith (org.junit.runner.RunWith)5 JUnit4 (org.junit.runners.JUnit4)5 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)4 ConfigWithDependencies (com.google.copybara.config.SkylarkParser.ConfigWithDependencies)4 RepoException (com.google.copybara.exception.RepoException)4 Change (com.google.copybara.revision.Change)4