Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/currently-failing/JDK-8144221.js
32281 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* Test that shebang handling works properly.25*26* @test27* @option -scripting28* @run29*/3031// The test generates three different JavaScript source files. The first two32// are generated at the beginning of the test and do not change.33// * a.js34// print("A: " + arguments)35// * b.js36// #!<path_to_jjs> -lalelu -- ignore37// print("B: " + arguments)38//39// The third file, shebang.js, is generated differently for each particular40// test case, containing different shebang lines and one statement:41// * shebang.js42// #!<path_to_jjs> <shebang_line>43// print("S: " + arguments)44//45// The path_to_jjs is extracted from the environment based on JAVA_HOME, so the46// latter must be set properly.47//48// Each shebang.js is run four times, in all possible combinations of values49// from the following two axes:50// * without passing any arguments, and passing the arguments 'a.js' and51// '"hello world"' (the latter being a quoted string);52// * run via jjs, and via direct shell execution (using shebang).5354var pseudosheb = "#!${jjs} -lalelu -- ignore",55System = Java.type('java.lang.System'),56Paths = Java.type('java.nio.file.Paths'),57Files = Java.type('java.nio.file.Files'),58Opt = Java.type('java.nio.file.StandardOpenOption'),59Arrays = Java.type('java.util.Arrays')6061var sep = Java.type('java.io.File').separator,62win = System.getProperty("os.name").startsWith("Windows"),63jjsName = "jjs" + (win ? ".exe" : ""),64javaHome = System.getProperty("java.home")6566var jjs = javaHome + "/../bin/".replace(/\//g, sep) + jjsName67if (!Files.exists(Paths.get(jjs))) {68jjs = javaHome + "/bin/".replace(/\//g, sep) + jjsName69}7071// Create and cwd to a temporary directory.7273var tmpdir = Files.createTempDirectory(null),74tmp = tmpdir.toAbsolutePath().toString(),75curpwd = $ENV.PWD7677$ENV.PWD = tmp7879// Test cases. Each case is documented with the expected output for the four80// different executions.8182var shebs = [83// No arguments on the shebang line.84// noargs jjs/shebang -> no output but "S" prefix85// args jjs/shebang -> output the arguments with "S" prefix86"",87// One interpreter argument.88// noargs jjs/shebang -> no output but "S" prefix89// args jjs/shebang -> output the arguments with "S" prefix90"--language=es6",91// Two interpreter arguments.92// noargs jjs/shebang -> no output but "S" prefix93// args jjs/shebang -> output the arguments with "S" prefix94"--language=es6 -scripting",95// One interpreter argument and a JavaScript file without shebang.96// (For shebang execution, this is a pathological example, as the97// JavaScript file passed as a shebang argument will be analyzed and98// shebang mode will not be entered.)99// noargs jjs -> no output but "S" prefix100// args jjs -> output the arguments with "S" prefix101// noargs shebang -> no output but "A" and "S" prefixes102// args shebang -> output "A", "S", and "A" prefixes, then the error103// message:104// "java.io.IOException: hello world is not a file"105"-scripting a.js",106// One interpreter argument and a JavaScript file with shebang. (This107// is another pathological example, as it will force shebang mode,108// leading to all subsequent arguments, including shebang.js, being109// treated as arguments to the script b.js.)110// noargs jjs -> no output but the "S" prefix111// args jjs -> output the arguments with "S" prefix112// noargs shebang -> output shebang.js with "B" prefix113// args shebang -> output shebang.js and the arguments with "B"114// prefix115"-scripting b.js"116]117118function write(file, lines) {119Files.write(Paths.get(tmp, file), Arrays.asList(lines), Opt.CREATE, Opt.WRITE)120}121122function insn(name) {123return "print('${name}:' + arguments)"124}125126function run(viajjs, name, arg1, arg2) {127var prefix = viajjs ? "${jjs} -scripting " : win ? 'sh -c "' : '',128suffix = viajjs ? '' : win ? '"' : ''129$EXEC("${prefix}./shebang.js ${arg1} ${arg2}${suffix}")130print("* ${name} via ${viajjs ? 'jjs' : 'shebang'}")131print($OUT.trim())132print($ERR.trim())133}134135write('a.js', insn('A'))136write('b.js', [pseudosheb, insn('B')])137138shebs.forEach(function(sheb) {139var shebang = "#!${jjs} ${sheb}"140print("<<< ${sheb} >>>")141write('shebang.js', [shebang, insn('S')])142$EXEC('chmod +x shebang.js')143run(false, 'noargs', '', '')144run(true, 'noargs', '', '')145run(false, 'withargs', 'a.js', "'hello world'")146run(true, 'withargs', 'a.js', "'hello world'")147$EXEC('rm shebang.js')148})149150// Cleanup.151152$EXEC('rm a.js b.js')153$ENV.PWD = curpwd154Files.delete(tmpdir)155156157