简体   繁体   English

为grunt插件编码一些测试时,如何动态更改grunt.tasks(…)使用的配置

[英]When coding some tests for a grunt plugin, how to alter dynamically the configuration used by grunt.tasks(…)

I writing a grunt plugin. 我写了一个咕unt的插件。 In my tests written with vows I want to alter some config object defined in my gruntfile before calling 在用誓言编写的测试中,我想在调用之前更改在gruntfile中定义的一些配置对象

grunt.tasks(["my_task"], {}, function(){
    // do something}
)

The goal is to execute some task I have defined in my gruntfile several times with different configurations. 目标是使用不同的配置多次执行我在gruntfile中定义的某些任务。

I can't use a classic nodeunit approach because the config I want to alter is not the config of my task. 我不能使用经典的nodeunit方法,因为我要更改的配置不是我任务的配置。 Plus I've got some issues with nodeunit launching grunt tasks. 另外,我在启动单位任务时遇到了一些问题。

I have something like this : 我有这样的事情:

grunt.initConfig({
    object_I_want_to_alter:{...},
    the_task_I_call:{...}
})

It seems there is no real solution to drive grunt from the API as it is a command-line tool. 似乎没有真正的解决方案可以从API发出咕unt声,因为它是一种命令行工具。 There is nonetheless two API methods which could help : grunt.util.spawn and grunt.tasks 尽管如此,仍有两种API方法可以提供帮助:grunt.util.spawn和grunt.tasks

Both seems to be black box which read the gruntfile when called. 两者似乎都是黑匣子,调用时会读取gruntfile。

If I try to do a first call to grunt.tasks() to preload the config, I can alter my object, but the second call to grunt.tasks() seems to reload entirely the gruntfile and doesn't use the previous config. 如果我尝试对grunt.tasks()进行第一次调用以预加载配置,则可以更改我的对象,但是对grunt.tasks()的第二次调用似乎完全重新加载了gruntfile,并且不使用先前的配置。

Does someone has some insight on this or another way to accomplish that goal ? 有人对实现该目标的这种或其他方式有一些见识吗?

Thanks. 谢谢。

OK, Found a solution. 好的,找到了解决方案。

This answer from a grunt committer was in my head for some time but I really understand it just now. 来自咕unt咕commit作响的提交人的答案在我脑海中已经存在了一段时间,但我现在才真正理解。

Indeed, if you remove the gruntfile and do everything from your tests by using the hack first, you can then play with your configuration all you want. 确实,如果您删除了gruntfile并首先通过使用hack完成了测试中的所有操作,那么您就可以随心所欲地进行配置了。

Thanks to Kyle Robinson Young for the info. 感谢Kyle Robinson Young提供的信息。

It would be great to have more intel on this on the grunt website though. 最好在grunt网站上拥有更多有关此的信息。

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

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