Files
mruby-mruby/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
T
dearblue a045b6b8d9 Allow to mixed and specify *.rb and *.mrb in bin/mruby
It is not decides by the extension.
In order to be recognized as a `.mrb` file, the following three points must be satisfied:
- File starts with "RITE"
- At least `sizeof(struct rite_binary_header)` bytes can be read
- `NUL` is included in the first 64 bytes of the file
If these are not met, it is judged as a text file and it is processed as a Ruby script.

The `bin/mruby -b` switch is still available which treats the given file as a `.mrb` file.

New `MRB_API` function:
- `include/mruby/compile.h` and `mrbgems/mruby-compiler/core/parse.y`
  - `mrb_load_detect_file_cxt()` (remove with `MRB_DISABLE_STDIO`)

NOTE:
- Even script files now always open in binary mode for `bin/mruby`.
  The `\r\n` is handled by the `nextc()` function already, so there is no problem even on Windows.
- The `nextc0()` function in `mrbgems/mruby-compiler/core/parse.y` can now specify a string buffer and a file pointer at the same time.
  In this case, get it from the string buffer first.

This patch includes modifies by comment of https://github.com/mruby/mruby/pull/5157.
2020-11-21 19:05:46 +09:00

74 lines
2.2 KiB
Ruby

require 'tempfile'
assert('no files') do
o = `#{cmd('mruby-strip')} 2>&1`
assert_equal 1, $?.exitstatus
assert_equal "no files to strip", o.split("\n")[0]
end
assert('file not found') do
o = `#{cmd('mruby-strip')} not_found.mrb 2>&1`
assert_equal 1, $?.exitstatus
assert_equal "can't open file for reading not_found.mrb\n", o
end
assert('not irep file') do
t = Tempfile.new('script.rb')
t.write 'p test\n'
t.flush
o = `#{cmd('mruby-strip')} #{t.path} 2>&1`
assert_equal 1, $?.exitstatus
assert_equal "can't read irep file #{t.path}\n", o
end
assert('success') do
script_file, compiled1, compiled2 =
Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
script_file.write "p 'test'\n"
script_file.flush
`#{cmd('mrbc')} -g -o #{compiled1.path} #{script_file.path}`
`#{cmd('mrbc')} -g -o #{compiled2.path} #{script_file.path}`
o = `#{cmd('mruby-strip')} #{compiled1.path}`
assert_equal 0, $?.exitstatus
assert_equal "", o
assert_equal `#{cmd('mruby')} #{script_file.path}`, `#{cmd('mruby')} #{compiled1.path}`
o = `#{cmd('mruby-strip')} #{compiled1.path} #{compiled2.path}`
assert_equal 0, $?.exitstatus
assert_equal "", o
end
assert('check debug section') do
script_file, with_debug, without_debug =
Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
script_file.write "p 'test'\n"
script_file.flush
`#{cmd('mrbc')} -o #{without_debug.path} #{script_file.path}`
`#{cmd('mrbc')} -g -o #{with_debug.path} #{script_file.path}`
assert_true with_debug.size >= without_debug.size
`#{cmd('mruby-strip')} #{with_debug.path}`
assert_equal without_debug.size, with_debug.size
end
assert('check lv section') do
script_file, with_lv, without_lv =
Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
script_file.write <<EOS
a, b = 0, 1
a += b
p Kernel.local_variables
EOS
script_file.flush
`#{cmd('mrbc')} -o #{with_lv.path} #{script_file.path}`
`#{cmd('mrbc')} -o #{without_lv.path} #{script_file.path}`
`#{cmd('mruby-strip')} -l #{without_lv.path}`
assert_true without_lv.size < with_lv.size
#
# assert_equal '[:a, :b]', `#{cmd('mruby')} -b #{with_lv.path}`.chomp
# assert_equal '[]', `#{cmd('mruby')} -b #{without_lv.path}`.chomp
end