背景
我正在进行对CI构建的更改,触发命令运行我的Xcode单元测试输出。这些测试记录了大量冗长的信息,以至于超过了4 MB的日志捕获限制,导致我的构建被终止。据我所知,没有办法使其变得不那么冗长(我想到的一个方法需要更改运行测试的命令,我正在进行中)。
我的解决方法
所以我决定聪明一点,尝试使用 sed
过滤我的输出,就像这样:
test_running_command | sed '/xctest\[/d; /^$/d'
当我在文件上运行sed
命令时,它按预期过滤掉包含xctest [
和空行的行。但是当我将其纳入到我的CI构建中时,我看到输出流到一定点,然后就停止了。在10分钟后,无论如何都会杀死我的CI构建,在我还没有机会达到4 MB限制之前。
问题
为什么sed
会出现这种情况?
已执行的故障排除
I tried using
awk
, like so, which similarly hangs.test_running_command | awk '$0 !~ /xctest\[/ && $0 !~ /^$/ {print}'
I tried a command from this answer to turn on line buffering for the original command, which just hung at a different place.
script -q /dev/null test_running_command | <sed or awk command from above>
As suggested by @CharlesDuffy in a comment, I used
tee
to write to file what goes into the pipe, and determined that the left side of the pipe definitely gets much further than the right. I observed (on my local machine) that while the output to the console was frozen, before_filter.txt was continuing to progress. This is the line I used:test_running_command | tee before.txt | sed '/xctest\[/d; /^$/d' | tee after.txt
As suggested by @LuisMuñoz in a comment, I tried using stdbuf (after installing with
brew install coreutils
) to disable buffering into and out ofsed
. I didn't see a difference in behavior. Output still froze at an arbitrary point.test_running_command | gstdbuf -i0 -o0 sed '/xctest\[/d; /^$/d'
test_running_command | tee before_filter | sed ... | tee after_filter
- Charles Duffysh -c 'test_running_command | sed "/xctest\[/d; /^\$/d"'
? - Michael Backawk
之前加上stdbuf -i0 -o0
,请参考这个答案。 - LMC