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
<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>
Creates a UTF-8 encoded text file.
@ -47,9 +47,20 @@ Creates a UTF-8 encoded text file.
<td>
optional. default is <code>False</code>
<p>
A boolean. Whether to make the output file executable. When
True, the rule's output can be executed using `bazel run` and can be
in the srcs of binary and test rules that require executable sources.
A boolean. Whether to make the output file executable.
When True, the rule's output can be executed using `bazel run` and can
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>
</td>
</tr>
@ -58,7 +69,7 @@ Creates a UTF-8 encoded text file.
<td>
optional.
<p>
further keyword arguments, e.g. `visibility`
further keyword arguments, e.g. <code>visibility</code>
</p>
</td>
</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.
"""
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(
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,
)
files = depset(direct = [ctx.outputs.out])
@ -33,14 +40,16 @@ def _common_impl(ctx, is_executable):
return [DefaultInfo(files = files, runfiles = runfiles)]
def _impl(ctx):
return _common_impl(ctx, False)
return _common_impl(ctx, ctx.attr.is_windows, False)
def _ximpl(ctx):
return _common_impl(ctx, True)
return _common_impl(ctx, ctx.attr.is_windows, True)
_ATTRS = {
"out": attr.output(mandatory = 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(
@ -56,7 +65,13 @@ _write_xfile = rule(
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.
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.
content: A list of strings. Lines of text, the contents of the file.
Newlines are added automatically after every line except the last one.
is_executable: A boolean. Whether to make the output file executable. When
True, the rule's output can be executed using `bazel run` and can be
in the srcs of binary and test rules that require executable sources.
**kwargs: further keyword arguments, e.g. `visibility`
is_executable: A boolean. Whether to make the output file executable.
When True, the rule's output can be executed using `bazel run` and can
be in the srcs of binary and test rules that require executable
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:
_write_xfile(
name = name,
content = content,
out = out,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)
else:
@ -81,5 +104,10 @@ def write_file(name, out, content = [], is_executable = False, **kwargs):
name = name,
content = content,
out = out,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -32,6 +32,7 @@
# field of it, so we assert that that field contains the output file of the
# rule
load("//rules:diff_test.bzl", "diff_test")
load("//rules:write_file.bzl", "write_file")
licenses(["notice"])
@ -117,3 +118,49 @@ write_file(
],
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",
)