My groovy script calls other commands via vagrant. One of those commands is to echo some quotes on a file within docker.
The goal is, so that within the container, i want to have BB_GENERATE_MIRROR_TARBALLS = "1". Now to do this on a bash script, i would need something like this:
BB_GENERATE_MIRROR_TARBALLS = \"1\"
The issue manifests itself when i have to escape double quotations on the groovy as well.
If i call vagrant("echo BB_GENERATE_MIRROR_TARBALLS = \\\"1\\\" >> ${yoctoDir}/build/conf/local.conf" on my groovy file, the outcome on the local.conf will be BB_GENERATE_MIRROR_TARBALLS=1 (without quotes).
The correct way to do this would be to include an extra backslash on both sides (3 for the groovy, 1 for the bash script), however when i do that, groovy doesnt run and gives me syntax errors.
What would be the correct way to insert this literal string(BB_GENERATE_MIRROR_TARBALLS=\"1\") on the groovy?
In groovy you can do the following:
def my_var = /BB_GENERATE_MIRROR_TARBALLS = "1"/
echo my_var >> ${yoctoDir}/build/conf/local.conf
Related
I'm trying to execute bash script using karate. I'm able to execute the script from karate-config.js and also from .feature file. I'm also able to pass the arguments to the script.
The problem is, that if the script fails (exits with something else than 0) the test execution continues and finishes as succesfull.
I found out that when the script echo-es something then i can access it as a result of the script so I could possibly echo the exit value and do assertion on it (in some re-usable feature), but this seems like a workaround rather than a valid clean solution. Is there some clean way of accessing the exit code without echo-ing it? Am I missing on something?
script
#!/bin/bash
#possible solution
#echo 3
exit 3;
karate-config.js
var result = karate.exec('script.sh arg1')
feture file
def result = karate.exec('script.sh arg1')
Great timing. We very recently did some work for CLI testing which I am sure you can use effectively. Here is a thread on Twitter: https://twitter.com/maxandersen/status/1276431309276151814
And we have just released version 0.9.6.RC4 and new we have a new karate.fork() option that returns an instance of Command on which you can call exitCode
Here's an example:
* def proc = karate.fork('script.sh arg1')
* proc.waitSync()
* match proc.exitCode == 0
You can get more ideas here: https://github.com/intuit/karate/issues/1191#issuecomment-650087023
Note that the argument to karate.fork() can take multiple forms. If you are using karate.exec() (which will block until the process completes) the same arguments work.
string - full command line as seen above
string array - e.g. ['script.sh', 'arg1']
json where the keys can be
line - string (OR)
args - string array
env - optional environment properties (as JSON)
redirectErrorStream - boolean, true by default which means Sys.err appears in Sys.out
workingDir - working directory
useShell - default false, auto-prepend cmd /c or sh -c depending on OS
And since karate.fork() is async, you need to call waitSync() if needed as in the example above.
Do provide feedback and we can tweak further if needed.
EDIT: here's a very advanced example that shows how to listen to the process output / log, collect the log, and conditionally exit: fork-listener.feature
Another answer which can be a useful reference: Conditional match based on OS
And here's how to use cURL for advanced HTTP tests ! https://stackoverflow.com/a/73230200/143475
In case you need to do a lot of local file manipulation, you can use the karate.toJavaFile() utility so you can convert a relative path or a "prefixed" path to an absolute path.
* def file = karate.toJavaFile('classpath:some/file.txt')
* def path = file.getPath()
I'm trying to parse my command line arguments using the apache commons CLI. It might be a bit heavy handed for the example here, but it makes sense in the context of the program I'm creating. I'm trying to read a file pattern filter, similar to what grep uses to select files to process.
My Argument looks like this:
Program --input *.*
I've written a test program to see what the parser is seeing;
public static void main(String[] args) {
Options options = new Options();
options.addOption(new Option(INPUT_FILTER_SHORT, INPUT_FILTER_LONG, true, INPUT_FILTER_DESCRIPTION));
CommandLineParser parser = new BasicParser();
CommandLine cmd = parser.parse(options, args);
System.out.println(cmd.getOptionValue(INPUT_FILTER_SHORT));
}
This prints out:
.classpath
If I change my arguments to:
Program --input test.txt
I get the output:
test.txt
I'm assuming that I have to do something to tell apache commons what * is not a special character? I can't seem to find anything about this online.
I'm experiencing this on Windows (7). I'm fairly certain it's the *.* which is causing the issue as when I swap to using patterns that don't use *, the expected pattern shows up.
Your problem isn't really to do with Commons CLI, but to do with how the shell and the Java executable together process the parameters.
To eliminate other factors, and see what's going on, use a short Java program:
public class ArgsDemo {
public static void main(String[] args) {
for(int i=0; i<args.length; i++) {
System.out.println("" + i + ": " + args[i]);
}
}
}
Play with java ArgsDemo hello world, java ArgsDemo * etc. and observe what happens.
On UNIX and Linux:
Java does no special processing of *. However, the shell does. So if you did:
$ mkdir x
$ cd x
$ touch a b
$ java -jar myjar.jar MyClass *
... then MyClass.main() would be invoked with the parameter array ["a","b"] -- because the UNIX shell expands * to files in the current directory.
You can suppress this by escaping:
$ java -jar myjar MyClass * // main() sees ["*"])
(Note that a UNIX shell wouldn't expand *.* to .classpath because this form would ignore "hidden" files starting with .)
On Windows
cmd.exe does not do UNIX-style wildcard expansion. If you supply * as a parameter to a command in Windows, the command gets a literal *. So for example, PKUNZIP *.zip passes *.zip to PKUNZIP.EXE, and it's up to that program to expand the wildcard if it wants to.
Since some release of Java 7, the Java executable for Windows does some wildcard to filename expansion of its own, before passing the parameters to your main() class.
I've not been able to find clear documentation of Java-for-Windows' wildcard expansion rules, but you should be able to control it with quoting, escaping the quotes to prevent cmd.exe interpreting them:
> java.exe -jar myjar.jar MyClass """*.*"""
(Untested as I don't have a Windows box handy, and quoting in cmd.exe is a bit of a beast - do please experiment and either edit the above or leave a comment)
When I tried to run Ansible with Runtime.getRuntime().exec with Java
Here is what I did:
String[] cmd = {"ansible-playbook", "/path/to/playbook", "--extra-vars", "'{\"filePath\":\"/path/to/file\"}'"};
Process process = Runtime.getRuntime().exec(cmd, null);
I got error message like this:
FAILED! => {"failed": true, "msg": "'filePath' is undefined"}
However when I executed the same command with terminal:
ansible-playbook /path/to/playbook --extra-vars '{"filePath":"/path/to/file"}'
Everything was fine...
I think there must be some differences between the command I ran in terminal and Java, maybe apostrophe or quotation mark ?
I'm wondering is there any way to get the real executed command of Runtime.getRuntime().exec? Just like I can get command line history of some user by history...
You are adding additional quotes in your third parameter:
"'{\"filePath\":\"/path/to/file\"}'"
If you do this, you're not executing the same command in your shell as you have above. You're actually executing (in bash):
ansible-playbook /path/to/playbook --extra-vars ''\''{"filePath":"/path/to/file"}'\'''
You don't need the single quotes around the value here: because you're passing these values directly, you don't have to worry about the quoting that you'd have to do in a shell. You can simply use:
"{\"filePath\":\"/path/to/file\"}"
I have a variable toPath (contains path like C:/Program Files(x86)/bla).
This variable I pass as agrument: '[-operation update -contents ' + toPath + ']'
But because I have a space in this variable I get IllegalArgumentException.
How can I fix this?
I'm not sure but it looks like you are trying to do a typical newcomer mistake.
If you are trying to run a command that is build from multiple variables you can be vulnerable to injection attacks. To prevent this, use the subprocess module and hand in all parameters as a list. The module will take care of all the stuff to make it work with spaces as well.
For example ls -l should be run as:
subprocess.call(["ls", "-l"])
Your example caontains [] and might be rather different but without it would be:
subprocess.call(['-operation','update', '-contents', toPath])
Please note that there are other functions than call() (which returns the return code only) in the subprocess module.
Pass argument in double quotes.
toPath = "\"C:/Program Files(x86)/bla\"";
try
'[-operation update -contents "' + toPath + '"]'
I saw the fallowing command for starting Selenium:
java -Xmx256m -Dauto=1 -jar selenium-server-standalone-2.25.0.jar -log /home/test/selenium.log -trustAllSSLCertificates
I googled to find what -Dauto=1 means but failed.
I'm pretty sure auto is no valid parameter in a current version of Selenium server. It might have been in the past or it was just used by some custom implementation.
java -jar selenium-server-standalone-2.25.0.jar -h
will list you all available parameters for Selenium, which you can set by adding
-D<variable>=<value>
to your start command.
-D is an important switch that allows you to set environment properties.
-Dproperty=value
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
java -D<propertyName>=<propertyValue>
You can call the following from anywhere in the code to read it.
String value = System.getProperty("propertyName");
or
String value = System.getProperty("propertyName", "defaultValue");