Search in sources :

Example 6 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class Main method getConsole.

protected Console getConsole(String[] args) {
    boolean verbose = isVerbose(args);
    // If System.console() is not present, we are forced to use LogConsole
    Console console;
    if (System.console() == null) {
        console = LogConsole.writeOnlyConsole(System.err, verbose);
    } else if (Arrays.asList(args).contains(GeneralOptions.NOANSI)) {
        // The System.console doesn't detect redirects/pipes, but at least we have
        // jobs covered.
        console = LogConsole.readWriteConsole(System.in, System.err, verbose);
    } else {
        console = new AnsiConsole(System.in, System.err, verbose);
    }
    Optional<String> maybeConsoleFilePath = findFlagValue(args, GeneralOptions.CONSOLE_FILE_PATH);
    if (!maybeConsoleFilePath.isPresent()) {
        return console;
    }
    Path consoleFilePath = Paths.get(maybeConsoleFilePath.get());
    try {
        Files.createDirectories(consoleFilePath.getParent());
    } catch (IOException e) {
        logger.atSevere().withCause(e).log("Could not create parent directories to file: %s. Redirecting will be disabled.", consoleFilePath);
        return console;
    }
    return new FileConsole(console, consoleFilePath, getConsoleFlushRate(args));
}
Also used : Path(java.nio.file.Path) FileConsole(com.google.copybara.util.console.FileConsole) FileConsole(com.google.copybara.util.console.FileConsole) AnsiConsole(com.google.copybara.util.console.AnsiConsole) Console(com.google.copybara.util.console.Console) LogConsole(com.google.copybara.util.console.LogConsole) IOException(java.io.IOException) AnsiConsole(com.google.copybara.util.console.AnsiConsole)

Example 7 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class MigrateCmd method loadConfig.

private Config loadConfig(Options options, ConfigLoader configLoader, String migrationName) throws IOException, ValidationException {
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    Console console = generalOptions.console();
    Config config = configLoader.load(console);
    console.progress("Validating configuration");
    ValidationResult result = configValidator.validate(config, migrationName);
    if (!result.hasErrors()) {
        return config;
    }
    result.getErrors().forEach(console::error);
    console.error("Configuration is invalid.");
    throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console) ValidationResult(com.google.copybara.config.ValidationResult)

Example 8 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class ValidateCmd 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.
 *
 * Returns true iff this configuration is valid.
 */
private ValidationResult validate(Options options, ConfigLoader configLoader, String migrationName) throws IOException {
    Console console = options.get(GeneralOptions.class).console();
    ValidationResult.Builder resultBuilder = new ValidationResult.Builder();
    try {
        Config config = configLoader.load(console);
        resultBuilder.append(configValidator.validate(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();
        }
        resultBuilder.error(error.toString());
    }
    return resultBuilder.build();
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console) ValidationResult(com.google.copybara.config.ValidationResult)

Example 9 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class ApiCheckerTest method testCheckInternalError.

@Test
public void testCheckInternalError() throws CheckerException {
    ApiChecker checker = new ApiChecker(new FakeChecker() {

        @Override
        public void doCheck(ImmutableMap<String, String> fields, Console console) throws IOException {
            throw new IOException("Tool error!");
        }
    }, new TestingConsole());
    RuntimeException e1 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object()));
    assertThat(e1).hasMessageThat().isEqualTo("Error running checker");
    RuntimeException e2 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object(), "bar", new Object()));
    assertThat(e2).hasMessageThat().isEqualTo("Error running checker");
    RuntimeException e3 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object(), "bar", new Object(), "baz", new Object()));
    assertThat(e3).hasMessageThat().isEqualTo("Error running checker");
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class ApiCheckerTest method testCheck.

@Test
public void testCheck() throws CheckerException {
    ApiChecker checker = new ApiChecker(new FakeChecker() {

        @Override
        public void doCheck(ImmutableMap<String, String> fields, Console console) {
        }
    }, new TestingConsole());
    checker.check("foo", new Object());
    checker.check("foo", new Object(), "bar", new Object());
    checker.check("foo", new Object(), "bar", new Object(), "baz", new Object());
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) 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 RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)4