File: test_command_line_switches.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (317 lines) | stat: -rw-r--r-- 10,248 bytes parent folder | download | duplicates (4)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require 'test/unit'
require 'test/test_helper'

require 'fileutils'

class TestCommandLineSwitches < Test::Unit::TestCase
  include TestHelper

  # FIXME: currently fails on Windows
  if (!WINDOWS)
    def test_dash_0_splits_records
      output = jruby_with_pipe("echo '1,2,3'", %Q{ -054 -n -e 'puts $_ + " "'})
      assert_equal 0, $?.exitstatus
      assert_equal "1, ,2, ,3\n ,", output
    end
  end

  def test_dash_little_c_checks_syntax
    with_jruby_shell_spawning do
      with_temp_script("bad : code") do |s|
        assert_match /SyntaxError/, jruby("-c #{s.path} 2>&1")
        assert_not_equal 0, $?.exitstatus
      end
    end
  end

  def test_dash_little_c_checks_syntax_only
    with_jruby_shell_spawning do
      with_temp_script(%q{ puts "a" }) do |s|
        assert_match /Syntax OK/, jruby(" -c #{s.path} 2>&1").chomp
        assert_equal 0, $?.exitstatus
      end
    end
  end
  
  # TODO -l: no idea what line ending processing is
  def test_dash_little_n_wraps_script_with_while_gets
    # FIXME: currently fails on windows and IBM JDK
    unless WINDOWS || IBM_JVM
      with_temp_script(%q{ puts "#{$_}#{$_}" }) do |s|
        output = IO.popen("echo \"a\nb\" | #{RUBY} -n #{s.path}", "r") { |p| p.read }
        assert_equal 0, $?.exitstatus
        assert_equal "a\na\nb\nb\n", output
      end
    end
  end

  def test_dash_little_p_wraps_script_with_while_gets_and_prints
    # FIXME: currently fails on Windows and IBM JDK
    unless WINDOWS || IBM_JVM
      with_temp_script(%q{ puts "#{$_}#{$_}" }) do |s|
        output = IO.popen("echo \"a\nb\" | #{RUBY} -p #{s.path}", "r") { |p| p.read }
        assert_equal 0, $?.exitstatus
        assert_equal "a\na\na\nb\nb\nb\n", output
      end
    end
  end

  # two args passed in which we can see as globals.  We also can see that
  # ARGV has removed those args from its list.  Also an improperly formatted
  # -s option (-g-a=123) is passed and is ignored.
  def test_dash_little_s
    with_temp_script(%q{puts $g, $v, $foo, *ARGV}) do |s|
      assert_equal "nil\n123\nbar\n4\n5\n6", `#{RUBY} -s #{s.path} -g-a=123 -v=123 -foo=bar 4 5 6`.chomp
      assert_equal 0, $?.exitstatus
    end
  end

  def test_dash_little_s_options_must_come_after_script
    with_temp_script(%q{puts $v, *ARGV}) do |s|
      assert_equal "nil\na\n-v=123\nb\nc", `#{RUBY} -s #{s.path} a -v=123 b c`.chomp
      assert_equal 0, $?.exitstatus
    end
  end

  # JRUBY-2693
  def test_dash_little_r_provides_program_name_to_loaded_library
    with_temp_script(%q{puts $0; puts $PROGRAM_NAME}) do |s|
      begin
        # tempfile does not put the .rb extension at the end, so -r does not find it
        path = s.path + ".rb"
        FileUtils.cp(s.path, path)
        assert_equal("#{path}\n#{path}\n#{path}\n#{path}\n",
                     jruby("-r#{path} #{path}"))
        assert_equal 0, $?.exitstatus
      ensure
        File.unlink(path) rescue nil
      end
    end
  end

  # This test is difficult to indicate meaning with. I am calling 
  # jgem, as it should not exist outside the jruby.bin directory.
  def test_dash_big_S_executes_script_in_jruby_bin_dir
    assert_match /^\d+\.\d+\.\d+/, `#{RUBY} -S jgem --version`
    assert_equal 0, $?.exitstatus
  end

  def test_dash_big_S_resolves_absolute___FILE___correctly
    with_temp_script(%q{puts __FILE__}) do |s|
      output = jruby("-S #{s.path}").chomp

      assert_equal 0, $?.exitstatus
      assert_equal s.path, output
    end
  end

  def test_dash_big_S_resolves_relative___FILE___correctly
    
    with_temp_script(%q{puts __FILE__}) do |s|
      Dir.chdir(Dir.tmpdir)
      relative_tmp = File.basename(s.path)
      output = jruby("-S #{relative_tmp}").chomp

      assert_equal 0, $?.exitstatus
      assert_equal relative_tmp, output
    end
  end

  

  def test_dash_little_v_version_verbose_T_taint_d_debug_K_kcode_r_require_b_benchmarks_a_splitsinput_I_loadpath_C_cwd_F_delimeter_J_javaprop
    e_line = 'puts $VERBOSE, $SAFE, $DEBUG, $KCODE, $F.join(59.chr), $LOAD_PATH.join(44.chr), Dir.pwd, Java::java::lang::System.getProperty(:foo.to_s)'
    args = " -J-Dfoo=bar -v -T3 -d -Ku -b -a -n -Ihello -C .. -F, -e #{q + e_line + q}"
    lines = jruby_with_pipe("echo 1,2,3", args).split("\n")
    assert_equal 0, $?.exitstatus
    parent_dir = Dir.chdir('..') { Dir.pwd }

    assert_match /ruby \d+\.\d+\.\d+/, lines[0]
    assert_match /true$/, lines[1]
    assert_equal "3", lines[2]
    assert_equal "true", lines[3]
    assert_equal "UTF8", lines[4]
    assert_equal "1;2;3", lines[5].rstrip
    assert_match /^hello/, lines[6]
    # The gsub is for windows
    assert_equal "#{parent_dir}", lines[7].gsub('\\', '/')
    assert_equal "bar", lines[8]
    assert_match /Runtime: \d+ ms/, lines[9]

    e_line = 'puts Gem'
    args = " -rrubygems -e #{q + e_line + q}"
    lines = jruby_with_pipe("echo 1,2,3", args).split("\n")
    assert_equal 0, $?.exitstatus

    assert_equal "Gem", lines[0]
  end

  def test_dash_big_C
    if (WINDOWS)
      res = jruby('-CC:/ -e "puts Dir.pwd"').rstrip
      assert_equal "C:/", res
    else
      res = jruby('-C/ -e "puts Dir.pwd"').rstrip
      assert_equal "/", res
    end
    assert_equal 0, $?.exitstatus
  end

  def test_dash_big_C_error
    out = jruby('-CaeAEUAOEUAeu_NOT_EXIST_xxx -e "puts Dir.pwd"').rstrip
    assert_equal 1, $?.exitstatus
    assert_match /chdir.*fatal/, out
  end

  def test_dash_little_w_turns_warnings_on
    with_jruby_shell_spawning do
      assert_match /warning/, `#{RUBY} -v -e "defined? true" 2>&1`
      assert_equal 0, $?.exitstatus
    end
  end

  def test_dash_big_w_sets_warning_level
    with_jruby_shell_spawning do
      with_temp_script("defined? true") do |s|
        assert_equal "", jruby("-W1 #{s.path} 2>&1")
        assert_equal 0, $?.exitstatus
        assert_match /warning/, jruby("-W2 #{s.path} 2>&1")
        assert_equal 0, $?.exitstatus
      end
    end    
  end

  def test_dash_big_x_sets_extended_options
    # turn on ObjectSpace
    with_temp_script("ObjectSpace.each_object(Fixnum) {|o| puts o.inspect}") do |s|
      assert_no_match /ObjectSpace is disabled/, jruby("-X+O #{s.path} 2>&1")
      assert_equal 0, $?.exitstatus
    end
  end

  def test_dash_dash_copyright_displays_copyright
     assert_match /Copyright \(C\) 2001-2.../, `#{RUBY} --copyright`
     assert_equal 0, $?.exitstatus
  end

  # TODO --debug: cannot figure out how to test

  # TODO --jdb: cannot figure out how to test

  def test_dash_dash_properties_shows_list_of_properties
    assert_match /^These properties can be used/, `#{RUBY} --properties`
    assert_equal 0, $?.exitstatus
  end

  def test_dash_dash_version_shows_version
    version_string = `#{RUBY} --version`
    assert_equal 0, $?.exitstatus
    assert_match /ruby \d+\.\d+\.\d+/, version_string
    assert_match /jruby \d+\.\d+\.\d+/, version_string
  end

  # JRUBY-2648 [Note: jre6 on windows does not ship server VM - use jdk]
  def test_server_vm_option
    # server VM when explicitly set --server
    result = jruby(%Q{--server -rjava \
      -e "print java.lang.management.ManagementFactory.getCompilationMXBean.name"})
    assert_equal 0, $?.exitstatus
    assert_match /(tiered|server|j9jit24|j9jit23|bea jrockit\(r\) optimizing compiler)/, result.downcase
  end

  # JRUBY-3962
  def test_args_with_rubyopt
    rubyopt_org = ENV['RUBYOPT']
    args = "-e 'puts ARGV.join' a b c"

    ENV['RUBYOPT'] = '-rrubygems'
    assert_equal 'abc', jruby(args).chomp

    ENV.delete 'RUBYOPT'
    assert_equal 'abc', jruby(args).chomp

    if rubyopt_org
      ENV['RUBYOPT'] = rubyopt_org
    end
  end


  # JRUBY-2648 [Note: Originally these tests had tests for default vm and
  # also for -J options in addition to jruby options (-J-client versus 
  # --client).  In other tests we test that -J works and passes thru and
  # we should not assume to know what versions of Java will have as their
  # default VM.
  def test_client_vm_option
    arch = java.lang.System.getProperty('sun.arch.data.model')
    if (arch == nil || arch == '64')
      # Either non-Sun JVM, or x64 JVM (which doesn't have client VM)
      return
    end

    # client VM when explicitly set via --client
    result = jruby(%Q{--client -rjava \
      -e "print java.lang.management.ManagementFactory.getCompilationMXBean.name"})
    assert_equal 0, $?.exitstatus
    assert_match /client|j9jit24|j9jit23|bea jrockit\(r\) optimizing compiler/, result.downcase
  end
  
  # JRUBY-2821
  def test_with_interesting_file_names
    names = ["test-q", "test-d", "test--", "test-_", "test_U", "test_S_", "___D_",
             "test__", "test_U_D", "_P_U_S_D"]
    rgxes = [/test-q/, /test-d/, /test--/, /test-_/, /test_U/, /test_S_/, /___D_/,
             /test__/, /test_U_D/, /_P_U_S_D/]

    names.each_with_index do |name, idx|
      with_jruby_shell_spawning do
        with_temp_script('print __FILE__', name) do |s|
          assert_match rgxes[idx], jruby("#{s.path}")
          assert_equal 0, $?.exitstatus
        end
      end
    end
  end

  # JRUBY-3467
  def test_blank_arg_ends_arg_processing
    config = org.jruby.RubyInstanceConfig.new
    config.process_arguments(["-v", "", "-d"].to_java :string)
    # -v argument should be processed
    assert config.verbose?
    # -d argument should not be processed as an interpreter arg
    assert !config.debug?
  end

  # JRUBY-4288
  if (WINDOWS)
    def test_case_insensitive_jruby
      weird_jruby = '"' + File.join([Config::CONFIG['bindir'], 'jRuBy']) << Config::CONFIG['EXEEXT'] + '"'
      with_jruby_shell_spawning do
        res = `cmd.exe /c #{weird_jruby} -e "puts 1"`.rstrip
        assert_equal '1', res
        assert_equal 0, $?.exitstatus
      end
    end
  end

  # JRUBY-4289
  if (WINDOWS)
    def test_uppercase_exe
      weird_jruby = '"' + File.join([Config::CONFIG['bindir'], 'jRuBy']) << '.ExE' + '"'
      with_jruby_shell_spawning do
        res = `#{weird_jruby} -e "puts 1"`.rstrip
        assert_equal '1', res
        assert_equal 0, $?.exitstatus
      end
    end
  end
  
  # JRUBY-4290
  if (WINDOWS)
    def test_uppercase_in_process
      version = `rUbY.ExE -v`.rstrip
      assert_equal 0, $?.exitstatus
      assert_match /java/, version
    end
  end
end