|
| 1 | +def _diff_integration_goldens_impl(ctx): |
| 2 | + # Extract the Python 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 `tests/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 | + srcs = ctx.files.srcs |
| 11 | + api_name = ctx.attr.name |
| 12 | + |
| 13 | + script = """ |
| 14 | + mkdir codegen_tmp |
| 15 | + unzip {input_srcs} -d codegen_tmp |
| 16 | + diff -r codegen_tmp $PWD/tests/integration/goldens/{api_name} > {diff_output} |
| 17 | + exit 0 # Avoid a build failure. |
| 18 | + """.format( |
| 19 | + diff_output = diff_output.path, |
| 20 | + input_srcs = gapic_library[DefaultInfo].files.to_list()[0].path, |
| 21 | + api_name = api_name, |
| 22 | + ) |
| 23 | + ctx.actions.run_shell( |
| 24 | + inputs = srcs + [ |
| 25 | + gapic_library[DefaultInfo].files.to_list()[0], |
| 26 | + ], |
| 27 | + outputs = [diff_output], |
| 28 | + command = script, |
| 29 | + ) |
| 30 | + |
| 31 | + # Check the generated diff_output file, if it is empty, that means there is no difference |
| 32 | + # between generated source code and goldens files, test should pass. If it is not empty, then |
| 33 | + # test will fail by exiting 1. |
| 34 | + |
| 35 | + check_diff_script_content = """ |
| 36 | + # This will not print diff_output to the console unless `--test_output=all` option |
| 37 | + # is enabled, it only emits the comparison results to the test.log. |
| 38 | + # We could not copy the diff_output.txt to the test.log ($XML_OUTPUT_FILE) because that |
| 39 | + # file is not existing at the moment. It is generated once test is finished. |
| 40 | + cat $PWD/tests/integration/{api_name}_diff_output.txt |
| 41 | + if [ -s $PWD/tests/integration/{api_name}_diff_output.txt ] |
| 42 | + then |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + """.format( |
| 46 | + api_name = api_name, |
| 47 | + ) |
| 48 | + |
| 49 | + ctx.actions.write( |
| 50 | + output = check_diff_script, |
| 51 | + content = check_diff_script_content, |
| 52 | + ) |
| 53 | + runfiles = ctx.runfiles(files = [ctx.outputs.diff_output]) |
| 54 | + return [DefaultInfo(executable = check_diff_script, runfiles = runfiles)] |
| 55 | + |
| 56 | +diff_integration_goldens_test = rule( |
| 57 | + attrs = { |
| 58 | + "gapic_library": attr.label(), |
| 59 | + "srcs": attr.label_list( |
| 60 | + allow_files = True, |
| 61 | + mandatory = True, |
| 62 | + ), |
| 63 | + }, |
| 64 | + outputs = { |
| 65 | + "diff_output": "%{name}_diff_output.txt", |
| 66 | + "check_diff_script": "%{name}_check_diff_script.sh", |
| 67 | + }, |
| 68 | + implementation = _diff_integration_goldens_impl, |
| 69 | + test = True, |
| 70 | +) |
| 71 | + |
| 72 | +def integration_test(name, target, data): |
| 73 | + # Bazel target `py_gapic_library` will generate 1 source jar that holds the |
| 74 | + # Gapic_library's python sources. |
| 75 | + diff_integration_goldens_test( |
| 76 | + name = name, |
| 77 | + gapic_library = target, |
| 78 | + srcs = data, |
| 79 | + ) |
| 80 | + |
| 81 | +def _overwrite_golden_impl(ctx): |
| 82 | + # Extract the Java source files from the generated 3 srcjars from API bazel target, |
| 83 | + # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`. |
| 84 | + # Overwrite the goldens folder e.g `tests/integration/goldens/redis` with the |
| 85 | + # code generation in `goldens_output_zip`. |
| 86 | + |
| 87 | + gapic_library = ctx.attr.gapic_library |
| 88 | + srcs = ctx.files.srcs |
| 89 | + |
| 90 | + # Convert the name of bazel rules e.g. `redis_update` to `redis` |
| 91 | + # because we will need to overwrite the goldens files in `redis` folder. |
| 92 | + api_name = "_".join(ctx.attr.name.split("_")[:-1]) |
| 93 | + goldens_output_zip = ctx.outputs.goldens_output_zip |
| 94 | + |
| 95 | + script = """ |
| 96 | + mkdir codegen_tmp |
| 97 | + unzip {input_srcs} -d codegen_tmp |
| 98 | + cd codegen_tmp |
| 99 | + zip -r ../{goldens_output_zip} . |
| 100 | + """.format( |
| 101 | + goldens_output_zip = goldens_output_zip.path, |
| 102 | + input_srcs = gapic_library[DefaultInfo].files.to_list()[0].path, |
| 103 | + ) |
| 104 | + |
| 105 | + ctx.actions.run_shell( |
| 106 | + inputs = srcs + [ |
| 107 | + gapic_library[DefaultInfo].files.to_list()[0], |
| 108 | + ], |
| 109 | + outputs = [goldens_output_zip], |
| 110 | + command = script, |
| 111 | + ) |
| 112 | + |
| 113 | + # Overwrite the goldens. |
| 114 | + golden_update_script_content = """ |
| 115 | + cd ${{BUILD_WORKSPACE_DIRECTORY}} |
| 116 | + # Filename pattern-based removal is needed to preserve the BUILD.bazel file. |
| 117 | + find tests/Integration/goldens/{api_name}/ -name \\*.py-type f -delete |
| 118 | + find tests/Integration/goldens/{api_name}/ -name \\*.json -type f -delete |
| 119 | + unzip -ao {goldens_output_zip} -d tests/integration/goldens/{api_name} |
| 120 | + """.format( |
| 121 | + goldens_output_zip = goldens_output_zip.path, |
| 122 | + api_name = api_name, |
| 123 | + ) |
| 124 | + ctx.actions.write( |
| 125 | + output = ctx.outputs.golden_update_script, |
| 126 | + content = golden_update_script_content, |
| 127 | + is_executable = True, |
| 128 | + ) |
| 129 | + return [DefaultInfo(executable = ctx.outputs.golden_update_script)] |
| 130 | + |
| 131 | +overwrite_golden = rule( |
| 132 | + attrs = { |
| 133 | + "gapic_library": attr.label(), |
| 134 | + "srcs": attr.label_list( |
| 135 | + allow_files = True, |
| 136 | + mandatory = True, |
| 137 | + ), |
| 138 | + }, |
| 139 | + outputs = { |
| 140 | + "goldens_output_zip": "%{name}.zip", |
| 141 | + "golden_update_script": "%{name}.sh", |
| 142 | + }, |
| 143 | + executable = True, |
| 144 | + implementation = _overwrite_golden_impl, |
| 145 | +) |
| 146 | + |
| 147 | +def golden_update(name, target, data): |
| 148 | + overwrite_golden( |
| 149 | + name = name, |
| 150 | + gapic_library = target, |
| 151 | + srcs = data, |
| 152 | + ) |
0 commit comments