Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/private/diff_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ def _diff_test_impl(ctx):
template = ctx.file._diff_test_tmpl_bat
file1_path = to_rlocation_path(ctx, file1)
file2_path = to_rlocation_path(ctx, file2)
fail_msg = ["@echo" + (" " + line if line.strip() != "" else ".") for line in ctx.attr.failure_message[:-1].split("\n")]
fail_msg = "\r\n".join(["@echo" + (" " + line if line.strip() != "" else ".") for line in ctx.attr.failure_message[:-1].splitlines()])
else:
test_suffix = "-test.sh"
template = ctx.file._diff_test_tmpl_sh
fail_msg = ctx.attr.failure_message.split("\n")
fail_msg = ctx.attr.failure_message

test_bin = ctx.actions.declare_file(ctx.label.name + test_suffix)
ctx.actions.expand_template(
template = template,
output = test_bin,
substitutions = {
"{BATCH_RLOCATION_FUNCTION}": BATCH_RLOCATION_FUNCTION,
"{BATCH_RLOCATION_FUNCTION}": "\r\n".join(BATCH_RLOCATION_FUNCTION.splitlines()),
"{name}": ctx.attr.name,
"{fail_msg}": "\n".join(fail_msg),
"{fail_msg}": fail_msg,
"{file1}": file1_path,
"{file2}": file2_path,
"{file1_sub_path}": file1_sub_path,
Expand Down
214 changes: 107 additions & 107 deletions lib/private/diff_test_tmpl.bat
Original file line number Diff line number Diff line change
@@ -1,107 +1,107 @@
@rem @generated by @bazel_lib//lib/private:diff_test.bzl
@echo off
:: TODO: Add support for XML_OUTPUT_FILE like in diff_test_tmpl.sh
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set RUNFILES_MANIFEST_ONLY=1
{BATCH_RLOCATION_FUNCTION}
set MF=%RUNFILES_MANIFEST_FILE:/=\\%
set PATH=%SYSTEMROOT%\\system32
call :rlocation {file1} RF1
call :rlocation {file2} RF2
set RF1=!RF1:/=\!
set RF2=!RF2:/=\!
if "{file1_sub_path}" neq "" (
set RF1=!RF1!\\{file1_sub_path}
)
if "{file2_sub_path}" neq "" (
set RF2=!RF2!\\{file2_sub_path}
)
set DF1=0
set DF2=0
if exist "!RF1!\\*" (
set DF1=1
)
if exist "!RF2!\\*" (
set DF2=1
)
if %DF1% equ 1 (
if %DF2% equ 0 (
echo>&2 ERROR: Cannot compare directory "{file1}" and a file "{file2}"
exit /b 1
)
)
if %DF1% equ 0 (
if %DF2% equ 1 (
echo>&2 ERROR: Cannot compare file "{file1}" and a directory "{file2}"
exit /b 1
)
)
set DFX=0
if %DF1% equ 1 (
if %DF2% equ 1 (
set DFX=1
)
)

if %DFX% equ 0 goto :compare_files
:compare_directories
for /f "delims=" %%F in (
'echo "."^&forfiles /s /p "!RF1!" /m "*" /c "cmd /c echo @relpath"'
) do (
if not exist "!RF2!\\%%~F" (
echo>&2 FAIL: file "%%~F" exists in "{file1}" and not in "{file2}".
GOTO fail
)
if not exist "!RF1!\\%%~F\\*" (
fc.exe "!RF1!\\%%~F" "!RF2!\\%%~F" 2>NUL 1>NUL
if !ERRORLEVEL! neq 0 (
if !ERRORLEVEL! equ 1 (
echo>&2 FAIL: files "!RF1!\\%%~F" and "!RF2!\\%%~F" differ.
set RF1=!RF1!\\%%~F
set RF2=!RF2!\\%%~F
GOTO fail
) else (
fc.exe "!RF1!\\%%~F" "!RF2!\\%%~F"
GOTO fail
)
)
)
)
for /f "delims=" %%F in (
'echo "."^&forfiles /s /p "!RF2!" /m "*" /c "cmd /c echo @relpath"'
) do (
if not exist "!RF1!\\%%~F" (
echo>&2 FAIL: file "%%~F" exists in "{file2}" and not in "{file1}".
GOTO fail
)
)
goto :success

:compare_files
echo compare_files
fc.exe "!RF1!" "!RF2!" 2>NUL 1>NUL
set result=%ERRORLEVEL%
if !result! neq 0 (
if !result! equ 1 (
echo>&2 FAIL: files "!RF1!" and "!RF2!" differ.
goto :fail
) else (
echo fc.exe "!RF1!" "!RF2!"
fc.exe "!RF1!" "!RF2!"
set result=%ERRORLEVEL%
exit /b !result!
)
) else (
echo fc returned 0
)
:success
exit /b 0

:fail
{fail_msg}
echo To see differences run:
echo.
echo diff "!RF1!" "!RF2!"
echo.
exit /b 1
@rem @generated by @bazel_lib//lib/private:diff_test.bzl
@echo off
:: TODO: Add support for XML_OUTPUT_FILE like in diff_test_tmpl.sh
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set RUNFILES_MANIFEST_ONLY=1
{BATCH_RLOCATION_FUNCTION}
set MF=%RUNFILES_MANIFEST_FILE:/=\\%
set PATH=%SYSTEMROOT%\\system32
call :rlocation {file1} RF1
call :rlocation {file2} RF2
set RF1=!RF1:/=\!
set RF2=!RF2:/=\!
if "{file1_sub_path}" neq "" (
set RF1=!RF1!\\{file1_sub_path}
)
if "{file2_sub_path}" neq "" (
set RF2=!RF2!\\{file2_sub_path}
)
set DF1=0
set DF2=0
if exist "!RF1!\\*" (
set DF1=1
)
if exist "!RF2!\\*" (
set DF2=1
)
if %DF1% equ 1 (
if %DF2% equ 0 (
echo>&2 ERROR: Cannot compare directory "{file1}" and a file "{file2}"
exit /b 1
)
)
if %DF1% equ 0 (
if %DF2% equ 1 (
echo>&2 ERROR: Cannot compare file "{file1}" and a directory "{file2}"
exit /b 1
)
)
set DFX=0
if %DF1% equ 1 (
if %DF2% equ 1 (
set DFX=1
)
)
if %DFX% equ 0 goto :compare_files
:compare_directories
for /f "delims=" %%F in (
'echo "."^&forfiles /s /p "!RF1!" /m "*" /c "cmd /c echo @relpath"'
) do (
if not exist "!RF2!\\%%~F" (
echo>&2 FAIL: file "%%~F" exists in "{file1}" and not in "{file2}".
GOTO fail
)
if not exist "!RF1!\\%%~F\\*" (
fc.exe "!RF1!\\%%~F" "!RF2!\\%%~F" 2>NUL 1>NUL
if !ERRORLEVEL! neq 0 (
if !ERRORLEVEL! equ 1 (
echo>&2 FAIL: files "!RF1!\\%%~F" and "!RF2!\\%%~F" differ.
set RF1=!RF1!\\%%~F
set RF2=!RF2!\\%%~F
GOTO fail
) else (
fc.exe "!RF1!\\%%~F" "!RF2!\\%%~F"
GOTO fail
)
)
)
)
for /f "delims=" %%F in (
'echo "."^&forfiles /s /p "!RF2!" /m "*" /c "cmd /c echo @relpath"'
) do (
if not exist "!RF1!\\%%~F" (
echo>&2 FAIL: file "%%~F" exists in "{file2}" and not in "{file1}".
GOTO fail
)
)
goto :success
:compare_files
echo compare_files
fc.exe "!RF1!" "!RF2!" 2>NUL 1>NUL
set result=%ERRORLEVEL%
if !result! neq 0 (
if !result! equ 1 (
echo>&2 FAIL: files "!RF1!" and "!RF2!" differ.
goto :fail
) else (
echo fc.exe "!RF1!" "!RF2!"
fc.exe "!RF1!" "!RF2!"
set result=%ERRORLEVEL%
exit /b !result!
)
) else (
echo fc returned 0
)
:success
exit /b 0
:fail
{fail_msg}
echo To see differences run:
echo.
echo diff "!RF1!" "!RF2!"
echo.
exit /b 1
3 changes: 2 additions & 1 deletion lib/private/write_source_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ if !ERRORLEVEL! neq 0 (
ctx.actions.write(
output = updater,
is_executable = True,
content = "\n".join(contents),
# since `contents` already contains some \n, splitting lines first avoids bogus \r\r\n
content = "\r\n".join([line for content in contents for line in content.splitlines()]),
)
return updater

Expand Down
9 changes: 9 additions & 0 deletions lib/tests/bats/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
load("//lib:bats.bzl", "bats_test")

NOT_WINDOWS = select({
Expand All @@ -15,6 +16,14 @@ bats_test(
target_compatible_with = NOT_WINDOWS,
)

# Verify line endings in the launcher script generated by bats_test: CRLF-only on Windows, LF-only otherwise
native_test(
name = "script_newlines_test",
src = "//lib/tests/check_newlines",
args = ["$(rlocationpath :basic)"],
data = [":basic"],
)

bats_test(
name = "env",
size = "small",
Expand Down
9 changes: 9 additions & 0 deletions lib/tests/check_newlines/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
name = "check_newlines",
testonly = True,
srcs = ["main.go"],
visibility = ["//lib/tests:__subpackages__"],
deps = ["@io_bazel_rules_go//go/runfiles"],
)
48 changes: 48 additions & 0 deletions lib/tests/check_newlines/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"bytes"
"fmt"
"os"
"runtime"

"github.com/bazelbuild/rules_go/go/runfiles"
)

func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: check_newlines <script>")
os.Exit(1)
}

r, err := runfiles.New()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize runfiles: %v\n", err)
os.Exit(1)
}

script, err := r.Rlocation(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to locate %s in runfiles: %v\n", os.Args[1], err)
os.Exit(1)
}

content, err := os.ReadFile(script)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read %s: %v\n", script, err)
os.Exit(1)
}

cr := bytes.Count(content, []byte("\r"))
lf := bytes.Count(content, []byte("\n"))
crlf := bytes.Count(content, []byte("\r\n"))
if runtime.GOOS == "windows" {
if cr != lf || cr != crlf {
fmt.Fprintf(os.Stderr, "%s contains non-Windows line endings (\\r=%d, \\n=%d, \\r\\n=%d)\n", script, cr, lf, crlf)
os.Exit(1)
}
} else if cr > 0 {
fmt.Fprintf(os.Stderr, "%s contains Windows line endings (\\r=%d, \\n=%d, \\r\\n=%d)\n", script, cr, lf, crlf)
os.Exit(1)
}
}
37 changes: 37 additions & 0 deletions lib/tests/diff_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@bazel_lib//lib:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

# Create two simple files to compare
write_file(
name = "gen_file1",
out = "file1.txt",
content = [
"line1",
"line2",
],
)

write_file(
name = "gen_file2",
out = "file2.txt",
content = [
"line1",
"line2",
],
)

# Have diff_test generate a script
diff_test(
name = "gen_script",
file1 = ":file1.txt",
file2 = ":file2.txt",
)

# Verify line endings in the script generated by diff_test: CRLF-only on Windows, LF-only otherwise
native_test(
name = "script_newlines_test",
src = "//lib/tests/check_newlines",
args = ["$(rlocationpath :gen_script)"],
data = [":gen_script"],
)
Loading
Loading