I write a lot of small bash scripts and almost never test them. This week I added two tests with bats and found a quoting bug that would have eaten a filename with spaces.
The tests aren't clever: make a temp dir, run the script, assert output. Ten lines.
I won't pretend this makes bash pleasant, but a couple of tests turn it from 'hope it works' into 'probably works'.
Top comments (1)
用临时目录加断言输出这个做法很对,十行测试就能把“希望能跑”变成“大概率能跑”。带空格文件名这个引号 bug 也很典型,平时手动跑一次很难覆盖到。我自己会再补一个非零退出码的用例,防止脚本中途失败还被当成成功。