Skip to content

Commit f49f74a

Browse files
authored
#287: fixed failing tests in FileAccessImplTest (#289)
1 parent 3b37c2c commit f49f74a

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

Diff for: cli/src/main/java/com/devonfw/tools/ide/environment/AbstractEnvironmentVariables.java

+32-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.devonfw.tools.ide.environment;
22

3+
import com.devonfw.tools.ide.context.IdeContext;
4+
import com.devonfw.tools.ide.variable.IdeVariables;
5+
import com.devonfw.tools.ide.variable.VariableDefinition;
6+
37
import java.nio.file.Path;
48
import java.util.ArrayList;
59
import java.util.Collection;
@@ -9,19 +13,14 @@
913
import java.util.regex.Matcher;
1014
import java.util.regex.Pattern;
1115

12-
import com.devonfw.tools.ide.context.IdeContext;
13-
import com.devonfw.tools.ide.variable.IdeVariables;
14-
import com.devonfw.tools.ide.variable.VariableDefinition;
15-
1616
/**
1717
* Abstract base implementation of {@link EnvironmentVariables}.
1818
*/
1919
public abstract class AbstractEnvironmentVariables implements EnvironmentVariables {
2020

2121
/**
22-
* When we replace variable expressions with their value the resulting {@link String} can change in size (shrink or
23-
* grow). By adding a bit of extra capacity we reduce the chance that the capacity is too small and a new buffer array
24-
* has to be allocated and array-copy has to be performed.
22+
* When we replace variable expressions with their value the resulting {@link String} can change in size (shrink or grow). By adding a bit of extra capacity
23+
* we reduce the chance that the capacity is too small and a new buffer array has to be allocated and array-copy has to be performed.
2524
*/
2625
private static final int EXTRA_CAPACITY = 8;
2726

@@ -124,7 +123,9 @@ private final Collection<VariableLine> collectVariables(boolean onlyExported) {
124123
boolean export = isExported(name);
125124
if (!onlyExported || export) {
126125
String value = get(name);
127-
variables.add(VariableLine.of(export, name, value));
126+
if (value != null) {
127+
variables.add(VariableLine.of(export, name, value));
128+
}
128129
}
129130
}
130131
return variables;
@@ -141,8 +142,7 @@ protected void collectVariables(Set<String> variables) {
141142
}
142143

143144
/**
144-
* @param propertiesFilePath the {@link #getPropertiesFilePath() propertiesFilePath} of the child
145-
* {@link EnvironmentVariables}.
145+
* @param propertiesFilePath the {@link #getPropertiesFilePath() propertiesFilePath} of the child {@link EnvironmentVariables}.
146146
* @param type the {@link #getType() type}.
147147
* @return the new {@link EnvironmentVariables}.
148148
*/
@@ -152,8 +152,7 @@ public AbstractEnvironmentVariables extend(Path propertiesFilePath, EnvironmentV
152152
}
153153

154154
/**
155-
* @return a new child {@link EnvironmentVariables} that will resolve variables recursively or this instance itself if
156-
* already satisfied.
155+
* @return a new child {@link EnvironmentVariables} that will resolve variables recursively or this instance itself if already satisfied.
157156
*/
158157
public EnvironmentVariables resolved() {
159158

@@ -169,35 +168,30 @@ public String resolve(String string, Object src) {
169168
/**
170169
* This method is called recursively. This allows you to resolve variables that are defined by other variables.
171170
*
172-
* @param value the {@link String} that potentially contains variables in the syntax "${«variable«}". Those will be
173-
* resolved by this method and replaced with their {@link #get(String) value}.
174-
* @param src the source where the {@link String} to resolve originates from. Should have a reasonable
175-
* {@link Object#toString() string representation} that will be used in error or log messages if a variable
176-
* could not be resolved.
171+
* @param value the {@link String} that potentially contains variables in the syntax "${«variable«}". Those will be resolved by this method and replaced with
172+
* their {@link #get(String) value}.
173+
* @param src the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation} that
174+
* will be used in error or log messages if a variable could not be resolved.
177175
* @param recursion the current recursion level. This is used to interrupt endless recursion.
178176
* @param rootSrc the root source where the {@link String} to resolve originates from.
179177
* @param rootValue the root value to resolve.
180-
* @param resolvedVars this is a reference to an object of {@link EnvironmentVariablesResolved} being the lowest level
181-
* in the {@link EnvironmentVariablesType hierarchy} of variables. In case of a self-referencing variable
182-
* {@code x} the resolving has to continue one level higher in the {@link EnvironmentVariablesType hierarchy}
183-
* to avoid endless recursion. The {@link EnvironmentVariablesResolved} is then used if another variable
184-
* {@code y} must be resolved, since resolving this variable has to again start at the lowest level. For
185-
* example: For levels {@code l1, l2} with {@code l1 < l2} and {@code x=${x} foo} and {@code y=bar} defined at
186-
* level {@code l1} and {@code x=test ${y}} defined at level {@code l2}, {@code x} is first resolved at level
187-
* {@code l1} and then up the {@link EnvironmentVariablesType hierarchy} at {@code l2} to avoid endless
188-
* recursion. However, {@code y} must be resolved starting from the lowest level in the
189-
* {@link EnvironmentVariablesType hierarchy} and therefore {@link EnvironmentVariablesResolved} is used.
178+
* @param resolvedVars this is a reference to an object of {@link EnvironmentVariablesResolved} being the lowest level in the
179+
* {@link EnvironmentVariablesType hierarchy} of variables. In case of a self-referencing variable {@code x} the resolving has to continue one level higher in
180+
* the {@link EnvironmentVariablesType hierarchy} to avoid endless recursion. The {@link EnvironmentVariablesResolved} is then used if another variable
181+
* {@code y} must be resolved, since resolving this variable has to again start at the lowest level. For example: For levels {@code l1, l2} with
182+
* {@code l1 < l2} and {@code x=${x} foo} and {@code y=bar} defined at level {@code l1} and {@code x=test ${y}} defined at level {@code l2}, {@code x} is
183+
* first resolved at level {@code l1} and then up the {@link EnvironmentVariablesType hierarchy} at {@code l2} to avoid endless recursion. However, {@code y}
184+
* must be resolved starting from the lowest level in the {@link EnvironmentVariablesType hierarchy} and therefore {@link EnvironmentVariablesResolved} is
185+
* used.
190186
* @return the given {@link String} with the variables resolved.
191187
*/
192-
private String resolve(String value, Object src, int recursion, Object rootSrc, String rootValue,
193-
AbstractEnvironmentVariables resolvedVars) {
188+
private String resolve(String value, Object src, int recursion, Object rootSrc, String rootValue, AbstractEnvironmentVariables resolvedVars) {
194189

195190
if (value == null) {
196191
return null;
197192
}
198193
if (recursion > MAX_RECURSION) {
199-
throw new IllegalStateException("Reached maximum recursion resolving " + value + " for root variable " + rootSrc
200-
+ " with value '" + rootValue + "'.");
194+
throw new IllegalStateException("Reached maximum recursion resolving " + value + " for root variable " + rootSrc + " with value '" + rootValue + "'.");
201195
}
202196
recursion++;
203197

@@ -210,15 +204,13 @@ private String resolve(String value, Object src, int recursion, Object rootSrc,
210204
String variableName = matcher.group(2);
211205
String variableValue = resolvedVars.getValue(variableName, false);
212206
if (variableValue == null) {
213-
this.context.warning("Undefined variable {} in '{}={}' for root '{}={}'", variableName, src, value, rootSrc,
214-
rootValue);
207+
this.context.warning("Undefined variable {} in '{}={}' for root '{}={}'", variableName, src, value, rootSrc, rootValue);
215208
continue;
216209
}
217210
EnvironmentVariables lowestFound = findVariable(variableName);
218211
if ((lowestFound == null) || !lowestFound.getFlat(variableName).equals(value)) {
219212
// looking for "variableName" starting from resolved upwards the hierarchy
220-
String replacement = resolvedVars.resolve(variableValue, variableName, recursion, rootSrc, rootValue,
221-
resolvedVars);
213+
String replacement = resolvedVars.resolve(variableValue, variableName, recursion, rootSrc, rootValue, resolvedVars);
222214
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
223215
} else { // is self referencing
224216
// finding next occurrence of "variableName" up the hierarchy of EnvironmentVariablesType
@@ -235,8 +227,8 @@ private String resolve(String value, Object src, int recursion, Object rootSrc,
235227
}
236228
// resolving a self referencing variable one level up the hierarchy of EnvironmentVariablesType, i.e. at "next",
237229
// to avoid endless recursion
238-
String replacement = ((AbstractEnvironmentVariables) next).resolve(next.getFlat(variableName), variableName,
239-
recursion, rootSrc, rootValue, resolvedVars);
230+
String replacement = ((AbstractEnvironmentVariables) next).resolve(next.getFlat(variableName), variableName, recursion, rootSrc, rootValue,
231+
resolvedVars);
240232
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
241233

242234
}
@@ -248,13 +240,11 @@ private String resolve(String value, Object src, int recursion, Object rootSrc,
248240
}
249241

250242
/**
251-
* Like {@link #get(String)} but with higher-level features including to resolve {@link IdeVariables} with their
252-
* default values.
243+
* Like {@link #get(String)} but with higher-level features including to resolve {@link IdeVariables} with their default values.
253244
*
254245
* @param name the name of the variable to get.
255-
* @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default
256-
* value} of a potential {@link VariableDefinition} shall be ignored, {@code false} to return default instead
257-
* of {@code null}.
246+
* @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default value} of a potential
247+
* {@link VariableDefinition} shall be ignored, {@code false} to return default instead of {@code null}.
258248
* @return the value of the variable.
259249
*/
260250
protected String getValue(String name, boolean ignoreDefaultValue) {

0 commit comments

Comments
 (0)