|
| 1 | +package com.googlecode.cppcheclipse.ui; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Collection; |
| 10 | + |
| 11 | +import org.apache.commons.io.FileUtils; |
| 12 | +import org.eclipse.cdt.core.CCorePlugin; |
| 13 | +import org.eclipse.cdt.core.CProjectNature; |
| 14 | +import org.eclipse.cdt.core.cdtvariables.CdtVariableException; |
| 15 | +import org.eclipse.cdt.core.cdtvariables.ICdtVariable; |
| 16 | +import org.eclipse.cdt.core.cdtvariables.ICdtVariableManager; |
| 17 | +import org.eclipse.cdt.core.cdtvariables.IUserVarSupplier; |
| 18 | +import org.eclipse.cdt.core.envvar.EnvironmentVariable; |
| 19 | +import org.eclipse.cdt.core.envvar.IContributedEnvironment; |
| 20 | +import org.eclipse.cdt.core.envvar.IEnvironmentVariable; |
| 21 | +import org.eclipse.cdt.core.envvar.IEnvironmentVariableManager; |
| 22 | +import org.eclipse.cdt.core.model.CoreModel; |
| 23 | +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; |
| 24 | +import org.eclipse.cdt.core.settings.model.ICProjectDescription; |
| 25 | +import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager; |
| 26 | +import org.eclipse.cdt.make.core.MakeCorePlugin; |
| 27 | +import org.eclipse.core.resources.IFile; |
| 28 | +import org.eclipse.core.resources.IFolder; |
| 29 | +import org.eclipse.core.resources.IPathVariableManager; |
| 30 | +import org.eclipse.core.resources.IProject; |
| 31 | +import org.eclipse.core.resources.IProjectDescription; |
| 32 | +import org.eclipse.core.resources.IResource; |
| 33 | +import org.eclipse.core.resources.IWorkspace; |
| 34 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 35 | +import org.eclipse.core.resources.IWorkspaceRunnable; |
| 36 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 37 | +import org.eclipse.core.runtime.CoreException; |
| 38 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 39 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 40 | +import org.junit.After; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.Ignore; |
| 43 | +import org.junit.Test; |
| 44 | +import org.junit.matchers.JUnitMatchers; |
| 45 | + |
| 46 | +public class ToolchainSettingsTest { |
| 47 | + |
| 48 | + private IProject project; |
| 49 | + private ICdtVariableManager cdtVariableManager; |
| 50 | + private IPathVariableManager pathVariableManager; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() throws CoreException, IOException { |
| 54 | + project = createProject("emptyProject"); |
| 55 | + |
| 56 | + cdtVariableManager = CCorePlugin.getDefault().getCdtVariableManager(); |
| 57 | + assertNotNull(cdtVariableManager); |
| 58 | + pathVariableManager = project.getWorkspace().getPathVariableManager(); |
| 59 | + assertNotNull(pathVariableManager); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void tearDown() throws CoreException { |
| 64 | + project.delete(true, null); |
| 65 | + } |
| 66 | + |
| 67 | + // compare with org.eclipse.cdt.core.tests.IEnvironmentVariableManagerTests |
| 68 | + public void addCdtEnvironmentVariable(IProject project, String name, |
| 69 | + String value) throws CoreException { |
| 70 | + ICProjectDescription prjDesc = CoreModel.getDefault() |
| 71 | + .getProjectDescription(project); |
| 72 | + |
| 73 | + IEnvironmentVariableManager envManager = CCorePlugin.getDefault() |
| 74 | + .getBuildEnvironmentManager(); |
| 75 | + IContributedEnvironment contribEnv = envManager |
| 76 | + .getContributedEnvironment(); |
| 77 | + |
| 78 | + ICConfigurationDescription activeConfiguration = prjDesc |
| 79 | + .getActiveConfiguration(); |
| 80 | + |
| 81 | + // Try setting an environment variable |
| 82 | + final IEnvironmentVariable var = new EnvironmentVariable(name, value); |
| 83 | + contribEnv.addVariable(var, activeConfiguration); |
| 84 | + |
| 85 | + // to make the changes on the project description become effective, we |
| 86 | + // have to call setProjectDescription |
| 87 | + CoreModel.getDefault().setProjectDescription(project, prjDesc); |
| 88 | + |
| 89 | + // Get an environment variable: |
| 90 | + IEnvironmentVariable var2 = envManager.getVariable(var.getName(), |
| 91 | + activeConfiguration, true); |
| 92 | + } |
| 93 | + |
| 94 | + public void addCdtUserVariable(IProject project, String name, String value) |
| 95 | + throws CoreException { |
| 96 | + ICProjectDescription prjDesc = CoreModel.getDefault() |
| 97 | + .getProjectDescription(project); |
| 98 | + ICConfigurationDescription activeConfiguration = prjDesc |
| 99 | + .getActiveConfiguration(); |
| 100 | + |
| 101 | + IUserVarSupplier userVarSupplier = CCorePlugin.getUserVarSupplier(); |
| 102 | + userVarSupplier.createMacro(name, ICdtVariable.VALUE_PATH_DIR, value, |
| 103 | + activeConfiguration); |
| 104 | + |
| 105 | + // to make the changes on the project description become effective, we |
| 106 | + // have to call setProjectDescription |
| 107 | + CoreModel.getDefault().setProjectDescription(project, prjDesc); |
| 108 | + } |
| 109 | + |
| 110 | + // add CDT nature |
| 111 | + // http://cdt-devel-faq.wikidot.com/#toc27 |
| 112 | + private void addCDTNature(IProject project) throws CoreException { |
| 113 | + // IProgressMonitor monitor |
| 114 | + CProjectNature.addCNature(project, null); |
| 115 | + ICProjectDescriptionManager mgr = CoreModel.getDefault() |
| 116 | + .getProjectDescriptionManager(); |
| 117 | + ICProjectDescription description = mgr.createProjectDescription( |
| 118 | + project, true); |
| 119 | + description.createConfiguration("config", "config-name", null); |
| 120 | + mgr.setProjectDescription(project, description); |
| 121 | + } |
| 122 | + |
| 123 | + // taken over from |
| 124 | + // org.eclipse.cdt.make.builder.tests.CDataProviderTests.java |
| 125 | + private IProject createProject(final String name) throws CoreException { |
| 126 | + final Object[] result = new Object[1]; |
| 127 | + ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { |
| 128 | + |
| 129 | + @Override |
| 130 | + public void run(IProgressMonitor monitor) throws CoreException { |
| 131 | + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 132 | + IProject project = root.getProject(name); |
| 133 | + IProjectDescription description = null; |
| 134 | + |
| 135 | + if (!project.exists()) { |
| 136 | + project.create(null); |
| 137 | + } else { |
| 138 | + project.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 139 | + } |
| 140 | + |
| 141 | + if (!project.isOpen()) { |
| 142 | + project.open(null); |
| 143 | + } |
| 144 | + |
| 145 | + description = project.getDescription(); |
| 146 | + |
| 147 | + CCorePlugin.getDefault().createCDTProject(description, project, |
| 148 | + MakeCorePlugin.CFG_DATA_PROVIDER_ID, |
| 149 | + new NullProgressMonitor()); |
| 150 | + result[0] = project; |
| 151 | + } |
| 152 | + }, null); |
| 153 | + return (IProject) result[0]; |
| 154 | + } |
| 155 | + |
| 156 | + // this test does only run on specific platforms (in this case only |
| 157 | + // Unix-based systems) |
| 158 | + @Test |
| 159 | + public void testResolveIncludePath() throws CoreException { |
| 160 | + // should make the test listed in issue 39, |
| 161 | + // http://code.google.com/a/eclipselabs.org/p/cppcheclipse/issues/detail?id=39 |
| 162 | + |
| 163 | + // URI |
| 164 | + addCdtUserVariable(project, "FOOBAR", "/test/a/b/c"); |
| 165 | + checkResolveIncludePath("${FOOBAR}/test", "/test/a/b/c/test"); |
| 166 | + |
| 167 | + // non URI with space |
| 168 | + addCdtUserVariable(project, "FOOBAR", "C:\\a a"); |
| 169 | + checkResolveIncludePath("${FOOBAR}\\test", "C:\\a a\\test"); |
| 170 | + |
| 171 | + // use Windows path as URI with space |
| 172 | + addCdtUserVariable(project, "FOOBAR", "/C:/a a"); |
| 173 | + checkResolveIncludePath("${FOOBAR}/test", "/C:/a a/test"); |
| 174 | + |
| 175 | + // check with Eclipse Workspace Variable |
| 176 | + checkResolveIncludePath("/${ProjName}/toto", "/emptyProject/toto"); |
| 177 | + } |
| 178 | + |
| 179 | + @Test(expected = CdtVariableException.class) |
| 180 | + public void testResolveIncludePathInexistentCdtVariable() |
| 181 | + throws CdtVariableException { |
| 182 | + checkResolveIncludePath("${FOOBAR}/test", "/test/a/b/c/test"); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @Ignore |
| 187 | + public void testResolveIncludePathLinked() throws CoreException { |
| 188 | + // add linked path |
| 189 | + IFolder linkedFolder = project.getFolder("linkedFolder"); |
| 190 | + IFolder realFolder = project.getFolder("realFolder"); |
| 191 | + realFolder.create(true, true, null); |
| 192 | + IFile file = realFolder.getFile("file1"); |
| 193 | + // file.create(); |
| 194 | + |
| 195 | + linkedFolder.createLink(realFolder.getLocationURI(), IResource.REPLACE, |
| 196 | + null); |
| 197 | + |
| 198 | + // TODO: check that the real file is resolved |
| 199 | + checkResolveIncludePath("/${ProjName}/toto", "/emptyProject/toto"); |
| 200 | + |
| 201 | + // TODO: check absolute filename |
| 202 | + } |
| 203 | + |
| 204 | + private void checkResolveIncludePath(String includePath, |
| 205 | + String expectedResolvedPath) throws CdtVariableException { |
| 206 | + |
| 207 | + ToolchainSettings toolchainSettings = new ToolchainSettings(project); |
| 208 | + Collection<File> files = toolchainSettings.resolveIncludePath(new File( |
| 209 | + includePath), pathVariableManager); |
| 210 | + assertEquals(1, files.size()); |
| 211 | + assertThat(files, JUnitMatchers.hasItem(new File(expectedResolvedPath))); |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments