threads / discuss / 29164

t0090-cache-tree fails due to wc whitespace

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

## tl;dr

8 messages between Dec 14, 2011 and Dec 20, 2011.

replies: 7people: 6as markdown or json

Brian Gernhardt· Dec 14, 2011, 14:35 UTC · lore
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
Stefano Lattarini· Dec 14, 2011, 14:57 UTC · re: Brian Gernhardt · lore

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

On Wednesday 14 December 2011, Brian Gernhardt wrote:
Show 5 quoted lines
> 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
Hallvard Breien Furuseth· Dec 14, 2011, 15:09 UTC · re: Brian Gernhardt · lore

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

Brian Gernhardt writes:
Show 6 quoted lines
> 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
Johannes Sixt· Dec 14, 2011, 15:41 UTC · re: Brian Gernhardt · lore

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

Am 12/14/2011 15:35, schrieb Brian Gernhardt:
Show 9 quoted lines
> 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
Johannes Sixt· Dec 20, 2011, 08:24 UTC · re: Johannes Sixt · lore

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

From: Johannes Sixt <j6t@kdbg.org>

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

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
Am 12/14/2011 16:41, schrieb Johannes Sixt:
Show 6 quoted lines
> 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
Thomas Rast· Dec 20, 2011, 09:17 UTC · re: Johannes Sixt · lore

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

Sorry for slacking off on replying to this thread...
Johannes Sixt <j.sixt@viscovery.net> writes:
Show 14 quoted lines
> Use 'printf %d $(whatever|wc -l)' so that the shell removes the blanks
> for us.
>
> Signed-off-by: Johannes Sixt <j6t@kdbg.org>
> ---
> 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.
Acked-by: Thomas Rast <trast@student.ethz.ch>

This is the best solution also because it the formatting more (instead of less) readable.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Thomas Rast· Dec 14, 2011, 15:43 UTC · re: Brian Gernhardt · lore

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

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
Andreas Schwab· Dec 14, 2011, 15:54 UTC · re: Thomas Rast · lore

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

Thomas Rast <trast@student.ethz.ch> writes:
Show 7 quoted lines
> (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.)

A space in the format string stands for one or more <blank>s. If only a single <space> is allowed the standard uses 𝚫.

Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

← back to recent threads