Skip to contentSkip to conversation

t0090-cache-tree fails due to wc whitespace

PATCH In review Brian Gernhardt 6 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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:

filtered renamed from expect +1 -1
@@ -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

#1 lore raw
Quoted 5 lines · 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.

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

#2 lore raw
In reply to Brian Gernhardt
Quoted 4 lines · 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.

Oops.

Quoted 3 lines

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:

[...]

Review

Quoted 2 lines
(quoted diff) +1 -1
-           "($(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.)

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#3 lore raw
Quoted 7 lines · Thomas Rast

(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.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#4 lore raw
In reply to Brian Gernhardt

Am 12/14/2011 15:35, schrieb Brian Gernhardt:

Quoted 9 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

#5 lore raw

PATCH 1/1 t0090: be prepared that 'wc -l' writes leading blanks

From: Johannes Sixt <[email protected]>

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

---
Am 12/14/2011 16:41, schrieb Johannes Sixt:

Quoted 6 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(-)
t/t0090-cache-tree.sh +2 -4
index f972562..6c33e28 100755
@@ -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
 }
 
Signature · 1 line
1.7.8.1499.g39f909
Acked-by on v1 1/1
#6 lore raw

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

Sorry for slacking off on replying to this thread...

Quoted 6 lines · Johannes Sixt

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

---
Am 12/14/2011 16:41, schrieb Johannes Sixt:

Quoted 8 lines
Quoted 6 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.

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

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#7 lore raw
In reply to Brian Gernhardt
Quoted 6 lines · Brian Gernhardt

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

(quoted diff) +1 -1
-           "($(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/^ *//'.

Signature · 1 line
Hallvard