""" Update snapshots per go_test that uses the snapshot library. Existing snapshots are passed as inputs, with its path passed as an attr. Any snapshots that are created, updated, or copied by the snapshot library are written to the out.path. """ _attrs = { "snapshot_test": attr.label( executable = True, cfg = "exec", doc = "go_test rule that uses snapshots", allow_files = True, ), "srcs": attr.label_list( allow_files = True, mandatory = True, doc = "source files for go_test, snapshot dir must be included (e.g. glob([testdata/**]))", ), "snapshot_path": attr.string( mandatory = True, doc = "location where snapshots are read from and written to", ), } def _snapshot_impl(ctx): out = ctx.actions.declare_directory(ctx.label.name) args = ctx.actions.args() args.add(out.path) # set read and write paths for the snapshot lib as they will differ env_var = {} env_var["SNAPSHOT_READ"] = ctx.attr.snapshot_path env_var["SNAPSHOT_WRITE"] = out.path env_var["UPDATE_SNAPSHOTS"] = "true" ctx.actions.run( arguments = [args], inputs = ctx.files.srcs, outputs = [out], env = env_var, executable = ctx.executable.snapshot_test, ) # TODO(wc185097_ncrvoyix): generate write_source_files inside this impl return [DefaultInfo(files = depset([out]))] generate_snapshots = rule( implementation = _snapshot_impl, attrs = _attrs, )