[Jython-checkins] jython: Fix indexer JUnit test failures on Windows.
jeff.allen
jython-checkins at python.org
Sat Mar 15 10:00:32 CET 2014
http://hg.python.org/jython/rev/1753dd61195c
changeset: 7193:1753dd61195c
user: Jeff Allen <ja.py at farowl.co.uk>
date: Fri Mar 14 22:59:40 2014 +0000
summary:
Fix indexer JUnit test failures on Windows.
Removes extensive hard-coding of "/" as file path separator in favour of
java.io.File to supply normalisation to platform. Also adds constant (in
TestBase) to activate the logging built into the Indexer.
files:
build.xml | 2 +
src/org/python/indexer/AstCache.java | 31 +++--
src/org/python/indexer/Indexer.java | 14 +-
src/org/python/indexer/Util.java | 28 ++--
tests/java/org/python/indexer/TestBase.java | 57 +++++++--
5 files changed, 86 insertions(+), 46 deletions(-)
diff --git a/build.xml b/build.xml
--- a/build.xml
+++ b/build.xml
@@ -269,6 +269,8 @@
<echo>deprecation = '${deprecation}'</echo>
<echo>debug = '${debug}'</echo>
<echo>nowarn = '${nowarn}'</echo>
+ <echo>test = '${test}'</echo>
+ <echo>test.source.dir = '${test.source.dir}'</echo>
<echo>--- properties (used for full-build only) ---</echo>
<echo>checkout.dir = '${checkout.dir}'</echo>
<echo>javahl.dir = '${javahl.dir}'</echo>
diff --git a/src/org/python/indexer/AstCache.java b/src/org/python/indexer/AstCache.java
--- a/src/org/python/indexer/AstCache.java
+++ b/src/org/python/indexer/AstCache.java
@@ -4,14 +4,6 @@
*/
package org.python.indexer;
-import org.antlr.runtime.ANTLRFileStream;
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CharStream;
-import org.antlr.runtime.RecognitionException;
-import org.python.antlr.AnalyzingParser;
-import org.python.antlr.base.mod;
-import org.python.indexer.ast.NModule;
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -24,13 +16,22 @@
import java.util.logging.Level;
import java.util.logging.Logger;
+import org.antlr.runtime.ANTLRFileStream;
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.RecognitionException;
+import org.python.antlr.AnalyzingParser;
+import org.python.antlr.base.mod;
+import org.python.indexer.ast.NModule;
+
/**
* Provides a factory for python source ASTs. Maintains configurable on-disk and
* in-memory caches to avoid re-parsing files during analysis.
*/
public class AstCache {
- public static final String CACHE_DIR = Util.getSystemTempDir() + "jython/ast_cache/";
+ public static final String CACHE_DIR = (new File(Util.getSystemTempDir(), "jython/ast_cache"))
+ .getAbsolutePath() + File.separator;
private static final Logger LOG = Logger.getLogger(AstCache.class.getCanonicalName());
@@ -86,7 +87,9 @@
* @throws Exception if anything unexpected occurs
*/
public NModule getAST(String path) throws Exception {
- if (path == null) throw new IllegalArgumentException("null path");
+ if (path == null) {
+ throw new IllegalArgumentException("null path");
+ }
return fetch(path);
}
@@ -98,8 +101,12 @@
* @param contents the source to parse
*/
public NModule getAST(String path, String contents) throws Exception {
- if (path == null) throw new IllegalArgumentException("null path");
- if (contents == null) throw new IllegalArgumentException("null contents");
+ if (path == null) {
+ throw new IllegalArgumentException("null path");
+ }
+ if (contents == null) {
+ throw new IllegalArgumentException("null contents");
+ }
// Cache stores null value if the parse failed.
if (cache.containsKey(path)) {
diff --git a/src/org/python/indexer/Indexer.java b/src/org/python/indexer/Indexer.java
--- a/src/org/python/indexer/Indexer.java
+++ b/src/org/python/indexer/Indexer.java
@@ -132,7 +132,7 @@
if (logger == null) {
throw new IllegalArgumentException("null logger param");
}
- logger = logger;
+ this.logger = logger;
}
public Logger getLogger() {
@@ -179,10 +179,12 @@
}
throw new IndexingException(cause);
}
- if (msg == null)
+ if (msg == null) {
msg = "<null msg>";
- if (cause == null)
+ }
+ if (cause == null) {
cause = new Exception();
+ }
logger.log(Level.WARNING, msg, cause);
}
@@ -243,7 +245,7 @@
}
public boolean isLibFile(String file) {
- if (file.startsWith("/")) {
+ if (file.startsWith(File.separator)) {
return true;
}
if (path != null) {
@@ -682,7 +684,7 @@
if (modname.endsWith(".py")) {
modname = modname.substring(0, modname.length() - 3);
}
- String modpath = modname.replace('.', '/');
+ String modpath = modname.replace('.', File.separatorChar);
// A nasty hack to avoid e.g. python2.5 becoming python2/5.
// Should generalize this for directory components containing '.'.
@@ -909,7 +911,7 @@
public List<String> getLoadedFiles() {
List<String> files = new ArrayList<String>();
for (String file : moduleTable.keySet()) {
- if (file.startsWith("/")) {
+ if (file.startsWith(File.separator)) {
files.add(file);
}
}
diff --git a/src/org/python/indexer/Util.java b/src/org/python/indexer/Util.java
--- a/src/org/python/indexer/Util.java
+++ b/src/org/python/indexer/Util.java
@@ -19,7 +19,10 @@
public class Util {
private static final String UTF_8 = "UTF-8";
-
+ private static final char SEPCHAR = File.separatorChar;
+ private static final String SEP = File.separator;
+ private static final String INIT_PY = "__init__.py";
+ private static final String SEP_INIT_PY = SEP + INIT_PY;
private static int gensymCount = -1;
public static String gensym(String base) {
@@ -29,11 +32,10 @@
public static String getSystemTempDir() {
String tmp = System.getProperty("java.io.tmpdir");
- String sep = System.getProperty("file.separator");
- if (tmp.endsWith(sep)) {
+ if (tmp.endsWith(SEP)) {
return tmp;
}
- return tmp + sep;
+ return tmp + SEP;
}
/**
@@ -62,15 +64,15 @@
* @return null if {@code file} is not somewhere under the load path
*/
public static String moduleQname(String file) {
- boolean initpy = file.endsWith("/__init__.py");
+ boolean initpy = file.endsWith(SEP_INIT_PY);
if (initpy) {
- file = file.substring(0, file.length() - "/__init__.py".length());
+ file = file.substring(0, file.length() - SEP_INIT_PY.length());
} else if (file.endsWith(".py")) {
file = file.substring(0, file.length() - ".py".length());
}
for (String root : Indexer.idx.getLoadPath()) {
if (file.startsWith(root)) {
- return file.substring(root.length()).replace('/', '.');
+ return file.substring(root.length()).replace(SEPCHAR, '.');
}
}
return null;
@@ -102,7 +104,7 @@
throw new IllegalStateException("failed assertion: " + path);
}
String fname = f.getName();
- if (fname.equals("__init__.py")) {
+ if (fname.equals(INIT_PY)) {
return f.getParentFile().getName();
}
return fname.substring(0, fname.lastIndexOf('.'));
@@ -113,10 +115,10 @@
}
public static File joinPath(String dir, String file) {
- if (dir.endsWith("/")) {
+ if (dir.endsWith(SEP)) {
return new File(dir + file);
}
- return new File(dir + "/" + file);
+ return new File(dir + SEP + file);
}
public static void writeFile(String path, String contents) throws Exception {
@@ -189,15 +191,15 @@
/**
* Return absolute path for {@code path}.
- * Make sure path ends with "/" if it's a directory.
+ * Make sure path ends with SEP if it's a directory.
* Does _not_ resolve symlinks, since the caller may need to play
* symlink tricks to produce the desired paths for loaded modules.
*/
public static String canonicalize(String path) {
File f = new File(path);
path = f.getAbsolutePath();
- if (f.isDirectory() && !path.endsWith("/")) {
- return path + "/";
+ if (f.isDirectory() && !path.endsWith(SEP)) {
+ return path + SEP;
}
return path;
}
diff --git a/tests/java/org/python/indexer/TestBase.java b/tests/java/org/python/indexer/TestBase.java
--- a/tests/java/org/python/indexer/TestBase.java
+++ b/tests/java/org/python/indexer/TestBase.java
@@ -4,33 +4,57 @@
*/
package org.python.indexer;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
+
import junit.framework.TestCase;
-import org.python.indexer.Def;
-import org.python.indexer.Ref;
-import org.python.indexer.ast.NNode;
import org.python.indexer.types.NType;
import org.python.indexer.types.NUnknownType;
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.InputStreamReader;
-
/**
* Test utilities for {@link IndexerTest}.
*/
public class TestBase extends TestCase {
- static protected final String TEST_SOURCE_DIR;
+ // Set this to control logging to the console from the Indexer (mostly at FINER level).
+ static protected final Level LOGGING_LEVEL = Level.OFF;
+
static protected final String TEST_DATA_DIR;
static protected final String TEST_LIB_DIR;
static {
- TEST_SOURCE_DIR =
- System.getProperties().getProperty("python.test.source.dir")
- + "/org/python/indexer/";
- TEST_DATA_DIR = TEST_SOURCE_DIR + "data/";
- TEST_LIB_DIR = System.getProperties().getProperty("python.home") + "/Lib/";
+ /*
+ * Locate cardinal directories in a way that insulates us from the vagueries of the
+ * environment, Ant, IDE and OS.
+ */
+ String home = System.getProperty("python.home", "dist");
+ String test = System.getProperty("python.test.source.dir", "tests/java");
+ File source = new File(test, "org/python/indexer"); // corrects to \ where needed.
+
+ // Program actually uses strings, with a trailing slash
+ TEST_DATA_DIR = (new File(source, "data")).getAbsolutePath() + File.separator;
+ TEST_LIB_DIR = (new File(home, "Lib")).getAbsolutePath() + File.separator;
+
+ // Give the logger used by Indexer an outlet
+ setUpLogging();
+ }
+
+ // Define a handler for the logger to use
+ static private void setUpLogging() {
+ // Enable tracing of the operation of the Indexer onto the console
+ Logger indexerLogger = Logger.getLogger(Indexer.class.getCanonicalName());
+ Handler logHandler = new ConsoleHandler();
+ logHandler.setFormatter(new SimpleFormatter());
+ logHandler.setLevel(Level.FINEST);
+ indexerLogger.addHandler(logHandler);
}
protected Indexer idx;
@@ -41,6 +65,7 @@
@Override
protected void setUp() throws Exception {
idx = new Indexer();
+ idx.getLogger().setLevel(LOGGING_LEVEL);
idx.enableAggressiveAssertions(true);
idx.setProjectDir(TEST_DATA_DIR);
AstCache.get().clearDiskCache();
@@ -108,13 +133,15 @@
* @throws IllegalArgumentException if the {@code n}th occurrence does not exist
*/
protected int nthIndexOf(String s, String find, int n) {
- if (n <= 0)
+ if (n <= 0) {
throw new IllegalArgumentException();
+ }
int index = -1;
for (int i = 0; i < n; i++) {
index = s.indexOf(find, index == -1 ? 0 : index + 1);
- if (index == -1)
+ if (index == -1) {
throw new IllegalArgumentException();
+ }
}
return index;
}
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list