蝙蝠测试命令输出未完全捕获

Bats testing command output not completely captured

上下文

在 bash 中使用 bats 测试 keepass2 安装过程时,我注意到终端中命令的输出与 [= 中捕获的输出不同14=] 测试。在终端命令 keepass2 --version returns:

Gtk-Message: 15:29:44.377: Failed to load module "canberra-gtk-module"
KeePass 2.45
Copyright © 2003-2020 Dominik Reichl

而以下测试的测试输出:

@test "Checking keepass2 version responds with Failed to load module canberra-gtk-module." {
    COMMAND_OUTPUT=$(keepass2 --version)

    EXPECTED_OUTPUT='Gtk-Message: 15:29:44.377: Failed to load module "canberra-gtk-module"
KeePass 2.45
Copyright © 2003-2020 Dominik Reichl'

    assert_equal "$COMMAND_OUTPUT" "$EXPECTED_OUTPUT"
}

是:

✗ Checking keepass2 version responds with: Failed to load module canberra-gtk-module.
   (from function `assert_equal' in file test/post_setup/../../test/libs/bats-assert/src/assert.bash, line 91,
    in test file test/post_setup/test_apt_install_keepass2_postsetup.bats, line 68)
     `assert_equal "$COMMAND_OUTPUT" "$EXPECTED_OUTPUT"' failed
   Gtk-Message: 15:51:27.775: Failed to load module "canberra-gtk-module"
   /home/a/Documents/git/Productivity-setup/test/post_setup/../../test/libs/bats-assert/src/assert.bash: line 65: [: KeePass 2.45
   Copyright © 2003-2020 Dominik Reichl: binary operator expected
   
   -- values do not equal --
   expected (3 lines):
     Gtk-Message: 15:29:44.377: Failed to load module "canberra-gtk-module"
     KeePass 2.45
     Copyright © 2003-2020 Dominik Reichl
   actual (2 lines):
     KeePass 2.45
     Copyright © 2003-2020 Dominik Reichl
   --

所以 bats 测试认为命令的输出只包含同一命令的命令行输出的最后两行,而我希望蝙蝠捕获我的全部 3 行输出在命令行中看到 运行 keepass2 --version.

问题

如何在 bats 测试中捕获命令 keepass2 --version 的命令行中显示的 Gtk-Message: 15:29:44.377: Failed to load module "canberra-gtk-module" 行?

备注

这个问题不是关于如何解决 canberra-gtk-module 警告的 xy 问题。它也不是关于如何在抛出警告 canberra 警告时通过测试。这个问题是关于如何在 bats 测试中从 version 命令中捕获警告。

这一行可能不是 stdout,而是 stderr

Gtk-Message: 15:29:44.377: Failed to load module "canberra-gtk-module"

此行仅将 stdout 捕获到 变量

COMMAND_OUTPUT=$(keepass2 --version)

EXPECTED_OUTPUT 中删除第一行应该可以解决问题。

(该错误对检查版本无关紧要,测试时可忽略)


捕获标准输出标准错误

COMMAND_ERR_OUT=$(keepass2 --version 2>&1)

只捕获标准错误

COMMAND_ERR=$(keepass2 --version 2>&1 >/dev/null)