1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

bug 284843: Environment Variable with value ":" gets replaced by empty string ""

Patch from Marc-Andre Laperle
This commit is contained in:
Andrew Gvozdev 2009-12-27 00:54:55 +00:00
parent a618ada877
commit 2128ed9b3a
2 changed files with 75 additions and 1 deletions

View file

@ -509,4 +509,68 @@ public class IEnvironmentVariableManagerTests extends TestCase {
assertNull(readVar); assertNull(readVar);
} }
/**
* This test checks if an environment variable is only processed as a list
* if it matches a certain pattern. ([^:]+:)+[^:]* (; on Windows)
*
* If a variable is a list, it is split into a String array depending on the
* delimiter given. At some point, this array is used to build a new
* String representing the list, separated by the delimiter. This should
* only happen when the variable matches the pattern. For example, if a
* variable has a value of ':' without the quotes, it shouldn't processed as
* a list even if it contains a delimiter because if it was, it would give
* an empty string when built since there are no items in the list.
*
* @throws Exception
*/
public void testBug284843() throws Exception{
final IProject project = ResourceHelper.createCDTProjectWithConfig("envProject");
ICProjectDescription prjDesc = CoreModel.getDefault().getProjectDescription(project);
IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
IContributedEnvironment contribEnv = envManager.getContributedEnvironment();
ICConfigurationDescription confDesc = prjDesc.getActiveConfiguration();
String delimiter = System.getProperty("path.separator");
// Create the test variables
IEnvironmentVariable varDelim = new EnvironmentVariable("DELIM",
delimiter);
String varListValue = "value1" + delimiter + "value2" + delimiter + "value3";
IEnvironmentVariable varList = new EnvironmentVariable("LIST", varListValue);
IEnvironmentVariable varListDelim = new EnvironmentVariable("LISTDELIM",
"value1" + delimiter);
IEnvironmentVariable varListDelims = new EnvironmentVariable("LISTDELIMS",
varListValue + delimiter);
String varInvalidListValue = delimiter + "value1" + delimiter + "value2" + delimiter + delimiter + "value3" + delimiter;
IEnvironmentVariable varInvalidList = new EnvironmentVariable("INVALIDLIST", varInvalidListValue);
// Add the variables to the contributed environment
contribEnv.addVariable(varDelim, confDesc);
contribEnv.addVariable(varList, confDesc);
contribEnv.addVariable(varListDelim, confDesc);
contribEnv.addVariable(varListDelims, confDesc);
contribEnv.addVariable(varInvalidList, confDesc);
// Get the processed variables
varDelim = envManager.getVariable(varDelim.getName(), confDesc, true);
varList = envManager.getVariable(varList.getName(), confDesc, true);
varListDelim = envManager.getVariable(varListDelim.getName(), confDesc, true);
varListDelims = envManager.getVariable(varListDelims.getName(), confDesc, true);
varInvalidList = envManager.getVariable(varInvalidList.getName(), confDesc, true);
// Should keep the same value, not a list
assertEquals(delimiter, varDelim.getValue());
// Should keep the same value, processed as a list
assertEquals(varListValue, varList.getValue());
// The delimiter will be trimmed, processed as a list
assertEquals("value1", varListDelim.getValue());
// The last delimiter will be trimmed, processed as a list
assertEquals(varListValue, varListDelims.getValue());
// Should keep the same value, not a list
assertEquals(varInvalidListValue,varInvalidList.getValue());
}
} }

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.internal.core.cdtvariables; package org.eclipse.cdt.internal.core.cdtvariables;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.cdt.core.cdtvariables.CdtVariable; import org.eclipse.cdt.core.cdtvariables.CdtVariable;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException; import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
@ -42,7 +43,7 @@ public class EnvironmentVariableSupplier extends CoreMacroSupplierBase {
String value = var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE ? String value = var.getOperation() != IEnvironmentVariable.ENVVAR_REMOVE ?
var.getValue() : null; var.getValue() : null;
if(delimiter != null && !"".equals(delimiter)){ //$NON-NLS-1$ if(isTextList(value,delimiter)){
fType = VALUE_TEXT_LIST; fType = VALUE_TEXT_LIST;
if(value != null){ if(value != null){
List<String> list = EnvVarOperationProcessor.convertToList(value,delimiter); List<String> list = EnvVarOperationProcessor.convertToList(value,delimiter);
@ -99,6 +100,15 @@ public class EnvironmentVariableSupplier extends CoreMacroSupplierBase {
fEnvironmentProvider = varProvider; fEnvironmentProvider = varProvider;
} }
private static boolean isTextList(String str, String delimiter) {
if (delimiter == null || "".equals(delimiter)) //$NON-NLS-1$
return false;
// Regex: ([^:]+:)+[^:]*
String patternStr = "([^" + delimiter + "]+" + delimiter + ")+[^" + delimiter + "]*"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
return Pattern.matches(patternStr, str);
}
public ICdtVariable createBuildMacro(IEnvironmentVariable var){ public ICdtVariable createBuildMacro(IEnvironmentVariable var){
if(var != null) if(var != null)
return new EnvVarMacro(var); return new EnvVarMacro(var);