Search in sources :

Example 6 with Config

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

the class ConfigBuilderTest method testGitToGitTemplate.

@Test
public void testGitToGitTemplate() throws Exception {
    ConfigBuilder underTest = new ConfigBuilder(new GitToGitTemplate());
    underTest.setNamedStringParameter("origin_url", "github.com/origin");
    underTest.setNamedStringParameter("destination_url", "github.com/destination");
    underTest.setNamedStringParameter("email", "Name <foo@bar.com>");
    underTest.addStringKeywordParameter("mode", "SQUASH");
    String builtConfig = underTest.build();
    assertThat(builtConfig).isEqualTo(GIT_TO_GIT_CONFIG);
    assertThat(underTest.isValid()).isTrue();
    // Verify that config is valid
    Config cfg = skylark.loadConfig(builtConfig);
    assertThat(cfg.getMigrations()).containsKey("default");
}
Also used : Config(com.google.copybara.config.Config) Test(org.junit.Test)

Example 7 with Config

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

the class MetadataModuleTest method testCurrentRevForSquashEmptyChanges.

@Test
public void testCurrentRevForSquashEmptyChanges() throws Exception {
    options.setForce(true);
    passThruAuthoring();
    options.setLastRevision(origin.resolve("HEAD").asString());
    origin.singleFileChange(42, "test", "excluded", "");
    DummyRevision expectedRev = origin.resolve("HEAD");
    Config config = loadConfig("" + "core.workflow(\n" + "    name = 'default',\n" + "    origin =  testing.origin(),\n" + "    origin_files =  glob(['**'], exclude = ['excluded']),\n" + "    authoring = " + authoring + "\n," + "    destination = testing.destination(),\n" + "    mode = '" + WorkflowMode.SQUASH + "',\n" + "    transformations = [" + "        metadata.replace_message('${COPYBARA_CURRENT_REV}')," + "    ]\n" + ")\n");
    config.getMigration("default").run(workdir, ImmutableList.of());
    assertThat(Iterables.getLast(destination.processed).getChangesSummary()).isEqualTo(expectedRev.asString());
}
Also used : Config(com.google.copybara.config.Config) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 8 with Config

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

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

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

the class CopybaraTest method setUp.

@Before
public void setUp() throws Exception {
    optionsBuilder = new OptionsBuilder();
    console = new TestingConsole();
    eventMonitor = new TestingEventMonitor();
    optionsBuilder.setConsole(console);
    optionsBuilder.general.withEventMonitor(eventMonitor);
    migration = mock(Migration.class);
    config = new Config(ImmutableMap.of("workflow", migration), "foo/copy.bara.sky");
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Migration(com.google.copybara.config.Migration) Config(com.google.copybara.config.Config) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) Before(org.junit.Before)

Aggregations

Config (com.google.copybara.config.Config)17 ValidationException (com.google.copybara.exception.ValidationException)8 Console (com.google.copybara.util.console.Console)8 Test (org.junit.Test)8 Migration (com.google.copybara.config.Migration)7 IOException (java.io.IOException)5 ImmutableList (com.google.common.collect.ImmutableList)4 MigrationReference (com.google.copybara.Info.MigrationReference)4 Author (com.google.copybara.authoring.Author)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)4 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)4 MessageType (com.google.copybara.util.console.Message.MessageType)4 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)4 Path (java.nio.file.Path)4 Before (org.junit.Before)4 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3