github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/util/CommandLineMatcher.groovy (about) 1 package util 2 3 import org.hamcrest.BaseMatcher 4 import org.hamcrest.Description 5 6 class CommandLineMatcher extends BaseMatcher { 7 8 String prolog 9 Set<String> args = (Set) [] 10 Set<MapEntry> opts = (Set) [] 11 12 String hint = '' 13 14 CommandLineMatcher hasProlog(prolog) { 15 this.prolog = prolog 16 return this 17 } 18 19 CommandLineMatcher hasDoubleQuotedOption(String key, String value) { 20 hasOption(key, "\"${value}\"") 21 return this 22 } 23 24 CommandLineMatcher hasSingleQuotedOption(String key, String value) { 25 hasOption(key, "\'${value}\'") 26 return this 27 } 28 29 CommandLineMatcher hasOption(String key, String value) { 30 this.opts.add(new MapEntry(key, value)) 31 return this 32 } 33 34 CommandLineMatcher hasSnippet(String snippet) { 35 this.args.add(snippet) 36 return this 37 } 38 39 CommandLineMatcher hasArgument(String arg) { 40 this.args.add(arg) 41 return this 42 } 43 44 @Override 45 boolean matches(Object o) { 46 47 for (String cmd : o) { 48 49 hint = '' 50 boolean matches = true 51 52 if (!cmd.matches(/${prolog}.*/)) { 53 hint = "A command line starting with \'${prolog}\'." 54 matches = false 55 } 56 57 for (MapEntry opt : opts) { 58 if (!cmd.matches(/.*[\s]*-${opt.key}[\s]*${opt.value}.*/)) { 59 hint = "A command line containing option \'${opt.key}\' with value \'${opt.value}\'" 60 matches = false 61 } 62 } 63 64 for (String arg : args) { 65 if (!cmd.matches(/.*[\s]*${arg}[\s]*.*/)) { 66 hint = "A command line having argument/snippet '${arg}'." 67 matches = false 68 } 69 } 70 71 if (matches) 72 return true 73 } 74 75 return false 76 } 77 78 @Override 79 public void describeTo(Description description) { 80 description.appendText(hint) 81 } 82 }