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

devonfw / IDEasy / 15041251660

15 May 2025 09:15AM UTC coverage: 67.712% (+0.02%) from 67.697%
15041251660

push

github

web-flow
#1271: Fixed aws input not being processed (#1305)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3104 of 4990 branches covered (62.2%)

Branch coverage included in aggregate %.

7977 of 11375 relevant lines covered (70.13%)

3.07 hits per line

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

80.47
cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java
1
package com.devonfw.tools.ide.process;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.lang.ProcessBuilder.Redirect;
8
import java.nio.charset.StandardCharsets;
9
import java.nio.file.Files;
10
import java.nio.file.Path;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Objects;
15
import java.util.concurrent.CompletableFuture;
16
import java.util.concurrent.ConcurrentLinkedQueue;
17
import java.util.stream.Collectors;
18

19
import com.devonfw.tools.ide.cli.CliProcessException;
20
import com.devonfw.tools.ide.common.SystemPath;
21
import com.devonfw.tools.ide.context.IdeContext;
22
import com.devonfw.tools.ide.environment.VariableLine;
23
import com.devonfw.tools.ide.log.IdeLogLevel;
24
import com.devonfw.tools.ide.os.SystemInfoImpl;
25
import com.devonfw.tools.ide.os.WindowsPathSyntax;
26
import com.devonfw.tools.ide.util.FilenameUtil;
27
import com.devonfw.tools.ide.variable.IdeVariables;
28

29
/**
30
 * Implementation of {@link ProcessContext}.
31
 */
