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

devonfw / IDEasy / 11963246771

21 Nov 2024 11:10PM UTC coverage: 67.428% (+0.2%) from 67.239%
11963246771

push

github

web-flow
#754: bullet proof solution for processable output and logging (#777)

2487 of 4028 branches covered (61.74%)

Branch coverage included in aggregate %.

6454 of 9232 relevant lines covered (69.91%)

3.09 hits per line

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

83.87
cli/src/main/java/com/devonfw/tools/ide/commandlet/EnvironmentCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.List;
4
import java.util.Map;
5
import java.util.stream.Collectors;
6

7
import com.devonfw.tools.ide.context.AbstractIdeContext;
8
import com.devonfw.tools.ide.context.IdeContext;
9
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
10
import com.devonfw.tools.ide.environment.VariableLine;
11
import com.devonfw.tools.ide.log.IdeLogLevel;
12
import com.devonfw.tools.ide.log.IdeSubLogger;
13
import com.devonfw.tools.ide.os.WindowsPathSyntax;
14
import com.devonfw.tools.ide.property.FlagProperty;
15

16
/**
17
 * {@link Commandlet} to print the environment variables.
18
 */
19
public final class EnvironmentCommandlet extends Commandlet {
20

21
  /** {@link FlagProperty} to enable Bash (MSys) path conversion on Windows. */
22
  public final FlagProperty bash;
23

24
  /**
25
   * The constructor.
26
   *
27
   * @param context the {@link IdeContext}.
28
   */
29
  public EnvironmentCommandlet(IdeContext context) {
30

31
    super(context);
3✔
32
    addKeyword(getName());
4✔
33
    this.bash = add(new FlagProperty("--bash"));
9✔
34
  }
1✔
35

36
  @Override
37
  public String getName() {
38

39
    return "env";
2✔
40
  }
41

42
  @Override
43
  public boolean isIdeHomeRequired() {
44

45
    return true;
2✔
46
  }
47

48
  @Override
49
  public boolean isProcessableOutput() {
50

51
    return true;
×
52
  }
53

54
  @Override
55
  public void run() {
56

57
    boolean winCmd = false;
2✔
58
    WindowsPathSyntax pathSyntax = null;
2✔
59
    IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
5✔
60
    if (this.context.getSystemInfo().isWindows()) {
5!
61
      if (this.bash.isTrue()) {
×
62
        pathSyntax = WindowsPathSyntax.MSYS;
×
63
      } else {
64
        winCmd = true;
×
65
        pathSyntax = WindowsPathSyntax.WINDOWS;
×
66
      }
67
    }
68
    ((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
5✔
69
    List<VariableLine> variables = this.context.getVariables().collectVariables();
5✔
70
    if (this.context.debug().isEnabled()) {
5✔
71
      Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variables.stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
11✔
72
      for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
16✔
73
        List<VariableLine> lines = type2lines.get(type);
5✔
74
        if (lines != null) {
2✔
75
          boolean sourcePrinted = false;
2✔
76
          sortVariables(lines);
2✔
77
          for (VariableLine line : lines) {
10✔
78
            if (!sourcePrinted) {
2✔
79
              this.context.debug("from {}:", line.getSource());
11✔
80
              sourcePrinted = true;
2✔
81
            }
82
            logger.log(format(line, winCmd));
6✔
83
          }
1✔
84
        }
85
      }
86
    } else {
1✔
87
      sortVariables(variables);
2✔
88
      for (VariableLine line : variables) {
10✔
89
        logger.log(format(line, winCmd));
6✔
90
      }
1✔
91
    }
92
  }
1✔
93

94
  private static void sortVariables(List<VariableLine> lines) {
95
    lines.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
9✔
96
  }
1✔
97

98
  private String format(VariableLine line, boolean winCmd) {
99

100
    if (winCmd) {
2!
101
      return line.getName() + "=" + line.getValue();
×
102
    } else {
103
      String lineValue = line.getValue();
3✔
104
      lineValue = "\"" + lineValue + "\"";
3✔
105
      line = line.withValue(lineValue);
4✔
106
      return line.toString();
3✔
107
    }
108
  }
109
}
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