karma-runner / maven-karma-plugin

Maven plugin for running tests using Karma.
Apache License 2.0
45 stars 26 forks source link

Is it possible to force Karma to use a local node install, instead of the globally installed node? #28

Open xavierhaurie opened 8 years ago

xavierhaurie commented 8 years ago

See the following: https://github.com/karma-runner/karma/issues/1975

I would like to achieve the same thing from the maven-karma-plugin

Thanks!

asa-git commented 8 years ago

The following is working fine on a project i have here:

<configuration>
  ...
   <karmaExecutable>SOME/WHERE/node.exe SOME/WHERE/ELSE/node_modules/karma/bin/karma</karmaExecutable>
  ...
</configuration>
xavierhaurie commented 8 years ago

@asa-git Thank you. I tried that but no cigar.

pom.xml:

<plugin>
                <groupId>com.kelveden</groupId>
                <artifactId>maven-karma-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- this configuration emulates running this on the command line: -->
                    <!--./target/node/node ./node_modules/.bin/karma start karma.conf.for.maven.build.js-->
                    <karmaExecutable>${basedir}/target/node/node ${basedir}/node_modules/.bin/karma</karmaExecutable>
                    <!--<karmaExecutable>target/node/node node_modules/.bin/karma</karmaExecutable>-->
                    <configFile>karma.conf.for.maven.build.js</configFile>
                </configuration>
            </plugin>

when running mvn install I get the following output:

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
...
...
...
[INFO] --- maven-karma-plugin:1.6:start (default) @ protocol-review-web ---
[INFO] Executing Karma Test Suite ...
/Users/xavier/IdeaProjects/protocol-review/web/target/node/node /Users/xavier/IdeaProjects/protocol-review/web/node_modules/.bin/karma start /Users/xavier/IdeaProjects/protocol-review/web/karma.conf.for.maven.build.js --single-run
 ...
...
...
[ERROR] Failed to execute goal com.kelveden:maven-karma-plugin:1.6:start (default) on project protocol-review-web: There was an error executing Karma. Cannot run program "/Users/xavier/IdeaProjects/protocol-review/web/target/node/node /Users/xavier/IdeaProjects/protocol-review/web/node_modules/.bin/karma" (in directory "/Users/xavier/IdeaProjects/protocol-review/web"): error=2, No such file or directory -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.kelveden:maven-karma-plugin:1.6:start (default) on project protocol-review-web: There was an error executing Karma.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: There was an error executing Karma.
    at com.kelveden.karma.StartMojo.createKarmaProcess(StartMojo.java:247)
    at com.kelveden.karma.StartMojo.execute(StartMojo.java:164)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
Caused by: java.io.IOException: Cannot run program "/Users/xavier/IdeaProjects/protocol-review/web/target/node/node /Users/xavier/IdeaProjects/protocol-review/web/node_modules/.bin/karma" (in directory "/Users/xavier/IdeaProjects/protocol-review/web"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at com.kelveden.karma.StartMojo.createKarmaProcess(StartMojo.java:243)
    ... 23 more
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 24 more
...
...
...

but if I run the same command line manually, it works!

CTSCI-MBP15-32-A6:protocol-review xavier$ /Users/xavier/IdeaProjects/protocol-review/web/target/node/node /Users/xavier/IdeaProjects/protocol-review/web/node_modules/.bin/karma start /Users/xavier/IdeaProjects/protocol-review/web/karma.conf.for.maven.build.js --single-run
09 03 2016 16:52:29.456:DEBUG [config]: autoWatch set to false, because of singleRun
09 03 2016 16:52:29.462:DEBUG [plugin]: Loading plugin karma-jasmine.
09 03 2016 16:52:29.466:DEBUG [plugin]: Loading plugin karma-chrome-launcher.
09 03 2016 16:52:29.477:DEBUG [plugin]: Loading plugin karma-firefox-launcher.
...
...
...
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 7 of 7 SUCCESS (0.014 secs / 0.004 secs)
09 03 2016 16:52:30.707:DEBUG [karma]: Run complete, exiting.
09 03 2016 16:52:30.707:DEBUG [launcher]: Disconnecting all browsers
09 03 2016 16:52:30.712:DEBUG [reporter.junit]: JUnit results written to "/Users/xavier/IdeaProjects/protocol-review/web/target/karma-maven-reports/junit/TESTS-PhantomJS_2.1.1_(Mac_OS_X_0.0.0).xml".
09 03 2016 16:52:30.723:DEBUG [launcher]: Process PhantomJS exited with code 0
09 03 2016 16:52:30.723:DEBUG [temp-dir]: Cleaning temp dir /var/folders/01/5q29l_fx1_1cwnnf5wl7mg380000gp/T/karma-58658118
09 03 2016 16:52:30.733:DEBUG [launcher]: Finished all browsers
CTSCI-MBP15-32-A6:protocol-review xavier$ 

I am stumped.

asa-git commented 8 years ago

here is an extract from the pom I updated to test your issue (I'm on windows)

<configuration>
        <karmaExecutable>C:/apps/nodejs/4.2.x64/node.exe ${basedir}/node_modules/karma/bin/karma</karmaExecutable>        
        <configFile>src/test/resources/conf/karma.conf.js</configFile>
</configuration>

Note that I'm not referring to the karma script in the node_modules/.bin but the one directly present in karma: node_modules/karma/bin/karma

Also, the generation of the path to execute is handled here: https://github.com/karma-runner/maven-karma-plugin/blob/master/src/main/java/com/kelveden/karma/KarmaUtils.java#L76 You might have to refer to node with the absolute path (directly or via the maven basedir placeholder as seen in the example above)

Hopefully that will fix your issue in your mac env.

silk-bahamut commented 8 years ago

It seems to be a problem from Unix with ProcessBuilder : http://stackoverflow.com/questions/33000007/processbuilder-no-such-file-or-directory-but-runtime-exec-works

On windows, no problems, but on linux This do not works, because a whitespace is present <karmaExecutable>${basedir}/target/node/node ${basedir}/node_modules/.bin/karma</karmaExecutable> And this works, since no whitespace is present <karmaExecutable>${basedir}/target/node/node node_modules/.bin/karma</karmaExecutable>

So for now on linux, using a local karma is possible, but not using a local node and a local karma

If we want it to work, osAgnosticPathToExe should be split before given to the ProcessBuilder https://github.com/karma-runner/maven-karma-plugin/blob/master/src/main/java/com/kelveden/karma/KarmaUtils.java#L84

Gugic commented 8 years ago

@kelveden Any updates on this?

kelveden commented 8 years ago

Sorry folks, I'd almost forgotten that this project even existed. It has been years since I even used it let alone looked at the code. I barely use maven these days. So I'm not in a good position to comment on the above, sorry. I'd suggest either forking and fixing. Alternatively, something I should have done years ago was to hand over control of this project to someone with more use for it - assuming that there is still the interest in it. If that's anyone here then let me know. Have added a notice to the README explaining as much.

silk-bahamut commented 8 years ago

I have post a pull request with the correction I am using locally. The only thing missing is merging and releasing to central maven

If you want to find about it : https://github.com/silk-bahamut/maven-karma-plugin

asa-git commented 8 years ago

If @silk-bahamut wants to take over, seems fair as he has a PR pending. If he can't or doesn't want to, I can I suppose. This plugin has very little need of maintenance anyway. Best regards.

moose-byte commented 6 years ago

To anyone that is still using this plugin and has this problem, I recommend the frontend-maven-plugin. You can replace this functionality and choose the node/npm runtime to execute with.

https://github.com/eirslett/frontend-maven-plugin#running-karma