启动服务器,运行测试,停止服务器

时间:2014-10-20 22:49:39

标签: makefile exitstatus

我有一个Makefile目标,如下所示

integration-test: git-hooks
    java -Djava.library.path=$$(pwd)/test/integration/lib/DynamoDBLocal_lib \
        -Djava.util.logging.config.file=/dev/null \
        -Dorg.eclipse.jetty.LEVEL=WARN \
        -Dlog4j.com.amazonaws.services.dynamodbv2.local.server.LocalDynamoDBServerHandler=OFF \
        -jar $$(pwd)/test/integration/lib/DynamoDBLocal.jar \
        -inMemory \
        -port 8000 &
    sleep 3
    ./node_modules/.bin/mocha --compilers coffee:coffee-script/register \
        --reporter spec \
        test/integration/main.coffee
    ps -ef | grep [D]ynamoDBLocal_lib | awk '{print $$2}' | xargs kill

这就是我正在做的事情:

  • Java命令启动Amazon的DynamoDB的本地实例。
  • 我开始3秒钟
  • 我运行集成测试
  • 我杀了数据库

我想要的是杀死数据库,无论测试是否通过。 为此,我想我需要test命令的退出状态并返回,如果测试失败或者它们是否成功。

正在发生的事情是,如果测试通过数据库被正确杀死,如果测试失败则不是。

我已经阅读了in the docs你可以在-前面加一个命令,让make忽略它,如果它产生非零退出状态,如果我这样做就会出现问题是因为我不知道测试是否失败,因为$?将始终返回0。

这种情况下的常规做法是什么?如果这解决了我的问题,我可以将目标分成更多目标。

谢谢。

1 个答案:

答案 0 :(得分:2)

您必须在一个shell中运行整个事情,这意味着您需要使用命令分隔符(例如;)和反斜杠来连接这些行。然后你可以存储结果并随之退出:

integration-test: git-hooks
        { java -Djava.library.path=$$(pwd)/test/integration/lib/DynamoDBLocal_lib \
            -Djava.util.logging.config.file=/dev/null \
            -Dorg.eclipse.jetty.LEVEL=WARN \
            -Dlog4j.com.amazonaws.services.dynamodbv2.local.server.LocalDynamoDBServerHandler=OFF \
            -jar $$(pwd)/test/integration/lib/DynamoDBLocal.jar \
            -inMemory \
            -port 8000 & }; \
        sleep 3; \
        ./node_modules/.bin/mocha --compilers coffee:coffee-script/register \
            --reporter spec \
            test/integration/main.coffee; \
        r=$$?; \
        ps -ef | grep [D]ynamoDBLocal_lib | awk '{print $$2}' | xargs kill; \
        exit $$r

但是,如果您使用单个shell,实际上可以做得更好,只需删除您想要的确切过程而不是使用ps

integration-test: git-hooks
        { java -Djava.library.path=$$(pwd)/test/integration/lib/DynamoDBLocal_lib \
            -Djava.util.logging.config.file=/dev/null \
            -Dorg.eclipse.jetty.LEVEL=WARN \
            -Dlog4j.com.amazonaws.services.dynamodbv2.local.server.LocalDynamoDBServerHandler=OFF \
            -jar $$(pwd)/test/integration/lib/DynamoDBLocal.jar \
            -inMemory \
            -port 8000 & }; \
        pid=$$!; \
        sleep 3; \
        ./node_modules/.bin/mocha --compilers coffee:coffee-script/register \
            --reporter spec \
            test/integration/main.coffee; \
        r=$$?; \
        kill $$pid; \
        exit $$r