t0090-cache-tree fails due to wc whitespace

6 messages, 5 authors, 2016-06-15 · open the first message on its own page

t0090-cache-tree fails due to wc whitespace

From: Brian Gernhardt <hidden>
Date: 2016-06-15 22:52:38

It's time for my periodic complaint:  People assuming `wc -l` outputs just a number.  wc on OS X (and perhaps other BSD-like systems) always aligns the output in columns, even with the -l flag.  Generally this results in a quick patch from me to remove some unneeded quotes.  However, this time it's used in a more complex manner:

	echo "SHA " \
	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
	cmp_cache_tree expect

This results in errors like:
--- expect	2011-12-14 14:26:26.000000000 +0000
+++ filtered	2011-12-14 14:26:26.000000000 +0000
@@ -1 +1 @@
-SHA  (       1 entries, 0 subtrees)
+SHA  (1 entries, 0 subtrees)
I was able to fix this by adding a sed command to remove leading spaces:

-           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&

But I'm not sure if this is the best way to solve the issue.

~~ Brian Gernhardt

Re: t0090-cache-tree fails due to wc whitespace

From: Stefano Lattarini <hidden>
Date: 2016-06-15 22:52:38

On Wednesday 14 December 2011, Brian Gernhardt wrote:
It's time for my periodic complaint:  People assuming `wc -l`
outputs just a number.  wc on OS X (and perhaps other BSD-like
systems) always aligns the output in columns, even with the -l
flag.
It surely does so on Solaris 10 as well:

$ echo x | wc -l
       1
$ for i in {1..1000}; do echo x; done | wc -l
    1000

Regards,
  Stefano

Re: t0090-cache-tree fails due to wc whitespace

From: Hallvard Breien Furuseth <hidden>
Date: 2016-06-15 22:52:38

Brian Gernhardt writes:
I was able to fix this by adding a sed command to remove leading spaces:

-           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&

But I'm not sure if this is the best way to solve the issue.
Well,  tr -d ' '  saves all of 7 characters from  sed -e 's/^ *//'.

-- 
Hallvard

Re: t0090-cache-tree fails due to wc whitespace

From: Johannes Sixt <hidden>
Date: 2016-06-15 22:52:38

Am 12/14/2011 15:35, schrieb Brian Gernhardt:
It's time for my periodic complaint:  People assuming `wc -l` outputs
just a number.  wc on OS X (and perhaps other BSD-like systems) always
aligns the output in columns, even with the -l flag.  Generally this
results in a quick patch from me to remove some unneeded quotes.
However, this time it's used in a more complex manner:

	echo "SHA " \
	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
	cmp_cache_tree expect
I'd solve it by moving the command substitution outside the quoted string:

 	printf "SHA (%d entries, 0 subtrees)\n" \
		$(git ls-files | wc -l) >expect &&

Other proposed solutions add another process. I don't like that on Windows ;)

-- Hannes

Re: t0090-cache-tree fails due to wc whitespace

From: Thomas Rast <hidden>
Date: 2016-06-15 22:52:38

Brian Gernhardt wrote:
It's time for my periodic complaint: People assuming `wc -l` outputs
just a number.  wc on OS X (and perhaps other BSD-like systems)
always aligns the output in columns, even with the -l flag.
Oops.
Generally this results in a quick patch from me to remove some
unneeded quotes.  However, this time it's used in a more complex
manner:
[...]
-           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&
I'm tempted to say we should define

test_wc_l () {
	test $# = 0 || error "bug in test script: passing arguments to wc -l is not portable"
	wc -l | tr -d -c 0-9
}

just to avoid issues if any wc comes across and prints a tab for
padding or says "hi, the number of lines you wanted to know is: 42".



(Oddly, according to 'man 1p wc' here, the POSIXly correct format in
the absence of options is

  "%d %d %d %s\n", <newlines>, <words>, <bytes>, <file>

Taking it literally would mean no padding/alignment whatsoever.
Neither GNU wc on my Linux exactly conforms to this.)

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

[PATCH] t0090: be prepared that 'wc -l' writes leading blanks

From: Johannes Sixt <hidden>
Date: 2016-06-15 22:52:40

From: Johannes Sixt <redacted>

Use 'printf %d $(whatever|wc -l)' so that the shell removes the blanks
for us.

Signed-off-by: Johannes Sixt <redacted>
---
Am 12/14/2011 16:41, schrieb Johannes Sixt:
I'd solve it by moving the command substitution outside the quoted string:

 	printf "SHA (%d entries, 0 subtrees)\n" \
		$(git ls-files | wc -l) >expect &&

Other proposed solutions add another process. I don't like that on Windows ;)
And here is a proper patch to that effect.

-- Hannes

 t/t0090-cache-tree.sh |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index f972562..6c33e28 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -17,15 +17,13 @@ cmp_cache_tree () {
 # test-dump-cache-tree already verifies that all existing data is
 # correct.
 test_shallow_cache_tree () {
-	echo "SHA " \
-	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+	printf "SHA  (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >expect &&
 	cmp_cache_tree expect
 }
 
 test_invalid_cache_tree () {
 	echo "invalid                                   (0 subtrees)" >expect &&
-	echo "SHA #(ref) " \
-	    "($(git ls-files|wc -l) entries, 0 subtrees)" >>expect &&
+	printf "SHA #(ref)  (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect &&
 	cmp_cache_tree expect
 }
 
-- 
1.7.8.1499.g39f909
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help