GitHub Action检查Playwright测试test.only时无匹配也失败的原因
问题原因与解决方案
问题原因
grep命令的特性是:匹配到目标内容时返回0(成功),未匹配到则返回非0(失败)。
你当前的命令用&&串联操作:
grep -r --include '*.spec.ts' 'test.only' . && echo 'Found at least one ONLY in tests' && exit 1
- 存在
test.only时,grep返回0,后续echo和exit 1执行,Action按预期失败; - 不存在
test.only时,grep直接返回非0退出码,后续操作不执行,但GitHub Action会将整个步骤的退出码判定为失败,导致流程终止。
修复方案
调整命令逻辑,让grep未匹配到内容时返回0(使Action步骤通过),匹配到时才触发失败。
方案1:用if语句实现清晰逻辑
- name: Check that no only is present run: | if grep -r --include '*.spec.ts' 'test.only' .; then echo 'Found at least one ONLY in tests' exit 1 fi
方案2:单行简洁写法
- name: Check that no only is present run: grep -r --include '*.spec.ts' 'test.only' . && echo 'Found at least one ONLY in tests' && exit 1 || exit 0
修改后效果:
- 存在
test.only时,grep返回0,执行echo和exit 1,Action失败; - 不存在时,
grep返回非0,触发|| exit 0,整个步骤返回0,Action正常通过。
内容的提问来源于stack exchange,提问作者Chen Peleg
相关产品推荐
相关产品推荐