32
public class ProcessContextImpl implements ProcessContext {
33

34
  private static final String PREFIX_USR_BIN_ENV = "/usr/bin/env ";
35

36
  /** The owning {@link IdeContext}. */
37
  protected final IdeContext context;
38

39
  private final ProcessBuilder processBuilder;
40

41
  private final List<String> arguments;
42

43
  private Path executable;
44

45
  private String overriddenPath;
46

47
  private final List<Path> extraPathEntries;
48

49
  private ProcessErrorHandling errorHandling;
50

51
  /**
52
   * The constructor.
53
   *
54
   * @param context the owning {@link IdeContext}.
55
   */
56
  public ProcessContextImpl(IdeContext context) {
57

58
    super();
2✔
59
    this.context = context;
3✔
60
    this.processBuilder = new ProcessBuilder();
7✔
61
    this.errorHandling = ProcessErrorHandling.THROW_ERR;
3✔
62
    Map<String, String> environment = this.processBuilder.environment();
4✔
63
    for (VariableLine var : this.context.getVariables().collectExportedVariables()) {
13✔
64
      if (var.isExport()) {
3!
65
        environment.put(var.getName(), var.getValue());
7✔
66
      }
67
    }
1✔
68
    this.arguments = new ArrayList<>();
5✔
69
    this.extraPathEntries = new ArrayList<>();
5✔
70
  }
1✔
71

72
  @Override
73
  public ProcessContext errorHandling(ProcessErrorHandling handling) {
74

75
    Objects.requireNonNull(handling);
3✔
76
    this.errorHandling = handling;
3✔
77
    return this;
2✔
78
  }
79

80
  @Override
81
  public ProcessContext directory(Path directory) {
82

83
    if (directory != null) {
×
84
      this.processBuilder.directory(directory.toFile());
×
85
    } else {
86
      this.context.debug(
×
87
          "Could not set the process builder's working directory! Directory of the current java process is used.");
88
    }
89

90
    return this;
×
91
  }
92

93
  @Override
94
  public ProcessContext executable(Path command) {
95

96
    if (!this.arguments.isEmpty()) {
4!
97
      throw new IllegalStateException("Arguments already present - did you forget to call run for previous call?");
×
98
    }
99

100
    this.executable = command;
3✔
101
    return this;
2✔
102
  }
103

104
  @Override
105
  public ProcessContext addArg(String arg) {
106

107
    this.arguments.add(arg);
5✔
108
    return this;
2✔
109
  }
110

111
  @Override
112
  public ProcessContext withEnvVar(String key, String value) {
113

114
    if (IdeVariables.PATH.getName().equals(key)) {
5!
115
      this.overriddenPath = value;
×
116
    } else {
117
      this.context.trace("Setting process environment variable {}={}", key, value);
14✔
118
      this.processBuilder.environment().put(key, value);
7✔
119
    }
120
    return this;
2✔
121
  }
122

123
  @Override
124
  public ProcessContext withPathEntry(Path path) {
125

126
    this.extraPathEntries.add(path);
5✔
127
    return this;
2✔
128
  }
129

130
  @Override
131
  public ProcessResult run(ProcessMode processMode) {
132

133
    if (processMode == ProcessMode.DEFAULT) {
3✔
134
      this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
7✔
135
    }
136

137
    if (processMode == ProcessMode.DEFAULT_SILENT) {
3!
138
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
×
139
    }
140

141
    if (this.executable == null) {
3✔
142
      throw new IllegalStateException("Missing executable to run process!");
5✔
143
    }
144

145
    SystemPath systemPath = this.context.getPath();
4✔
146
    if ((this.overriddenPath != null) || !this.extraPathEntries.isEmpty()) {
7!
147
      systemPath = systemPath.withPath(this.overriddenPath, this.extraPathEntries);
7✔
148
    }
149
    String path = systemPath.toString();
3✔
150
    this.context.trace("Setting PATH for process execution of {} to {}", this.executable.getFileName(), path);
16✔
151
    this.executable = systemPath.findBinary(this.executable);
6✔
152
    this.processBuilder.environment().put(IdeVariables.PATH.getName(), path);
8✔
153
    List<String> args = new ArrayList<>(this.arguments.size() + 4);
9✔
154
    String interpreter = addExecutable(args);
4✔
155
    args.addAll(this.arguments);
5✔
156
    String command = createCommand();
3✔
157
    if (this.context.debug().isEnabled()) {
5!
158
      String message = createCommandMessage(interpreter, " ...");
5✔
159
      this.context.debug(message);
4✔
160
    }
161

162
    try {
163

164
      if (processMode == ProcessMode.DEFAULT_CAPTURE) {
3✔
165
        this.processBuilder.redirectOutput(Redirect.PIPE).redirectError(Redirect.PIPE);
8✔
166
      } else if (processMode.isBackground()) {
3✔
167
        modifyArgumentsOnBackgroundProcess(processMode);
4✔
168
      } else {
169
        this.processBuilder.redirectInput(Redirect.INHERIT);
5✔
170
      }
171

172
      this.processBuilder.command(args);
5✔
173

174
      ConcurrentLinkedQueue<OutputMessage> output = new ConcurrentLinkedQueue<>();
4✔
175

176
      Process process = this.processBuilder.start();
4✔
177

178
      try {
179
        if (processMode == ProcessMode.DEFAULT_CAPTURE) {
3✔
180
          CompletableFuture<Void> outFut = readInputStream(process.getInputStream(), false, output);
6✔
181
          CompletableFuture<Void> errFut = readInputStream(process.getErrorStream(), true, output);
6✔
182
          outFut.get();
3✔
183
          errFut.get();
3✔
184
        }
185

186
        int exitCode;
187

188
        if (processMode.isBackground()) {
3✔
189
          exitCode = ProcessResult.SUCCESS;
3✔
190
        } else {
191
          exitCode = process.waitFor();
3✔
192
        }
193

194
        List<OutputMessage> finalOutput = new ArrayList<>(output);
5✔
195
        ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, finalOutput);
11✔
196

197
        performLogging(result, exitCode, interpreter);
5✔
198

199
        return result;
4✔
200
      } finally {
201
        if (!processMode.isBackground()) {
3✔
202
          process.destroy();
2✔
203
        }
204
      }
205
    } catch (CliProcessException | IllegalStateException e) {
1✔
206
      // these exceptions are thrown from performLogOnError and we do not want to wrap them (see #593)
207
      throw e;
2✔
208
    } catch (Exception e) {
1✔
209
      String msg = e.getMessage();
3✔
210
      if ((msg == null) || msg.isEmpty()) {
5!
211
        msg = e.getClass().getSimpleName();
×
212
      }
213
      throw new IllegalStateException(createCommandMessage(interpreter, " failed: " + msg), e);
10✔
214
    } finally {
215
      this.arguments.clear();
3✔
216
    }
217
  }
