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();
}
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!");
}
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;
}
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);
}
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);
}
Aggregations