Search in sources :

Example 11 with RecordsProcessCallDestination

use of com.google.copybara.testing.RecordsProcessCallDestination in project copybara by google.

the class WorkflowTest method setup.

@Before
public void setup() throws Exception {
    options = new OptionsBuilder();
    options.setOutputRootToTmpDir();
    authoring = "authoring.overwrite('" + DEFAULT_AUTHOR + "')";
    includeReleaseNotes = false;
    workdir = Files.createTempDirectory("workdir");
    Files.createDirectories(workdir);
    origin = new DummyOrigin().setAuthor(ORIGINAL_AUTHOR);
    originFiles = "glob(['**'], exclude = ['copy.bara.sky', 'excluded/**'])";
    destinationFiles = "glob(['**'])";
    destination = new RecordsProcessCallDestination();
    transformations = ImmutableList.of("" + "        core.replace(\n" + "             before = '${linestart}${number}',\n" + "             after = '${linestart}" + PREFIX + "${number}',\n" + "             regex_groups = {\n" + "                 'number'    : '[0-9]+',\n" + "                 'linestart' : '^',\n" + "             },\n" + "             multiline = True," + "        )");
    TestingConsole console = new TestingConsole();
    options.setConsole(console);
    options.testingOptions.origin = origin;
    options.testingOptions.destination = destination;
    // Force by default unless we are testing the flag.
    options.setForce(true);
    skylark = new SkylarkTestExecutor(options);
    eventMonitor = new TestingEventMonitor();
    options.general.enableEventMonitor("test", eventMonitor);
    transformWork = TransformWorks.of(workdir, "example", console);
    setRevId = true;
    smartPrune = false;
    migrateNoopChangesField = false;
    extraWorkflowFields = ImmutableList.of();
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) DummyOrigin(com.google.copybara.testing.DummyOrigin) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) Before(org.junit.Before)

Example 12 with RecordsProcessCallDestination

use of com.google.copybara.testing.RecordsProcessCallDestination in project copybara by google.

the class WorkflowTest method testOnFinishHook_error.

