# Hand crafted tests for GNU M4. -*- Autotest -*- # Copyright (C) 2001, 2006-2010, 2013-2014, 2017 Free Software # Foundation, Inc. # This file is part of GNU M4. # # GNU M4 is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # GNU M4 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . AT_BANNER([Options.]) ## ------------------ ## ## deprecated options ## ## ------------------ ## AT_SETUP([deprecated options]) dnl -N/--diversions was removed after 1.4.x AT_CHECK_M4([--diversions=1], [1], [], [stderr]) AT_CHECK([$SED 's/Try.*help/Try `m4 --help/' stderr], [0], [[m4: bad option Try `m4 --help' for more information. ]]) dnl -H/--hashsize are no-ops since 2.0 AT_CHECK_M4([-H1 --hashsize=1], [0], [], [[m4: warning: `-H' is deprecated m4: warning: `--hashsize' is deprecated ]]) dnl -S/-T are no-ops for compatibility AT_CHECK_M4([-S1 -T1], [0], [], [[m4: warning: `-S' is deprecated m4: warning: `-T' is deprecated ]]) dnl -Bint can be confused with its no-op meaning in 1.4.x, but all other dnl uses of -B or its long option are okay AT_CHECK_M4([-B1], [0], [], [[m4: warning: recommend using `-B ./1' instead ]]) AT_CHECK_M4([-B./1 --prepend-include=1], [0], [], [[]]) dnl --error-output is a misleading name AT_CHECK_M4([--error-output=trace], [0], [], [[m4: warning: `--error-output' is deprecated, use `--debugfile' instead ]]) AT_CHECK([rm trace]) dnl -o will change meaning in the future AT_CHECK_M4([-otrace], [0], [], [[m4: warning: `-o' is deprecated, use `--debugfile' instead ]]) AT_CHECK([rm trace]) dnl -e is an odd spelling for --interactive AT_CHECK_M4([-e], [0], [], [[m4: warning: `-e' is deprecated, use `-i' instead ]]) dnl --arglength is an older spelling for --debuglen AT_CHECK_M4([--arglength=10], [0], [], [[m4: warning: `--arglength' is deprecated, use `--debuglen' instead ]]) AT_CHECK_M4([--arglength=x], [1], [], [[m4: warning: `--arglength' is deprecated, use `--debuglen' instead m4: invalid --arglength argument 'x' ]]) dnl --word-regexp from 1.4.x is no longer supported. Can't test for its dnl alternate spelling -W, as that now means --warnings. AT_CHECK_M4([[--word-regexp='[a-zA-Z0-9_]+']], [0], [], [[m4: warning: `--word-regexp' is deprecated ]]) AT_CLEANUP ## ---------- ## ## file names ## ## ---------- ## AT_SETUP([file names]) dnl Check that all files are processed even after missing file AT_DATA([in], [[hello world ]]) AT_CHECK_M4([oops in], [1], [[hello world ]], [[m4: cannot open file 'oops': No such file or directory ]]) dnl Check that '-' means stdin, even if ./- exists. AT_DATA([-], [[hi ]]) AT_CHECK_M4([-], [0]) AT_CHECK_M4([- --], [0]) AT_CHECK_M4([-- -], [0]) AT_CHECK_M4([./-], [0], [[hi ]]) AT_CHECK_M4([./- --], [0], [[hi ]]) AT_CHECK_M4([-- ./-], [0], [[hi ]]) AT_CLEANUP ## --------------- ## ## option grouping ## ## --------------- ## AT_SETUP([option grouping]) dnl this test group depends on correct settings of POSIXLY_CORRECT if test -n "${POSIXLY_CORRECT+set}" ; then (unset POSIXLY_CORRECT) >/dev/null 2>&1 || { echo "Problem unsetting POSIXLY_CORRECT" AT_CHECK([exit 77]) } unset POSIXLY_CORRECT fi dnl This comes from the "Invoking m4" node; it was easier to write the dnl test separately than to let generate.awk fiddle with it. AT_DATA([a], [[foo 1 m@&t@4_dnl() ]]) AT_DATA([-d+f], [[hi ]]) dnl First, run with GNU semantics AT_CHECK_M4([-QPDfoo -d a -d+f], [0], [[ 1 ]]) AT_CHECK_M4([-Q -P -D foo -d -d+f -- a], [0], [[ 1 ]]) AT_CHECK_M4([-QPDfoo -d -- a -d+f], [0], [[ 1 hi ]]) AT_CHECK_M4([-Q -P -D foo -d ./a ./-d+f], [0], [[ 1 hi ]]) AT_CHECK_M4([--def foo --debug a], [0], [[ 1 m@&t@4_dnl() ]]) AT_CHECK_M4([--define=foo --debug= -- a], [0], [[ 1 m@&t@4_dnl() ]]) dnl Next, run with POSIX semantics POSIXLY_CORRECT=1 export POSIXLY_CORRECT AT_CHECK_M4([-QPDfoo -d a -d+f], [0], [[ 1 ]]) AT_CHECK_M4([-Q -P -D foo -d -d+f -- ./a], [0], [[ 1 ]]) AT_CHECK_M4([-QPDfoo -d -- a -d+f], [0], [[ 1 hi ]]) AT_CHECK_M4([-Q -P -D foo -d ./a ./-d+f], [0], [[ 1 hi ]]) AT_CHECK_M4([--def foo --debug a], [0], [[ 1 m@&t@4_dnl() ]]) AT_CHECK_M4([--define=foo --debug= -- a], [0], [[ 1 m@&t@4_dnl() ]]) AT_CLEANUP ## --------------- ## ## POSIXLY_CORRECT ## ## --------------- ## AT_SETUP([POSIXLY_CORRECT]) dnl this test group depends on correct settings of POSIXLY_CORRECT if test -n "${POSIXLY_CORRECT+set}" ; then (unset POSIXLY_CORRECT) >/dev/null 2>&1 || { echo "Problem unsetting POSIXLY_CORRECT" AT_CHECK([exit 77]) } unset POSIXLY_CORRECT fi dnl By default, GNU extensions and warnings are enabled AT_DATA([in], [[__line__ dnl() ]]) AT_CHECK_M4([in], [0], [[1 ]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 ]]) AT_CHECK_M4([in -g], [0], [[1 ]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 ]]) AT_CHECK_M4([in -G], [0], [[__line__ ]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 ]]) AT_CHECK_M4([in -W], [0], [[1 ]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 ]]) AT_CHECK_M4([in -Q], [0], [[1 ]]) dnl Next, run with POSIX semantics POSIXLY_CORRECT=1 export POSIXLY_CORRECT dnl POSIXLY_CORRECT implies -G -Q AT_DATA([in], [[__line__ dnl() ]]) AT_CHECK_M4([in], [0], [[__line__ ]]) AT_CHECK_M4([in -g], [0], [[1 ]]) AT_CHECK_M4([in -G], [0], [[__line__ ]]) AT_CHECK_M4([in -W], [0], [[__line__ ]], [[m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 ]]) AT_CHECK_M4([in -Q], [0], [[__line__ ]]) AT_CLEANUP ## -------------- ## ## unknown option ## ## -------------- ## AT_SETUP([unknown option]) AT_CHECK_M4([--unknown], [1], [], [stderr]) AT_CHECK([$SED 's/Try.*help/Try `m4 --help/' stderr], [0], [[m4: bad option Try `m4 --help' for more information. ]]) AT_CLEANUP ## --------- ## ## debugfile ## ## --------- ## AT_SETUP([--debugfile]) dnl For a while, CVS m4 mistakenly sent debug output to stdout dnl when stdout and stderr were the same file. AT_DATA([[in]], [[foo ]]) AT_CHECK_M4([--debugfile=trace1 -tfoo -Dfoo=bar in 2>&1], [0], [[bar ]]) AT_CHECK_M4([--debugfile=trace2 -tfoo -Dfoo=bar in], [0], [[bar ]]) AT_CHECK([cmp trace1 trace2]) dnl m4 1.4.x mistakenly created the trace file. --help and --version dnl should always override, even if they come later in the command line AT_CHECK_M4([--debugfile=trace3 --help], [0], [ignore]) AT_CHECK_M4([--debugfile=trace3 --version], [0], [ignore]) AT_CHECK([test -f trace3], [1]) dnl check that trace file failure causes an error, but allows processing AT_CHECK_M4([--debugfile=no_such_dir/trace -tfoo -Dfoo=bar in], [1], [[bar ]], [[m4: cannot set debug file 'no_such_dir/trace': No such file or directory m4trace: -1- foo -> `bar' ]]) dnl check that empty trace file discards trace data AT_CHECK_M4([--debugfile= -tfoo -Dfoo=bar in], [0], [[bar ]]) dnl check that all tracing gets diverted to the trace file. Don't use dnl AT_CHECK_M4 on the first run, because sanitizing stderr breaks the dnl comparison with the raw data in the trace file of the second run. AT_CHECK([$M4 -dV in], [0], [[foo ]], [stderr]) mv stderr expout AT_CHECK_M4([--debugfile=trace4 -dV in], [0], [[foo ]]) AT_CHECK([cat trace4], [0], [expout]) dnl check that order matters AT_DATA([in1], [[bar ]]) AT_DATA([in2], [[errprint(`hi ')dnl bar ]]) AT_CHECK_M4([-Dbar=hello -tbar --debugfile= in1 --debugfile in2], [0], [[hello hello ]], [[hi m4trace: -1- bar -> `hello' ]]) AT_CLEANUP ## -------- ## ## debuglen ## ## -------- ## AT_SETUP([--debuglen]) dnl Check for argument validation. AT_DATA([in], [[define(`echo', `$@')dnl traceon(`echo')dnl echo(`long string') ]]) AT_CHECK_M4([--debuglen=-1 in], [1], [], [[m4: invalid --debuglen argument '-1' ]]) AT_CHECK_M4([--debuglen oops in], [1], [], [[m4: invalid --debuglen argument 'oops' ]]) AT_CHECK_M4([-l 10oops in], [1], [], [[m4: invalid suffix in -l argument '10oops' ]]) dnl MiB is the suffix to implict 1, resulting in 1048576 AT_CHECK_M4([-lMiB in], [0], [[long string ]], [[m4trace: -1- echo(`long string') -> ``long string'' ]]) dnl this assumes size_t is no bigger than 64 bits AT_CHECK_M4([-l 123456789012345678901234567890 in], [1], [], [[m4: -l argument '123456789012345678901234567890' too large ]]) AT_CHECK_M4([--debugl 123456789012345678901234567890 in], [1], [], [[m4: --debuglen argument '123456789012345678901234567890' too large ]]) dnl per POSIX guidelines, this is a decimal number 10, not octal 8 AT_CHECK_M4([-l 010 in], [0], [[long string ]], [[m4trace: -1- echo(`long strin...') -> ``long stri...' ]]) AT_CHECK_M4([-l 3 in], [0], [[long string ]], [[m4trace: -1- echo(`lon...') -> ``lo...' ]]) AT_CHECK_M4([--debuglen=3 -l0 in], [0], [[long string ]], [[m4trace: -1- echo(`long string') -> ``long string'' ]]) AT_CLEANUP ## --------- ## ## debugmode ## ## --------- ## AT_SETUP([--debugmode]) AT_DATA([[in]], [[include(`nested')dnl len(`abc') ]]) AT_DATA([[nested]], [[m4wrap(`divnum ')dnl ]]) dnl AT_CHECK_M4 starts life with -d. Make sure it looks like -d+adeq. AT_CHECK_M4([-tlen in], [0], [[3 0 ]], [[m4trace: -1- len(`abc') -> `3' ]]) AT_CHECK_M4([-tlen -dfl -d in], [0], [[3 0 ]], [[m4trace:in:2: -1- len(`abc') -> `3' ]]) dnl -d after file is deferred (but this is still useful for some flags). AT_CHECK_M4([-tlen in -dfli], [0], [[3 0 ]], [[m4trace: -1- len(`abc') -> `3' m4debug: input from m4wrap recursion level 1 m4debug: input from m4wrap exhausted ]]) dnl Test all flags. AT_CHECK_M4([-dV in], [0], [[3 0 ]], [[m4debug: module m4: opening file m4debug: module m4: init hook called m4debug: module m4: opened m4debug: module m4: builtins loaded m4debug: module gnu: opening file m4debug: module gnu: init hook called m4debug: module gnu: opened m4debug: module gnu: builtins loaded m4debug: module gnu: macros loaded m4debug: path search for 'in' found 'in' m4debug: input read from 'in' m4trace:in:1: -1- id 1: include ... = {m4} m4debug:in:1: path search for 'nested' found 'nested' m4debug:in:1: input read from 'nested' m4trace:in:1: -1- id 1: include(`nested') -> m4trace:nested:1: -1- id 2: m4wrap ... = {m4} m4trace:nested:1: -1- id 2: m4wrap(`divnum ') -> `' m4trace:nested:2: -1- id 3: dnl ... = {m4} m4trace:nested:2: -1- id 3: dnl -> `' m4debug:nested:3: input reverted to in, line 1 m4trace:in:1: -1- id 4: dnl ... = {m4} m4trace:in:1: -1- id 4: dnl -> `' m4trace:in:2: -1- id 5: len ... = {m4} m4trace:in:2: -1- id 5: len(`abc') -> `3' m4debug:in:3: input exhausted m4debug: input from m4wrap recursion level 1 m4trace:nested:1: -1- id 6: divnum ... = {m4} m4trace:nested:1: -1- id 6: divnum -> `0' m4debug: input from m4wrap exhausted ]]) dnl Test addition and subtraction of flags. AT_CHECK_M4([--debug=-e --debugmode=+xt in], [0], [[3 0 ]], [[m4trace: -1- id 1: include(`nested') m4trace: -1- id 2: m4wrap(`divnum ') m4trace: -1- id 3: dnl m4trace: -1- id 4: dnl m4trace: -1- id 5: len(`abc') m4trace: -1- id 6: divnum ]]) dnl Test that shorter prefix is ambiguous. AT_CHECK_M4([--debu], [1], [], [stderr]) AT_CHECK([$SED -e 's/Try.*--help/Try `m4 --help/' stderr], [0], [[m4: bad option Try `m4 --help' for more information. ]]) AT_CLEANUP ## ---------------- ## ## discard comments ## ## ---------------- ## AT_SETUP([--discard-comments]) AT_DATA([[in]], [[This is not a comment # but this is. # This line should disappear completely. This should not disappear. changecom(`') html ends. ]]) AT_CHECK_M4([-c in], [0], [[This is not a comment This should not disappear. html ends. ]]) dnl check that even when discarding, a comment must be terminated by EOF AT_DATA([in], [[changecom(`/*', `*/')dnl /* unterminated comment ]]) AT_CHECK_M4([--discard-comments in], [1], [], [[m4:in:2: end of file in comment ]]) AT_CLEANUP ## -------------- ## ## fatal warnings ## ## -------------- ## AT_SETUP([--fatal-warnings]) AT_DATA([[in]], [[dnl() dnl() dnl() ]]) dnl By default, warnings don't affect exit status AT_CHECK_M4([in], [0], [], [[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0 m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 m4:in:3: warning: dnl: extra arguments ignored: 1 > 0 ]]) dnl With one -E, exit status changes, but execution continues AT_CHECK_M4([-E in], [1], [], [[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0 m4:in:2: warning: dnl: extra arguments ignored: 1 > 0 m4:in:3: warning: dnl: extra arguments ignored: 1 > 0 ]]) dnl With two -E, execution halts immediately AT_CHECK_M4([--fatal-warnings --fatal-warnings in], [1], [], [[m4:in:1: warning: dnl: extra arguments ignored: 1 > 0 ]]) dnl Exit status can't be affected if there were no warnings AT_CHECK_M4([-EEQ in], [0]) AT_CLEANUP ## ---------------- ## ## help and version ## ## ---------------- ## AT_SETUP([--help and --version]) AT_CHECK_M4([--help], [0], [stdout]) AT_CHECK([[$SED -n -e 's|Usage:.*\[OPTION\]... \[FILE\]...|success|p' stdout]], [0], [success ]) AT_CHECK_M4([--version], [0], [stdout]) AT_CHECK([[$SED -n -e 's|There is NO WARRANTY.*|success|p' stdout]], [0], [success ]) dnl make sure option specified first takes precedence AT_CHECK_M4([--help --version], [0], [stdout]) AT_CHECK([[$SED -n -e 's|Usage:.*\[OPTION\]... \[FILE\]...|success|p' stdout]], [0], [success ]) AT_CHECK_M4([--version --help], [0], [stdout]) AT_CHECK([[$SED -n -e 's|There is NO WARRANTY.*|success|p' stdout]], [0], [success ]) AT_CLEANUP ## ------------------ ## ## import-environment ## ## ------------------ ## AT_SETUP([--import-environment]) AT_DATA([[in]], [[`TEST'=TEST `ZAPPED'=ZAPPED `OVERRIDE'=OVERRIDE ]]) TEST='This is an environment variable' export TEST ZAPPED='This is an environment variable which we will delete' export ZAPPED OVERRIDE='This is an environment variable which we will change' export OVERRIDE AT_CHECK_M4([--import-environment -UZAPPED -DOVERRIDE='It is changed.' in], [0], [[TEST=This is an environment variable ZAPPED=ZAPPED OVERRIDE=It is changed. ]]) AT_CLEANUP ## ------------- ## ## nesting-limit ## ## ------------- ## AT_SETUP([--nesting-limit]) dnl Check for argument validation. AT_DATA([in], [[define(`echo', `$@')dnl echo(echo(echo(echo(`nested string')))) echo(echo(echo(echo(echo(echo(echo(echo(echo(`nested string'))))))))) ]]) AT_CHECK_M4([--nesting-limit=-1 in], [1], [], [[m4: invalid --nesting-limit argument '-1' ]]) AT_CHECK_M4([--nesting-limit oops in], [1], [], [[m4: invalid --nesting-limit argument 'oops' ]]) AT_CHECK_M4([-L 10oops in], [1], [], [[m4: invalid suffix in -L argument '10oops' ]]) dnl MiB is the suffix to implict 1, resulting in 1048576 AT_CHECK_M4([-LMiB in], [0], [[nested string nested string ]]) dnl this assumes size_t is no bigger than 64 bits AT_CHECK_M4([-L 123456789012345678901234567890 in], [1], [], [[m4: -L argument '123456789012345678901234567890' too large ]]) AT_CHECK_M4([--nest 123456789012345678901234567890 in], [1], [], [[m4: --nesting-limit argument '123456789012345678901234567890' too large ]]) AT_CHECK_M4([-L 5 in], [1], [[nested string ]], [[m4:in:3: recursion limit of 5 exceeded, use -L to change it ]]) dnl per POSIX guidelines, this is a decimal number 10, not octal 8 AT_CHECK_M4([-L 010 in], [0], [[nested string nested string ]]) AT_CHECK_M4([--nesting-limit=3 -L0 in], [0], [[nested string nested string ]]) AT_CLEANUP ## --------------- ## ## prepend-include ## ## --------------- ## AT_SETUP([--prepend-include]) dnl Lots of data to set up. AT_DATA([[in]], [[include(`foo')dnl include(`bar')dnl include(`bad')dnl include(`blah')dnl ]]) AT_CHECK([mkdir pre post]) AT_DATA([[pre/foo]], [[in pre/foo ]]) AT_DATA([[foo]], [[in ./foo ]]) AT_DATA([[bar]], [[in ./bar ]]) AT_DATA([[post/bar]], [[in post/bar ]]) AT_DATA([[post/blah]], [[in post/blah ]]) dnl Make circular links in the subdirectories, to ensure that the error dnl message when bad cannot be opened comes from the search in ., regardless dnl of what else was searched. AT_CHECK([ln -s bad pre/bad]) AT_CHECK([ln -s bad post/bad]) dnl Finally, see that it all works. AT_CHECK_M4([-I post -B pre in], [1], [[in pre/foo in ./bar in post/blah ]], [[m4:in:3: include: cannot open file 'bad': No such file or directory ]]) AT_CLEANUP ## ------------- ## ## regexp-syntax ## ## ------------- ## AT_SETUP([--regexp-syntax]) dnl test argument validation AT_DATA([[in]], [[regexp(`(', `(') ]]) AT_CHECK_M4([--regexp-syntax=unknown in], [1], [], [[m4: bad syntax-spec: 'unknown' ]]) AT_CHECK_M4([--regexp-syntax= in], [0], [[0 ]]) AT_CHECK_M4([-rEXTENDED in], [0], [[ ]], [[m4:in:1: warning: regexp: bad regular expression '(': Unmatched ( or \( ]]) AT_CHECK_M4([-rgnu-m4 in], [0], [[0 ]]) AT_CHECK_M4([-r"gnu M4" in], [0], [[0 ]]) dnl Test behavior of -r intermixed with files AT_CHECK_M4([-rEXTENDED in --regexp-syntax in], [0], [[ 0 ]], [[m4:in:1: warning: regexp: bad regular expression '(': Unmatched ( or \( ]]) AT_CLEANUP ## ----- ## ## safer ## ## ----- ## AT_SETUP([--safer]) dnl with --safer, the debugfile macro is crippled, but --debugfile is not AT_DATA([[in]], [[define(`foo', `1')foo debugfile(`trace2') define(`foo', `2')foo ]]) AT_CHECK_M4([--safer --debugfile=trace1 -t foo in], [1], [[1 2 ]], [[m4:in:2: debugfile: disabled by --safer ]]) AT_CHECK([test -f trace2], [1]) AT_CHECK([cat trace1], [0], [[m4trace: -1- foo -> `1' m4trace: -1- foo -> `2' ]]) dnl make sure builtin cannot bypass --safer, and that mkstemp does not dnl create file AT_DATA([[in]], [[builtin(`mkstemp', `./fooXXXXXX') ]]) AT_CHECK([echo foo*], [0], [foo* ]) AT_CHECK_M4([--safer in], [1], [[ ]], [[m4:in:1: mkstemp: disabled by --safer ]]) AT_CHECK([echo foo*], [0], [foo* ]) AT_CLEANUP ## ---------- ## ## syncoutput ## ## ---------- ## AT_SETUP([--syncoutput]) dnl -s/--synclines implies --syncoutput=1 AT_DATA([in], [[hi ]]) AT_CHECK_M4([-s in], [0], [[#line 1 "in" hi ]]) AT_CHECK_M4([--synclines in], [0], [[#line 1 "in" hi ]]) dnl test intermixing option and files AT_CHECK_M4([in --syncoutput=1 in --syncoutput in], [0], [[hi #line 1 "in" hi hi ]]) dnl test parse error AT_CHECK_M4([--syncoutput=huh in], [0], [[hi ]], [[m4: warning: --syncoutput: unknown directive 'huh' ]]) AT_CLEANUP AT_SETUP([--syncoutput and diversions]) dnl synclines should always start at the beginning of a line. dnl this does not (yet) happen reliably when diversions do not dnl always end in newline AT_XFAIL_IF([:]) AT_DATA([in.m4], [[divert(2)2divert(1)1 dnl undivert ]]) AT_CHECK_M4([-s in.m4], [0], [[1 #line 1 "stdin" 2 ]]) AT_CLEANUP ## -------------------- ## ## traceon and traceoff ## ## -------------------- ## AT_SETUP([--traceon and --traceoff]) dnl test intermixing with files AT_DATA([[in]], [[divnum ]]) AT_CHECK_M4([-d+x in --traceon=divnum in --traceoff=divnum in], [0], [[0 0 0 ]], [[m4trace: -1- id 2: divnum -> `0' ]]) dnl test that --traceoff does not warn on untraced name AT_CHECK_M4([--traceoff=unknown], [0]) AT_CLEANUP