我如何在bash条件语句中检查`npm run test`的结果 [英] How do I check result of `npm run test` in bash conditional statement

查看:57
本文介绍了我如何在bash条件语句中检查`npm run test`的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署脚本,我只想在测试成功的情况下运行,但是我的条件语句 if ["$ VALID"]

I have a deploy script I only want to run if my test is successful but believe there is an issue with my conditional statement 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

如果成功,我的测试结果如下:

My test result if successful looks like:

> 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)

推荐答案

您可以执行以下操作:

VALID="$(npm test | grep -o 'failing')"

然后:

if [[ $VALID != "failing" ]] ...

另一种方法是选择"0通过":

Another way would be to pick up on " 0 passing":

VALID="$(npm test | grep -o '  0 passing')"

对于条件:

if [[ $VALID != "  0 passing" ]] ...

无论哪种情况,如果在返回的字符串中找到单词失败"或"0通过",则表明测试失败.关键是找到通过/失败输出所特有的内容.

In either case if the word(s) "failing" or " 0 passing" are found in the returned string it would indicate the test was a failure. The key is to find something unique to the passing/failure output.

注意:仔细研究输出;"0通过"字符串不是万无一失的,因为从技术上讲您可以进行10个测试... grep将看到"10通过",并把它误认为是失败.也许你不有这么多的测试,但一定要知道,或者包括导致空格的确切数目.

NOTE: Study the output carefully; The "0 passing" string is not fool proof, since you technically could have 10 tests... grep would see the "10 passing" and mistake it for a failure. Maybe you don't have that many tests, but definitely be aware of it, or include the exact number of spaces that are leading up to it.

这篇关于我如何在bash条件语句中检查`npm run test`的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