简体   繁体   English

Makefile 命令在子文件夹中运行反应测试套件

[英]Makefile command to run react test suite in child folder

I'm trying to create a Makefile that runs the test suite for my react app.我正在尝试创建一个 Makefile 来运行我的反应应用程序的测试套件。 From the root folder the react app is located at frontend/ .从根文件夹中,react 应用程序位于frontend/

What make command can I create the achieve this?我可以创建什么命令来实现这一点? I've tried the following but no luck:我尝试了以下但没有运气:

test-frontend:
    cd ./frontend/
    npm test

test-frontend:
    npm test ./frontend/

test-frontend:
    cd ./frontend/
    npm run-script test

Here's the error I get:这是我得到的错误:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /path/to/dir/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/path/to/dir/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /$HOME/.npm/_logs/2019-10-24T02_23_23_195Z-debug.log
make: *** [test-frontend] Error 254

The comments answered my question.评论回答了我的问题。 The answer is:答案是:

test-frontend:
    cd ./frontend/ && npm run-script test

Or if you want to use separate lines for readability:或者,如果您想使用单独的行来提高可读性:

test-frontend:
    cd ./frontend/ && \
    npm run-script test

You are trying to do things for which a Makefile is not designed for.您正在尝试做 Makefile 不适合做的事情。 You already got bitten by one of Makefile caveats: each command line is run on it's own shell.您已经被 Makefile 警告之一咬住了:每个命令行都在它自己的 shell 上运行。

The rule of thumb is: a recipe of a rule should create a filename named like the target of the rule.经验法则是:规则的配方应该创建一个命名为规则目标的文件名。

Here is a rule (to clarify the terms):这是一个规则(澄清条款):

target:
  recipe command lines
  should create file named target

There are some exceptions to this rule of thumb.这个经验法则有一些例外。 Most notably make clean and make install .最值得注意的是make cleanmake install Both typically do not create files named clean or install .两者通常都不会创建名为cleaninstall的文件。 One can argue that make test can also be an exception to this rule of thumb.有人可能会争辩说make test也可以是这一经验法则的一个例外。

You only stumbled upon the change directory caveat.您只是偶然发现了更改目录警告。 Which is a common caveat to stumble when writing makefiles.在编写 makefile 时,这是一个常见的警告。 It is even mentioned in the manual: https://www.gnu.org/software/make/manual/html_node/Execution.html .手册中甚至提到: https://www.gnu.org/software/make/manual/html_node/Execution.html But keep in mind this is only one of many caveats that make has.但请记住,这只是 make 的许多警告之一。

If this is the extend of your (ab)use of makefile to run commands then all maybe well.如果这是您(ab)使用 makefile 运行命令的扩展,那么一切可能都很好。 But if you want to expand the functionality and you start to stumble from caveat to caveat then it is time to rethink your approach.但是,如果您想扩展功能并且开始从一个警告到另一个警告,那么是时候重新考虑您的方法了。 Either extract the recipe to its own script or write a wrapper script around your makefile.要么将配方提取到它自己的脚本中,要么在你的 makefile 周围编写一个包装脚本。 And if you are thinking about adding argument handling to your makefile then read this first: Passing arguments to "make run"如果您正在考虑为您的 makefile 添加参数处理,请先阅读以下内容:将 arguments 传递给“make run”

Ironically you also use the command execution feature of npm: npm run-script .具有讽刺意味的是,您还使用了 npm 的命令执行功能: npm run-script That certainly has its own set of caveats.这当然有它自己的一套警告。 I do not know because I have not used npm that much.我不知道,因为我没有用过 npm 那么多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM