write_file: support different line endings (#150)

The user can specify which line endings they want
write_file to use. This helps avoiding line ending
mismatches with diff_test.

Example: diff_test verifies that a rule generates
correct output by comparing it to a checked-in
"golden" file. Both files are text files, and the
user builds on Windows but the golden file was
written on Linux and git checkout preserved
original line endings.

Without explicitly specifying which line endings
to use, this diff_test would fail on an otherwise
good output.

With explicit line endings we don't need to check
in the golden file to git, we can just generate it
with "auto" line endings.
This commit is contained in:
László Csomor 2019-05-09 15:29:44 +02:00 committed by GitHub
parent 67ecd63273
commit 6bf6443975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 14 deletions

View File

@ -1,7 +1,7 @@
## write_file ## write_file
<pre> <pre>
write_file(<a href="#write_file-name">name</a>, <a href="#write_file-out">out</a>, <a href="#write_file-content">content</a>, <a href="#write_file-is_executable">is_executable</a>, <a href="#write_file-kwargs">kwargs</a>) write_file(<a href="#write_file-name">name</a>, <a href="#write_file-out">out</a>, <a href="#write_file-content">content</a>, <a href="#write_file-is_executable">is_executable</a>, <a href="#write_file-newline">newline</a>, <a href="#write_file-kwargs">kwargs</a>)
</pre> </pre>
Creates a UTF-8 encoded text file. Creates a UTF-8 encoded text file.
@ -47,9 +47,20 @@ Creates a UTF-8 encoded text file.
<td> <td>
optional. default is <code>False</code> optional. default is <code>False</code>
<p> <p>
A boolean. Whether to make the output file executable. When A boolean. Whether to make the output file executable.
True, the rule's output can be executed using `bazel run` and can be When True, the rule's output can be executed using `bazel run` and can
in the srcs of binary and test rules that require executable sources. be in the srcs of binary and test rules that require executable
sources.
</p>
</td>
</tr>
<tr id="write_file-newline">
<td><code>newline</code></td>
<td>
optional. default is <code>"auto"</code>
<p>
one of ["auto", "unix", "windows"]: line endings to use. "auto"
for platform-determined, "unix" for LF, and "windows" for CRLF.
</p> </p>
</td> </td>
</tr> </tr>
@ -58,7 +69,7 @@ Creates a UTF-8 encoded text file.
<td> <td>
optional. optional.
<p> <p>
further keyword arguments, e.g. `visibility` further keyword arguments, e.g. <code>visibility</code>
</p> </p>
</td> </td>
</tr> </tr>

View File

@ -18,11 +18,18 @@ These rules write a UTF-8 encoded text file, using Bazel's FileWriteAction.
'_write_xfile' marks the resulting file executable, '_write_file' does not. '_write_xfile' marks the resulting file executable, '_write_file' does not.
""" """
def _common_impl(ctx, is_executable): def _common_impl(ctx, is_windows, is_executable):
if ctx.attr.newline == "auto":
newline = "\r\n" if is_windows else "\n"
elif ctx.attr.newline == "windows":
newline = "\r\n"
else:
newline = "\n"
# ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding. # ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding.
ctx.actions.write( ctx.actions.write(
output = ctx.outputs.out, output = ctx.outputs.out,
content = "\n".join(ctx.attr.content) if ctx.attr.content else "", content = newline.join(ctx.attr.content) if ctx.attr.content else "",
is_executable = is_executable, is_executable = is_executable,
) )
files = depset(direct = [ctx.outputs.out]) files = depset(direct = [ctx.outputs.out])
@ -33,14 +40,16 @@ def _common_impl(ctx, is_executable):
return [DefaultInfo(files = files, runfiles = runfiles)] return [DefaultInfo(files = files, runfiles = runfiles)]
def _impl(ctx): def _impl(ctx):
return _common_impl(ctx, False) return _common_impl(ctx, ctx.attr.is_windows, False)
def _ximpl(ctx): def _ximpl(ctx):
return _common_impl(ctx, True) return _common_impl(ctx, ctx.attr.is_windows, True)
_ATTRS = { _ATTRS = {
"out": attr.output(mandatory = True), "out": attr.output(mandatory = True),
"content": attr.string_list(mandatory = False, allow_empty = True), "content": attr.string_list(mandatory = False, allow_empty = True),
"newline": attr.string(values = ["unix", "windows", "auto"], default = "auto"),
"is_windows": attr.bool(mandatory = True),
} }
_write_file = rule( _write_file = rule(
@ -56,7 +65,13 @@ _write_xfile = rule(
attrs = _ATTRS, attrs = _ATTRS,
) )
def write_file(name, out, content = [], is_executable = False, **kwargs): def write_file(
name,
out,
content = [],
is_executable = False,
newline = "auto",
**kwargs):
"""Creates a UTF-8 encoded text file. """Creates a UTF-8 encoded text file.
Args: Args:
@ -64,16 +79,24 @@ def write_file(name, out, content = [], is_executable = False, **kwargs):
out: Path of the output file, relative to this package. out: Path of the output file, relative to this package.
content: A list of strings. Lines of text, the contents of the file. content: A list of strings. Lines of text, the contents of the file.
Newlines are added automatically after every line except the last one. Newlines are added automatically after every line except the last one.
is_executable: A boolean. Whether to make the output file executable. When is_executable: A boolean. Whether to make the output file executable.
True, the rule's output can be executed using `bazel run` and can be When True, the rule's output can be executed using `bazel run` and can
in the srcs of binary and test rules that require executable sources. be in the srcs of binary and test rules that require executable
**kwargs: further keyword arguments, e.g. `visibility` sources.
newline: one of ["auto", "unix", "windows"]: line endings to use. "auto"
for platform-determined, "unix" for LF, and "windows" for CRLF.
**kwargs: further keyword arguments, e.g. <code>visibility</code>
""" """
if is_executable: if is_executable:
_write_xfile( _write_xfile(
name = name, name = name,
content = content, content = content,
out = out, out = out,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs **kwargs
) )
else: else:
@ -81,5 +104,10 @@ def write_file(name, out, content = [], is_executable = False, **kwargs):
name = name, name = name,
content = content, content = content,
out = out, out = out,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs **kwargs
) )

View File

@ -32,6 +32,7 @@
# field of it, so we assert that that field contains the output file of the # field of it, so we assert that that field contains the output file of the
# rule # rule
load("//rules:diff_test.bzl", "diff_test")
load("//rules:write_file.bzl", "write_file") load("//rules:write_file.bzl", "write_file")
licenses(["notice"]) licenses(["notice"])
@ -117,3 +118,49 @@ write_file(
], ],
is_executable = True, is_executable = True,
) )
write_file(
name = "newline_unix_actual",
out = "out/newline_unix_actual.txt",
content = [
"ab",
"cd",
"ef",
],
newline = "unix",
)
write_file(
name = "newline_unix_exp",
out = "out/newline_unix_exp.txt",
content = ["ab\ncd\nef"],
)
diff_test(
name = "unix_line_ending_test",
file1 = ":newline_unix_actual",
file2 = ":newline_unix_exp",
)
write_file(
name = "newline_win_actual",
out = "out/newline_win_actual.txt",
content = [
"ab",
"cd",
"ef",
],
newline = "windows",
)
write_file(
name = "newline_win_exp",
out = "out/newline_win_exp.txt",
content = ["ab\r\ncd\r\nef"],
)
diff_test(
name = "win_line_ending_test",
file1 = ":newline_win_actual",
file2 = ":newline_win_exp",
)