File: invocation_argument_parsing.coffee

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,360 kB
  • sloc: makefile: 20; xml: 9; sh: 6; javascript: 5
file content (114 lines) | stat: -rw-r--r-- 5,286 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
return unless require?

path = require 'path'
{ execFileSync, spawnSync } = require 'child_process'

# Get the folder containing the compiled `coffee` executable and make it the
# PATH so that `#!/usr/bin/env coffee` resolves to our locally built file.
coffeeBinFolder = path.dirname require.resolve '../bin/coffee'
# For some reason, Windows requires `coffee` to be executed as `node coffee`.
coffeeCommand = if isWindows() then 'node coffee' else 'coffee'
spawnOptions =
  cwd: coffeeBinFolder
  encoding: 'utf8'
  env: Object.assign {}, process.env,
    PATH: coffeeBinFolder + (if isWindows() then ';' else ':') + process.env.PATH
  shell: isWindows()

shebangScript = require.resolve './importing/shebang.coffee'
initialSpaceScript = require.resolve './importing/shebang_initial_space.coffee'
extraArgsScript = require.resolve './importing/shebang_extra_args.coffee'
initialSpaceExtraArgsScript = require.resolve './importing/shebang_initial_space_extra_args.coffee'

test "parse arguments for shebang scripts correctly (on *nix platforms)", ->
  return if isWindows()

  stdout = execFileSync shebangScript, ['-abck'], spawnOptions
  expectedArgs = ['coffee', shebangScript, '-abck']
  realArgs = JSON.parse stdout
  arrayEq expectedArgs, realArgs

  stdout = execFileSync initialSpaceScript, ['-abck'], spawnOptions
  expectedArgs = ['coffee', initialSpaceScript, '-abck']
  realArgs = JSON.parse stdout
  arrayEq expectedArgs, realArgs

test "warn and remove -- if it is the second positional argument", ->
  result = spawnSync coffeeCommand, [shebangScript, '--'], spawnOptions
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', shebangScript]
  ok stderr.match /^coffee was invoked with '--'/m
  posArgs = stderr.match(/^The positional arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(posArgs), [shebangScript, '--']
  ok result.status is 0

  result = spawnSync coffeeCommand, ['-b', shebangScript, '--'], spawnOptions
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', shebangScript]
  ok stderr.match /^coffee was invoked with '--'/m
  posArgs = stderr.match(/^The positional arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(posArgs), [shebangScript, '--']
  ok result.status is 0

  result = spawnSync(
    coffeeCommand, ['-b', shebangScript, '--', 'ANOTHER'], spawnOptions)
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', shebangScript, 'ANOTHER']
  ok stderr.match /^coffee was invoked with '--'/m
  posArgs = stderr.match(/^The positional arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(posArgs), [shebangScript, '--', 'ANOTHER']
  ok result.status is 0

  result = spawnSync(
    coffeeCommand, ['--', initialSpaceScript, 'arg'], spawnOptions)
  expectedArgs = ['coffee', initialSpaceScript, 'arg']
  realArgs = JSON.parse result.stdout
  arrayEq expectedArgs, realArgs
  ok result.stderr.toString() is ''
  ok result.status is 0

test "warn about non-portable shebang lines", ->
  result = spawnSync coffeeCommand, [extraArgsScript, 'arg'], spawnOptions
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', extraArgsScript, 'arg']
  ok stderr.match /^The script to be run begins with a shebang line with more than one/m
  [_, firstLine, file] = stderr.match(/^The shebang line was: '([^']+)' in file '([^']+)'/m)
  ok (firstLine is '#!/usr/bin/env coffee --')
  ok (file is extraArgsScript)
  args = stderr.match(/^The arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(args), ['coffee', '--']
  ok result.status is 0

  result = spawnSync coffeeCommand, [initialSpaceScript, 'arg'], spawnOptions
  stderr = result.stderr.toString()
  ok stderr is ''
  arrayEq JSON.parse(result.stdout), ['coffee', initialSpaceScript, 'arg']
  ok result.status is 0

  result = spawnSync(
    coffeeCommand, [initialSpaceExtraArgsScript, 'arg'], spawnOptions)
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', initialSpaceExtraArgsScript, 'arg']
  ok stderr.match /^The script to be run begins with a shebang line with more than one/m
  [_, firstLine, file] = stderr.match(/^The shebang line was: '([^']+)' in file '([^']+)'/m)
  ok (firstLine is '#! /usr/bin/env coffee extra')
  ok (file is initialSpaceExtraArgsScript)
  args = stderr.match(/^The arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(args), ['coffee', 'extra']
  ok result.status is 0

test "both warnings will be shown at once", ->
  result = spawnSync(
    coffeeCommand, [initialSpaceExtraArgsScript, '--', 'arg'], spawnOptions)
  stderr = result.stderr.toString()
  arrayEq JSON.parse(result.stdout), ['coffee', initialSpaceExtraArgsScript, 'arg']
  ok stderr.match /^The script to be run begins with a shebang line with more than one/m
  [_, firstLine, file] = stderr.match(/^The shebang line was: '([^']+)' in file '([^']+)'/m)
  ok (firstLine is '#! /usr/bin/env coffee extra')
  ok (file is initialSpaceExtraArgsScript)
  args = stderr.match(/^The arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(args), ['coffee', 'extra']
  ok stderr.match /^coffee was invoked with '--'/m
  posArgs = stderr.match(/^The positional arguments were: (.*)$/m)[1]
  arrayEq JSON.parse(posArgs), [initialSpaceExtraArgsScript, '--', 'arg']
  ok result.status is 0