2022-01-28 02:15:28 +00:00
|
|
|
"""Tests for write_source_files"""
|
|
|
|
# Inspired by https://github.com/cgrindel/bazel-starlib/blob/main/updatesrc/private/updatesrc_update_test.bzl
|
|
|
|
|
2022-03-28 21:53:58 +00:00
|
|
|
load("//lib/private:write_source_file.bzl", _write_source_file = "write_source_file")
|
2022-03-15 00:33:52 +00:00
|
|
|
load("//lib/private:directory_path.bzl", "DirectoryPathInfo")
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-09-20 22:23:56 +00:00
|
|
|
def _write_source_file_test_impl_sh(ctx, in_file_path, out_file_path):
|
2022-01-28 02:15:28 +00:00
|
|
|
test = ctx.actions.declare_file(
|
|
|
|
ctx.label.name + "_test.sh",
|
|
|
|
)
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
contents = []
|
|
|
|
|
|
|
|
contents.append("""
|
2022-01-28 02:15:28 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -o errexit -o nounset -o pipefail
|
|
|
|
|
|
|
|
assert_different() {
|
|
|
|
local in_file="${1}"
|
|
|
|
local out_file="${2}"
|
|
|
|
diff "${in_file}" "${out_file}" > /dev/null && (echo >&2 "Expected files to differ. in: ${in_file}, out: ${out_file}" && return -1)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_same() {
|
|
|
|
local in_file="${1}"
|
|
|
|
local out_file="${2}"
|
|
|
|
diff "${in_file}" "${out_file}" || (echo >&2 "Expected files to be same. in: ${in_file}, out: ${out_file}" && return -1)
|
2022-03-15 00:33:52 +00:00
|
|
|
}""")
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
contents.append("""
|
2022-01-28 02:15:28 +00:00
|
|
|
# Check that in and out files are different
|
2022-03-15 00:33:52 +00:00
|
|
|
assert_different {in_file} {out_file}
|
|
|
|
""".format(
|
|
|
|
in_file = in_file_path,
|
|
|
|
out_file = out_file_path,
|
|
|
|
))
|
|
|
|
|
|
|
|
contents.append("""# Write to the source files
|
2022-01-28 02:15:28 +00:00
|
|
|
{write_source_files}
|
2022-03-15 00:33:52 +00:00
|
|
|
""".format(write_source_files = ctx.file.write_source_file_target.short_path))
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
contents.append("""# Check that in and out files are the same
|
|
|
|
assert_same {in_file} {out_file}""".format(
|
|
|
|
in_file = in_file_path,
|
|
|
|
out_file = out_file_path,
|
|
|
|
))
|
|
|
|
|
|
|
|
ctx.actions.write(
|
|
|
|
output = test,
|
|
|
|
is_executable = True,
|
|
|
|
content = "\n".join(contents),
|
2022-01-28 02:15:28 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 23:08:24 +00:00
|
|
|
return test
|
|
|
|
|
2022-09-20 22:23:56 +00:00
|
|
|
def _write_source_file_test_impl_bat(ctx, in_file_path, out_file_path):
|
2022-02-17 23:08:24 +00:00
|
|
|
test = ctx.actions.declare_file(
|
|
|
|
ctx.label.name + "_test.bat",
|
|
|
|
)
|
|
|
|
|
|
|
|
# BUG: Windows is having trouble executing the symlink to the write_source_files executable,
|
|
|
|
# but it is able to execute the actual output script so point to that for now.
|
|
|
|
#
|
|
|
|
# What we would use if this bug didn't exist:
|
2022-03-15 00:33:52 +00:00
|
|
|
# write_source_files = ctx.executable.write_source_file_target.short_path.replace("/", "\\")
|
2022-02-17 23:08:24 +00:00
|
|
|
#
|
|
|
|
# Instead back out of the runfiles execution directory:
|
|
|
|
# write_to_source_files_test_test.bat.runfiles/aspect_bazel_lib
|
|
|
|
# And point to the output script.
|
2022-03-15 00:33:52 +00:00
|
|
|
write_source_files = "..\\..\\%s" % ctx.executable.write_source_file_target.basename
|
|
|
|
|
|
|
|
contents = []
|
2022-02-17 23:08:24 +00:00
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
contents.append("""
|
2022-04-29 07:49:15 +00:00
|
|
|
@rem @generated by @aspect_bazel_lib//:lib/tests/write_source_files:write_source_file_test.bzl
|
2022-02-17 23:08:24 +00:00
|
|
|
@echo off
|
|
|
|
@rem Check that in and out files are different
|
|
|
|
call :assert_different {in_file}, {out_file}
|
|
|
|
if %errorlevel% neq 0 exit /b 1
|
|
|
|
""".format(
|
2022-03-15 00:33:52 +00:00
|
|
|
in_file = in_file_path.replace("/", "\\"),
|
|
|
|
out_file = out_file_path.replace("/", "\\"),
|
|
|
|
))
|
|
|
|
|
|
|
|
contents.append("""
|
2022-02-17 23:08:24 +00:00
|
|
|
@rem Write to the source files
|
|
|
|
call {write_source_files}
|
|
|
|
if %errorlevel% neq 0 exit /b 1
|
2022-03-15 00:33:52 +00:00
|
|
|
""".format(write_source_files = write_source_files))
|
2022-02-17 23:08:24 +00:00
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
contents.append("""
|
2022-02-17 23:08:24 +00:00
|
|
|
@rem Check that in and out files are the same
|
|
|
|
call :assert_same {in_file}, {out_file}
|
|
|
|
if %errorlevel% neq 0 exit /b 1
|
|
|
|
""".format(
|
2022-03-15 00:33:52 +00:00
|
|
|
in_file = in_file_path.replace("/", "\\"),
|
|
|
|
out_file = out_file_path.replace("/", "\\"),
|
|
|
|
))
|
|
|
|
|
|
|
|
contents.append("""
|
2022-02-17 23:08:24 +00:00
|
|
|
exit /b 0
|
|
|
|
|
|
|
|
:assert_different
|
|
|
|
fc /b %~1 %~2 > nul
|
|
|
|
if %errorlevel% equ 1 exit /b 0
|
|
|
|
echo Error: %~1 and %~2 are not different
|
|
|
|
exit /b 1
|
|
|
|
|
|
|
|
:assert_same
|
|
|
|
fc /b %~1 %~2 > nul
|
|
|
|
if %errorlevel% equ 0 exit /b 0
|
|
|
|
echo Error: %~1 and %~2 are not the same
|
|
|
|
exit /b 1
|
2022-03-15 00:33:52 +00:00
|
|
|
""")
|
2022-02-17 23:08:24 +00:00
|
|
|
|
|
|
|
ctx.actions.write(
|
|
|
|
output = test,
|
|
|
|
is_executable = True,
|
2022-03-15 00:33:52 +00:00
|
|
|
content = "\n".join(contents).replace("\n", "\r\n"),
|
2022-02-17 23:08:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return test
|
|
|
|
|
2022-09-20 22:23:56 +00:00
|
|
|
def _write_source_file_test_impl(ctx):
|
2022-04-29 07:36:51 +00:00
|
|
|
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
if DirectoryPathInfo in ctx.attr.in_file:
|
|
|
|
in_file = ctx.attr.in_file[DirectoryPathInfo].directory
|
|
|
|
in_file_path = "/".join([in_file.short_path, ctx.attr.in_file[DirectoryPathInfo].path])
|
|
|
|
else:
|
|
|
|
if len(ctx.files.in_file) != 1:
|
|
|
|
fail("in_file must be a single file or a target that provides a DirectoryPathInfo")
|
|
|
|
in_file = ctx.files.in_file[0]
|
|
|
|
in_file_path = in_file.short_path
|
|
|
|
|
2022-04-29 07:36:51 +00:00
|
|
|
if is_windows:
|
2022-09-20 22:23:56 +00:00
|
|
|
test = _write_source_file_test_impl_bat(ctx, in_file_path, ctx.file.out_file.short_path)
|
2022-02-17 23:08:24 +00:00
|
|
|
else:
|
2022-09-20 22:23:56 +00:00
|
|
|
test = _write_source_file_test_impl_sh(ctx, in_file_path, ctx.file.out_file.short_path)
|
2022-02-17 23:08:24 +00:00
|
|
|
|
2022-01-28 02:15:28 +00:00
|
|
|
return DefaultInfo(
|
|
|
|
executable = test,
|
2022-02-17 23:08:24 +00:00
|
|
|
runfiles = ctx.runfiles(
|
2022-03-15 00:33:52 +00:00
|
|
|
files = [ctx.executable.write_source_file_target, in_file, ctx.file.out_file],
|
2022-02-17 23:08:24 +00:00
|
|
|
),
|
2022-01-28 02:15:28 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
_write_source_file_test = rule(
|
2022-09-20 22:23:56 +00:00
|
|
|
implementation = _write_source_file_test_impl,
|
2022-01-28 02:15:28 +00:00
|
|
|
attrs = {
|
2022-03-15 00:33:52 +00:00
|
|
|
"write_source_file_target": attr.label(
|
2022-01-28 02:15:28 +00:00
|
|
|
allow_single_file = True,
|
|
|
|
executable = True,
|
2022-02-17 23:08:24 +00:00
|
|
|
# Should be cfg = "exec" but a bazel bug causes a wrong executable symlink on windows
|
|
|
|
cfg = "target",
|
|
|
|
mandatory = True,
|
2022-01-28 02:15:28 +00:00
|
|
|
),
|
2022-03-15 00:33:52 +00:00
|
|
|
"out_file": attr.label(
|
|
|
|
allow_single_file = True,
|
2022-01-28 02:15:28 +00:00
|
|
|
mandatory = True,
|
|
|
|
),
|
2022-03-15 00:33:52 +00:00
|
|
|
"in_file": attr.label(
|
2022-01-28 02:15:28 +00:00
|
|
|
allow_files = True,
|
|
|
|
mandatory = True,
|
|
|
|
),
|
2022-04-29 07:36:51 +00:00
|
|
|
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
|
2022-01-28 02:15:28 +00:00
|
|
|
},
|
|
|
|
test = True,
|
|
|
|
)
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
def write_source_file_test(name, in_file, out_file):
|
2022-01-28 02:15:28 +00:00
|
|
|
"""Stamp a write_source_files executable and a test to run against it"""
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
_write_source_file(
|
2022-01-28 02:15:28 +00:00
|
|
|
name = name + "_updater",
|
2022-03-15 00:33:52 +00:00
|
|
|
in_file = in_file,
|
|
|
|
out_file = out_file,
|
2022-03-28 21:53:58 +00:00
|
|
|
diff_test = False,
|
2022-01-28 02:15:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Note that for testing we update the source files in the sandbox,
|
|
|
|
# not the actual source tree.
|
2022-03-15 00:33:52 +00:00
|
|
|
_write_source_file_test(
|
2022-01-28 02:15:28 +00:00
|
|
|
name = name,
|
2022-03-15 00:33:52 +00:00
|
|
|
write_source_file_target = name + "_updater",
|
|
|
|
in_file = in_file,
|
|
|
|
out_file = out_file,
|
2022-08-20 19:53:12 +00:00
|
|
|
timeout = "short",
|
2022-01-28 02:15:28 +00:00
|
|
|
)
|