mhaupt@1823: /* mhaupt@1825: * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. mhaupt@1823: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mhaupt@1823: * mhaupt@1823: * This code is free software; you can redistribute it and/or modify it mhaupt@1823: * under the terms of the GNU General Public License version 2 only, as mhaupt@1823: * published by the Free Software Foundation. mhaupt@1823: * mhaupt@1823: * This code is distributed in the hope that it will be useful, but WITHOUT mhaupt@1823: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mhaupt@1823: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mhaupt@1823: * version 2 for more details (a copy is included in the LICENSE file that mhaupt@1823: * accompanied this code). mhaupt@1823: * mhaupt@1823: * You should have received a copy of the GNU General Public License version mhaupt@1823: * 2 along with this work; if not, write to the Free Software Foundation, mhaupt@1823: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mhaupt@1823: * mhaupt@1823: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mhaupt@1823: * or visit www.oracle.com if you need additional information or have any mhaupt@1823: * questions. mhaupt@1823: */ mhaupt@1823: mhaupt@1823: /** mhaupt@1823: * Test that shebang handling works properly. mhaupt@1823: * mhaupt@1823: * @test mhaupt@1823: * @option -scripting mhaupt@1823: * @run mhaupt@1823: */ mhaupt@1823: mhaupt@1823: // The test generates three different JavaScript source files. The first two mhaupt@1823: // are generated at the beginning of the test and do not change. mhaupt@1823: // * a.js mhaupt@1823: // print("A: " + arguments) mhaupt@1823: // * b.js mhaupt@1823: // #! -lalelu -- ignore mhaupt@1823: // print("B: " + arguments) mhaupt@1823: // mhaupt@1823: // The third file, shebang.js, is generated differently for each particular mhaupt@1823: // test case, containing different shebang lines and one statement: mhaupt@1823: // * shebang.js mhaupt@1823: // #! mhaupt@1823: // print("S: " + arguments) mhaupt@1823: // mhaupt@1823: // The path_to_jjs is extracted from the environment based on JAVA_HOME, so the mhaupt@1823: // latter must be set properly. mhaupt@1823: // mhaupt@1823: // Each shebang.js is run four times, in all possible combinations of values mhaupt@1823: // from the following two axes: mhaupt@1823: // * without passing any arguments, and passing the arguments 'a.js' and mhaupt@1823: // '"hello world"' (the latter being a quoted string); mhaupt@1823: // * run via jjs, and via direct shell execution (using shebang). mhaupt@1823: mhaupt@1823: var pseudosheb = "#!${jjs} -lalelu -- ignore", mhaupt@1823: System = Java.type('java.lang.System'), mhaupt@1823: Paths = Java.type('java.nio.file.Paths'), mhaupt@1823: Files = Java.type('java.nio.file.Files'), mhaupt@1823: Opt = Java.type('java.nio.file.StandardOpenOption'), mhaupt@1823: Arrays = Java.type('java.util.Arrays') mhaupt@1823: mhaupt@1823: var sep = Java.type('java.io.File').separator, mhaupt@1823: win = System.getProperty("os.name").startsWith("Windows"), mhaupt@1823: jjsName = "jjs" + (win ? ".exe" : ""), mhaupt@1823: javaHome = System.getProperty("java.home") mhaupt@1823: mhaupt@1823: var jjs = javaHome + "/../bin/".replace(/\//g, sep) + jjsName mhaupt@1823: if (!Files.exists(Paths.get(jjs))) { mhaupt@1823: jjs = javaHome + "/bin/".replace(/\//g, sep) + jjsName mhaupt@1823: } mhaupt@1823: mhaupt@1823: // Create and cwd to a temporary directory. mhaupt@1823: mhaupt@1823: var tmpdir = Files.createTempDirectory(null), mhaupt@1823: tmp = tmpdir.toAbsolutePath().toString(), mhaupt@1823: curpwd = $ENV.PWD mhaupt@1823: mhaupt@1823: $ENV.PWD = tmp mhaupt@1823: mhaupt@1823: // Test cases. Each case is documented with the expected output for the four mhaupt@1823: // different executions. mhaupt@1823: mhaupt@1823: var shebs = [ mhaupt@1823: // No arguments on the shebang line. mhaupt@1823: // noargs jjs/shebang -> no output but "S" prefix mhaupt@1823: // args jjs/shebang -> output the arguments with "S" prefix mhaupt@1823: "", mhaupt@1823: // One interpreter argument. mhaupt@1823: // noargs jjs/shebang -> no output but "S" prefix mhaupt@1823: // args jjs/shebang -> output the arguments with "S" prefix mhaupt@1823: "--language=es6", mhaupt@1823: // Two interpreter arguments. mhaupt@1823: // noargs jjs/shebang -> no output but "S" prefix mhaupt@1823: // args jjs/shebang -> output the arguments with "S" prefix mhaupt@1823: "--language=es6 -scripting", mhaupt@1823: // One interpreter argument and a JavaScript file without shebang. mhaupt@1823: // (For shebang execution, this is a pathological example, as the mhaupt@1823: // JavaScript file passed as a shebang argument will be analyzed and mhaupt@1823: // shebang mode will not be entered.) mhaupt@1823: // noargs jjs -> no output but "S" prefix mhaupt@1823: // args jjs -> output the arguments with "S" prefix mhaupt@1823: // noargs shebang -> no output but "A" and "S" prefixes mhaupt@1823: // args shebang -> output "A", "S", and "A" prefixes, then the error mhaupt@1823: // message: mhaupt@1823: // "java.io.IOException: hello world is not a file" mhaupt@1823: "-scripting a.js", mhaupt@1823: // One interpreter argument and a JavaScript file with shebang. (This mhaupt@1823: // is another pathological example, as it will force shebang mode, mhaupt@1823: // leading to all subsequent arguments, including shebang.js, being mhaupt@1823: // treated as arguments to the script b.js.) mhaupt@1823: // noargs jjs -> no output but the "S" prefix mhaupt@1823: // args jjs -> output the arguments with "S" prefix mhaupt@1823: // noargs shebang -> output shebang.js with "B" prefix mhaupt@1823: // args shebang -> output shebang.js and the arguments with "B" mhaupt@1823: // prefix mhaupt@1823: "-scripting b.js" mhaupt@1823: ] mhaupt@1823: mhaupt@1823: function write(file, lines) { mhaupt@1823: Files.write(Paths.get(tmp, file), Arrays.asList(lines), Opt.CREATE, Opt.WRITE) mhaupt@1823: } mhaupt@1823: mhaupt@1823: function insn(name) { mhaupt@1823: return "print('${name}:' + arguments)" mhaupt@1823: } mhaupt@1823: mhaupt@1823: function run(viajjs, name, arg1, arg2) { mhaupt@1825: var prefix = viajjs ? "${jjs} -scripting " : win ? 'sh -c "' : '', mhaupt@1825: suffix = viajjs ? '' : win ? '"' : '' mhaupt@1825: $EXEC("${prefix}./shebang.js ${arg1} ${arg2}${suffix}") mhaupt@1823: print("* ${name} via ${viajjs ? 'jjs' : 'shebang'}") mhaupt@1823: print($OUT.trim()) mhaupt@1823: print($ERR.trim()) mhaupt@1823: } mhaupt@1823: mhaupt@1823: write('a.js', insn('A')) mhaupt@1823: write('b.js', [pseudosheb, insn('B')]) mhaupt@1823: mhaupt@1823: shebs.forEach(function(sheb) { mhaupt@1823: var shebang = "#!${jjs} ${sheb}" mhaupt@1823: print("<<< ${sheb} >>>") mhaupt@1823: write('shebang.js', [shebang, insn('S')]) mhaupt@1823: $EXEC('chmod +x shebang.js') mhaupt@1823: run(false, 'noargs', '', '') mhaupt@1823: run(true, 'noargs', '', '') mhaupt@1825: run(false, 'withargs', 'a.js', "'hello world'") mhaupt@1825: run(true, 'withargs', 'a.js', "'hello world'") mhaupt@1823: $EXEC('rm shebang.js') mhaupt@1823: }) mhaupt@1823: mhaupt@1823: // Cleanup. mhaupt@1823: mhaupt@1823: $EXEC('rm a.js b.js') mhaupt@1823: $ENV.PWD = curpwd mhaupt@1823: Files.delete(tmpdir)