218

219
  /**
220
   * Asynchronously and parallel reads {@link InputStream input stream} and stores it in {@link CompletableFuture}. Inspired by: <a href=
221
   * "https://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki/57483714#57483714">StackOverflow</a>
222
   *
223
   * @param is {@link InputStream}.
224
   * @param errorStream to identify if the output came from stdout or stderr
225
   * @return {@link CompletableFuture}.
226
   */
227
  private static CompletableFuture<Void> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue<OutputMessage> outputMessages) {
228

229
    return CompletableFuture.supplyAsync(() -> {
6✔
230

231
      try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {
10✔
232

233
        String line;
234
        while ((line = br.readLine()) != null) {
5✔
235
          OutputMessage outputMessage = new OutputMessage(errorStream, line);
6✔
236
          outputMessages.add(outputMessage);
4✔
237
        }
1✔
238

239
        return null;
4✔
240
      } catch (Throwable e) {
1✔
241
        throw new RuntimeException("There was a problem while executing the program", e);
6✔
242
      }
243
    });
244
  }
245

246
  private String createCommand() {
247
    String cmd = this.executable.toString();
4✔
248
    StringBuilder sb = new StringBuilder(cmd.length() + this.arguments.size() * 4);
12✔
249
    sb.append(cmd);
4✔
250
    for (String arg : this.arguments) {
11✔
251
      sb.append(' ');
4✔
252
      sb.append(arg);
4✔
253
    }
1✔
254
    return sb.toString();
3✔
255
  }
256

257
  private String createCommandMessage(String interpreter, String suffix) {
258

259
    StringBuilder sb = new StringBuilder();
4✔
260
    sb.append("Running command '");
4✔
261
    sb.append(this.executable);
5✔
262
    sb.append("'");
4✔
263
    if (interpreter != null) {
2✔
264
      sb.append(" using ");
4✔
265
      sb.append(interpreter);
4✔
266
    }
267
    int size = this.arguments.size();
4✔
268
    if (size > 0) {
2✔
269
      sb.append(" with arguments");
4✔
270
      for (int i = 0; i < size; i++) {
7✔
271
        String arg = this.arguments.get(i);
6✔
272
        sb.append(" '");
4✔
273
        sb.append(arg);
4✔
274
        sb.append("'");
4✔
275
      }
276
    }
277
    sb.append(suffix);
4✔
278
    return sb.toString();
3✔
279
  }
280

281
  private String getSheBang(Path file) {
282

283
    try (InputStream in = Files.newInputStream(file)) {
5✔
284
      // "#!/usr/bin/env bash".length() = 19
285
      byte[] buffer = new byte[32];
3✔
286
      int read = in.read(buffer);
4✔
287
      if ((read > 2) && (buffer[0] == '#') && (buffer[1] == '!')) {
13!
288
        int start = 2;
2✔
289
        int end = 2;
2✔
290
        while (end < read) {
3!
291
          byte c = buffer[end];
4✔
292
          if ((c == '\n') || (c == '\r') || (c > 127)) {
9!
293
            break;
×
294
          } else if ((end == start) && (c == ' ')) {
6!
295
            start++;
×
296
          }
297
          end++;
1✔
298
        }
1✔
299
        String sheBang = new String(buffer, start, end - start, StandardCharsets.US_ASCII).trim();
11✔
300
        if (sheBang.startsWith(PREFIX_USR_BIN_ENV)) {
4!
301
          sheBang = sheBang.substring(PREFIX_USR_BIN_ENV.length());
×
302
        }
303
        return sheBang;
4✔
304
      }
305
    } catch (IOException e) {
5!
306
      // ignore...
307
    }
1✔
308
    return null;
2✔
309
  }
310

311
  private String addExecutable(List<String> args) {
312

313
    String interpreter = null;
2✔
314
    String fileExtension = FilenameUtil.getExtension(this.executable.getFileName().toString());
6✔
315
    boolean isBashScript = "sh".equals(fileExtension);
4✔
316
    this.context.getFileAccess().makeExecutable(this.executable, true);
7✔
317
    if (!isBashScript) {
2✔
318
      String sheBang = getSheBang(this.executable);
5✔
319
      if (sheBang != null) {
2✔
320
        String cmd = sheBang;
2✔
321
        int lastSlash = cmd.lastIndexOf('/');
4✔
322
        if (lastSlash >= 0) {
2!
323
          cmd = cmd.substring(lastSlash + 1);
6✔
324
        }
325
        if (cmd.equals("bash")) {
4!
326
          isBashScript = true;
2✔
327
        } else {
328
          // currently we do not support other interpreters...
329
        }
330
      }
331
    }
332
    if (isBashScript) {
2✔
333
      interpreter = "bash";
2✔
334
      args.add(this.context.findBashRequired());
6✔
335
    }
336
    if ("msi".equalsIgnoreCase(fileExtension)) {
4!
337
      args.add(0, "/i");
×
338
      args.add(0, "msiexec");
×
339
    }
340
    args.add(this.executable.toString());
6✔
341
    return interpreter;
2✔
342
  }
343

344
  private void performLogging(ProcessResult result, int exitCode, String interpreter) {
345

346
    if (!result.isSuccessful() && (this.errorHandling != ProcessErrorHandling.NONE)) {
7!
347
      IdeLogLevel ideLogLevel = this.errorHandling.getLogLevel();
4✔
348
      String message = createCommandMessage(interpreter, "\nfailed with exit code " + exitCode + "!");
6✔
349

350
      context.level(ideLogLevel).log(message);
6✔
351
      result.log(ideLogLevel, context);
5✔
352

353
      if (this.errorHandling == ProcessErrorHandling.THROW_CLI) {
4!
354
        throw new CliProcessException(message, result);
×
355
      } else if (this.errorHandling == ProcessErrorHandling.THROW_ERR) {
4✔
356
        throw new IllegalStateException(message);
5✔
357
      }
358
    }
359
  }
1✔
360

361
  private void modifyArgumentsOnBackgroundProcess(ProcessMode processMode) {
362

363
    if (processMode == ProcessMode.BACKGROUND) {
3✔
364
      this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
8✔
365
    } else if (processMode == ProcessMode.BACKGROUND_SILENT) {
3!
366
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
8✔
367
    } else {
368
      throw new IllegalStateException("Cannot handle non background process mode!");
×
369
    }
370

371
    String bash = this.context.findBash();
4✔
372
    if (bash == null) {
2!
373
      this.context.warning(
×
374
          "Cannot start background process via bash because no bash installation was found. Hence, output will be discarded.");
375
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
×
376
      return;
×
377
    }
378

379
    String commandToRunInBackground = buildCommandToRunInBackground();
3✔
380

381
    this.arguments.clear();
3✔
382
    this.arguments.add(bash);
5✔
383
    this.arguments.add("-c");
5✔
384
    commandToRunInBackground += " & disown";
3✔
385
    this.arguments.add(commandToRunInBackground);
5✔
386

387
  }
1✔
388

389
  private String buildCommandToRunInBackground() {
390

391
    if (this.context.getSystemInfo().isWindows()) {
5!
392

393
      StringBuilder stringBuilder = new StringBuilder();
×
394

395
      for (String argument : this.arguments) {
×
396

397
        if (SystemInfoImpl.INSTANCE.isWindows() && SystemPath.isValidWindowsPath(argument)) {
×
398
          argument = WindowsPathSyntax.MSYS.normalize(argument);
×
399
        }
400

401
        stringBuilder.append(argument);
×
402
        stringBuilder.append(" ");
×
403
      }
×
404
      return stringBuilder.toString().trim();
×
405
    } else {
406
      return this.arguments.stream().map(Object::toString).collect(Collectors.joining(" "));
10✔
407
    }
408
  }
409
}
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

© 2025 Coveralls, Inc