• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 21174441650

20 Jan 2026 02:06PM UTC coverage: 70.447% (-0.02%) from 70.471%
21174441650

push

github

web-flow
#1679: fix determine version if npm list has exit code 1 (#1681)

4031 of 6308 branches covered (63.9%)

Branch coverage included in aggregate %.

10479 of 14289 relevant lines covered (73.34%)

3.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.41
cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java
1
package com.devonfw.tools.ide.process;
2

3
import java.nio.file.Path;
4
import java.util.List;
5
import java.util.Objects;
6
import java.util.function.Predicate;
7

8
import com.devonfw.tools.ide.log.IdeSubLogger;
9

10
/**
11
 * Wrapper for {@link ProcessBuilder} to simplify its usage and avoid common mistakes and pitfalls.
12
 */
13
public interface ProcessContext extends EnvironmentContext {
14

15
  /**
16
   * @param handling the desired {@link ProcessErrorHandling}.
17
   * @return this {@link ProcessContext} for fluent API calls.
18
   */
19
  ProcessContext errorHandling(ProcessErrorHandling handling);
20

21
  /**
22
   * @param directory the {@link Path} to the working directory from where to execute the command.
23
   * @return this {@link ProcessContext} for fluent API calls.
24
   */
25
  ProcessContext directory(Path directory);
26

27
  /**
28
   * Sets the executable command to be {@link #run()}.
29
   *
30
   * @param executable the {@link Path} to the command to be executed by {@link #run()}. Depending on your operating system and the extension of the
31
   *     executable or OS specific conventions. So e.g. a *.cmd or *.bat file will be called via CMD shell on windows while a *.sh file will be called via Bash,
32
   *     etc.
33
   * @return this {@link ProcessContext} for fluent API calls.
34
   */
35
  ProcessContext executable(Path executable);
36

37
  /**
38
   * Sets the executable command to be {@link #run()}.
39
   *
40
   * @param executable the command to be executed by {@link #run()}.
41
   * @return this {@link ProcessContext} for fluent API calls.
42
   * @see #executable(Path)
43
   */
44
  default ProcessContext executable(String executable) {
45

46
    return executable(Path.of(executable));
7✔
47
  }
48

49
  /**
50
   * Adds a single argument for {@link #executable(Path) command}.
51
   *
52
   * @param arg the next argument for {@link #executable(Path) command} to be added.
53
   * @return this {@link ProcessContext} for fluent API calls.
54
   */
55
  ProcessContext addArg(String arg);
56

57
  /**
58
   * Adds a single argument for {@link #executable(Path) command}.
59
   *
60
   * @param arg the next argument for {@link #executable(Path) command} to be added.
61
   * @return this {@link ProcessContext} for fluent API calls.
62
   */
63
  default ProcessContext addArg(Object arg) {
64

65
    Objects.requireNonNull(arg);
3✔
66
    return addArg(arg.toString());
5✔
67
  }
68

69
  /**
70
   * Adds the given arguments for {@link #executable(Path) command}. E.g. for {@link #executable(Path) command} "mvn" the arguments "clean" and "install" may be
71
   * added here to run "mvn clean install". If this method would be called again with "-Pmyprofile" and "-X" before {@link #run()} gets called then "mvn clean
72
   * install -Pmyprofile -X" would be run.
73
   *
74
   * @param args the arguments for {@link #executable(Path) command} to be added.
75
   * @return this {@link ProcessContext} for fluent API calls.
76
   */
77
  default ProcessContext addArgs(String... args) {
78

79
    for (String arg : args) {
16✔
80
      addArg(arg);
4✔
81
    }
82
    return this;
2✔
83
  }
84

85
  /**
86
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
87
   * of these arguments to the command.
88
   *
89
   * @param args the arguments for {@link #executable(Path) command} to be added.
90
   * @return this {@link ProcessContext} for fluent API calls.
91
   */
92
  default ProcessContext addArgs(Object... args) {
93

94
    for (Object arg : args) {
×
95
      addArg(arg);
×
96
    }
97
    return this;
×
98
  }
99

100
  /**
101
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
102
   * of these arguments to the command.
103
   *
104
   * @param args the {@link List} of arguments for {@link #executable(Path) command} to be added.
105
   * @return this {@link ProcessContext} for fluent API calls.
106
   */
107
  default ProcessContext addArgs(List<?> args) {
108

109
    for (Object arg : args) {
9✔
110
      addArg(arg);
4✔
111
    }
1✔
112
    return this;
2✔
113
  }
114

115
  @Override
116
  ProcessContext withEnvVar(String key, String value);
117

118
  @Override
119
  ProcessContext withPathEntry(Path path);
120

121
  /**
122
   * @param exitCodeAcceptor the {@link Predicate} that {@link Predicate#test(Object) tests} which {@link ProcessResult#getExitCode() exit codes} should be
123
   *     accepted as {@link ProcessResult#isSuccessful() success}.
124
   * @return this {@link ProcessContext} for fluent API calls.
125
   */
126
  ProcessContext withExitCodeAcceptor(Predicate<Integer> exitCodeAcceptor);
127

128
  /**
129
   * @return a new child {@link ProcessContext} connected with this {@link ProcessContext} sharing the same {@link #withEnvVar(String, String) environment} and
130
   *     {@link #withPathEntry(Path) PATH} but not specific things like {@link #withExitCodeAcceptor(Predicate) exitCodeAcceptor} or
131
   *     {@link #errorHandling(ProcessErrorHandling) errorHandling}.
132
   */
133
  ProcessContext createChild();
134

135
  /**
136
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
137
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
138
   *
139
   * @return the exit code. Will be {@link ProcessResult#SUCCESS} on successful completion of the {@link Process}.
140
   */
141
  default int run() {
142

143
    return run(ProcessMode.DEFAULT).getExitCode();
5✔
144
  }
145

146
  /**
147
   * Runs the given {@code executable} with the given {@code arguments}.
148
   *
149
   * @param executable the executable program.
150
   * @param arguments the program arguments.
151
   * @throws IllegalStateException if the command failed.
152
   */
153
  default void run(String executable, String... arguments) {
154

155
    executable(executable).addArgs(arguments).errorHandling(ProcessErrorHandling.THROW_ERR).run();
×
156
  }
×
157

158
  /**
159
   * Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
160
   * {@link ProcessResult#getOut() standard output}.
161
   *
162
   * @param executable the executable program.
163
   * @param arguments the program arguments.
164
   * @return the single line printed from the command.
165
   * @throws IllegalStateException if the command failed or did not print a single line as expected.
166
   */
167
  default String runAndGetSingleOutput(String executable, String... arguments) {
168

169
    return runAndGetSingleOutput(null, executable, arguments);
6✔
170
  }
171

172
  /**
173
   * Runs the given {@code executable} with the given {@code arguments} and returns the output from its {@link ProcessResult#getOut() standard output}.
174
   *
175
   * @param executable the executable program.
176
   * @param arguments the program arguments.
177
   * @return the output printed from the command.
178
   * @throws IllegalStateException if the command failed.
179
   */
180
  default List<String> runAndGetOutput(String executable, String... arguments) {
181

182
    return runAndGetOutput(null, executable, arguments);
×
183
  }
184

185
  /**
186
   * Runs the given {@code executable} with the given {@code arguments} and returns the output from its {@link ProcessResult#getOut() standard output}.
187
   *
188
   * @param logger the {@link IdeSubLogger} used to log errors instead of throwing an exception.
189
   * @param executable the executable program.
190
   * @param arguments the program arguments.
191
   * @return the output printed from the command.
192
   * @throws IllegalStateException if the command failed.
193
   */
194
  default List<String> runAndGetOutput(IdeSubLogger logger, String executable, String... arguments) {
195

196
    executable(executable).addArgs(arguments);
×
197
    if (logger == null) {
×
198
      errorHandling(ProcessErrorHandling.THROW_ERR);
×
199
    }
200
    ProcessResult result = run(ProcessMode.DEFAULT_CAPTURE);
×
201
    return result.getOutput(logger);
×
202
  }
203

204
  /**
205
   * Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
206
   * {@link ProcessResult#getOut() standard output}.
207
   *
208
   * @param logger the {@link IdeSubLogger} used to log errors instead of throwing an exception.
209
   * @param executable the executable program.
210
   * @param arguments the program arguments.
211
   * @return the single line printed from the command.
212
   * @throws IllegalStateException if the command did not print a single line as expected.
213
   */
214
  default String runAndGetSingleOutput(IdeSubLogger logger, String executable, String... arguments) {
215

216
    executable(executable).addArgs(arguments);
6✔
217
    if (logger == null) {
2!
218
      errorHandling(ProcessErrorHandling.THROW_ERR);
4✔
219
    }
220
    ProcessResult result = run(ProcessMode.DEFAULT_CAPTURE);
4✔
221
    return result.getSingleOutput(logger);
4✔
222
  }
223

224
  /**
225
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
226
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
227
   *
228
   * @param processMode {@link ProcessMode}
229
   * @return the {@link ProcessResult}.
230
   */
231
  ProcessResult run(ProcessMode processMode);
232

233
  /**
234
   * Sets the {@link OutputListener} that will receive output events. This method is intended to be used by subclasses that support output listening. The
235
   * default implementation does nothing.
236
   *
237
   * @param listener the {@code OutputListener} to receive output events
238
   */
239
  default void setOutputListener(OutputListener listener) {
240

241
  }
×
242

243

244
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc