#! /bin/sh
# $NetBSD: t_errors.sh,v 1.39 2025/01/03 23:37:18 rillig Exp $
#
# Copyright (c) 2021 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
atf_test_case 'option_int_wrong_argument'
option_int_wrong_argument_body()
{
expect_error \
'indent: Command line: argument "x" to option "-ts" must be an integer' \
-tsx
}
atf_test_case 'option_profile_not_found'
option_profile_not_found_body()
{
expect_error \
'indent: profile ./nonexistent: No such file or directory' \
-P./nonexistent
}
atf_test_case 'option_tabsize_negative'
option_tabsize_negative_body()
{
expect_error \
'indent: Command line: argument "-1" to option "-ts" must be between 1 and 80' \
-ts-1
}
atf_test_case 'option_tabsize_zero'
option_tabsize_zero_body()
{
expect_error \
'indent: Command line: argument "0" to option "-ts" must be between 1 and 80' \
-ts0
}
atf_test_case 'option_tabsize_large'
option_tabsize_large_body()
{
expect_error \
'indent: Command line: argument "81" to option "-ts" must be between 1 and 80' \
-ts81
}
atf_test_case 'option_tabsize_very_large'
option_tabsize_very_large_body()
{
# Integer overflow, on both ILP32 and LP64 platforms.
expect_error \
'indent: Command line: argument "3000000000" to option "-ts" must be between 1 and 80' \
-ts3000000000
}
atf_test_case 'option_indent_size_zero'
option_indent_size_zero_body()
{
expect_error \
'indent: Command line: argument "0" to option "-i" must be between 1 and 80' \
-i0
}
atf_test_case 'option_int_trailing_garbage'
option_int_trailing_garbage_body()
{
expect_error \
'indent: Command line: argument "3garbage" to option "-i" must be an integer' \
-i3garbage
}
atf_test_case 'option_cli_trailing_garbage'
option_cli_trailing_garbage_body()
{
expect_error \
'indent: Command line: argument "3garbage" to option "-cli" must be numeric' \
-cli3garbage
}
# Due to the strange backup suffix '/subdir', indent tries to create
# a file named 'code.c/subdir', but 'code.c' is already a regular
# file, not a directory.
atf_check -s 'exit:1' \
-e 'inline:indent: code.c/subdir: Not a directory\n' \
env SIMPLE_BACKUP_SUFFIX="/subdir" "$indent" code.c
# Since there was an early error, the original file is kept as is.
atf_check -o 'file:code.c.orig' \
cat code.c
}
atf_test_case 'argument_input_enoent'
argument_input_enoent_body()
{
atf_check -s 'exit:1' \
-e 'inline:indent: ./nonexistent.c: No such file or directory\n' \
"$indent" ./nonexistent.c
}
expect_error \
'error: code.c:1: Statement nesting error' \
code.c
# Despite the error message, the original file got overwritten with a
# best-effort rewrite of the code.
atf_check \
-o 'inline:int i = 3};\n' \
cat code.c
}
atf_test_case 'unbalanced_parentheses'
unbalanced_parentheses_body()
{
cat <<-\EOF > code.c
int var =
(
;
)
;
EOF
cat <<-\EOF > stderr.exp
error: code.c:3: Unbalanced parentheses
warning: code.c:4: Extra ')'
EOF
atf_test_case 'crash_comment_after_controlling_expression'
crash_comment_after_controlling_expression_body()
{
# Before 2023-05-11, indent crashed while
# trying to format the following artificial code.
printf '{if(expr\n)/*c*/;}\n' > code.c
cat <<\EOF > code.exp
{
if (expr
) /* c */
;
}
EOF
atf_test_case 'comment_fits_in_one_line'
comment_fits_in_one_line_body()
{
# The comment is placed after 'if (0) ...'. Before NetBSD pr_comment.c
# 1.91 from 2021-10-30, this produced an assertion failure in
# fits_in_one_line.
cat <<EOF > code.c
int f(void)
{
if (0)
/* 0123456789012345678901 */;
}
EOF
# Indent tries hard to make the comment fit to the 34-character line
# length, but it is just not possible.
cat <<EOF > expected.out
int
f(void)
{
if (0)
/*
* 0123456789012345678901
*/;
}
EOF