Search in sources :

Example 1 with ValidationResult

use of com.google.copybara.config.ValidationResult 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 2 with ValidationResult

use of com.google.copybara.config.ValidationResult 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 3 with ValidationResult

use of com.google.copybara.config.ValidationResult in project copybara by google.

the class ReadConfigFromChangeWorkflowTest method testWriterStateMaintained.

/**
 * A test that check that we can mutate the glob in iterative mode
 */
@SuppressWarnings("unchecked")
@Test
public void testWriterStateMaintained() throws Exception {
    options.workflowOptions.lastRevision = "0";
    String configCode = mutatingWorkflow("*");
    Config cfg = skylark.loadConfig(configCode);
    ConfigLoader constantConfigLoader = new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configCode), options.general.getStarlarkMode()) {

        @Override
        protected Config doLoadForRevision(Console console, Revision revision) throws ValidationException {
            try {
                return skylark.loadConfig(mutatingWorkflow(revision.asString()));
            } catch (IOException e) {
                throw new AssertionError("Should not fail", e);
            }
        }
    };
    ReadConfigFromChangeWorkflow<?, ?> wf = new ReadConfigFromChangeWorkflow<>((Workflow) cfg.getMigration("default"), options.build(), constantConfigLoader, new ConfigValidator() {

        @Override
        public ValidationResult validate(Config config, String migrationName) {
            return ValidationResult.EMPTY;
        }
    });
    origin.singleFileChange(0, "base", "fileB", "b");
    origin.singleFileChange(1, "one", "file1", "b");
    origin.singleFileChange(2, "two", "file2", "b");
    origin.singleFileChange(3, "three", "file3", "b");
    wf.run(Files.createTempDirectory("workdir"), ImmutableList.of("3"));
    assertThat(destination.processed).hasSize(3);
    assertThat(destination.processed.get(0).getDestinationFiles().toString()).contains("file1");
    assertThat(destination.processed.get(0).getWorkdir()).containsExactly("file1", "b");
    assertThat(destination.processed.get(1).getDestinationFiles().toString()).contains("file2");
    assertThat(destination.processed.get(1).getWorkdir()).containsExactly("file2", "b");
    assertThat(destination.processed.get(2).getDestinationFiles().toString()).contains("file3");
    assertThat(destination.processed.get(2).getWorkdir()).containsExactly("file3", "b");
}
Also used : ConfigValidator(com.google.copybara.config.ConfigValidator) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) ValidationResult(com.google.copybara.config.ValidationResult) Test(org.junit.Test)

Example 4 with ValidationResult

use of com.google.copybara.config.ValidationResult in project copybara by google.

the class ValidateCmd method run.

@Override
public ExitCode run(CommandEnv commandEnv) throws ValidationException, IOException, RepoException {
    ConfigFileArgs configFileArgs = commandEnv.parseConfigFileArgs(this, /*useSourceRef*/
    false);
    ConfigLoader configLoader = configLoaderProvider.newLoader(configFileArgs.getConfigPath(), configFileArgs.getSourceRef());
    ValidationResult result = validate(commandEnv.getOptions(), configLoader, configFileArgs.getWorkflowName());
    Console console = commandEnv.getOptions().get(GeneralOptions.class).console();
    for (ValidationMessage message : result.getAllMessages()) {
        switch(message.getLevel()) {
            case WARNING:
                console.warn(message.getMessage());
                break;
            case ERROR:
                console.error(message.getMessage());
                break;
        }
    }
    if (result.hasErrors()) {
        console.errorFmt("Configuration '%s' is invalid.", configLoader.location());
        return ExitCode.CONFIGURATION_ERROR;
    }
    console.infoFmt("Configuration '%s' is valid.", configLoader.location());
    return ExitCode.SUCCESS;
}
Also used : ValidationMessage(com.google.copybara.config.ValidationResult.ValidationMessage) Console(com.google.copybara.util.console.Console) ValidationResult(com.google.copybara.config.ValidationResult)

Aggregations

ValidationResult (com.google.copybara.config.ValidationResult)4 Console (com.google.copybara.util.console.Console)4 Config (com.google.copybara.config.Config)3 ValidationException (com.google.copybara.exception.ValidationException)2 ConfigValidator (com.google.copybara.config.ConfigValidator)1 ValidationMessage (com.google.copybara.config.ValidationResult.ValidationMessage)1 IOException (java.io.IOException)1 Test (org.junit.Test)1