summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby-require/test/test2.rb
blob: b66b9fdf31c2d941170d7cf3e58981158ea6adb2 (plain)
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
$dir = File.join(Dir.tmpdir, "mruby-require-test-#{Time.now.to_i}.#{Time.now.usec}")

def test_setup
  Dir.mkdir($dir)  unless File.exist?($dir)

  File.open(File.join($dir, "test.rb"), "w") do |fp|
    fp.puts "$require_test_variable = 123"
  end

  File.open(File.join($dir, "test_dir.rb"), "w") do |fp|
    fp.puts "$test_dir = 'test_dir'"
  end
  Dir.mkdir(File.join($dir, "test_dir"))
  File.open(File.join($dir, "test_dir", "test_dir.rb"), "w") do |fp|
    fp.puts "$test_dir2 = 'test_dir/test_dir'"
  end

  File.open(File.join($dir, "test_conf.conf"), "w") do |fp|
    fp.puts "$test_conf = 'test_conf'"
  end

  File.open(File.join($dir, "empty.rb"), "w")

  test_reset
end

def test_reset
  $require_test_variable = nil
  $test_dir = nil
  $test_dir2 = nil
  $test_conf = nil
  $LOAD_PATH = [$dir]
  $" = []
end

def remove_file_recursive(path)
  if File.directory? path
    Dir.entries(path).each do |entry|
      next if ['.', '..'].include?(entry)
      remove_file_recursive File.join(path, entry)
    end
    Dir.unlink path
  else
    File.unlink path
  end
end

def test_cleanup
  if $dir && File.exist?($dir)
    remove_file_recursive $dir
  end
end

#####
test_setup
#####

assert("require 'test' should be success") do
  test_reset

  assert_true require("test"), "require returns true when success"
  assert_equal [File.join($dir, "test.rb")], $"
  assert_equal 123, $require_test_variable
  $require_test_variable = 789
  assert_false require("test"), "2nd require should returns false"
  assert_equal 789, $require_test_variable

  test_reset

  assert_true require("test.rb"), "require should be success with '.rb'"
  assert_equal [File.join($dir, "test.rb")], $"
end

assert("require with absolute path should be success") do
  test_reset
  assert_true require(File.join($dir, "test"))
  assert_equal [File.join($dir, "test.rb")], $"

  test_reset
  assert_true require(File.join($dir, "test.rb"))
  assert_equal [File.join($dir, "test.rb")], $"
end

assert("require with absolute path && empty load_path") do
  test_reset
  $LOAD_PATH = []

  assert_raise LoadError, "cannot load test.rb" do
    require "test"
  end
  assert_equal true, require(File.join($dir, "test"))
end

assert("require 'test_dir' should be success") do
  test_reset

  assert_true require("test_dir"), "require 'test_dir' should be load 'test_dir.rb'"
  assert_equal [File.join($dir, "test_dir.rb")], $"
  assert_true require("test_dir/test_dir"), "require 'test_dir/test_dir' should be success"
  assert_equal 'test_dir/test_dir', $test_dir2
end

assert("require 'test_conf' should be fail") do
  test_reset

  assert_raise LoadError, "require 'test_conf.conf' should be fail" do
    require("test_conf.conf")
  end
  assert_raise LoadError, "require method can't load *.conf" do
    require File.join($dir, "test_conf.conf")
  end
end

assert("require 'empty' should be success") do
  test_reset

  assert_true require("empty")
  assert_equal 0, File.size(File.join($dir, "empty.rb"))
end

assert("load 'test.rb' should be success") do
  test_reset

  assert_true load(File.join($dir, "test.rb"))
  assert_equal 123, $require_test_variable
  assert_true $".empty?
end

assert("load 'test_conf.conf' should be success") do
  test_reset

  assert_equal true, load(File.join($dir, "test_conf.conf"))
  assert_equal "test_conf", $test_conf
end


#####
test_cleanup
#####