Mon, 18 Jan 2016 11:31:43 +0100
8145305: fix Nashorn shebang handling on Cygwin
Reviewed-by: hannesw, sundar
mhaupt@1823 | 1 | /* |
mhaupt@1825 | 2 | * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
mhaupt@1823 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mhaupt@1823 | 4 | * |
mhaupt@1823 | 5 | * This code is free software; you can redistribute it and/or modify it |
mhaupt@1823 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mhaupt@1823 | 7 | * published by the Free Software Foundation. |
mhaupt@1823 | 8 | * |
mhaupt@1823 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mhaupt@1823 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mhaupt@1823 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mhaupt@1823 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mhaupt@1823 | 13 | * accompanied this code). |
mhaupt@1823 | 14 | * |
mhaupt@1823 | 15 | * You should have received a copy of the GNU General Public License version |
mhaupt@1823 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mhaupt@1823 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mhaupt@1823 | 18 | * |
mhaupt@1823 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mhaupt@1823 | 20 | * or visit www.oracle.com if you need additional information or have any |
mhaupt@1823 | 21 | * questions. |
mhaupt@1823 | 22 | */ |
mhaupt@1823 | 23 | |
mhaupt@1823 | 24 | /** |
mhaupt@1823 | 25 | * Test that shebang handling works properly. |
mhaupt@1823 | 26 | * |
mhaupt@1823 | 27 | * @test |
mhaupt@1823 | 28 | * @option -scripting |
mhaupt@1823 | 29 | * @run |
mhaupt@1823 | 30 | */ |
mhaupt@1823 | 31 | |
mhaupt@1823 | 32 | // The test generates three different JavaScript source files. The first two |
mhaupt@1823 | 33 | // are generated at the beginning of the test and do not change. |
mhaupt@1823 | 34 | // * a.js |
mhaupt@1823 | 35 | // print("A: " + arguments) |
mhaupt@1823 | 36 | // * b.js |
mhaupt@1823 | 37 | // #!<path_to_jjs> -lalelu -- ignore |
mhaupt@1823 | 38 | // print("B: " + arguments) |
mhaupt@1823 | 39 | // |
mhaupt@1823 | 40 | // The third file, shebang.js, is generated differently for each particular |
mhaupt@1823 | 41 | // test case, containing different shebang lines and one statement: |
mhaupt@1823 | 42 | // * shebang.js |
mhaupt@1823 | 43 | // #!<path_to_jjs> <shebang_line> |
mhaupt@1823 | 44 | // print("S: " + arguments) |
mhaupt@1823 | 45 | // |
mhaupt@1823 | 46 | // The path_to_jjs is extracted from the environment based on JAVA_HOME, so the |
mhaupt@1823 | 47 | // latter must be set properly. |
mhaupt@1823 | 48 | // |
mhaupt@1823 | 49 | // Each shebang.js is run four times, in all possible combinations of values |
mhaupt@1823 | 50 | // from the following two axes: |
mhaupt@1823 | 51 | // * without passing any arguments, and passing the arguments 'a.js' and |
mhaupt@1823 | 52 | // '"hello world"' (the latter being a quoted string); |
mhaupt@1823 | 53 | // * run via jjs, and via direct shell execution (using shebang). |
mhaupt@1823 | 54 | |
mhaupt@1823 | 55 | var pseudosheb = "#!${jjs} -lalelu -- ignore", |
mhaupt@1823 | 56 | System = Java.type('java.lang.System'), |
mhaupt@1823 | 57 | Paths = Java.type('java.nio.file.Paths'), |
mhaupt@1823 | 58 | Files = Java.type('java.nio.file.Files'), |
mhaupt@1823 | 59 | Opt = Java.type('java.nio.file.StandardOpenOption'), |
mhaupt@1823 | 60 | Arrays = Java.type('java.util.Arrays') |
mhaupt@1823 | 61 | |
mhaupt@1823 | 62 | var sep = Java.type('java.io.File').separator, |
mhaupt@1823 | 63 | win = System.getProperty("os.name").startsWith("Windows"), |
mhaupt@1823 | 64 | jjsName = "jjs" + (win ? ".exe" : ""), |
mhaupt@1823 | 65 | javaHome = System.getProperty("java.home") |
mhaupt@1823 | 66 | |
mhaupt@1823 | 67 | var jjs = javaHome + "/../bin/".replace(/\//g, sep) + jjsName |
mhaupt@1823 | 68 | if (!Files.exists(Paths.get(jjs))) { |
mhaupt@1823 | 69 | jjs = javaHome + "/bin/".replace(/\//g, sep) + jjsName |
mhaupt@1823 | 70 | } |
mhaupt@1823 | 71 | |
mhaupt@1823 | 72 | // Create and cwd to a temporary directory. |
mhaupt@1823 | 73 | |
mhaupt@1823 | 74 | var tmpdir = Files.createTempDirectory(null), |
mhaupt@1823 | 75 | tmp = tmpdir.toAbsolutePath().toString(), |
mhaupt@1823 | 76 | curpwd = $ENV.PWD |
mhaupt@1823 | 77 | |
mhaupt@1823 | 78 | $ENV.PWD = tmp |
mhaupt@1823 | 79 | |
mhaupt@1823 | 80 | // Test cases. Each case is documented with the expected output for the four |
mhaupt@1823 | 81 | // different executions. |
mhaupt@1823 | 82 | |
mhaupt@1823 | 83 | var shebs = [ |
mhaupt@1823 | 84 | // No arguments on the shebang line. |
mhaupt@1823 | 85 | // noargs jjs/shebang -> no output but "S" prefix |
mhaupt@1823 | 86 | // args jjs/shebang -> output the arguments with "S" prefix |
mhaupt@1823 | 87 | "", |
mhaupt@1823 | 88 | // One interpreter argument. |
mhaupt@1823 | 89 | // noargs jjs/shebang -> no output but "S" prefix |
mhaupt@1823 | 90 | // args jjs/shebang -> output the arguments with "S" prefix |
mhaupt@1823 | 91 | "--language=es6", |
mhaupt@1823 | 92 | // Two interpreter arguments. |
mhaupt@1823 | 93 | // noargs jjs/shebang -> no output but "S" prefix |
mhaupt@1823 | 94 | // args jjs/shebang -> output the arguments with "S" prefix |
mhaupt@1823 | 95 | "--language=es6 -scripting", |
mhaupt@1823 | 96 | // One interpreter argument and a JavaScript file without shebang. |
mhaupt@1823 | 97 | // (For shebang execution, this is a pathological example, as the |
mhaupt@1823 | 98 | // JavaScript file passed as a shebang argument will be analyzed and |
mhaupt@1823 | 99 | // shebang mode will not be entered.) |
mhaupt@1823 | 100 | // noargs jjs -> no output but "S" prefix |
mhaupt@1823 | 101 | // args jjs -> output the arguments with "S" prefix |
mhaupt@1823 | 102 | // noargs shebang -> no output but "A" and "S" prefixes |
mhaupt@1823 | 103 | // args shebang -> output "A", "S", and "A" prefixes, then the error |
mhaupt@1823 | 104 | // message: |
mhaupt@1823 | 105 | // "java.io.IOException: hello world is not a file" |
mhaupt@1823 | 106 | "-scripting a.js", |
mhaupt@1823 | 107 | // One interpreter argument and a JavaScript file with shebang. (This |
mhaupt@1823 | 108 | // is another pathological example, as it will force shebang mode, |
mhaupt@1823 | 109 | // leading to all subsequent arguments, including shebang.js, being |
mhaupt@1823 | 110 | // treated as arguments to the script b.js.) |
mhaupt@1823 | 111 | // noargs jjs -> no output but the "S" prefix |
mhaupt@1823 | 112 | // args jjs -> output the arguments with "S" prefix |
mhaupt@1823 | 113 | // noargs shebang -> output shebang.js with "B" prefix |
mhaupt@1823 | 114 | // args shebang -> output shebang.js and the arguments with "B" |
mhaupt@1823 | 115 | // prefix |
mhaupt@1823 | 116 | "-scripting b.js" |
mhaupt@1823 | 117 | ] |
mhaupt@1823 | 118 | |
mhaupt@1823 | 119 | function write(file, lines) { |
mhaupt@1823 | 120 | Files.write(Paths.get(tmp, file), Arrays.asList(lines), Opt.CREATE, Opt.WRITE) |
mhaupt@1823 | 121 | } |
mhaupt@1823 | 122 | |
mhaupt@1823 | 123 | function insn(name) { |
mhaupt@1823 | 124 | return "print('${name}:' + arguments)" |
mhaupt@1823 | 125 | } |
mhaupt@1823 | 126 | |
mhaupt@1823 | 127 | function run(viajjs, name, arg1, arg2) { |
mhaupt@1825 | 128 | var prefix = viajjs ? "${jjs} -scripting " : win ? 'sh -c "' : '', |
mhaupt@1825 | 129 | suffix = viajjs ? '' : win ? '"' : '' |
mhaupt@1825 | 130 | $EXEC("${prefix}./shebang.js ${arg1} ${arg2}${suffix}") |
mhaupt@1823 | 131 | print("* ${name} via ${viajjs ? 'jjs' : 'shebang'}") |
mhaupt@1823 | 132 | print($OUT.trim()) |
mhaupt@1823 | 133 | print($ERR.trim()) |
mhaupt@1823 | 134 | } |
mhaupt@1823 | 135 | |
mhaupt@1823 | 136 | write('a.js', insn('A')) |
mhaupt@1823 | 137 | write('b.js', [pseudosheb, insn('B')]) |
mhaupt@1823 | 138 | |
mhaupt@1823 | 139 | shebs.forEach(function(sheb) { |
mhaupt@1823 | 140 | var shebang = "#!${jjs} ${sheb}" |
mhaupt@1823 | 141 | print("<<< ${sheb} >>>") |
mhaupt@1823 | 142 | write('shebang.js', [shebang, insn('S')]) |
mhaupt@1823 | 143 | $EXEC('chmod +x shebang.js') |
mhaupt@1823 | 144 | run(false, 'noargs', '', '') |
mhaupt@1823 | 145 | run(true, 'noargs', '', '') |
mhaupt@1825 | 146 | run(false, 'withargs', 'a.js', "'hello world'") |
mhaupt@1825 | 147 | run(true, 'withargs', 'a.js', "'hello world'") |
mhaupt@1823 | 148 | $EXEC('rm shebang.js') |
mhaupt@1823 | 149 | }) |
mhaupt@1823 | 150 | |
mhaupt@1823 | 151 | // Cleanup. |
mhaupt@1823 | 152 | |
mhaupt@1823 | 153 | $EXEC('rm a.js b.js') |
mhaupt@1823 | 154 | $ENV.PWD = curpwd |
mhaupt@1823 | 155 | Files.delete(tmpdir) |