From 2ce22c4cdd342b82d8c684fe2a76f787ccfd55fa Mon Sep 17 00:00:00 2001 From: Neil Guzman Date: Wed, 13 Nov 2013 17:39:05 -0500 Subject: RPM: filter out %{?...} macros from outline The eval function is calling the "rpm" command but to do this the plugin tries to get a RemoteService to get the system environment variables. The RemoteService is returning null because it cannot find a remoteService extension point for RSE scheme. For now it is temporarily better to filter out the %{?...} macros from the outline entries rather than having NPEs and showing nothing at all. Bug 421480 Change-Id: I941c2a33b4642097c87410ff860630a5f079a263 Reviewed-on: https://git.eclipse.org/r/18359 Tested-by: Hudson CI Reviewed-by: Alexander Kurtakov IP-Clean: Alexander Kurtakov Tested-by: Alexander Kurtakov --- .../linuxtools/rpm/ui/editor/tests/AllTests.java | 2 +- .../rpm/ui/editor/tests/LabelProviderTest.java | 70 ++++++++++++++++++++++ .../ui/editor/outline/SpecfileLabelProvider.java | 64 ++++++++++++-------- 3 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/LabelProviderTest.java diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/AllTests.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/AllTests.java index cea2154349..d9208db2c7 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/AllTests.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/AllTests.java @@ -25,6 +25,6 @@ import org.junit.runners.Suite; LinePositionTest.class, RpmMacroProposalsListTest.class, RpmPackageProposalsListTest.class, ActionsAllTests.class, ScannersAllTests.class, ParserAllTests.class, HyperlinkAllTests.class, - SpecfileCompletionProcessorTest.class }) + SpecfileCompletionProcessorTest.class, LabelProviderTest.class }) public class AllTests { } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/LabelProviderTest.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/LabelProviderTest.java new file mode 100644 index 0000000000..0ea2beee15 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/LabelProviderTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2013 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.rpm.ui.editor.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.eclipse.linuxtools.internal.rpm.ui.editor.outline.SpecfileLabelProvider; +import org.junit.Before; +import org.junit.Test; + +/** + * Test case to make sure that the label provider is filtering out + * some macros (e.g., %{?scl_prefix}). + */ +public class LabelProviderTest extends FileTestCase { + + private SpecfileLabelProvider labelProvider; + private String correctResult = "eclipse-plugin"; + + @Before + public void initialize() { + labelProvider = new SpecfileLabelProvider(); + } + + /** + * Test to see if %{?...} macros will be shown. They should not. + */ + @Test + public void testLabelForUnresolvedMacro() { + String testText = "%{?some_macro}eclipse-plugin"; + String result = labelProvider.getText(testText); + assertEquals(result, correctResult); + + testText = "eclipse-plugin%{?some_macro}"; + result = labelProvider.getText(testText); + assertEquals(result, correctResult); + + testText = "%{?some_macro}eclipse-plugin%{?some_macro}"; + result = labelProvider.getText(testText); + assertEquals(result, correctResult); + } + + /** + * Test for incorrect labels. + */ + @Test + public void testLabelForIncorrectString() { + // for sake of test, this is just to show that the filterMacros + // method would not filter %{...} regular macros. + // It is the job of SpecfileDefine to do that. + String testText = "%{no_question_mark}eclipse-plugin"; + String result = labelProvider.getText(testText); + assertNotEquals(result, correctResult); + + testText = "{?no_percent_sign}eclipse-plugin"; + result = labelProvider.getText(testText); + assertNotEquals(result, correctResult); + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/outline/SpecfileLabelProvider.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/outline/SpecfileLabelProvider.java index 985816bbb1..38cd030e5e 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/outline/SpecfileLabelProvider.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/outline/SpecfileLabelProvider.java @@ -11,14 +11,14 @@ package org.eclipse.linuxtools.internal.rpm.ui.editor.outline; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator; -import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePreamble; -import org.eclipse.linuxtools.rpm.core.utils.RPMQuery; import org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile; import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileElement; import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfilePackage; @@ -68,29 +68,24 @@ public class SpecfileLabelProvider implements ILabelProvider { @Override public String getText(Object element) { String str = ""; //$NON-NLS-1$ - try { - if (element instanceof SpecfileSection) { - SpecfileSection specfileSection = (SpecfileSection) element; - str = specfileSection.toString(); - } else if (element instanceof Specfile) { - str = ((Specfile) element).getName(); - } else if (element instanceof SpecfilePackageContainer) { - str = Messages.SpecfileLabelProvider_0; - } else if (element instanceof SpecfilePreamble){ - str = Messages.SpecfileLabelProvider_1; - } else if (element instanceof SpecfileElement) { - SpecfileElement specfileElement = (SpecfileElement) element; - str = specfileElement.getName(); - } else if (element instanceof String) { - str = (String) element; - } else if (element instanceof SpecfilePackage) { - str = ((SpecfilePackage) element).getName(); - } - str = RPMQuery.eval(project, str).trim(); - } catch (CoreException e) { - SpecfileLog.logError("Within Project Outline, unable to evaluate " + str, e); //$NON-NLS-1$ + if (element instanceof SpecfileSection) { + SpecfileSection specfileSection = (SpecfileSection) element; + str = specfileSection.toString(); + } else if (element instanceof Specfile) { + str = ((Specfile) element).getName(); + } else if (element instanceof SpecfilePackageContainer) { + str = Messages.SpecfileLabelProvider_0; + } else if (element instanceof SpecfilePreamble){ + str = Messages.SpecfileLabelProvider_1; + } else if (element instanceof SpecfileElement) { + SpecfileElement specfileElement = (SpecfileElement) element; + str = specfileElement.getName(); + } else if (element instanceof String) { + str = (String) element; + } else if (element instanceof SpecfilePackage) { + str = ((SpecfilePackage) element).getName(); } - return str; + return filterMacros(str.trim()); } /** @@ -100,4 +95,23 @@ public class SpecfileLabelProvider implements ILabelProvider { protected void setProject(IProject project) { this.project=project; } + + /** + * Remove any unresolved macros from the string. These are + * macros that follow the format %{?...} (e.g. %{?scl_prefix}). + * + * @param text The text to filter macros out from. + * @return A string without unresolved macros. + * + * @since 2.1 + */ + private String filterMacros(String text) { + Pattern variablePattern = Pattern.compile("%\\{\\?\\w+\\}"); //$NON-NLS-1$ + Matcher variableMatcher = variablePattern.matcher(text); + while (variableMatcher.find()) { + text = text.replace(variableMatcher.group(0), ""); //$NON-NLS-1$ + } + return text; + } + } -- cgit v1.2.3