I have this cucumber runner class:
#RunWith(Cucumber.class)
#CucumberOptions(plugin = {"pretty", "html:target/cucumber"},
features="classpath:features",
tags= {"#concrete"})
public class RunCuke {
}
This is the code available in git repository. While developing my own features, I want to use the same class but want to pass my own tags for it to run. But I don't want to modify this class (passing my own tag, say, #outline instead of #concrete), because this is a nuisance while committing code because each time I want to commit, I have to revert this file back to what is in the code repo (ie back to #concrete). Only way I think of is to pass the command line argument to this class.
So I am using this command line argument (program argument in the eclipse IDE):
-Dcucumber.options=”–tags #outline”
Unfortunately it is not working. It is not overriding what is there in #CucumberOptions.
How do I make it work?
You need to select 'Run Configurations' and go to the specific runner config. Go to the 'Environment' tab. Add a new variable 'cucumber.options' and set it to '--tag #outline'. You might need to switch the 'Append environment to native environment' and 'Replace native environment with specified environment', though I have found the default of 'append' works perfectly. Apply and Run.
Related
I am trying to start rascal with clair from the command line, however I do not understand why this happens:
java -cp "rascal-0.18.0.jar;clair_0.1.0.202005281059.jar;org.eclipse.cdt.core_6.11.0.202003081657.jar" org.rascalmpl.shell.RascalShell
Version: 0.18.0
INFO: detected |lib://rascal| at |jar+file:///C:/ws/rascal-0.18.0.jar!/|
INFO: detected |lib://clair| at |jar+file:///C:/ws/clair_0.1.0.202005281059.jar!/|
rascal>
But when the order of the jars changes, it fails:
java -cp "clair_0.1.0.202005281059.jar;rascal-0.18.0.jar;org.eclipse.cdt.core_6.11.0.202003081657.jar" org.rascalmpl.shell.RascalShell
Version: 0.18.0
INFO: detected |lib://clair| at |jar+file:///C:/ws/clair_0.1.0.202005281059.jar!/|
INFO: detected |lib://rascal| at |jar+file:///C:/ws/rascal-0.18.0.jar!/|
main function should either have one argument of type list[str], or keyword parameters
Usage: java -jar ...
Is this normal behavior with classpaths?
Note: The clair jar does not contain a org.rascalmpl.shell.RascalShell class.
Update: Removing from META-INF/RASCAL.MF the line:
Main-Function: main
Main-Module: lang::cpp::IDE
resolves the problem, so it seems to be an issue with Rascal (and a rascal function) and not with Java (and a Java function).
RascalShell's main function behaves differently if there are commandline parameters and differently depending on the first RASCAL.MF file it finds in the classpath.
If there is a parameter, then it loads that parameter as a module name and invokes the main function in that module, passing it the other commandline parameters
Otherwise it starts the REPL
But: if the first RASCAL.MF file it finds in the classpath has a Main-Module and a Main-Function, then it loads this module always and starts its main function.
I suspect the latter is at work: the order of the classpath changes which RASCAL.MF file is found and therefore the REPL does not start but some module is being loaded and not found. I'm not sure though, since I can't set a breakpoint from here ;-)
I think you have found two bugs in the REPL:
The second behavior is the bug that we try to run the Main-Function definition which is intended for the IDE plugin, also in the terminal. I think the issue/bug is that we use the same configuration for command line as ide integration point, we might need to add a seperate tag for this.
We should provide a way to say which RASCAL.MF we want to run, since now it just picks the first one it sees (rascal also has a RASCAL.MF file).
I have a Spring Boot project. In it I have a java class BookTests which contains:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"pretty",
"html:target/acceptance-test-report",
"json:target/cucumber/cucumber.json"
},
glue = {
"com.my.sdk.test",
"com.my.book.test"
}
,tags = {"#findConsideringGrants"}
)
public class BookTests {
// No implementation required. The Cucumber annotations drive the test execution.
}
I'm using Junit 4. when I "Debug As JUnit Test" this class it only runs the feature file which is annotated with "#findConsideringGrants (as it should)
Is there a way i don't have to hard-code this tags annotation in the java class but instead specify the tags as Program argumnents or VM arguuments?
I tried configuring it with Program argumnents, but with all the following attempts it ignored the argument and ran the tests on all my features/tags:
--tags findConsideringGrants
-tags findConsideringGrants
--tags #findConsideringGrants
-tags #findConsideringGrants
So to summerize, i'm looking for a way to
1) debug in eclipse a certain feature
2) without hardcoding the name of the feature into the java source. Somehow by just configuring the debug configuration.
is this even possible?
If you are using maven you can use -- mvn test -Dcucumber.options="--tags #findConsideringGrants".
Or you can use the cucumber cli command to run -- Main.main(new String[]{"-g", "classpath to step definition file","-t", "tags to use", "-p", "plugins to use", "Full path to feature file"}). The feature file path has to be the last option.
Running from eclipse --
Go to the "Environment" tab of the "Debug Configurations" or "Run Configurations" window. Click on "New" button. For the "Name" add "cucumber.options" and "Value" add "--tags #findConsideringGrants". Apply and Run. If you want to add a specific feature file path add only the path to the end of this.
I noticed that IntelliJ can parse .kts files as Kotlin and the code editor picks them up as free-floating Kotlin files. You are also able to run the script in IntelliJ as you would a Kotlin file with a main method. The script executes from top to bottom.
This form is PERFECT for the project I'm working on, if only I knew an easy way to use them from within Java or Kotlin.
What's the idiomatic way to "run" these scripts from Java or Kotlin?
Note that script files support in Kotlin is still pretty much experimental. This is an undocumented feature which we're still in the process of designing. What's working today may change, break or disappear tomorrow.
That said, currently there are two ways to invoke a script. You can use the command line compiler:
kotlinc -script foo.kts <args>
Or you can invoke the script directly from IntelliJ IDEA, by right-clicking in the editor or in the project view on a .kts file and selecting "Run ...":
KtsRunner
I've published a simple library that let's you run scripts from regular Kotlin programs.
https://github.com/s1monw1/KtsRunner
Example
The example class
data class ClassFromScript(val x: String)
The .kts file
import de.swirtz.ktsrunner.objectloader.ClassFromScript
ClassFromScript("I was created in kts")
The code to load the class
val scriptReader = Files.newBufferedReader(Paths.get("path/classDeclaration.kts"))
val loadedObj: ClassFromScript = KtsObjectLoader().load<ClassFromScript>(scriptReader)
println(loadedObj.x) // >> I was created in kts
As shown, the KtsObjectLoader class can be used for executing a .kts script and return its result. The example shows a script that creates an instance of the ClassFromScript type that is loaded via KtsObjectLoader and then processed in the regular program.
As of 2020 (Kotlin 1.3.70), you can just use the straightforward
kotlin script.main.kts
Note that using the file extension .main.kts instead of .kts seems to be important.
Note that for me this does not seem to run the main() function if defined, I had to add a manual call to main() at the top level.
One of the advantages of Kotlin script is the ability to declare code and dependencies inside a single file (with #file:DependsOn, see for example here)
early 2020ies kscript that you find at https://github.com/holgerbrandl/kscript
seems to be the most convenient and well supported way to go ...
jgo can fetch and run code from Maven repositories, so can be used to invoke
https://github.com/scijava/scijava-common and https://github.com/scripting-kotlin to execute a local Foo.kt like so:
jgo --repository scijava.public=maven.scijava.org/content/groups/public org.scijava:scijava-common:#ScriptREPL+org.scijava:scripting-kotlin Foo.kt
If no Foo.kt is provided, it launches a Kotlin REPL.
In most projects that use Spring extensively there are a few tests that use #IfProfileValue to mark it as integration test, performance test or similar. When you run these with maven you do something like this:
mvn install -Dperformance-test=true
for a class annotated like this:
#IfProfileValue(name = "performance-test", value = "true")
But if I run this test in IntelliJ I get:
Test '.Tests in Progress.MyTest' ignored
which I can go around in IntelliJ by commenting out the annotation, but I would prefer if the test just ran without having to remove the line, so I don't accidentally commit/push the class without the marker.
Is that possible?
Edit the run-configurations and set your parameter -Dperformance-test=true
as VM option.
See the IDEA Run Config for detailed info.
I'm using Grails 2.1.0, and I have a Groovy class that I've written that's not dependent on services, controllers, or any of the other Grails goodness. It uses some .jar libraries and other classes that are already in the Grails classpath.
I want to:
Run the Groovy class (or a Java class, it shouldn't mattter) use the other libraries/classes that Grails already has on its classpath (not services, not controllers, none of that).
Be able to access the command line arguments [this is required]
Does not require bootstrapping the entire Grails environment (I need the classpath obviously, but nothing else)
Ideally, I'd like to be able to do something like this:
java -classpath (I_HAVE_NO_IDEA_HOW_TO_DETERMINE_THIS) com.something.MyClass param1 param2 param3
Things I've already looked into:
Using "grails create-script" which results in a Gant script.
Using "grails run-script"
The first one (using a Gant script) seems horribly wrong to me. Using an Gant script as some sort of intermediary wrapper seems to require bootstrapping the whole Grails environment, plus I have to figure out how to get a reference to the actual class I want to call which seems to be difficult (but I Am Not A Gant Expert, so enlighten me). =)
The second one (using run-script) sort of works... I've used this approach to call service methods before, but it has two problems: first, it bootstraps the entire Grails environment; second, there does not appear to be any way to pass the command-line arguments easily.
Really, I just want the stuff in the classpath (and my command-line parameters) and to be able to call the main() method of my class with minimial frustration. That being said, if you can come up with a working example of any sort that solves the issue (even if it involves some intermediary Gant or other class) I'll be happy to use that approach.
Thanks.
Update: A solution that works with a Gant task, still open to better ideas if anyone has any...
In scripts/FooBar.groovy
includeTargets << grailsScript("_GrailsInit")
target(main: "Runs a generic script and passes parameters") {
def myclass = classLoader.loadClass('com.whatever.scripting.GenericRunScript')
myclass.execute(args);
}
setDefaultTarget(main)
In src/groovy/com/whatever/scripting/GenericRunScript.groovy
package com.whatever.scripting
class GenericRunScript {
public static execute(def args) {
println "args=" + args.inspect()
}
}
Then from the command line, at while in the root directory of the Grails project:
$ grails compile
| Environment set to development.....
| Compiling 2 source files.
$ grails foo-bar test one two
| Environment set to development....
args='test\none\ntwo'
Note 1: When I first did this, I kept forgetting the compile statement, so I added that in.
Note 2: Yes, the args are separated by carriage returns; fixing that is left as an exercise to the reader.
The way described above would work but all grails facility will be gone including domains and dependencies.
If you require everything that you have defined in your grails project, the run-script command will do the trick
grails run-script [path to your groovy file]
http://grails.org/doc/latest/ref/Command%20Line/run-script.html
As described in http://grails.org/doc/latest/guide/commandLine.html, you can include targets _GrailsClasspath and _GrailsArgParsing, and whatever else you need. For example, if you want to parse command-line arguments without creating a second script:
$ grails create-script ArgsScript
| Created file scripts/ArgsScript.groovy
Now edit the script scripts/ArgsScript.groovy as follows:
includeTargets << grailsScript("_GrailsArgParsing") // grailsScript("_GrailsInit")
target(main: "The description of the script goes here!") {
println argsMap
for (p in argsMap['params'])
println p
}
setDefaultTarget(main)
See the result:
$ grails args-script one two three=four
| Environment set to development....
[params:[one, two, three=four]]
one
two
three=four
Update: well, it is not as easy as I thought. Basically, you can either run a script as a Gant task, e.g. by doing grails myscript, or as a script, e.g. by doing grails run-script src/groovy/MyScript.groovy. In the first case you have access to parameters, as I already explained, but you still miss some of the Grails environment, which is, perhaps, a bug. For example, you can't really access scripts or classes defined in src/groovy/ from a Gant task. On the other hand, as was already discussed, if you use run-script, you can't get the arguments.
However, you can use System.getProperty to pass command-line arguments with the -Dproperty=value syntax. Also see Java system properties and environment variables