diff --git a/bin/dcat.dart b/bin/dcat.dart index c026f8e..fed17f8 100644 --- a/bin/dcat.dart +++ b/bin/dcat.dart @@ -113,13 +113,13 @@ Future setupArgsParser() async { negatable: false, abbr: 's', help: 'suppress repeated empty output lines'); - parser.addFlag(versionFlag, - negatable: false, help: 'output version information and exit'); parser.addFlag('ignored', negatable: false, hide: true, abbr: 'u'); parser.addFlag(showNonPrintingFlag, negatable: false, abbr: 'v', help: 'use ^ and M- notation, except for LFD and TAB'); + parser.addFlag(versionFlag, + negatable: false, help: 'output version information and exit'); return parser; } diff --git a/lib/dcat.dart b/lib/dcat.dart index 5bf9b96..40867de 100644 --- a/lib/dcat.dart +++ b/lib/dcat.dart @@ -36,8 +36,6 @@ class CatResult { /// The list of errors. final List errors = []; - CatResult(); - /// Returns `true` if the [exitCode] is [exitFailure]. bool get isFailure => exitCode == exitFailure; @@ -47,11 +45,7 @@ class CatResult { /// Adds an error [message] and [path]. void addError(String message, {String? path}) { exitCode = exitFailure; - if (path != null && path.isNotEmpty) { - errors.add(CatError(message, path: path)); - } else { - errors.add(CatError(message)); - } + errors.add(CatError(message, path: path)); } } @@ -147,11 +141,10 @@ Future _copyStream( squeeze = 0; } } - if (showLineNumbers || numberNonBlank) { - if (!numberNonBlank || ch != _lineFeed) { - buff.addAll('${++lastLine.lineNumber}'.padLeft(6).codeUnits); - buff.add(tab); - } + if ((showLineNumbers || numberNonBlank) && + (!numberNonBlank || ch != _lineFeed)) { + buff.addAll('${++lastLine.lineNumber}'.padLeft(6).codeUnits); + buff.add(tab); } } lastLine.lastChar = ch; diff --git a/test.sh b/test.sh index 9f6beaa..34747b3 100755 --- a/test.sh +++ b/test.sh @@ -5,18 +5,27 @@ TMPDCAT=/tmp/tmp-dcat RED='\033[0;31m' GREEN='\033[0;32m' -for o in -A -b -e -E -n -s -t -T -v +checkDiff () { + if ! $(cmp -s $TMPCAT $TMPDCAT); + then + echo -e "${RED}Test failed: $1" >&2 + exit + fi +} + +for o in '' -A -b -e -E -n -s -t -T -v '-Abs' do echo -e "Testing: cat $o" cat $o test/* > $TMPCAT bin/dcat $o test/* > $TMPDCAT - if ! $(diff -q $TMPCAT $TMPDCAT); - then - echo -e "${RED}Test failed: $o" >&2 - exit - fi + checkDiff "cat $o" done +echo -e "Testing: cat -" +echo "This is a test" | cat - test/* > $TMPCAT +echo "This is a test" | bin/dcat - test/* > $TMPDCAT +checkDiff "cat -" + rm -rf $TMPCAT $TMPDCAT echo -e "${GREEN}All tests passed." diff --git a/test/dcat_test.dart b/test/dcat_test.dart index e7707b6..a9f477b 100644 --- a/test/dcat_test.dart +++ b/test/dcat_test.dart @@ -16,16 +16,16 @@ void main() { const sourceFile = 'bin/dcat.dart'; int exitCode; - final tempDir = Directory.systemTemp.createTempSync(); + final tmpDir = Directory.systemTemp.createTempSync(); Stream> mockStdin({String text = sampleText}) async* { yield text.codeUnits; } File makeTmpFile() => - File("${tempDir.path}/tmp-${DateTime.now().millisecondsSinceEpoch}.txt"); + File("${tmpDir.path}/${DateTime.now().millisecondsSinceEpoch}.txt"); - tearDownAll(() => tempDir.delete(recursive: true)); + tearDownAll(() => tmpDir.delete(recursive: true)); group('app', () { test('--help', () async {