|
1 | | -def _diff_integration_goldens_impl(ctx): |
2 | | - # Extract the Java source files from the generated 3 srcjars from API bazel target, |
3 | | - # and put them in the temporary folder `codegen_tmp`. |
4 | | - # Compare the `codegen_tmp` with the goldens folder e.g `test/integration/goldens/redis` |
5 | | - # and save the differences in output file `diff_output.txt`. |
6 | | - |
7 | | - diff_output = ctx.outputs.diff_output |
8 | | - check_diff_script = ctx.outputs.check_diff_script |
9 | | - gapic_library = ctx.attr.gapic_library |
10 | | - resource_name_library = ctx.attr.resource_name_library |
11 | | - test_library = ctx.attr.test_library |
12 | | - srcs = ctx.files.srcs |
13 | | - api_name = ctx.attr.name |
14 | | - |
15 | | - script = """ |
16 | | - mkdir codegen_tmp |
17 | | - unzip {input} -d codegen_tmp |
18 | | - unzip {input_resource_name} -d codegen_tmp |
19 | | - unzip {input_test} -d codegen_tmp |
20 | | - cd codegen_tmp |
21 | | - # Remove unneeded non-Java files, like MANIFEST |
22 | | - rm -rf $(find ./ -type f ! -name '*.java' -a ! -name '*gapic_metadata.json') |
23 | | - rm -rf $(find ./ -type f -name 'PlaceholderFile.java') |
24 | | - rm -r $(find ./ -type d -empty) |
25 | | - cd .. |
26 | | - diff -r codegen_tmp test/integration/goldens/{api_name} > {diff_output} |
27 | | - # Bash `diff` command will return exit code 1 when there are differences between the two |
28 | | - # folders. So we explicitly `exit 0` after the diff command to avoid build failure. |
29 | | - exit 0 |
30 | | - """.format( |
31 | | - diff_output = diff_output.path, |
32 | | - input = gapic_library[JavaInfo].source_jars[0].path, |
33 | | - input_resource_name = resource_name_library[JavaInfo].source_jars[0].path, |
34 | | - input_test = test_library[JavaInfo].source_jars[0].path, |
35 | | - api_name = api_name, |
36 | | - ) |
37 | | - ctx.actions.run_shell( |
38 | | - inputs = srcs + [ |
39 | | - gapic_library[JavaInfo].source_jars[0], |
40 | | - resource_name_library[JavaInfo].source_jars[0], |
41 | | - test_library[JavaInfo].source_jars[0], |
42 | | - ], |
43 | | - outputs = [diff_output], |
44 | | - command = script, |
45 | | - ) |
46 | | - |
47 | | - # Check the generated diff_output file, if it is empty, that means there is no difference |
48 | | - # between generated source code and goldens files, test should pass. If it is not empty, then |
49 | | - # test will fail by exiting 1. |
50 | | - |
51 | | - check_diff_script_content = """ |
52 | | - # This will not print diff_output to the console unless `--test_output=all` option |
53 | | - # is enabled, it only emits the comparison results to the test.log. |
54 | | - # We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that |
55 | | - # file is not existing at the moment. It is generated once test is finished. |
56 | | - cat $PWD/test/integration/{api_name}_diff_output.txt |
57 | | - if [ -s $PWD/test/integration/{api_name}_diff_output.txt ] |
58 | | - then |
59 | | - exit 1 |
60 | | - fi |
61 | | - """.format( |
62 | | - api_name = api_name, |
63 | | - ) |
64 | | - |
65 | | - ctx.actions.write( |
66 | | - output = check_diff_script, |
67 | | - content = check_diff_script_content, |
68 | | - ) |
69 | | - runfiles = ctx.runfiles(files = [ctx.outputs.diff_output]) |
70 | | - return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)] |
71 | | - |
72 | | -diff_integration_goldens_test = rule( |
73 | | - attrs = { |
74 | | - "gapic_library": attr.label(), |
75 | | - "resource_name_library": attr.label(), |
76 | | - "test_library": attr.label(), |
77 | | - "srcs": attr.label_list( |
78 | | - allow_files = True, |
79 | | - mandatory = True, |
80 | | - ), |
81 | | - }, |
82 | | - outputs = { |
83 | | - "diff_output": "%{name}_diff_output.txt", |
84 | | - "check_diff_script": "%{name}_check_diff_script.sh", |
85 | | - }, |
86 | | - implementation = _diff_integration_goldens_impl, |
87 | | - test = True, |
88 | | -) |
89 | | - |
90 | | -def integration_test(name, target, data): |
91 | | - # Bazel target `java_gapic_library` will generate 3 source jars including the |
92 | | - # the source Java code of the gapic_library, resource_name_library and test_library. |
93 | | - diff_integration_goldens_test( |
94 | | - name = name, |
95 | | - gapic_library = target, |
96 | | - resource_name_library = "%s_resource_name" % target, |
97 | | - test_library = "%s_test" % target, |
98 | | - srcs = data, |
99 | | - ) |
100 | | - |
101 | 1 | def _overwrite_golden_impl(ctx): |
102 | 2 | # Extract the Java source files from the generated 3 srcjars from API bazel target, |
103 | 3 | # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`. |
|
0 commit comments