// Validates that the hook is executed when the workflow throws ValidationException, and that
// the correct effect is populated
@Test
public void testOnFinishHook_error() throws Exception {
    options.testingOptions.destination = new RecordsProcessCallDestination() {

        @Override
        public Writer<Revision> newWriter(WriterContext writerContext) {
            return new RecordsProcessCallDestination.WriterImpl(false) {

                @Override
                public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException {
                    throw new ValidationException("Validation exception!");
                }
            };
        }
    };
    verifyHookForException(ValidationException.class, Type.ERROR, "Validation exception!");
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 13 with RecordsProcessCallDestination

use of com.google.copybara.testing.RecordsProcessCallDestination in project copybara by google.

the class WorkflowTest method checkIterativeModeWithError.

@SuppressWarnings("unchecked")
private <T extends Exception> T checkIterativeModeWithError(T exception) throws IOException, ValidationException {
    for (int timestamp = 0; timestamp < 10; timestamp++) {
        origin.addSimpleChange(timestamp);
    }
    // Override destination with one that always throws EmptyChangeException.
    options.testingOptions.destination = new RecordsProcessCallDestination() {

        @Override
        public Writer<Revision> newWriter(WriterContext writerContext) {
            return new WriterImpl(writerContext.isDryRun()) {

                @Override
                public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException, RepoException {
                    assert exception != null;
                    Throwables.propagateIfPossible(exception, ValidationException.class, RepoException.class);
                    throw new RuntimeException(exception);
                }
            };
        }
    };
    Workflow<?, ?> workflow = iterativeWorkflow(/*previousRef=*/
    "1");
    try {
        workflow.run(workdir, ImmutableList.of("3"));
        fail();
    } catch (Exception expected) {
        assertThat(expected).isInstanceOf(expected.getClass());
        return (T) expected;
    }
    return exception;
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) RepoException(com.google.copybara.exception.RepoException) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) VoidOperationException(com.google.copybara.exception.VoidOperationException) NotADestinationFileException(com.google.copybara.exception.NotADestinationFileException) RepoException(com.google.copybara.exception.RepoException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Glob(com.google.copybara.util.Glob)

Example 14 with RecordsProcessCallDestination

use of com.google.copybara.testing.RecordsProcessCallDestination in project copybara by google.

the class GitOriginTest method autoDetectBranchAtGitOrigin.

@Test
public void autoDetectBranchAtGitOrigin() throws Exception {
    Files.createDirectories(remote.resolve("include"));
    Files.write(remote.resolve("include/fileA.txt"), new byte[0]);
    git("add", "include/fileA.txt");
    git("commit", "-m", "not include");
    defaultBranch = repo.getPrimaryBranch(url);
    git("checkout", defaultBranch);
    Files.createDirectories(remote.resolve("include"));
    Files.write(remote.resolve("include/mainline-file.txt"), new byte[0]);
    git("add", "include/mainline-file.txt");
    git("commit", "-m", "message_a!");
    options.setForce(true);
    RecordsProcessCallDestination destination = new RecordsProcessCallDestination();
    options.testingOptions.destination = destination;
    options.setLastRevision(firstCommitRef);
    @SuppressWarnings("unchecked") Workflow<GitRevision, Revision> wf = (Workflow<GitRevision, Revision>) skylark.loadConfig("" + "core.workflow(\n" + "    name = 'default',\n" + "    origin = git.origin(\n" + // Intentionally pick the "wrong" ref.
    "         ref = '" + (defaultBranch.equals("master") ? "main" : "master") + "',\n" + "         url = '" + url + "',\n" + "         primary_branch_migration = True,\n" + "    ),\n" + "    origin_files = glob(['include/mainline-file.txt']),\n" + "    destination = testing.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = authoring.pass_thru('example <example@example.com>'),\n" + ")\n").getMigration("default");
    wf.run(Files.createTempDirectory("foo"), ImmutableList.of("HEAD"));
    List<ProcessedChange> changes = destination.processed;
    assertThat(changes).hasSize(1);
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Revision(com.google.copybara.Revision) Workflow(com.google.copybara.Workflow) Test(org.junit.Test)

Example 15 with RecordsProcessCallDestination

use of com.google.copybara.testing.RecordsProcessCallDestination in project copybara by google.

the class GitOriginTest method doNotCountCommitsOutsideOfOriginFileRoots.

@Test
public void doNotCountCommitsOutsideOfOriginFileRoots() throws Exception {
    writeFile(remote, "excluded_file.txt", "some content");
    repo.add().files("excluded_file.txt").run();
    git("commit", "-m", "excluded_file", "--date", COMMIT_TIME);
    // Note that one of the roots looks like a flag for "git log"
    originFiles = createGlob(ImmutableList.of("include/**", "--parents/**"), ImmutableList.of());
    RecordsProcessCallDestination destination = new RecordsProcessCallDestination();
    options.testingOptions.destination = destination;
    options.setForce(true);
    Path workdir = Files.createTempDirectory("workdir");
    // No files are in the included roots - make sure we can get an empty list of changes.
    GitRevision firstRef = origin.resolve(firstCommitRef);
    options.setLastRevision(firstCommitRef);
    String config = "" + "core.workflow(\n" + "    name = 'default',\n" + "    origin = git.origin(\n" + "         url = 'file://" + repo.getGitDir() + "',\n" + "         ref = 'other',\n" + "    ),\n" + "    origin_files = glob(['include/**', '--parents/**']),\n" + "    destination = testing.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = authoring.pass_thru('example <example@example.com>'),\n" + ")\n";
    assertThrows(EmptyChangeException.class, () -> skylark.loadConfig(config).getMigration("default").run(workdir, ImmutableList.of("HEAD")));
    // Now add a file in an included root and make sure we get that change from the Reader.
    Files.createDirectories(remote.resolve("--parents"));
    writeFile(remote, "--parents/included_file.txt", "some content");
    repo.add().files("--parents/included_file.txt").run();
    git("commit", "-m", "included_file", "--date", COMMIT_TIME);
    String firstExpected = repo.parseRef("HEAD");
    skylark.loadConfig(config).getMigration("default").run(workdir, ImmutableList.of("HEAD"));
    GitRevision firstIncludedRef = (GitRevision) Iterables.getOnlyElement(destination.processed).getOriginRef();
    assertThat(firstIncludedRef.getSha1()).isEqualTo(firstExpected);
    // Add an excluded file, and make sure the commit is skipped.
    writeFile(remote, "excluded_file_2.txt", "some content");
    repo.add().files("excluded_file_2.txt").run();
    git("commit", "-m", "excluded_file_2", "--date", COMMIT_TIME);
    destination.processed.clear();
    skylark.loadConfig(config).getMigration("default").run(workdir, ImmutableList.of("HEAD"));
    firstIncludedRef = (GitRevision) Iterables.getOnlyElement(destination.processed).getOriginRef();
    // Still only change is the one that changed included files.
    assertThat(firstIncludedRef.getSha1()).isEqualTo(firstExpected);
    // Add another included file, and make sure we only get the 2 included changes, and the
    // intervening excluded one is absent.
    writeFile(remote, "--parents/included_file_2.txt", "some content");
    repo.add().files("--parents/included_file_2.txt").run();
    git("commit", "-m", "included_file_2", "--date", COMMIT_TIME);
    String secondExpected = repo.parseRef("HEAD");
    destination.processed.clear();
    skylark.loadConfig(config).getMigration("default").run(workdir, ImmutableList.of("HEAD"));
    assertThat(destination.processed).hasSize(2);
    assertThat(destination.processed.get(0).getOriginRef().asString()).isEqualTo(firstExpected);
    assertThat(destination.processed.get(1).getOriginRef().asString()).isEqualTo(secondExpected);
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) Test(org.junit.Test)

Aggregations

RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)18 Test (org.junit.Test)11 Workflow (com.google.copybara.Workflow)7 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)7 Revision (com.google.copybara.Revision)6 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)6 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 Before (org.junit.Before)6 DummyOrigin (com.google.copybara.testing.DummyOrigin)5 ValidationException (com.google.copybara.exception.ValidationException)4 ProcessedChange (com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange)4 Console (com.google.copybara.util.console.Console)4 Path (java.nio.file.Path)4 ImmutableList (com.google.common.collect.ImmutableList)3 Glob (com.google.copybara.util.Glob)3 ChangeRejectedException (com.google.copybara.exception.ChangeRejectedException)2 RepoException (com.google.copybara.exception.RepoException)2 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)2 IOException (java.io.IOException)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1