Skip to content

Commit 13231ff

Browse files
committed
ScriptInfo: improve behavior of script identifiers
Script IDs are now of the form "script:${name}" for named scripts, "script:${path}" for unnamed scripts with a file path given, and "script:<${hex}>" where "${hex}" is a hexidecimal hash code of the script contents when it has neither name nor path. In this way, script IDs should hopefully always be unique, except in the case where they are intended to collide -- e.g., to dynamically replace one version of a script with another.
1 parent b060732 commit 13231ff

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,12 @@ public void setContext(final Context context) {
359359

360360
@Override
361361
public String getIdentifier() {
362-
return "script:" + (path == null ? "<inline>" : path);
362+
final String name = getName();
363+
final String prefix = "script:";
364+
if (name != null) return prefix + name;
365+
if (path != null) return prefix + path;
366+
if (script != null) return prefix + "<" + DigestUtils.bestHex(script) + ">";
367+
return prefix + "<unknown>";
363368
}
364369

365370
// -- Locatable methods --

src/test/java/org/scijava/script/ScriptInfoTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ public void tearDown() {
9595

9696
// -- Tests --
9797

98+
/** Tests script identifiers. */
99+
@Test
100+
public void testGetIdentifier() {
101+
final String name = "strategerize";
102+
103+
final String namedPath = "victory.bsizes";
104+
final String named = "" + //
105+
"#@script(name = '" + name + "')\n" + //
106+
"zxywvutsrqponmlkjihgfdcba\n";
107+
108+
final String unnamedPath = "alphabet.bsizes";
109+
final String unnamed = "" + //
110+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" + //
111+
"0123456789\n";
112+
113+
// Test named, with explicit path.
114+
assertEquals("script:" + name, id(namedPath, named));
115+
116+
// Test named, and no path given.
117+
assertEquals("script:" + name, id(null, named));
118+
119+
// Test unnamed, with explicit path.
120+
assertEquals("script:" + unnamedPath, id(unnamedPath, unnamed));
121+
122+
// Test unnamed, and no path given.
123+
final String hex = DigestUtils.bestHex(unnamed);
124+
assertEquals("script:<" + hex + ">", id(null, unnamed));
125+
}
126+
98127
/** Tests whether new-style parameter syntax are parsed correctly. */
99128
@Test
100129
public void testNewStyle() throws Exception {
@@ -323,6 +352,15 @@ public void testReaderSanity() throws Exception {
323352
assertEquals("Readers are not independent.", reader1.read(), reader2.read());
324353
}
325354

355+
// -- Helper methods --
356+
357+
private String id(final String path, final String script) {
358+
final ScriptInfo info = //
359+
new ScriptInfo(context, path, new StringReader(script));
360+
info.inputs(); // NB: Force parameter parsing.
361+
return info.getIdentifier();
362+
}
363+
326364
private void assertItem(final String name, final Class<?> type,
327365
final String label, final ItemIO ioType, final boolean required,
328366
final boolean persist, final String persistKey, final String style,

0 commit comments

Comments
 (0)