Junit Console Launcher .. support for tag expressions - junit

My junit-console-launcher command vaguely resembles,
java -jar ./junit-platform-console-standalone.jar -cp . -c pkg.Class1 -c pkg1.Class2 -t A --details=verbose
I do this because I need to send in my classes in a certain order. I needed help in using tag expressions which is documented here,
https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions
-t 'A & B' does not seems to be recognized by junit console launcher. I need to select tests which are tagged both A and B. Is this supported? Any ideas?

Copied from https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
-t, --include-tag=TAG
Provide a tag or tag expression to include only tests whose tags match. When this option is repeated, all patterns will be combined using OR semantics.
This indicates, that "tag expression" are supported.
You just need to convince your OS/shell command to pass the A&B string as a single argument without the quotes.
Note: you also need to pass a class name filter pattern that matches your test classes, something like --include-classname .*Class? Or wait until 1.7.0-M2 is released: https://github.com/junit-team/junit5/issues/2259

Related

Ubuntu Bash Script : error while executing function [duplicate]

This question already has answers here:
How can I store a command in a variable in a shell script?
(12 answers)
Closed 4 years ago.
These work as advertised:
grep -ir 'hello world' .
grep -ir hello\ world .
These don't:
argumentString1="-ir 'hello world'"
argumentString2="-ir hello\\ world"
grep $argumentString1 .
grep $argumentString2 .
Despite 'hello world' being enclosed by quotes in the second example, grep interprets 'hello (and hello\) as one argument and world' (and world) as another, which means that, in this case, 'hello will be the search pattern and world' will be the search path.
Again, this only happens when the arguments are expanded from the argumentString variables. grep properly interprets 'hello world' (and hello\ world) as a single argument in the first example.
Can anyone explain why this is? Is there a proper way to expand a string variable that will preserve the syntax of each character such that it is correctly interpreted by shell commands?
Why
When the string is expanded, it is split into words, but it is not re-evaluated to find special characters such as quotes or dollar signs or ... This is the way the shell has 'always' behaved, since the Bourne shell back in 1978 or thereabouts.
Fix
In bash, use an array to hold the arguments:
argumentArray=(-ir 'hello world')
grep "${argumentArray[#]}" .
Or, if brave/foolhardy, use eval:
argumentString="-ir 'hello world'"
eval "grep $argumentString ."
On the other hand, discretion is often the better part of valour, and working with eval is a place where discretion is better than bravery. If you are not completely in control of the string that is eval'd (if there's any user input in the command string that has not been rigorously validated), then you are opening yourself to potentially serious problems.
Note that the sequence of expansions for Bash is described in Shell Expansions in the GNU Bash manual. Note in particular sections 3.5.3 Shell Parameter Expansion, 3.5.7 Word Splitting, and 3.5.9 Quote Removal.
When you put quote characters into variables, they just become plain literals (see http://mywiki.wooledge.org/BashFAQ/050; thanks #tripleee for pointing out this link)
Instead, try using an array to pass your arguments:
argumentString=(-ir 'hello world')
grep "${argumentString[#]}" .
In looking at this and related questions, I'm surprised that no one brought up using an explicit subshell. For bash, and other modern shells, you can execute a command line explicitly. In bash, it requires the -c option.
argumentString="-ir 'hello world'"
bash -c "grep $argumentString ."
Works exactly as original questioner desired. There are two restrictions to this technique:
You can only use single quotes within the command or argument strings.
Only exported environment variables will be available to the command
Also, this technique handles redirection and piping, and other shellisms work as well. You also can use bash internal commands as well as any other command that works at the command line, because you are essentially asking a subshell bash to interpret it directly as a command line. Here's a more complex example, a somewhat gratuitously complex ls -l variant.
cmd="prefix=`pwd` && ls | xargs -n 1 echo \'In $prefix:\'"
bash -c "$cmd"
I have built command processors both this way and with parameter arrays. Generally, this way is much easier to write and debug, and it's trivial to echo the command you are executing. OTOH, param arrays work nicely when you really do have abstract arrays of parameters, as opposed to just wanting a simple command variant.

How to determine whether a function exists in a POSIX shell?

This is basically the same question as Determine if a function exists in bash, except that this time it's not aiming at Bash, but at a POSIX shell:
How to determine whether a shell function with a given name exists?
It seems that none of the typical built-ins like type are mandated by POSIX, so the matter is more difficult or maybe even impossible.
POSIX (more precisely the X/Open Portability Guide) specify the type command. It doesn't state what the type command should return to tell what the argument is. However, the standard says it typically identifies the operand, so it would be very unlikely for a type implementation not to include the string "function" in its reply when passed a function name.
This should then work with most, if not all, POSIX compliant shells:
isFunction()
{
type "$1" | sed "s/$1//" | grep -qwi function
}
You might also run command -V instead of type here, with the same comment about the unspecified output format. I never do, given the fact the former is shorter to type and easier to remember. This would, however, be mandatory if you run a shell that decided not to include XSI (likely posh), i.e., a shell that breaks portability with many existing scripts by limiting the utilities it tries to comply with to the strict POSIX set.
You can use command for this in shells that implement the 2013 version of POSIX, or the User Portability Utilities option of the older spec:
isFunction() {
command -V "$1" 2>/dev/null | grep -qwi function
}
However, note that the spec doesn't actually dictate the form of the command's output. It requires that functions be identified as such, so it is highly likely that the output will include the word function if and only if the requested name is a function, but it is not strictly guaranteed. The above solution can be fooled pretty easily (see #jiliagre's comment).
A different part of the spec mandates a type command that does much the same thing (with the same caveats about unspecified output format). Oddly, it's not listed as one of the commands required to be a shell builtin, but as the informational notes say, it pretty much has to be one in order to work as specified.
For the sake of completeness: it is possible to use type or command -V without spawning any extra subprocesses like sed or grep (although you still have to spawn one for $(type ...)):
is_function() {
case "$(type -- "$1" 2>/dev/null)" in
*function*) return 0 ;;
esac
return 1
}

Use SED to extract value of all input elements with a certain name

How do I get the value attribute based on a search of some other attribute?
For example:
<body>
<input name="dummy" value="foo">
<input name="alpha" value="bar">
</body>
How do I get the value of the input element with the name "dummy"?
Since you're looking for a solution using bash and sed, I'm assuming you're looking for a Linux command line option.
Use hxselect html parsing tool to extract element; use sed to extract value from element
I did a Google search for "linux bash parse html tool" and came across this: https://unix.stackexchange.com/questions/6389/how-to-parse-hundred-html-source-code-files-in-shell
The accepted answer suggests using the hxselect tool from the html-xml-utils package which extracts elements based on a css selector.
So after installing (downoad, unzip, ./configure, make, make install), you can run this command using the given css selector
hxselect "input[name='dummy']" < example.html
(Given that example.html contains your example html from the question.) This will return:
<input name="dummy" value="foo"/>
Almost there. We need to extract the value from that line:
hxselect "input[name='dummy']" < example.html | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p"
Which returns "foo".
why you would / would not want to use this approach
using regex to parse out the attributes is complicated, and often the wrong way to go
the hxselect tool (in my other answer) is a pain to install
BUT, this approach accepts malformed html, which is what is argued for in this answer to the question linked above. By the way, that question has very thorough discussion on the regex+html debate.
Since you're asking for SED, I'll assume you want a command line option. However, a tool built for html parsing may be more effective. The problem with my first answer is that I don't know of a way in css to select the value of an attribute (does anyone else?). However, with xml you can select attributes like you could other elements. Here is a command line option for using an xml parsing tool.
Treat it as XML; use XPATH
Install xmlstarlet with your package manager
Run xmlstarlet sel -t -v //input[#name=\'dummy\']/#value example.html (where example.html contains your html
If your html isn't valid xml, follow the warnings from xmlstarlet to make the necessary changes (in this case, <input> must be changed to <input/>
Run the command again. Returns: foo
why you might/might not use this approach
it is way more simple and robust than hand-rolling a regex html parser, but
it requires well formed html
Parsing HTML with sed is generally a bad idea, since sed works in a line-based manner and HTML does not usually consider newlines syntactically important. It's not good if your HTML-handling tools break when the HTML is reformatted.
Instead, consider using Python, which has an HTML push parser in its standard library. For example:
#!/usr/bin/python
from HTMLParser import HTMLParser
from sys import argv
# Our parser. It inherits the standard HTMLParser that does most of
# the work.
class MyParser(HTMLParser):
# We just hook into the handling of start tags to extract the
# attribute
def handle_starttag(self, tag, attrs):
# Build a dictionary from the attribute list for easier
# handling
attrs_dict = dict(attrs)
# Then, if the tag matches our criteria
if tag == 'input' \
and 'name' in attrs_dict \
and attrs_dict['name'] == 'dummy':
# Print the value attribute (or an empty string if it
# doesn't exist)
print attrs_dict['value'] if 'value' in attrs_dict else ""
# After we defined the parser, all that's left is to use it. So,
# build one:
p = MyParser()
# And feed a file to it (here: the first command line argument)
with open(argv[1], 'rb') as f:
p.feed(f.read())
Save this code as, say, foo.py, then run
python foo.py foo.html
where foo.html is your HTML file.

Break on namespace function in gdb (llvm)

I'm trying to step through llvm's opt program (for an assignment) and the instructor suggested setting a breakpoint at runOnFunction. I see this in one of the files:
bool InstCombiner::runOnFunction(Function &F) { /* (Code removed for SO) */ }
but gdb does not seem to find the runOnFunction breakpoint. It occurred to me that the problem might be namespaces? I tried this but gdb never breaks, it just creates the fooOpt.s file:
(gdb) b runOnFunction
Function "runOnFunction" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (runOnFunction) pending.
(gdb) r -S -instcombine -debug -o ~/Desktop/fooOpt.s ~/Desktop/foo.s
I'm on a Mac so I don't have objdump but otool produces 5.6 million lines, wading through that for a starting point does not seem reasonable as runOnFunction appears more than once there.
Gdb has several builtin commands to find name of such functions. First is info functions, which can be used with optional regexp argument to grep all available functions, https://sourceware.org/gdb/current/onlinedocs/gdb/Symbols.html
info functions regexp
Print the names and data types of all defined functions whose names contain a match for regular expression regexp. Thus, ‘info fun step’ finds all functions whose names include step; ‘info fun ^step’ finds those whose names start with step. If a function name contains characters that conflict with the regular expression language (e.g. ‘operator*()’), they may be quoted with a backslash.
So, you can try info functions runOnFunction to get the name. Sometimes it can be useful to add quotes around name when doing break command.
The other way is to use rbreak command instead of break (b). rbreak will do regexp search in functions names and may define several breakpoints: https://sourceware.org/gdb/current/onlinedocs/gdb/Set-Breaks.html#Set-Breaks
rbreak regex
Set breakpoints on all functions matching the regular expression regex. This command sets an unconditional breakpoint on all matches, printing a list of all breakpoints it set. ...
The syntax of the regular expression is the standard one used with tools like grep. Note that this is different from the syntax used by shells, so for instance foo* matches all functions that include an fo followed by zero or more os. There is an implicit .* leading and trailing the regular expression you supply, so to match only functions that begin with foo, use ^foo.
(or even rbreak file:regex to limit search to single source file)
PS: if you want, you can turn on or off C++ function name demangling with set print demangle on or off (https://sourceware.org/gdb/current/onlinedocs/gdb/Debugging-C-Plus-Plus.html#Debugging-C-Plus-Plus). With demangling turned off it will be easier to copy function name to break command.

"info command rtExMath" in Tk with vtk

I am new for Tcl/Tk. I am using vtk with Tk command window for running vtk tcl/tk examples. Here is a code which include Tk expression as condition of if and I am not getting it.
if { [info command rtExMath] != "" } {
##Do something related VTK
}
I have explored info of Tk but there is a combination with keyword command and also no any good explanation I found for rtExMath.
Please explain me above.
The info commands command (info command is just an unambiguous prefix) returns a list of all commands, or a list of all commands that match the given glob pattern. In the case you're looking at, the glob pattern is actually going to be a string-equality check (and is even optimised to such internally); there's no glob metacharacters in it. The result of that is that [info command rtExMath] != "" is a condition that is true exactly when the command rtExMath exists.
Tcl itself does not define any command called rtExMath; I conclude that it must be part of some specialist extension or application. However, Googling makes me suspect that it is actually a somewhat-standard name for an instance of the vtkMath class in Vtk, but I don't really know for sure. (I'm guessing that the binding of that class to Tcl was done by SWIG…)