| 1 | package com.github.dakusui.actionunit.actions.cmd; | |
| 2 | ||
| 3 | import com.github.dakusui.actionunit.actions.ContextVariable; | |
| 4 | import com.github.dakusui.actionunit.actions.RetryOption; | |
| 5 | import com.github.dakusui.actionunit.core.Action; | |
| 6 | import com.github.dakusui.actionunit.core.Context; | |
| 7 | import com.github.dakusui.actionunit.core.context.ContextFunctions; | |
| 8 | import com.github.dakusui.actionunit.core.context.StreamGenerator; | |
| 9 | import com.github.dakusui.actionunit.core.context.multiparams.Params; | |
| 10 | import com.github.dakusui.processstreamer.core.process.ProcessStreamer; | |
| 11 | import com.github.dakusui.processstreamer.core.process.Shell; | |
| 12 | import org.slf4j.Logger; | |
| 13 | import org.slf4j.LoggerFactory; | |
| 14 | ||
| 15 | import java.io.File; | |
| 16 | import java.util.Map; | |
| 17 | import java.util.function.Consumer; | |
| 18 | import java.util.function.Function; | |
| 19 | import java.util.function.Predicate; | |
| 20 | import java.util.stream.Stream; | |
| 21 | ||
| 22 | import static com.github.dakusui.actionunit.core.ActionSupport.leaf; | |
| 23 | import static com.github.dakusui.actionunit.core.context.ContextFunctions.multiParamsConsumerFor; | |
| 24 | import static com.github.dakusui.actionunit.core.context.ContextFunctions.multiParamsPredicateFor; | |
| 25 | import static com.github.dakusui.actionunit.utils.InternalUtils.toStringIfOverriddenOrNoname; | |
| 26 | import static com.github.dakusui.printables.PrintableFunctionals.printableConsumer; | |
| 27 | import static com.github.dakusui.printables.PrintableFunctionals.printablePredicate; | |
| 28 | import static java.lang.String.format; | |
| 29 | import static java.util.Objects.requireNonNull; | |
| 30 | import static java.util.stream.Collectors.joining; | |
| 31 | ||
| 32 | public enum CommanderUtils { | |
| 33 | ; | |
| 34 | ||
| 35 | private static final Logger LOGGER = LoggerFactory.getLogger(CommanderUtils.class); | |
| 36 | ||
| 37 | public static String quoteWithApostropheForShell(String s) { | |
| 38 |
1
1. quoteWithApostropheForShell : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::quoteWithApostropheForShell → KILLED |
return String.format("'%s'", escapeSingleQuotesForShell(s)); |
| 39 | } | |
| 40 | ||
| 41 | private static String escapeSingleQuotesForShell(String s) { | |
| 42 |
1
1. escapeSingleQuotesForShell : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::escapeSingleQuotesForShell → KILLED |
return requireNonNull(s).replaceAll("('+)", "'\"$1\"'"); |
| 43 | } | |
| 44 | ||
| 45 | static Action createAction(Commander<?> commander) { | |
| 46 |
1
1. createAction : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createAction → KILLED |
return RetryOption.retryAndTimeOut( |
| 47 | leaf(createContextConsumer(commander)), | |
| 48 | commander.retryOption()); | |
| 49 | } | |
| 50 | ||
| 51 | static Function<Context, Stream<String>> createStreamGenerator( | |
| 52 | Commander<?> commander) { | |
| 53 |
1
1. createStreamGenerator : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createStreamGenerator → KILLED |
return StreamGenerator.fromContextWith( |
| 54 | new Function<Params, Stream<String>>() { | |
| 55 | @Override | |
| 56 | public Stream<String> apply(Params params) { | |
| 57 |
1
1. apply : replaced return value with Stream.empty for com/github/dakusui/actionunit/actions/cmd/CommanderUtils$1::apply → KILLED |
return createProcessStreamerBuilder(commander, params) |
| 58 | .checker(commander.checker()) | |
| 59 | .build() | |
| 60 | .stream() | |
| 61 | .peek(commander.downstreamConsumer()); | |
| 62 | } | |
| 63 | ||
| 64 | @Override | |
| 65 | public String toString() { | |
| 66 |
1
1. toString : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils$1::toString → SURVIVED |
return format("(%s)", commander.buildCommandLineComposer().format()); |
| 67 | } | |
| 68 | }, | |
| 69 | commander.variables() | |
| 70 | ); | |
| 71 | } | |
| 72 | ||
| 73 | static Consumer<Context> createContextConsumer(Commander<?> commander) { | |
| 74 | requireNonNull(commander); | |
| 75 |
1
1. createContextConsumer : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createContextConsumer → KILLED |
return multiParamsConsumerFor(commander.variables()) |
| 76 | .toContextConsumer( | |
| 77 | printableConsumer( | |
| 78 | (Params params) -> createProcessStreamerBuilder(commander, params) | |
| 79 | .checker(commander.checker()) | |
| 80 | .build() | |
| 81 | .stream() | |
| 82 |
1
1. lambda$createContextConsumer$0 : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(commander.downstreamConsumer())) |
| 83 |
1
1. lambda$createContextConsumer$1 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::lambda$createContextConsumer$1 → KILLED |
.describe(() -> commander.buildCommandLineComposer().format())); |
| 84 | } | |
| 85 | ||
| 86 | static Predicate<Context> createContextPredicate(Commander<?> commander) { | |
| 87 |
1
1. createContextPredicate : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createContextPredicate → KILLED |
return multiParamsPredicateFor(commander.variables()) |
| 88 | .toContextPredicate(printablePredicate( | |
| 89 | (Params params) -> { | |
| 90 | ProcessStreamer.Builder processStreamerBuilder = createProcessStreamerBuilder(commander, params); | |
| 91 | try { | |
| 92 | processStreamerBuilder | |
| 93 | .checker(commander.checker()) | |
| 94 | .build() | |
| 95 | .stream() | |
| 96 |
1
1. lambda$createContextPredicate$2 : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(commander.downstreamConsumer()); |
| 97 |
1
1. lambda$createContextPredicate$2 : replaced boolean return with false for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::lambda$createContextPredicate$2 → KILLED |
return true; |
| 98 | } catch (ProcessStreamer.Failure failure) { | |
| 99 | String msg = format("Condition '%s' was not satisfied: %s", commander.checker(), failure.getMessage()); | |
| 100 | LOGGER.debug(msg); | |
| 101 | LOGGER.trace(msg, failure); | |
| 102 |
1
1. lambda$createContextPredicate$2 : replaced boolean return with true for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::lambda$createContextPredicate$2 → KILLED |
return false; |
| 103 | } | |
| 104 | }) | |
| 105 |
1
1. lambda$createContextPredicate$3 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::lambda$createContextPredicate$3 → SURVIVED |
.describe(() -> format( |
| 106 | "outputOf[command:'%s'].matches[%s]", | |
| 107 | commander.buildCommandLineComposer().format(), | |
| 108 | toStringIfOverriddenOrNoname(commander.checker())))); | |
| 109 | } | |
| 110 | ||
| 111 | static Function<Context, String> createContextFunction(Commander<?> commander) { | |
| 112 |
1
1. createContextFunction : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createContextFunction → KILLED |
return ContextFunctions.<String>multiParamsFunctionFor(commander.variables()) |
| 113 | .toContextFunction(params -> | |
| 114 | createProcessStreamerBuilder(commander, params) | |
| 115 | .checker(commander.checker()) | |
| 116 | .build() | |
| 117 | .stream() | |
| 118 | .peek(commander.downstreamConsumer()) | |
| 119 |
1
1. lambda$createContextFunction$4 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::lambda$createContextFunction$4 → SURVIVED |
.collect(joining(format("%n")))); |
| 120 | } | |
| 121 | ||
| 122 | static ProcessStreamer.Builder createProcessStreamerBuilder(Commander<?> commander, Params params) { | |
| 123 |
1
1. createProcessStreamerBuilder : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createProcessStreamerBuilder → KILLED |
return createProcessStreamerBuilder( |
| 124 | commander.stdin(), | |
| 125 | commander.shellManager().shellFor(commander.host()), | |
| 126 | commander.cwd().orElse(null), | |
| 127 | commander.envvars(), | |
| 128 | commander.buildCommandLineComposer(), | |
| 129 | params | |
| 130 | ); | |
| 131 | } | |
| 132 | ||
| 133 | static ProcessStreamer.Builder createProcessStreamerBuilder( | |
| 134 | Stream<String> stdin, | |
| 135 | Shell shell, | |
| 136 | File cwd, | |
| 137 | Map<String, String> envvars, | |
| 138 | CommandLineComposer commandLineComposer, | |
| 139 | Params params) { | |
| 140 | ContextVariable[] variables = params.parameters().toArray(new ContextVariable[0]); // values | |
| 141 | Object[] variableValues = params.parameters()// values | |
| 142 | .stream() | |
| 143 | .map(params::valueOf) | |
| 144 | .toArray(); | |
| 145 | String commandLine = commandLineComposer | |
| 146 | .apply(variables) | |
| 147 | .apply(params.context(), variableValues); | |
| 148 | LOGGER.info("Command Line:{}", commandLine); | |
| 149 | LOGGER.trace("Shell:{}", shell); | |
| 150 |
1
1. createProcessStreamerBuilder : negated conditional → SURVIVED |
if (cwd != null) |
| 151 | LOGGER.debug("Cwd:{}", cwd); | |
| 152 |
1
1. createProcessStreamerBuilder : negated conditional → SURVIVED |
if (!envvars.isEmpty()) |
| 153 | LOGGER.debug("Environment variables:{}", envvars); | |
| 154 | ProcessStreamer.Builder ret; | |
| 155 |
1
1. createProcessStreamerBuilder : negated conditional → KILLED |
if (stdin == null) |
| 156 | ret = ProcessStreamer.source(shell); | |
| 157 | else | |
| 158 | ret = ProcessStreamer.pipe(stdin, shell); | |
| 159 |
1
1. createProcessStreamerBuilder : removed call to java/util/Map::forEach → KILLED |
envvars.forEach(ret::env); |
| 160 |
1
1. createProcessStreamerBuilder : negated conditional → SURVIVED |
if (cwd != null) |
| 161 | ret.cwd(cwd); | |
| 162 |
1
1. createProcessStreamerBuilder : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/CommanderUtils::createProcessStreamerBuilder → KILLED |
return ret.command(commandLine); |
| 163 | } | |
| 164 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 42 |
1.1 |
|
| 46 |
1.1 |
|
| 53 |
1.1 |
|
| 57 |
1.1 |
|
| 66 |
1.1 |
|
| 75 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 87 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 102 |
1.1 |
|
| 105 |
1.1 |
|
| 112 |
1.1 |
|
| 119 |
1.1 |
|
| 123 |
1.1 |
|
| 150 |
1.1 |
|
| 152 |
1.1 |
|
| 155 |
1.1 |
|
| 159 |
1.1 |
|
| 160 |
1.1 |
|
| 162 |
1.1 |