Search in sources :

Example 16 with Transformation

use of com.google.copybara.Transformation in project copybara by google.

the class FilterReplaceTest method testSimple.

@Test
public void testSimple() throws Exception {
    String original = "" + "afoo\n" + "aaaaaa\n" + "bbbbbb\n" + "abaz\n" + "";
    write("file1.txt", original);
    write("file2.txt", "other\n");
    Transformation transformation = eval(Core.SIMPLE_FILTER_REPLACE_EXAMPLE);
    transform(transformation);
    assertThatPath(checkoutDir).containsFile("file1.txt", "" + "abar\n" + "aaaaaa\n" + "bbbbbb\n" + "abam\n").containsFile("file2.txt", "other\n").containsNoMoreFiles();
    transform(transformation.reverse());
    assertThatPath(checkoutDir).containsFile("file1.txt", original).containsFile("file2.txt", "other\n").containsNoMoreFiles();
}
Also used : Transformation(com.google.copybara.Transformation) Test(org.junit.Test)

Example 17 with Transformation

use of com.google.copybara.Transformation in project copybara by google.

the class FilterReplaceTest method testNestedFilterReplace.

@Test
public void testNestedFilterReplace() throws Exception {
    String original = "" + "before\n" + "// BEGIN SCRUBBER\n" + "// some comment\n" + "// some other comment\n" + "//   with indentation\n" + "// END SCRUBBER\n" + "// other comment\n" + "some code" + "// BEGIN SCRUBBER\n" + "// some comment\n" + "// some other comment\n" + "//   with indentation\n" + "// END SCRUBBER\n" + "after\n";
    write("file.txt", original);
    Transformation t = filterReplace("" + "regex = '// BEGIN SCRUBBER\\n((\\n|.)*?\\n)// END SCRUBBER\\n',\n" + "mapping = core.filter_replace(\n" + "   regex = '// .*\\n',\n" + "   mapping = core.replace_mapper([\n" + "     core.replace(before = '// BEGIN SCRUBBER\\n', after = '', multiline = True),\n" + "     core.replace(before = '// END SCRUBBER\\n', after = '', multiline = True),\n" + "     core.replace(\n" + "       before = '// ${content}\\n',\n" + "       after = '${content}\\n',\n" + "       regex_groups = {'content' : '.*'},\n" + "       multiline = True," + "      )\n" + "   ]),\n" + ")");
    transform(t);
    assertThatPath(checkoutDir).containsFile("file.txt", "" + "before\n" + "some comment\n" + "some other comment\n" + "  with indentation\n" + "// other comment\n" + "some code" + "some comment\n" + "some other comment\n" + "  with indentation\n" + "after\n").containsNoMoreFiles();
}
Also used : Transformation(com.google.copybara.Transformation) Test(org.junit.Test)

Example 18 with Transformation

use of com.google.copybara.Transformation in project copybara by google.

the class FilterReplaceTest method testWithVariablesInContent.

@Test
public void testWithVariablesInContent() throws Exception {
    String original = "" + "afoo\n" + "a${foo}\\${bar}\n" + "$\n";
    write("file1.txt", original);
    Transformation transformation = eval("" + "core.filter_replace(\n" + "    regex = 'a.*',\n" + "    mapping = {\n" + "        'afoo': 'abar',\n" + "    }\n" + ")\n");
    transform(transformation);
    assertThatPath(checkoutDir).containsFile("file1.txt", "" + "abar\n" + "a${foo}\\${bar}\n" + "$\n").containsNoMoreFiles();
}
Also used : Transformation(com.google.copybara.Transformation) Test(org.junit.Test)

Example 19 with Transformation

use of com.google.copybara.Transformation in project copybara by google.

the class FilterReplaceTest method testCustomReverse.

@Test
public void testCustomReverse() throws Exception {
    String original = "" + "#import <foo1>\n" + "#import <foo2>\n" + "#import <bar1>\n" + "";
    write("file.txt", original);
    Transformation t = filterReplace("" + "regex = '#import <fo.*>'," + "reverse = '#import <.*> // by Copybara'," + "mapping = {" + "    '#import <foo1>': '#import <bar1> // by Copybara',\n" + "    '#import <foo2>': '#import <bar2> // by Copybara',\n" + "},");
    transform(t);
    assertThatPath(checkoutDir).containsFile("file.txt", "" + "#import <bar1> // by Copybara\n" + "#import <bar2> // by Copybara\n" + "#import <bar1>\n").containsNoMoreFiles();
    transform(t.reverse());
    assertThatPath(checkoutDir).containsFile("file.txt", original).containsNoMoreFiles();
}
Also used : Transformation(com.google.copybara.Transformation) Test(org.junit.Test)

Example 20 with Transformation

use of com.google.copybara.Transformation in project copybara by google.

the class FilterReplaceTest method testCoreReplace.

@Test
public void testCoreReplace() throws Exception {
    String original = "" + "import foo.bar.last.Class;\n" + "import other.bar.last.Class;\n" + "import bar.foo.last.Class;\n";
    write("file.txt", original);
    Transformation t = filterReplace("" + "regex = '(^|\\n)import (.*);\\n'," + "group = 2," + "mapping = core.replace_mapper([" + "  core.replace(" + "      before = '${start}foo${pkg}.${last_pkg}.${n}'," + "      after = '${start}prefix.foo${pkg}.${last_pkg}.${last_pkg}.${n}'," + "      regex_groups = {'start': '^', 'pkg' : '.*', 'last_pkg' : '.*','n' : '[A-Z].*', }," + "      repeated_groups = True," + "  )," + "  core.replace(" + "      before = '${start}bar${pkg}.${last_pkg}.${n}'," + "      after = '${start}prefix.bar${pkg}.${last_pkg}.${last_pkg}.${n}'," + "      regex_groups = {'start': '^', 'pkg' : '.*', 'last_pkg' : '.*','n' : '[A-Z].*', }," + "      repeated_groups = True," + "  )," + "]),");
    transform(t);
    assertThatPath(checkoutDir).containsFile("file.txt", "" + "import prefix.foo.bar.last.last.Class;\n" + "import other.bar.last.Class;\n" + "import prefix.bar.foo.last.last.Class;\n").containsNoMoreFiles();
    transform(t.reverse());
    assertThatPath(checkoutDir).containsFile("file.txt", original).containsNoMoreFiles();
}
Also used : Transformation(com.google.copybara.Transformation) Test(org.junit.Test)

Aggregations

Transformation (com.google.copybara.Transformation)42 Test (org.junit.Test)38 TransformWork (com.google.copybara.TransformWork)23 TransformationStatus (com.google.copybara.TransformationStatus)12 Author (com.google.copybara.authoring.Author)3 DummyRevision (com.google.copybara.testing.DummyRevision)3 Changes (com.google.copybara.Changes)2 Sequence (com.google.copybara.transform.Sequence)2 Transformations.toTransformation (com.google.copybara.transform.Transformations.toTransformation)2 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)1 ValidationException (com.google.copybara.exception.ValidationException)1 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1