我有一个部署脚本我只想在测试成功的情况下运行但是我认为条件语句存在问题if [ "$VALID" ]
#!/bin/bash
# install dependencies
echo 'INSTALLING YARN'
npm install yarn -g
echo "INSTALLING DEPENDENCIES"
yarn install
echo "TESTING"
VALID="$(npm test)"
if [ "$VALID" ]
then
# ZIP up the code
echo 'INSTALLING ZIP'
apt-get update
echo "y" | apt-get install zip
echo 'ZIPPING'
zip -r ./Lambda-Image-Compression.zip index.js node_modules
# install aws cli so we can deploy code
echo 'INSTALLING PIP'
# echo "y" | apt-get install python-pip
echo "y" | apt-get install python-pip python-dev build-essential
echo "y" | pip install --upgrade pip
# echo "y" | sudo pip install --upgrade virtualenv
echo 'INSTALLING AWSCLI'
pip install awscli
# Copy config file to root so AWS config & credentials are set
echo 'MAKING AWS CREDENTIALS'
CREDENTIALS="[default]
aws_access_key_id = $AWS_ACCESS_KEY_ID
aws_secret_access_key = $AWS_SECRET_ACCESS_KEY"
touch .aws/credentials
"$CREDENTIALS" > .aws/credentials
echo "MOVING AWS CONFIG"
cp -R .aws ~/.
# Upload to AWS
echo 'UPDATING LAMBDA FUNCTION'
aws lambda update-function-code \
--function-name resizeHandler \
--zip-file fileb://Lambda-Image-Compression.zip \
--region ap-southeast-2
fi
如果成功,我的测试结果如下:
> Lambda-Image-Compression@1.0.0 test
> mocha
myLambda
RUNNING OPTIMSATION
download
downloadImage: 69.381ms
End of step null
✓ Should move testImage.png from srcBucket to dstBucket and return true (286ms)
1 passing (299ms)
失败显示以下内容:
TEST RESULT:
> Lambda-Image-Compression@1.0.0 test
> mocha
myLambda
RUNNING OPTIMSATION
download
1) Should move testImage.png from srcBucket to dstBucket and return true
0 passing (22ms)
1 failing
1) myLambda Should move testImage.png from srcBucket to dstBucket and return true:
ReferenceError: s3 is not defined
at download (index.js:32:4)
at nextTask (node_modules/async/dist/async.js:5273:14)
at Object.waterfall (node_modules/async/dist/async.js:5283:5)
at exports.handler (index.js:24:8)
at error (node_modules/lambda-tester/lib/runner.js:151:25)
at Promise.resolve.then (node_modules/lambda-tester/lib/runner.js:138:24)
答案 0 :(得分:2)
您可以采取以下措施:
VALID="$(npm test | grep -o 'failing')"
然后:
if [[ $VALID != "failing" ]] ...
另一种方法是接受“0传递”:
VALID="$(npm test | grep -o ' 0 passing')"
对于条件:
if [[ $VALID != " 0 passing" ]] ...
在任何一种情况下,如果在返回的字符串中找到单词“失败”或“0通过”,则表示测试失败。关键是找到传递/失败输出的独特之处。
注意:仔细研究输出; “0传递”字符串不是万无一失的,因为你在技术上可以进行10次测试...... grep会 看到“10路过去”并将其误认为失败。也许你没有 有那么多的测试,但绝对要注意它,或包括 确切的空格数量。