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

devonfw / IDEasy / 13373333149

17 Feb 2025 03:11PM UTC coverage: 67.971% (+0.05%) from 67.919%
13373333149

push

github

web-flow
#925: IDEasy cannot handle faulty JAVA_HOME definition (#997)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
Co-authored-by: jan-vcapgemini <jan-vincent.hoelzle@capgemini.com>

2970 of 4803 branches covered (61.84%)

Branch coverage included in aggregate %.

7711 of 10911 relevant lines covered (70.67%)

3.07 hits per line

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

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

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

8
import com.devonfw.tools.ide.cli.CliExitException;
9
import com.devonfw.tools.ide.context.AbstractIdeContext;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
12
import com.devonfw.tools.ide.environment.VariableLine;
13
import com.devonfw.tools.ide.environment.VariableSource;
14
import com.devonfw.tools.ide.log.IdeLogLevel;
15
import com.devonfw.tools.ide.log.IdeSubLogger;
16
import com.devonfw.tools.ide.os.WindowsPathSyntax;
17
import com.devonfw.tools.ide.process.EnvironmentContext;
18
import com.devonfw.tools.ide.process.EnvironmentVariableCollectorContext;
19
import com.devonfw.tools.ide.property.FlagProperty;
20
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
21
import com.devonfw.tools.ide.tool.ToolInstallation;
22
import com.devonfw.tools.ide.version.VersionIdentifier;
23

24
/**
25
 * {@link Commandlet} to print the environment variables.
26
 */
27
public final class EnvironmentCommandlet extends Commandlet {
28

29
  /** {@link FlagProperty} to enable Bash (MSys) path conversion on Windows. */
30
  public final FlagProperty bash;
31

32
  /**
33
   * The constructor.
34
   *
35
   * @param context the {@link IdeContext}.
36
   */
37
  public EnvironmentCommandlet(IdeContext context) {
38

39
    super(context);
3✔
40
    addKeyword(getName());
4✔
41
    this.bash = add(new FlagProperty("--bash"));
9✔
42
  }
1✔
43

44
  @Override
45
  public String getName() {
46

47
    return "env";
2✔
48
  }
49

50
  @Override
51
  public boolean isIdeHomeRequired() {
52

53
    return false;
2✔
54
  }
55

56
  @Override
57
  public boolean isProcessableOutput() {
58

59
    return true;
×
60
  }
61

62
  @Override
63
  public void run() {
64
    if (this.context.getIdeHome() == null) {
4!
65
      throw new CliExitException();
×
66
    }
67

68
    boolean winCmd = false;
2✔
69
    WindowsPathSyntax pathSyntax = null;
2✔
70
    IdeSubLogger logger = this.context.level(IdeLogLevel.PROCESSABLE);
5✔
71
    if (this.context.getSystemInfo().isWindows()) {
5!
72
      if (this.bash.isTrue()) {
×
73
        pathSyntax = WindowsPathSyntax.MSYS;
×
74
      } else {
75
        winCmd = true;
×
76
        pathSyntax = WindowsPathSyntax.WINDOWS;
×
77
      }
78
    }
79

80
    ((AbstractIdeContext) this.context).setPathSyntax(pathSyntax);
5✔
81
    List<VariableLine> variables = this.context.getVariables().collectVariables();
5✔
82
    Map<String, VariableLine> variableMap = variables.stream().collect(Collectors.toMap(VariableLine::getName, v -> v));
10✔
83

84
    EnvironmentVariableCollectorContext environmentVariableCollectorContext = new EnvironmentVariableCollectorContext(variableMap,
8✔
85
        new VariableSource(EnvironmentVariablesType.TOOL, this.context.getSoftwarePath()), pathSyntax);
5✔
86
    setEnvironmentVariablesInLocalTools(environmentVariableCollectorContext);
3✔
87

88
    printLines(variableMap, logger, winCmd);
5✔
89
  }
1✔
90

91
  private void printLines(Map<String, VariableLine> variableMap, IdeSubLogger logger, boolean winCmd) {
92
    if (this.context.debug().isEnabled()) {
5✔
93
      Map<EnvironmentVariablesType, List<VariableLine>> type2lines = variableMap.values().stream().collect(Collectors.groupingBy(l -> l.getSource().type()));
12✔
94
      for (EnvironmentVariablesType type : EnvironmentVariablesType.values()) {
16✔
95
        List<VariableLine> lines = type2lines.get(type);
5✔
96
        if (lines != null) {
2✔
97
          boolean sourcePrinted = false;
2✔
98
          sortVariables(lines);
2✔
99
          for (VariableLine line : lines) {
10✔
100
            if (!sourcePrinted) {
2✔
101
              this.context.debug("from {}:", line.getSource());
11✔
102
              sourcePrinted = true;
2✔
103
            }
104
            logger.log(format(line, winCmd));
6✔
105
          }
1✔
106
        }
107
      }
108
    } else {
1✔
109
      List<VariableLine> variables = new ArrayList<>(variableMap.values());
6✔
110
      sortVariables(variables);
2✔
111
      for (VariableLine line : variables) {
10✔
112
        logger.log(format(line, winCmd));
6✔
113
      }
1✔
114
    }
115
  }
1✔
116

117
  private static void sortVariables(List<VariableLine> lines) {
118

119
    lines.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
9✔
120
  }
1✔
121

122
  private String format(VariableLine line, boolean winCmd) {
123

124
    if (winCmd) {
2!
125
      return line.getName() + "=" + line.getValue();
×
126
    } else {
127
      String lineValue = line.getValue();
3✔
128
      lineValue = "\"" + lineValue + "\"";
3✔
129
      line = line.withValue(lineValue);
4✔
130
      return line.toString();
3✔
131
    }
132
  }
133

134
  private void setEnvironmentVariablesInLocalTools(EnvironmentContext environmentContext) {
135
    // installed tools in IDE_HOME/software
136
    for (Commandlet commandlet : this.context.getCommandletManager().getCommandlets()) {
13✔
137
      if (commandlet instanceof LocalToolCommandlet tool) {
6✔
138
        VersionIdentifier installedVersion = tool.getInstalledVersion();
3✔
139
        if (installedVersion != null) {
2✔
140
          ToolInstallation toolInstallation = new ToolInstallation(tool.getToolPath(), tool.getToolPath(),
7✔
141
              tool.getToolBinPath(), installedVersion, false);
5✔
142
          tool.setEnvironment(environmentContext, toolInstallation, false);
5✔
143
        }
144
      }
145
    }
1✔
146
  }
1✔
147
}
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