From 4c795d543908ea4715b3e0bd6c6cf908315936d8 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Wed, 7 Jan 2015 18:30:28 -0500 Subject: split: new -t option to select record separator * src/split.c (eolchar): A new variable to hold the separator character (unibyte for now). This is reference throughout rather than hardcoding '\n'. (usage): Describe the new --separator option, and mention records along with lines so there is no ambiguity that all options treat lines and records equivalently. (main): Have -t update eolchar, or default to '\n'. * tests/split/record-sep.sh: New test case. * tests/local.mk: Reference the new test. * doc/coreutils.texi (split invocation): Document the new option. Adjust --lines, --line-bytes, --number=[lr]/... to mention they pertain to records if --separator is specified. * NEWS: Mention the new feature. --- tests/local.mk | 5 +-- tests/split/record-sep.sh | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100755 tests/split/record-sep.sh (limited to 'tests') diff --git a/tests/local.mk b/tests/local.mk index 6fc859961..5dcbd5595 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -358,6 +358,7 @@ all_tests = \ tests/split/line-bytes.sh \ tests/split/l-chunk.sh \ tests/split/r-chunk.sh \ + tests/split/record-sep.sh \ tests/split/numeric.sh \ tests/split/guard-input.sh \ tests/misc/stat-birthtime.sh \ @@ -402,7 +403,7 @@ all_tests = \ tests/misc/xattr.sh \ tests/tail-2/wait.sh \ tests/tail-2/retry.sh \ - tests/tail-2/symlink.sh \ + tests/tail-2/symlink.sh \ tests/tail-2/tail-c.sh \ tests/chmod/c-option.sh \ tests/chmod/equal-x.sh \ @@ -483,7 +484,7 @@ all_tests = \ tests/dd/ascii.sh \ tests/dd/direct.sh \ tests/dd/misc.sh \ - tests/dd/no-allocate.sh \ + tests/dd/no-allocate.sh \ tests/dd/nocache.sh \ tests/dd/not-rewound.sh \ tests/dd/reblock.sh \ diff --git a/tests/split/record-sep.sh b/tests/split/record-sep.sh new file mode 100755 index 000000000..f41215a45 --- /dev/null +++ b/tests/split/record-sep.sh @@ -0,0 +1,78 @@ +#!/bin/sh +# test split with custom record separators + +# Copyright (C) 2015 Free Software Foundation, Inc. + +# This program 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. + +# This program 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 . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +NL=' +' + +for sep in "$NL" '\0' ':'; do + + test "$sep" = "$NL" && tr='\n' || tr="$sep" + + for mode in '--lines=2' '--line-bytes=4' '--number=l/3' '--number=r/3'; do + + # Generate in default mode for comparison + printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + split $mode in || fail=1 + tr '\n' "$tr" < xaa > exp1 + tr '\n' "$tr" < xab > exp2 + tr '\n' "$tr" < xac > exp3 + + rm -f x?? + + # Generate output with specified --separator + printf '1\n2\n3\n4\n5\n' | tr '\n' "$tr" > in || framework_failure_ + split $mode -t "$sep" in || fail=1 + + compare exp1 xaa || fail=1 + compare exp2 xab || fail=1 + compare exp3 xac || fail=1 + test -f xad && fail=1 + done + +done + + +# +# Test usage edge cases +# + +# Should fail: '-t' requires an argument +{ split -t /dev/null 2>/dev/null || test $? -ne 1; } && + { warn_ "-t without argument did not trigger an error" ; fail=1 ; } + +# should fail: multi-character separator +{ split -txx /dev/null 2>&1 || test $? -ne 1; } && + { warn_ "-txx did not trigger an error" ; fail=1 ; } + +# should fail: different separators used +{ split -ta -tb /dev/null 2>&1 || test $? -ne 1; } && + { warn_ "-ta -tb did not trigger an error" ; fail=1 ; } + +# should fail: different separators used, including default +{ split -t"$NL" -tb /dev/null 2>&1 || test $? -ne 1; } && + { warn_ "-t\$NL -tb did not trigger an error" ; fail=1 ; } + +# should not fail: same separator used multiple times +split -t: -t: /dev/null 2>&1 || + { warn_ "-t: -t: triggered an error" ; fail=1 ; } + + +Exit $fail -- cgit v1.2.3-54-g00ecf