From 6bf64439752b2a98418a731ff21eb9d58ed5631c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Csomor?= Date: Thu, 9 May 2019 15:29:44 +0200 Subject: [PATCH] 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. --- docs/write_file_doc.md | 21 ++++++++++--- rules/private/write_file_private.bzl | 46 +++++++++++++++++++++------ tests/write_file/BUILD | 47 ++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 14 deletions(-) diff --git a/docs/write_file_doc.md b/docs/write_file_doc.md index ecd96bd..d8f098e 100755 --- a/docs/write_file_doc.md +++ b/docs/write_file_doc.md @@ -1,7 +1,7 @@ ## write_file
-write_file(name, out, content, is_executable, kwargs)
+write_file(name, out, content, is_executable, newline, kwargs)
 
Creates a UTF-8 encoded text file. @@ -47,9 +47,20 @@ Creates a UTF-8 encoded text file. optional. default is False

- 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. +

+ + + + newline + + optional. default is "auto" +

+ one of ["auto", "unix", "windows"]: line endings to use. "auto" + for platform-determined, "unix" for LF, and "windows" for CRLF.

@@ -58,7 +69,7 @@ Creates a UTF-8 encoded text file. optional.

- further keyword arguments, e.g. `visibility` + further keyword arguments, e.g. visibility

diff --git a/rules/private/write_file_private.bzl b/rules/private/write_file_private.bzl index 64a3361..ac8ce9c 100644 --- a/rules/private/write_file_private.bzl +++ b/rules/private/write_file_private.bzl @@ -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. visibility """ 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 ) diff --git a/tests/write_file/BUILD b/tests/write_file/BUILD index 321bcc0..9ea3609 100644 --- a/tests/write_file/BUILD +++ b/tests/write_file/BUILD @@ -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", +)