简体   繁体   中英

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

I writing a grunt plugin. In my tests written with vows I want to alter some config object defined in my gruntfile before calling

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.

I can't use a classic nodeunit approach because the config I want to alter is not the config of my task. 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. There is nonetheless two API methods which could help : grunt.util.spawn and grunt.tasks

Both seems to be black box which read the gruntfile when called.

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.

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.

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.

Thanks to Kyle Robinson Young for the info.

It would be great to have more intel on this on the grunt website though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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