Search in sources :

Example 21 with OptionsBuilder

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

the class VerifyMatchTest method setup.

@Before
public void setup() throws IOException {
    FileSystem fs = Jimfs.newFileSystem();
    checkoutDir = fs.getPath("/");
    Files.createDirectories(checkoutDir);
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console);
    skylark = new SkylarkTestExecutor(options, Core.class);
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) FileSystem(java.nio.file.FileSystem) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Core(com.google.copybara.Core) Before(org.junit.Before)

Example 22 with OptionsBuilder

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

the class MetadataModuleTest method setup.

@Before
public void setup() throws Exception {
    options = new OptionsBuilder();
    authoring = "authoring.overwrite('" + DEFAULT_AUTHOR + "')";
    workdir = Files.createTempDirectory("workdir");
    Files.createDirectories(workdir);
    origin = new DummyOrigin().setAuthor(ORIGINAL_AUTHOR);
    destination = new RecordsProcessCallDestination();
    options.setConsole(new TestingConsole());
    options.testingOptions.origin = origin;
    options.testingOptions.destination = destination;
    skylark = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, TestingModule.class, MetadataModule.class));
    skylarkExecutor = new SkylarkTestExecutor(options, MetadataModule.class);
    origin.addSimpleChange(0, "first commit\n\nExtended text").setAuthor(FOO_BAR).addSimpleChange(1, "second commit\n\nExtended text").setAuthor(FOO_BAZ).addSimpleChange(2, "third commit\n\nExtended text");
    options.setLastRevision("0");
    // We don't care about already migrated code
    options.setForce(true);
    testingConsole = new TestingConsole();
    options.setConsole(testingConsole);
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) SkylarkParser(com.google.copybara.config.SkylarkParser) Authoring(com.google.copybara.authoring.Authoring) 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) Before(org.junit.Before)

Example 23 with OptionsBuilder

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

the class GitMirrorTest method setup.

@Before
public void setup() throws Exception {
    workdir = Files.createTempDirectory("workdir");
    options = new OptionsBuilder().setEnvironment(GitTestUtil.getGitEnv()).setOutputRootToTmpDir().setWorkdirToRealTempDir().setConsole(new TestingConsole());
    originRepo = newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true).withWorkTree(Files.createTempDirectory("worktree"));
    originRepo.init();
    destRepo = bareRepo(Files.createTempDirectory("destinationFolder"));
    destRepo.init();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    Files.write(originRepo.getWorkTree().resolve("test.txt"), "some content".getBytes());
    originRepo.add().files("test.txt").run();
    originRepo.simpleCommand("commit", "-m", "first file");
    originRepo.simpleCommand("branch", "other");
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 24 with OptionsBuilder

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

the class GithubPrOriginTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GithubPrDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    localHub = Files.createTempDirectory("localHub");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    options.git = new TestGitOptions(localHub, () -> this.options.general, new Validator() {

        @Override
        public void validateFetch(String url, boolean prune, boolean force, Iterable<String> refspecs) {
            for (String refspec : refspecs) {
                // WARNING! This check is important. While using short names like
                // 'master' in git fetch works for local git invocations, other
                // implementations of GitRepository might have problems if we don't
                // pass the whole reference.
                assertThat(refspec).startsWith("refs/");
                assertThat(refspec).contains(":refs/");
            }
        }
    });
    options.github = new GithubOptions(() -> options.general, options.git) {

        @Override
        public GithubApi getApi(String project) throws RepoException {
            assertThat(project).isEqualTo(expectedProject);
            return super.getApi(project);
        }

        @Override
        protected HttpTransport getHttpTransport() {
            return gitApiMockHttpTransport;
        }
    };
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    options.git.credentialHelperStorePath = credentialsFile.toString();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    skylarkParser = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, FolderModule.class, GitModule.class));
}
Also used : Path(java.nio.file.Path) SkylarkParser(com.google.copybara.config.SkylarkParser) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) GithubApi(com.google.copybara.git.github.api.GithubApi) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) HttpTransport(com.google.api.client.http.HttpTransport) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) Authoring(com.google.copybara.authoring.Authoring) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) Before(org.junit.Before)

Example 25 with OptionsBuilder

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

the class ExamplesTest method testExamples.

@Test
public void testExamples() throws ValidationException {
    SkylarkTestExecutor executor = new SkylarkTestExecutor(new OptionsBuilder(), Iterables.toArray(getUserModules(), Class.class));
    boolean anyFound = false;
    for (Class<?> module : executor.getModules()) {
        for (Field field : module.getDeclaredFields()) {
            Examples examples = field.getAnnotation(Examples.class);
            ImmutableList<Example> samples;
            if (examples == null) {
                Example singleSample = field.getAnnotation(Example.class);
                if (singleSample != null) {
                    samples = ImmutableList.of(singleSample);
                } else {
                    continue;
                }
            } else {
                samples = ImmutableList.copyOf(examples.value());
            }
            for (Example example : samples) {
                anyFound = true;
                Object val = null;
                String exampleRef = module.getName() + "#" + field.getName() + ": " + example.title();
                try {
                    val = Strings.isNullOrEmpty(example.testExistingVariable()) ? executor.eval("a", "a=" + example.code()) : executor.eval(example.testExistingVariable(), example.code());
                } catch (ValidationException e) {
                    throw new ValidationException(e, "'%s' contains errors.", exampleRef);
                }
                assertWithMessage(exampleRef).that(val).isNotNull();
            }
        }
    }
    assertWithMessage("Could not find any example to run!").that(anyFound).isTrue();
}
Also used : Field(java.lang.reflect.Field) ValidationException(com.google.copybara.exception.ValidationException) Example(com.google.copybara.doc.annotations.Example) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) Examples(com.google.copybara.doc.annotations.Examples) Test(org.junit.Test)

Aggregations

OptionsBuilder (com.google.copybara.testing.OptionsBuilder)34 Before (org.junit.Before)33 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)28 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)26 FileSystem (java.nio.file.FileSystem)7 Core (com.google.copybara.Core)5 SkylarkParser (com.google.copybara.config.SkylarkParser)5 DummyOrigin (com.google.copybara.testing.DummyOrigin)5 Path (java.nio.file.Path)5 Authoring (com.google.copybara.authoring.Authoring)4 RepoException (com.google.copybara.exception.RepoException)4 RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)4 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)4 HttpTransport (com.google.api.client.http.HttpTransport)3 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)2 GeneralOptions (com.google.copybara.GeneralOptions)2 GithubApi (com.google.copybara.git.github.api.GithubApi)2 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)2 IOException (java.io.IOException)2