首页 > 解决方案 > 从另一个 bash 函数运行 bats 测试会产生:`./main.sh: line 21: 1..3: command not found`

问题描述

语境

在安装后运行一些测试时,我注意到当我从同一文件中的不同函数bats运行测试文件时,测试会引发错误。直接使用以下命令执行测试时有效:batsmain.sh

./test/libs/bats/bin/bats test/test_install_git_postsetup.bats

run_prompt_user_choice()从在中执行的第一个函数开始main.sh。但是,当我复制该命令并将其放入时:

function run_some_test() {
    ./test/libs/bats/bin/bats test/test_install_git_postsetup.bats
}

run_prompt_user_choice()并使用 command:从内部调用该函数($run_some_tests),它会抛出 error: ./main.sh: line 21: 1..3: command not found

代码

的代码main.sh包括:

#!/bin/bash
. src/ask_user_choice.sh

function run_some_test() {
    ./test/libs/bats/bin/bats test/test_install_git_postsetup.bats
}

function run_prompt_user_choice() { 
    # install selected packages.
    $(install_user_choices)
    
    # test selected packages using a function.
    $(run_some_test)
    
    # Run the github test file directly
    ./test/libs/bats/bin/bats test/test_install_git_postsetup.bats
}
run_prompt_user_choice "$@"

输出

运行的输出sudo ./main.sh是:

./main.sh: line 21: 1..3: command not found
 ✓ running the apt update function in some file and verifying log output.
 ✓ running the apt upgrade function in some file and verifying log output.
 ✓ running the apt install git function in some file and verifying log output.

3 tests, 0 failures

问题

如何从函数调用的任意函数运行这些测试run_prompt_user_choice()

标签: bashfunctiontestingbats-core

解决方案


推荐阅读