Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c45672b3de40fff414131d9733e031d11f49ee74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*******************************************************************************
 * Copyright (c) 2008, 2013 Nokia and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Nokia (Ed Swartz) - initial API and implementation
 *     Wind River Systems - Bug 338936
 *******************************************************************************/
package org.eclipse.cdt.make.core.tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IMakefileReaderProvider;
import org.eclipse.cdt.make.core.makefile.IRule;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class MakefileReaderProviderTests extends TestCase {
	private String[] inclDirs;

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		String basePath = "";
		File baseDir = getPluginRelativeFile(new Path("data"));
		if (baseDir != null)
			basePath = baseDir.getAbsolutePath() + File.separator;
		inclDirs = new String[] { basePath + "bogus", basePath + "incl" };
	}

	public void testNoReaderProvider() throws Exception {
		IPath path = new Path("data/Makefile.main");
		File file = getPluginRelativeFile(path);
		// doesn't work in packaged plugin, which is fine
		if (file != null) {
			IMakefile makefile = MakeCorePlugin.createMakefile(URIUtil.toURI(file.getAbsolutePath()), true, inclDirs);
			assertMakefileContents(makefile);
		}
	}

	public void testNullReaderProvider() throws Exception {
		IPath path = new Path("data/Makefile.main");
		File file = getPluginRelativeFile(path);
		// doesn't work in packaged plugin, which is fine
		if (file != null) {
			IMakefile makefile = MakeCorePlugin.createMakefile(URIUtil.toURI(file.getAbsolutePath()), true, inclDirs,
					null);
			assertMakefileContents(makefile);
		}
	}

	public void testInputStreamReaderProvider() throws Exception {
		IPath path = new Path("Makefile.main");

		// get base directory for searches
		final URL url = getPluginRelativeURL(new Path("data").addTrailingSeparator());
		IMakefile makefile = MakeCorePlugin.createMakefile(URIUtil.toURI(path), true, inclDirs,
				new IMakefileReaderProvider() {

					@Override
					public Reader getReader(URI fileURI) throws IOException {
						URL fileUrl;
						try {
							fileUrl = new URL(url, fileURI.getPath());
						} catch (MalformedURLException e) {
							fileUrl = new URL("file", null, fileURI.getPath());
						}
						InputStream is = fileUrl.openStream();
						return new InputStreamReader(is);
					}

				});

		assertMakefileContents(makefile);
	}

	public void testInMemoryReaderProvider() throws Exception {
		IMakefile makefile = MakeCorePlugin.createMakefile(URIUtil.toURI("/memory/Makefile.main"), true, inclDirs,
				new IMakefileReaderProvider() {

					@Override
					public Reader getReader(URI fileURI) throws IOException {
						String name = new File(fileURI).getName();
						if (name.equals("Makefile.main"))
							return new StringReader("VAR = foo\r\n" + "\r\n" + "include Makefile.incl\r\n" + "\r\n"
									+ "main: $(VAR)\r\n" + "	nothing\r\n");
						if (name.equals("Makefile.incl"))
							return new StringReader("INCLVAR = bar\r\n" + "\r\n" + "foo.o: .PHONY\r\n");

						throw new FileNotFoundException(fileURI.getPath());
					}

				});

		assertMakefileContents(makefile);
	}

	public void testReaderIsClosed_Bug338936() throws Exception {
		final boolean[] streamIsClosed = { false };
		MakeCorePlugin.createMakefile(URIUtil.toURI("Makefile.main"), true, inclDirs, new IMakefileReaderProvider() {
			@Override
			public Reader getReader(URI fileURI) throws IOException {
				return new StringReader("") {
					@Override
					public void close() {
						super.close();
						streamIsClosed[0] = true;
					}
				};
			}

		});
		assertTrue("Stream is not closed", streamIsClosed[0]);
	}

	/**
	 * @param makefile
	 */
	private void assertMakefileContents(IMakefile makefile) {
		assertNotNull(makefile);
		IMacroDefinition[] macroDefinitions = makefile.getMacroDefinitions();
		assertNotNull(macroDefinitions);
		assertEquals(2, macroDefinitions.length);
		assertEquals("VAR", macroDefinitions[0].getName());
		assertEquals("INCLVAR", macroDefinitions[1].getName());

		IRule[] rules = makefile.getRules();
		assertEquals(2, rules.length);
		assertEquals("foo.o", rules[0].getTarget().toString());
		assertEquals("main", rules[1].getTarget().toString());
	}

	/**
	 * Try to get a file in the development version of a plugin --
	 * will return <code>null</code> for a jar-packaged plugin.
	 * @param path
	 * @return
	 * @throws Exception
	 */
	private File getPluginRelativeFile(IPath path) throws Exception {
		URL url = getPluginRelativeURL(path);
		assertNotNull(url);
		if (url.getProtocol().equals("file"))
			return new File(url.getPath());
		return null;
	}

	private URL getPluginRelativeURL(IPath path) throws Exception {
		if (MakeTestsPlugin.getDefault() != null) {
			URL url = FileLocator.find(MakeTestsPlugin.getDefault().getBundle(), path, null);
			return url != null ? FileLocator.toFileURL(url) : null;
		} else {
			return new URL("file", null, path.toFile().getAbsolutePath());
		}
	}

	public static Test suite() {
		return new TestSuite(MakefileReaderProviderTests.class);
	}
}

Back to the top