faster_pathname 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,212 @@
1
+ require "open3"
2
+ require "timeout"
3
+
4
+ module EnvUtil
5
+ def rubybin
6
+ unless ENV["RUBYOPT"]
7
+
8
+ end
9
+ if ruby = ENV["RUBY"]
10
+ return ruby
11
+ end
12
+ ruby = "ruby"
13
+ rubyexe = ruby+".exe"
14
+ 3.times do
15
+ if File.exist? ruby and File.executable? ruby and !File.directory? ruby
16
+ return File.expand_path(ruby)
17
+ end
18
+ if File.exist? rubyexe and File.executable? rubyexe
19
+ return File.expand_path(rubyexe)
20
+ end
21
+ ruby = File.join("..", ruby)
22
+ end
23
+ if defined?(RbConfig.ruby)
24
+ RbConfig.ruby
25
+ else
26
+ "ruby"
27
+ end
28
+ end
29
+ module_function :rubybin
30
+
31
+ LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
32
+
33
+ def invoke_ruby(args, stdin_data="", capture_stdout=false, capture_stderr=false, opt={})
34
+ in_c, in_p = IO.pipe
35
+ out_p, out_c = IO.pipe if capture_stdout
36
+ err_p, err_c = IO.pipe if capture_stderr && capture_stderr != :merge_to_stdout
37
+ opt = opt.dup
38
+ opt[:in] = in_c
39
+ opt[:out] = out_c if capture_stdout
40
+ opt[:err] = capture_stderr == :merge_to_stdout ? out_c : err_c if capture_stderr
41
+ if enc = opt.delete(:encoding)
42
+ out_p.set_encoding(enc) if out_p
43
+ err_p.set_encoding(enc) if err_p
44
+ end
45
+ timeout = opt.delete(:timeout) || 10
46
+ c = "C"
47
+ child_env = {}
48
+ LANG_ENVS.each {|lc| child_env[lc] = c}
49
+ if Array === args and Hash === args.first
50
+ child_env.update(args.shift)
51
+ end
52
+ args = [args] if args.kind_of?(String)
53
+ pid = spawn(child_env, EnvUtil.rubybin, *args, opt)
54
+ in_c.close
55
+ out_c.close if capture_stdout
56
+ err_c.close if capture_stderr && capture_stderr != :merge_to_stdout
57
+ if block_given?
58
+ return yield in_p, out_p, err_p, pid
59
+ else
60
+ th_stdout = Thread.new { out_p.read } if capture_stdout
61
+ th_stderr = Thread.new { err_p.read } if capture_stderr && capture_stderr != :merge_to_stdout
62
+ in_p.write stdin_data.to_str
63
+ in_p.close
64
+ if (!th_stdout || th_stdout.join(timeout)) && (!th_stderr || th_stderr.join(timeout))
65
+ stdout = th_stdout.value if capture_stdout
66
+ stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout
67
+ else
68
+ raise Timeout::Error
69
+ end
70
+ out_p.close if capture_stdout
71
+ err_p.close if capture_stderr && capture_stderr != :merge_to_stdout
72
+ Process.wait pid
73
+ status = $?
74
+ return stdout, stderr, status
75
+ end
76
+ ensure
77
+ [in_c, in_p, out_c, out_p, err_c, err_p].each do |io|
78
+ io.close if io && !io.closed?
79
+ end
80
+ [th_stdout, th_stderr].each do |th|
81
+ (th.kill; th.join) if th
82
+ end
83
+ end
84
+ module_function :invoke_ruby
85
+
86
+ alias rubyexec invoke_ruby
87
+ class << self
88
+ alias rubyexec invoke_ruby
89
+ end
90
+
91
+ def verbose_warning
92
+ class << (stderr = "")
93
+ alias write <<
94
+ end
95
+ stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
96
+ yield stderr
97
+ ensure
98
+ stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
99
+ return stderr
100
+ end
101
+ module_function :verbose_warning
102
+
103
+ def suppress_warning
104
+ verbose, $VERBOSE = $VERBOSE, nil
105
+ yield
106
+ ensure
107
+ $VERBOSE = verbose
108
+ end
109
+ module_function :suppress_warning
110
+
111
+ def under_gc_stress
112
+ stress, GC.stress = GC.stress, true
113
+ yield
114
+ ensure
115
+ GC.stress = stress
116
+ end
117
+ module_function :under_gc_stress
118
+ end
119
+
120
+ module Test
121
+ module Unit
122
+ module Assertions
123
+ public
124
+ def assert_normal_exit(testsrc, message = '', opt = {})
125
+ if opt.include?(:child_env)
126
+ opt = opt.dup
127
+ child_env = [opt.delete(:child_env)] || []
128
+ else
129
+ child_env = []
130
+ end
131
+ out, _, status = EnvUtil.invoke_ruby(child_env + %W'-W0', testsrc, true, :merge_to_stdout, opt)
132
+ pid = status.pid
133
+ faildesc = proc do
134
+ signo = status.termsig
135
+ signame = Signal.list.invert[signo]
136
+ sigdesc = "signal #{signo}"
137
+ if signame
138
+ sigdesc = "SIG#{signame} (#{sigdesc})"
139
+ end
140
+ if status.coredump?
141
+ sigdesc << " (core dumped)"
142
+ end
143
+ full_message = ''
144
+ if !message.empty?
145
+ full_message << message << "\n"
146
+ end
147
+ full_message << "pid #{pid} killed by #{sigdesc}"
148
+ if !out.empty?
149
+ out << "\n" if /\n\z/ !~ out
150
+ full_message << "\n#{out.gsub(/^/, '| ')}"
151
+ end
152
+ full_message
153
+ end
154
+ assert !status.signaled?, faildesc
155
+ end
156
+
157
+ def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil, opt={})
158
+ stdout, stderr, status = EnvUtil.invoke_ruby(args, test_stdin, true, true, opt)
159
+ if block_given?
160
+ yield(stdout.lines.map {|l| l.chomp }, stderr.lines.map {|l| l.chomp })
161
+ else
162
+ if test_stdout.is_a?(Regexp)
163
+ assert_match(test_stdout, stdout, message)
164
+ else
165
+ assert_equal(test_stdout, stdout.lines.map {|l| l.chomp }, message)
166
+ end
167
+ if test_stderr.is_a?(Regexp)
168
+ assert_match(test_stderr, stderr, message)
169
+ else
170
+ assert_equal(test_stderr, stderr.lines.map {|l| l.chomp }, message)
171
+ end
172
+ status
173
+ end
174
+ end
175
+
176
+ def assert_ruby_status(args, test_stdin="", message=nil, opt={})
177
+ _, _, status = EnvUtil.invoke_ruby(args, test_stdin, false, false, opt)
178
+ m = message ? "#{message} (#{status.inspect})" : "ruby exit status is not success: #{status.inspect}"
179
+ assert(status.success?, m)
180
+ end
181
+
182
+ def assert_warn(msg)
183
+ stderr = EnvUtil.verbose_warning { yield }
184
+ assert(msg === stderr, "warning message #{stderr.inspect} is expected to match #{msg.inspect}")
185
+ end
186
+
187
+
188
+ def assert_is_minus_zero(f)
189
+ assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0")
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ begin
196
+ require 'rbconfig'
197
+ rescue LoadError
198
+ else
199
+ module RbConfig
200
+ @ruby = EnvUtil.rubybin
201
+ class << self
202
+ undef ruby if method_defined?(:ruby)
203
+ attr_reader :ruby
204
+ end
205
+ dir = File.dirname(ruby)
206
+ name = File.basename(ruby, CONFIG['EXEEXT'])
207
+ CONFIG['bindir'] = dir
208
+ CONFIG['ruby_install_name'] = name
209
+ CONFIG['RUBY_INSTALL_NAME'] = name
210
+ Gem::ConfigMap[:bindir] = dir if defined?(Gem::ConfigMap)
211
+ end
212
+ end
@@ -0,0 +1,1302 @@
1
+ require 'pathname'
2
+ require File.expand_path("../../lib/faster_pathname", __FILE__)
3
+
4
+ require 'test/unit'
5
+ require 'fileutils'
6
+ require 'tmpdir'
7
+ require 'enumerator'
8
+
9
+ require File.expand_path("../envutil", __FILE__)
10
+
11
+ class TestPathname < Test::Unit::TestCase
12
+ def self.define_assertion(name, linenum, &block)
13
+ name = "test_#{name}_#{linenum}"
14
+ define_method(name, &block)
15
+ end
16
+
17
+ def self.get_linenum
18
+ if /:(\d+):/ =~ caller[1]
19
+ $1.to_i
20
+ else
21
+ nil
22
+ end
23
+ end
24
+
25
+ def self.defassert(name, result, *args)
26
+ define_assertion(name, get_linenum) {
27
+ mesg = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
28
+ assert_nothing_raised(mesg) {
29
+ assert_equal(result, self.send(name, *args), mesg)
30
+ }
31
+ }
32
+ end
33
+
34
+ def self.defassert_raise(name, exc, *args)
35
+ define_assertion(name, get_linenum) {
36
+ message = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
37
+ assert_raise(exc, message) { self.send(name, *args) }
38
+ }
39
+ end
40
+
41
+ DOSISH = File::ALT_SEPARATOR != nil
42
+ DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
43
+ DOSISH_UNC = File.dirname("//") == "//"
44
+
45
+ def cleanpath_aggressive(path)
46
+ Pathname.new(path).cleanpath.to_s
47
+ end
48
+
49
+ defassert(:cleanpath_aggressive, '/', '/')
50
+ defassert(:cleanpath_aggressive, '.', '')
51
+ defassert(:cleanpath_aggressive, '.', '.')
52
+ defassert(:cleanpath_aggressive, '..', '..')
53
+ defassert(:cleanpath_aggressive, 'a', 'a')
54
+ defassert(:cleanpath_aggressive, '/', '/.')
55
+ defassert(:cleanpath_aggressive, '/', '/..')
56
+ defassert(:cleanpath_aggressive, '/a', '/a')
57
+ defassert(:cleanpath_aggressive, '.', './')
58
+ defassert(:cleanpath_aggressive, '..', '../')
59
+ defassert(:cleanpath_aggressive, 'a', 'a/')
60
+ defassert(:cleanpath_aggressive, 'a/b', 'a//b')
61
+ defassert(:cleanpath_aggressive, 'a', 'a/.')
62
+ defassert(:cleanpath_aggressive, 'a', 'a/./')
63
+ defassert(:cleanpath_aggressive, '.', 'a/..')
64
+ defassert(:cleanpath_aggressive, '.', 'a/../')
65
+ defassert(:cleanpath_aggressive, '/a', '/a/.')
66
+ defassert(:cleanpath_aggressive, '..', './..')
67
+ defassert(:cleanpath_aggressive, '..', '../.')
68
+ defassert(:cleanpath_aggressive, '..', './../')
69
+ defassert(:cleanpath_aggressive, '..', '.././')
70
+ defassert(:cleanpath_aggressive, '/', '/./..')
71
+ defassert(:cleanpath_aggressive, '/', '/../.')
72
+ defassert(:cleanpath_aggressive, '/', '/./../')
73
+ defassert(:cleanpath_aggressive, '/', '/.././')
74
+ defassert(:cleanpath_aggressive, 'a/b/c', 'a/b/c')
75
+ defassert(:cleanpath_aggressive, 'b/c', './b/c')
76
+ defassert(:cleanpath_aggressive, 'a/c', 'a/./c')
77
+ defassert(:cleanpath_aggressive, 'a/b', 'a/b/.')
78
+ defassert(:cleanpath_aggressive, '.', 'a/../.')
79
+ defassert(:cleanpath_aggressive, '/a', '/../.././../a')
80
+ defassert(:cleanpath_aggressive, '../../d', 'a/b/../../../../c/../d')
81
+
82
+ if DOSISH_UNC
83
+ defassert(:cleanpath_aggressive, '//a/b/c', '//a/b/c/')
84
+ else
85
+ defassert(:cleanpath_aggressive, '/', '///')
86
+ defassert(:cleanpath_aggressive, '/a', '///a')
87
+ defassert(:cleanpath_aggressive, '/', '///..')
88
+ defassert(:cleanpath_aggressive, '/', '///.')
89
+ defassert(:cleanpath_aggressive, '/', '///a/../..')
90
+ end
91
+
92
+ def cleanpath_conservative(path)
93
+ Pathname.new(path).cleanpath(true).to_s
94
+ end
95
+
96
+ defassert(:cleanpath_conservative, '/', '/')
97
+ defassert(:cleanpath_conservative, '.', '')
98
+ defassert(:cleanpath_conservative, '.', '.')
99
+ defassert(:cleanpath_conservative, '..', '..')
100
+ defassert(:cleanpath_conservative, 'a', 'a')
101
+ defassert(:cleanpath_conservative, '/', '/.')
102
+ defassert(:cleanpath_conservative, '/', '/..')
103
+ defassert(:cleanpath_conservative, '/a', '/a')
104
+ defassert(:cleanpath_conservative, '.', './')
105
+ defassert(:cleanpath_conservative, '..', '../')
106
+ defassert(:cleanpath_conservative, 'a/', 'a/')
107
+ defassert(:cleanpath_conservative, 'a/b', 'a//b')
108
+ defassert(:cleanpath_conservative, 'a/.', 'a/.')
109
+ defassert(:cleanpath_conservative, 'a/.', 'a/./')
110
+ defassert(:cleanpath_conservative, 'a/..', 'a/../')
111
+ defassert(:cleanpath_conservative, '/a/.', '/a/.')
112
+ defassert(:cleanpath_conservative, '..', './..')
113
+ defassert(:cleanpath_conservative, '..', '../.')
114
+ defassert(:cleanpath_conservative, '..', './../')
115
+ defassert(:cleanpath_conservative, '..', '.././')
116
+ defassert(:cleanpath_conservative, '/', '/./..')
117
+ defassert(:cleanpath_conservative, '/', '/../.')
118
+ defassert(:cleanpath_conservative, '/', '/./../')
119
+ defassert(:cleanpath_conservative, '/', '/.././')
120
+ defassert(:cleanpath_conservative, 'a/b/c', 'a/b/c')
121
+ defassert(:cleanpath_conservative, 'b/c', './b/c')
122
+ defassert(:cleanpath_conservative, 'a/c', 'a/./c')
123
+ defassert(:cleanpath_conservative, 'a/b/.', 'a/b/.')
124
+ defassert(:cleanpath_conservative, 'a/..', 'a/../.')
125
+ defassert(:cleanpath_conservative, '/a', '/../.././../a')
126
+ defassert(:cleanpath_conservative, 'a/b/../../../../c/../d', 'a/b/../../../../c/../d')
127
+
128
+ if DOSISH_UNC
129
+ defassert(:cleanpath_conservative, '//', '//')
130
+ else
131
+ defassert(:cleanpath_conservative, '/', '//')
132
+ end
133
+
134
+ # has_trailing_separator?(path) -> bool
135
+ def has_trailing_separator?(path)
136
+ Pathname.allocate.__send__(:has_trailing_separator?, path)
137
+ end
138
+
139
+ defassert(:has_trailing_separator?, false, "/")
140
+ defassert(:has_trailing_separator?, false, "///")
141
+ defassert(:has_trailing_separator?, false, "a")
142
+ defassert(:has_trailing_separator?, true, "a/")
143
+
144
+ def add_trailing_separator(path)
145
+ Pathname.allocate.__send__(:add_trailing_separator, path)
146
+ end
147
+
148
+ def del_trailing_separator(path)
149
+ Pathname.allocate.__send__(:del_trailing_separator, path)
150
+ end
151
+
152
+ defassert(:del_trailing_separator, "/", "/")
153
+ defassert(:del_trailing_separator, "/a", "/a")
154
+ defassert(:del_trailing_separator, "/a", "/a/")
155
+ defassert(:del_trailing_separator, "/a", "/a//")
156
+ defassert(:del_trailing_separator, ".", ".")
157
+ defassert(:del_trailing_separator, ".", "./")
158
+ defassert(:del_trailing_separator, ".", ".//")
159
+
160
+ if DOSISH_DRIVE_LETTER
161
+ defassert(:del_trailing_separator, "A:", "A:")
162
+ defassert(:del_trailing_separator, "A:/", "A:/")
163
+ defassert(:del_trailing_separator, "A:/", "A://")
164
+ defassert(:del_trailing_separator, "A:.", "A:.")
165
+ defassert(:del_trailing_separator, "A:.", "A:./")
166
+ defassert(:del_trailing_separator, "A:.", "A:.//")
167
+ end
168
+
169
+ if DOSISH_UNC
170
+ defassert(:del_trailing_separator, "//", "//")
171
+ defassert(:del_trailing_separator, "//a", "//a")
172
+ defassert(:del_trailing_separator, "//a", "//a/")
173
+ defassert(:del_trailing_separator, "//a", "//a//")
174
+ defassert(:del_trailing_separator, "//a/b", "//a/b")
175
+ defassert(:del_trailing_separator, "//a/b", "//a/b/")
176
+ defassert(:del_trailing_separator, "//a/b", "//a/b//")
177
+ defassert(:del_trailing_separator, "//a/b/c", "//a/b/c")
178
+ defassert(:del_trailing_separator, "//a/b/c", "//a/b/c/")
179
+ defassert(:del_trailing_separator, "//a/b/c", "//a/b/c//")
180
+ else
181
+ defassert(:del_trailing_separator, "/", "///")
182
+ defassert(:del_trailing_separator, "///a", "///a/")
183
+ end
184
+
185
+ if DOSISH
186
+ defassert(:del_trailing_separator, "a", "a\\")
187
+ defassert(:del_trailing_separator, "\225\\".force_encoding("cp932"), "\225\\\\".force_encoding("cp932"))
188
+ defassert(:del_trailing_separator, "\225".force_encoding("cp437"), "\225\\\\".force_encoding("cp437"))
189
+ end
190
+
191
+ def test_plus
192
+ assert_kind_of(Pathname, Pathname("a") + Pathname("b"))
193
+ end
194
+
195
+ def plus(path1, path2) # -> path
196
+ (Pathname.new(path1) + Pathname.new(path2)).to_s
197
+ end
198
+
199
+ defassert(:plus, '/', '/', '/')
200
+ defassert(:plus, 'a/b', 'a', 'b')
201
+ defassert(:plus, 'a', 'a', '.')
202
+ defassert(:plus, 'b', '.', 'b')
203
+ defassert(:plus, '.', '.', '.')
204
+ defassert(:plus, '/b', 'a', '/b')
205
+
206
+ defassert(:plus, '/', '/', '..')
207
+ defassert(:plus, '.', 'a', '..')
208
+ defassert(:plus, 'a', 'a/b', '..')
209
+ defassert(:plus, '../..', '..', '..')
210
+ defassert(:plus, '/c', '/', '../c')
211
+ defassert(:plus, 'c', 'a', '../c')
212
+ defassert(:plus, 'a/c', 'a/b', '../c')
213
+ defassert(:plus, '../../c', '..', '../c')
214
+
215
+ defassert(:plus, 'a//b/d//e', 'a//b/c', '../d//e')
216
+
217
+ def test_parent
218
+ assert_equal(Pathname("."), Pathname("a").parent)
219
+ end
220
+
221
+ def parent(path) # -> path
222
+ Pathname.new(path).parent.to_s
223
+ end
224
+
225
+ defassert(:parent, '/', '/')
226
+ defassert(:parent, '/', '/a')
227
+ defassert(:parent, '/a', '/a/b')
228
+ defassert(:parent, '/a/b', '/a/b/c')
229
+ defassert(:parent, '.', 'a')
230
+ defassert(:parent, 'a', 'a/b')
231
+ defassert(:parent, 'a/b', 'a/b/c')
232
+ defassert(:parent, '..', '.')
233
+ defassert(:parent, '../..', '..')
234
+
235
+ def test_join
236
+ r = Pathname("a").join(Pathname("b"), Pathname("c"))
237
+ assert_equal(Pathname("a/b/c"), r)
238
+ end
239
+
240
+ def test_absolute
241
+ assert_equal(true, Pathname("/").absolute?)
242
+ assert_equal(false, Pathname("a").absolute?)
243
+ end
244
+
245
+ def relative?(path)
246
+ Pathname.new(path).relative?
247
+ end
248
+
249
+ defassert(:relative?, false, '/')
250
+ defassert(:relative?, false, '/a')
251
+ defassert(:relative?, false, '/..')
252
+ defassert(:relative?, true, 'a')
253
+ defassert(:relative?, true, 'a/b')
254
+
255
+ if DOSISH_DRIVE_LETTER
256
+ defassert(:relative?, false, 'A:')
257
+ defassert(:relative?, false, 'A:/')
258
+ defassert(:relative?, false, 'A:/a')
259
+ end
260
+
261
+ if File.dirname('//') == '//'
262
+ defassert(:relative?, false, '//')
263
+ defassert(:relative?, false, '//a')
264
+ defassert(:relative?, false, '//a/')
265
+ defassert(:relative?, false, '//a/b')
266
+ defassert(:relative?, false, '//a/b/')
267
+ defassert(:relative?, false, '//a/b/c')
268
+ end
269
+
270
+ def relative_path_from(dest_directory, base_directory)
271
+ Pathname.new(dest_directory).relative_path_from(Pathname.new(base_directory)).to_s
272
+ end
273
+
274
+ defassert(:relative_path_from, "../a", "a", "b")
275
+ defassert(:relative_path_from, "../a", "a", "b/")
276
+ defassert(:relative_path_from, "../a", "a/", "b")
277
+ defassert(:relative_path_from, "../a", "a/", "b/")
278
+ defassert(:relative_path_from, "../a", "/a", "/b")
279
+ defassert(:relative_path_from, "../a", "/a", "/b/")
280
+ defassert(:relative_path_from, "../a", "/a/", "/b")
281
+ defassert(:relative_path_from, "../a", "/a/", "/b/")
282
+
283
+ defassert(:relative_path_from, "../b", "a/b", "a/c")
284
+ defassert(:relative_path_from, "../a", "../a", "../b")
285
+
286
+ defassert(:relative_path_from, "a", "a", ".")
287
+ defassert(:relative_path_from, "..", ".", "a")
288
+
289
+ defassert(:relative_path_from, ".", ".", ".")
290
+ defassert(:relative_path_from, ".", "..", "..")
291
+ defassert(:relative_path_from, "..", "..", ".")
292
+
293
+ defassert(:relative_path_from, "c/d", "/a/b/c/d", "/a/b")
294
+ defassert(:relative_path_from, "../..", "/a/b", "/a/b/c/d")
295
+ defassert(:relative_path_from, "../../../../e", "/e", "/a/b/c/d")
296
+ defassert(:relative_path_from, "../b/c", "a/b/c", "a/d")
297
+
298
+ defassert(:relative_path_from, "../a", "/../a", "/b")
299
+ defassert(:relative_path_from, "../../a", "../a", "b")
300
+ defassert(:relative_path_from, ".", "/a/../../b", "/b")
301
+ defassert(:relative_path_from, "..", "a/..", "a")
302
+ defassert(:relative_path_from, ".", "a/../b", "b")
303
+
304
+ defassert(:relative_path_from, "a", "a", "b/..")
305
+ defassert(:relative_path_from, "b/c", "b/c", "b/..")
306
+
307
+ defassert_raise(:relative_path_from, ArgumentError, "/", ".")
308
+ defassert_raise(:relative_path_from, ArgumentError, ".", "/")
309
+ defassert_raise(:relative_path_from, ArgumentError, "a", "..")
310
+ defassert_raise(:relative_path_from, ArgumentError, ".", "..")
311
+
312
+ def with_tmpchdir(base=nil)
313
+ Dir.mktmpdir(base) {|d|
314
+ d = Pathname.new(d).realpath.to_s
315
+ Dir.chdir(d) {
316
+ yield d
317
+ }
318
+ }
319
+ end
320
+
321
+ def has_symlink?
322
+ begin
323
+ File.symlink(nil, nil)
324
+ rescue NotImplementedError
325
+ return false
326
+ rescue TypeError
327
+ end
328
+ return true
329
+ end
330
+
331
+ def realpath(path, basedir=nil)
332
+ Pathname.new(path).realpath(basedir).to_s
333
+ end
334
+
335
+ def test_realpath
336
+ return if !has_symlink?
337
+ with_tmpchdir('rubytest-pathname') {|dir|
338
+ assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
339
+ File.symlink("not-exist-target", "#{dir}/not-exist")
340
+ assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
341
+
342
+ File.symlink("loop", "#{dir}/loop")
343
+ assert_raise(Errno::ELOOP) { realpath("#{dir}/loop") }
344
+ assert_raise(Errno::ELOOP) { realpath("#{dir}/loop", dir) }
345
+
346
+ File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
347
+ assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist2") }
348
+
349
+ File.open("#{dir}/exist-target", "w") {}
350
+ File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist2")
351
+ assert_nothing_raised { realpath("#{dir}/exist2") }
352
+
353
+ File.symlink("loop-relative", "loop-relative")
354
+ assert_raise(Errno::ELOOP) { realpath("#{dir}/loop-relative") }
355
+
356
+ Dir.mkdir("exist")
357
+ assert_equal("#{dir}/exist", realpath("exist"))
358
+ assert_raise(Errno::ELOOP) { realpath("../loop", "#{dir}/exist") }
359
+
360
+ File.symlink("loop1/loop1", "loop1")
361
+ assert_raise(Errno::ELOOP) { realpath("#{dir}/loop1") }
362
+
363
+ File.symlink("loop2", "loop3")
364
+ File.symlink("loop3", "loop2")
365
+ assert_raise(Errno::ELOOP) { realpath("#{dir}/loop2") }
366
+
367
+ Dir.mkdir("b")
368
+
369
+ File.symlink("b", "c")
370
+ assert_equal("#{dir}/b", realpath("c"))
371
+ assert_equal("#{dir}/b", realpath("c/../c"))
372
+ assert_equal("#{dir}/b", realpath("c/../c/../c/."))
373
+
374
+ File.symlink("..", "b/d")
375
+ assert_equal("#{dir}/b", realpath("c/d/c/d/c"))
376
+
377
+ File.symlink("#{dir}/b", "e")
378
+ assert_equal("#{dir}/b", realpath("e"))
379
+
380
+ Dir.mkdir("f")
381
+ Dir.mkdir("f/g")
382
+ File.symlink("f/g", "h")
383
+ assert_equal("#{dir}/f/g", realpath("h"))
384
+ File.chmod(0000, "f")
385
+ assert_raise(Errno::EACCES) { realpath("h") }
386
+ File.chmod(0755, "f")
387
+ }
388
+ end
389
+
390
+ def realdirpath(path)
391
+ Pathname.new(path).realdirpath.to_s
392
+ end
393
+
394
+ def test_realdirpath
395
+ return if !has_symlink?
396
+ Dir.mktmpdir('rubytest-pathname') {|dir|
397
+ rdir = realpath(dir)
398
+ assert_equal("#{rdir}/not-exist", realdirpath("#{dir}/not-exist"))
399
+ assert_raise(Errno::ENOENT) { realdirpath("#{dir}/not-exist/not-exist-child") }
400
+ File.symlink("not-exist-target", "#{dir}/not-exist")
401
+ assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist"))
402
+ File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
403
+ assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist2"))
404
+ File.open("#{dir}/exist-target", "w") {}
405
+ File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist")
406
+ assert_equal("#{rdir}/exist-target", realdirpath("#{dir}/exist"))
407
+ File.symlink("loop", "#{dir}/loop")
408
+ assert_raise(Errno::ELOOP) { realdirpath("#{dir}/loop") }
409
+ }
410
+ end
411
+
412
+ def descend(path)
413
+ Pathname.new(path).enum_for(:descend).map {|v| v.to_s }
414
+ end
415
+
416
+ defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
417
+ defassert(:descend, %w[a a/b a/b/c], "a/b/c")
418
+ defassert(:descend, %w[. ./a ./a/b ./a/b/c], "./a/b/c")
419
+ defassert(:descend, %w[a/], "a/")
420
+
421
+ def ascend(path)
422
+ Pathname.new(path).enum_for(:ascend).map {|v| v.to_s }
423
+ end
424
+
425
+ defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
426
+ defassert(:ascend, %w[a/b/c a/b a], "a/b/c")
427
+ defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
428
+ defassert(:ascend, %w[a/], "a/")
429
+
430
+ def test_initialize
431
+ p1 = Pathname.new('a')
432
+ assert_equal('a', p1.to_s)
433
+ p2 = Pathname.new(p1)
434
+ assert_equal(p1, p2)
435
+ end
436
+
437
+ def test_initialize_nul
438
+ assert_raise(ArgumentError) { Pathname.new("a\0") }
439
+ end
440
+
441
+ class AnotherStringLike # :nodoc:
442
+ def initialize(s) @s = s end
443
+ def to_str() @s end
444
+ def ==(other) @s == other end
445
+ end
446
+
447
+ def test_equality
448
+ obj = Pathname.new("a")
449
+ str = "a"
450
+ sym = :a
451
+ ano = AnotherStringLike.new("a")
452
+ assert_equal(false, obj == str)
453
+ assert_equal(false, str == obj)
454
+ assert_equal(false, obj == ano)
455
+ assert_equal(false, ano == obj)
456
+ assert_equal(false, obj == sym)
457
+ assert_equal(false, sym == obj)
458
+
459
+ obj2 = Pathname.new("a")
460
+ assert_equal(true, obj == obj2)
461
+ assert_equal(true, obj === obj2)
462
+ assert_equal(true, obj.eql?(obj2))
463
+ end
464
+
465
+ def test_hashkey
466
+ h = {}
467
+ h[Pathname.new("a")] = 1
468
+ h[Pathname.new("a")] = 2
469
+ assert_equal(1, h.size)
470
+ end
471
+
472
+ def assert_pathname_cmp(e, s1, s2)
473
+ p1 = Pathname.new(s1)
474
+ p2 = Pathname.new(s2)
475
+ r = p1 <=> p2
476
+ assert(e == r,
477
+ "#{p1.inspect} <=> #{p2.inspect}: <#{e}> expected but was <#{r}>")
478
+ end
479
+ def test_comparison
480
+ assert_pathname_cmp( 0, "a", "a")
481
+ assert_pathname_cmp( 1, "b", "a")
482
+ assert_pathname_cmp(-1, "a", "b")
483
+ ss = %w(
484
+ a
485
+ a/
486
+ a/b
487
+ a.
488
+ a0
489
+ )
490
+ s1 = ss.shift
491
+ ss.each {|s2|
492
+ assert_pathname_cmp(-1, s1, s2)
493
+ s1 = s2
494
+ }
495
+ end
496
+
497
+ def test_comparison_string
498
+ assert_equal(nil, Pathname.new("a") <=> "a")
499
+ assert_equal(nil, "a" <=> Pathname.new("a"))
500
+ end
501
+
502
+ def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
503
+ defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
504
+
505
+ def pathsubext(path, repl) Pathname.new(path).sub_ext(repl).to_s end
506
+ defassert(:pathsubext, 'a.o', 'a.c', '.o')
507
+ defassert(:pathsubext, 'a.o', 'a.c++', '.o')
508
+ defassert(:pathsubext, 'a.png', 'a.gif', '.png')
509
+ defassert(:pathsubext, 'ruby.tar.bz2', 'ruby.tar.gz', '.bz2')
510
+ defassert(:pathsubext, 'd/a.o', 'd/a.c', '.o')
511
+ defassert(:pathsubext, 'foo', 'foo.exe', '')
512
+ defassert(:pathsubext, 'lex.yy.o', 'lex.yy.c', '.o')
513
+ defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
514
+ defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.o')
515
+ defassert(:pathsubext, 'long_enough.bug-3664', 'long_enough.not_to_be_embeded[ruby-core:31640]', '.bug-3664')
516
+
517
+ def test_sub_matchdata
518
+ result = Pathname("abc.gif").sub(/\..*/) {
519
+ assert_not_nil($~)
520
+ assert_equal(".gif", $~[0])
521
+ ".png"
522
+ }
523
+ assert_equal("abc.png", result.to_s)
524
+ end
525
+
526
+ def root?(path)
527
+ Pathname.new(path).root?
528
+ end
529
+
530
+ defassert(:root?, true, "/")
531
+ defassert(:root?, true, "//")
532
+ defassert(:root?, true, "///")
533
+ defassert(:root?, false, "")
534
+ defassert(:root?, false, "a")
535
+
536
+ def test_mountpoint?
537
+ r = Pathname("/").mountpoint?
538
+ assert_include([true, false], r)
539
+ end
540
+
541
+ def test_destructive_update
542
+ path = Pathname.new("a")
543
+ path.to_s.replace "b"
544
+ assert_equal(Pathname.new("a"), path)
545
+ end
546
+
547
+ def test_null_character
548
+ assert_raise(ArgumentError) { Pathname.new("\0") }
549
+ end
550
+
551
+ def test_taint
552
+ obj = Pathname.new("a"); assert_same(obj, obj.taint)
553
+ obj = Pathname.new("a"); assert_same(obj, obj.untaint)
554
+
555
+ assert_equal(false, Pathname.new("a" ) .tainted?)
556
+ assert_equal(false, Pathname.new("a" ) .to_s.tainted?)
557
+ assert_equal(true, Pathname.new("a" ).taint .tainted?)
558
+ assert_equal(true, Pathname.new("a" ).taint.to_s.tainted?)
559
+ assert_equal(true, Pathname.new("a".taint) .tainted?)
560
+ assert_equal(true, Pathname.new("a".taint) .to_s.tainted?)
561
+ assert_equal(true, Pathname.new("a".taint).taint .tainted?)
562
+ assert_equal(true, Pathname.new("a".taint).taint.to_s.tainted?)
563
+
564
+ str = "a"
565
+ path = Pathname.new(str)
566
+ str.taint
567
+ assert_equal(false, path .tainted?)
568
+ assert_equal(false, path.to_s.tainted?)
569
+ end
570
+
571
+ def test_untaint
572
+ obj = Pathname.new("a"); assert_same(obj, obj.untaint)
573
+
574
+ assert_equal(false, Pathname.new("a").taint.untaint .tainted?)
575
+ assert_equal(false, Pathname.new("a").taint.untaint.to_s.tainted?)
576
+
577
+ str = "a".taint
578
+ path = Pathname.new(str)
579
+ str.untaint
580
+ assert_equal(true, path .tainted?)
581
+ assert_equal(true, path.to_s.tainted?)
582
+ end
583
+
584
+ def test_freeze
585
+ obj = Pathname.new("a"); assert_same(obj, obj.freeze)
586
+
587
+ assert_equal(false, Pathname.new("a" ) .frozen?)
588
+ assert_equal(false, Pathname.new("a".freeze) .frozen?)
589
+ assert_equal(true, Pathname.new("a" ).freeze .frozen?)
590
+ assert_equal(true, Pathname.new("a".freeze).freeze .frozen?)
591
+ assert_equal(false, Pathname.new("a" ) .to_s.frozen?)
592
+ assert_equal(false, Pathname.new("a".freeze) .to_s.frozen?)
593
+ assert_equal(false, Pathname.new("a" ).freeze.to_s.frozen?)
594
+ assert_equal(false, Pathname.new("a".freeze).freeze.to_s.frozen?)
595
+ end
596
+
597
+ def test_freeze_and_taint
598
+ obj = Pathname.new("a")
599
+ obj.freeze
600
+ assert_equal(false, obj.tainted?)
601
+ assert_raise(RuntimeError) { obj.taint }
602
+
603
+ obj = Pathname.new("a")
604
+ obj.taint
605
+ assert_equal(true, obj.tainted?)
606
+ obj.freeze
607
+ assert_equal(true, obj.tainted?)
608
+ assert_nothing_raised { obj.taint }
609
+ end
610
+
611
+ def test_to_s
612
+ str = "a"
613
+ obj = Pathname.new(str)
614
+ assert_equal(str, obj.to_s)
615
+ assert_not_same(str, obj.to_s)
616
+ assert_not_same(obj.to_s, obj.to_s)
617
+ end
618
+
619
+ def test_kernel_open
620
+ count = 0
621
+ result = Kernel.open(Pathname.new(__FILE__)) {|f|
622
+ assert(File.identical?(__FILE__, f))
623
+ count += 1
624
+ 2
625
+ }
626
+ assert_equal(1, count)
627
+ assert_equal(2, result)
628
+ end
629
+
630
+ def test_each_filename
631
+ result = []
632
+ Pathname.new("/usr/bin/ruby").each_filename {|f| result << f }
633
+ assert_equal(%w[usr bin ruby], result)
634
+ assert_equal(%w[usr bin ruby], Pathname.new("/usr/bin/ruby").each_filename.to_a)
635
+ end
636
+
637
+ def test_kernel_pathname
638
+ assert_equal(Pathname.new("a"), Pathname("a"))
639
+ end
640
+
641
+ def test_children
642
+ with_tmpchdir('rubytest-pathname') {|dir|
643
+ open("a", "w") {}
644
+ open("b", "w") {}
645
+ Dir.mkdir("d")
646
+ open("d/x", "w") {}
647
+ open("d/y", "w") {}
648
+ assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], Pathname(".").children.sort)
649
+ assert_equal([Pathname("d/x"), Pathname("d/y")], Pathname("d").children.sort)
650
+ assert_equal([Pathname("x"), Pathname("y")], Pathname("d").children(false).sort)
651
+ }
652
+ end
653
+
654
+ def test_each_child
655
+ with_tmpchdir('rubytest-pathname') {|dir|
656
+ open("a", "w") {}
657
+ open("b", "w") {}
658
+ Dir.mkdir("d")
659
+ open("d/x", "w") {}
660
+ open("d/y", "w") {}
661
+ a = []; Pathname(".").each_child {|v| a << v }; a.sort!
662
+ assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], a)
663
+ a = []; Pathname("d").each_child {|v| a << v }; a.sort!
664
+ assert_equal([Pathname("d/x"), Pathname("d/y")], a)
665
+ a = []; Pathname("d").each_child(false) {|v| a << v }; a.sort!
666
+ assert_equal([Pathname("x"), Pathname("y")], a)
667
+ }
668
+ end
669
+
670
+ def test_each_line
671
+ with_tmpchdir('rubytest-pathname') {|dir|
672
+ open("a", "w") {|f| f.puts 1, 2 }
673
+ a = []
674
+ Pathname("a").each_line {|line| a << line }
675
+ assert_equal(["1\n", "2\n"], a)
676
+
677
+ a = []
678
+ Pathname("a").each_line("2") {|line| a << line }
679
+ assert_equal(["1\n2", "\n"], a)
680
+
681
+ a = []
682
+ Pathname("a").each_line(1) {|line| a << line }
683
+ assert_equal(["1", "\n", "2", "\n"], a)
684
+
685
+ a = []
686
+ Pathname("a").each_line("2", 1) {|line| a << line }
687
+ assert_equal(["1", "\n", "2", "\n"], a)
688
+
689
+ a = []
690
+ enum = Pathname("a").each_line
691
+ enum.each {|line| a << line }
692
+ assert_equal(["1\n", "2\n"], a)
693
+ }
694
+ end
695
+
696
+ def test_readlines
697
+ with_tmpchdir('rubytest-pathname') {|dir|
698
+ open("a", "w") {|f| f.puts 1, 2 }
699
+ a = Pathname("a").readlines
700
+ assert_equal(["1\n", "2\n"], a)
701
+ }
702
+ end
703
+
704
+ def test_read
705
+ with_tmpchdir('rubytest-pathname') {|dir|
706
+ open("a", "w") {|f| f.puts 1, 2 }
707
+ assert_equal("1\n2\n", Pathname("a").read)
708
+ }
709
+ end
710
+
711
+ def test_binread
712
+ with_tmpchdir('rubytest-pathname') {|dir|
713
+ open("a", "w") {|f| f.write "abc" }
714
+ str = Pathname("a").binread
715
+ assert_equal("abc", str)
716
+ assert_equal(Encoding::ASCII_8BIT, str.encoding)
717
+ }
718
+ end
719
+
720
+ def test_sysopen
721
+ with_tmpchdir('rubytest-pathname') {|dir|
722
+ open("a", "w") {|f| f.write "abc" }
723
+ fd = Pathname("a").sysopen
724
+ io = IO.new(fd)
725
+ begin
726
+ assert_equal("abc", io.read)
727
+ ensure
728
+ io.close
729
+ end
730
+ }
731
+ end
732
+
733
+ def test_atime
734
+ assert_kind_of(Time, Pathname(__FILE__).atime)
735
+ end
736
+
737
+ def test_ctime
738
+ assert_kind_of(Time, Pathname(__FILE__).ctime)
739
+ end
740
+
741
+ def test_mtime
742
+ assert_kind_of(Time, Pathname(__FILE__).mtime)
743
+ end
744
+
745
+ def test_chmod
746
+ with_tmpchdir('rubytest-pathname') {|dir|
747
+ open("a", "w") {|f| f.write "abc" }
748
+ path = Pathname("a")
749
+ old = path.stat.mode
750
+ path.chmod(0444)
751
+ assert_equal(0444, path.stat.mode & 0777)
752
+ path.chmod(old)
753
+ }
754
+ end
755
+
756
+ def test_lchmod
757
+ return if !has_symlink?
758
+ with_tmpchdir('rubytest-pathname') {|dir|
759
+ open("a", "w") {|f| f.write "abc" }
760
+ File.symlink("a", "l")
761
+ path = Pathname("l")
762
+ old = path.lstat.mode
763
+ begin
764
+ path.lchmod(0444)
765
+ rescue NotImplementedError
766
+ next
767
+ end
768
+ assert_equal(0444, path.lstat.mode & 0777)
769
+ path.chmod(old)
770
+ }
771
+ end
772
+
773
+ def test_chown
774
+ with_tmpchdir('rubytest-pathname') {|dir|
775
+ open("a", "w") {|f| f.write "abc" }
776
+ path = Pathname("a")
777
+ old_uid = path.stat.uid
778
+ old_gid = path.stat.gid
779
+ begin
780
+ path.chown(0, 0)
781
+ rescue Errno::EPERM
782
+ next
783
+ end
784
+ assert_equal(0, path.stat.uid)
785
+ assert_equal(0, path.stat.gid)
786
+ path.chown(old_uid, old_gid)
787
+ }
788
+ end
789
+
790
+ def test_lchown
791
+ return if !has_symlink?
792
+ with_tmpchdir('rubytest-pathname') {|dir|
793
+ open("a", "w") {|f| f.write "abc" }
794
+ File.symlink("a", "l")
795
+ path = Pathname("l")
796
+ old_uid = path.stat.uid
797
+ old_gid = path.stat.gid
798
+ begin
799
+ path.lchown(0, 0)
800
+ rescue Errno::EPERM
801
+ next
802
+ end
803
+ assert_equal(0, path.stat.uid)
804
+ assert_equal(0, path.stat.gid)
805
+ path.lchown(old_uid, old_gid)
806
+ }
807
+ end
808
+
809
+ def test_fnmatch
810
+ path = Pathname("a")
811
+ assert_equal(true, path.fnmatch("*"))
812
+ assert_equal(false, path.fnmatch("*.*"))
813
+ assert_equal(false, Pathname(".foo").fnmatch("*"))
814
+ assert_equal(true, Pathname(".foo").fnmatch("*", File::FNM_DOTMATCH))
815
+ end
816
+
817
+ def test_fnmatch?
818
+ path = Pathname("a")
819
+ assert_equal(true, path.fnmatch?("*"))
820
+ assert_equal(false, path.fnmatch?("*.*"))
821
+ end
822
+
823
+ def test_ftype
824
+ with_tmpchdir('rubytest-pathname') {|dir|
825
+ open("f", "w") {|f| f.write "abc" }
826
+ assert_equal("file", Pathname("f").ftype)
827
+ Dir.mkdir("d")
828
+ assert_equal("directory", Pathname("d").ftype)
829
+ }
830
+ end
831
+
832
+ def test_make_link
833
+ with_tmpchdir('rubytest-pathname') {|dir|
834
+ open("a", "w") {|f| f.write "abc" }
835
+ Pathname("l").make_link(Pathname("a"))
836
+ assert_equal("abc", Pathname("l").read)
837
+ }
838
+ end
839
+
840
+ def test_open
841
+ with_tmpchdir('rubytest-pathname') {|dir|
842
+ open("a", "w") {|f| f.write "abc" }
843
+ path = Pathname("a")
844
+
845
+ path.open {|f|
846
+ assert_equal("abc", f.read)
847
+ }
848
+
849
+ path.open("r") {|f|
850
+ assert_equal("abc", f.read)
851
+ }
852
+
853
+ Pathname("b").open("w", 0444) {|f| f.write "def" }
854
+ assert_equal(0444, File.stat("b").mode & 0777)
855
+ assert_equal("def", File.read("b"))
856
+
857
+ Pathname("c").open("w", 0444, {}) {|f| f.write "ghi" }
858
+ assert_equal(0444, File.stat("c").mode & 0777)
859
+ assert_equal("ghi", File.read("c"))
860
+
861
+ g = path.open
862
+ assert_equal("abc", g.read)
863
+ g.close
864
+ }
865
+ end
866
+
867
+ def test_readlink
868
+ return if !has_symlink?
869
+ with_tmpchdir('rubytest-pathname') {|dir|
870
+ open("a", "w") {|f| f.write "abc" }
871
+ File.symlink("a", "l")
872
+ assert_equal(Pathname("a"), Pathname("l").readlink)
873
+ }
874
+ end
875
+
876
+ def test_rename
877
+ with_tmpchdir('rubytest-pathname') {|dir|
878
+ open("a", "w") {|f| f.write "abc" }
879
+ Pathname("a").rename(Pathname("b"))
880
+ assert_equal("abc", File.read("b"))
881
+ }
882
+ end
883
+
884
+ def test_stat
885
+ with_tmpchdir('rubytest-pathname') {|dir|
886
+ open("a", "w") {|f| f.write "abc" }
887
+ s = Pathname("a").stat
888
+ assert_equal(3, s.size)
889
+ }
890
+ end
891
+
892
+ def test_lstat
893
+ return if !has_symlink?
894
+ with_tmpchdir('rubytest-pathname') {|dir|
895
+ open("a", "w") {|f| f.write "abc" }
896
+ File.symlink("a", "l")
897
+ s = Pathname("l").lstat
898
+ assert_equal(true, s.symlink?)
899
+ s = Pathname("l").stat
900
+ assert_equal(false, s.symlink?)
901
+ assert_equal(3, s.size)
902
+ s = Pathname("a").lstat
903
+ assert_equal(false, s.symlink?)
904
+ assert_equal(3, s.size)
905
+ }
906
+ end
907
+
908
+ def test_make_symlink
909
+ return if !has_symlink?
910
+ with_tmpchdir('rubytest-pathname') {|dir|
911
+ open("a", "w") {|f| f.write "abc" }
912
+ Pathname("l").make_symlink(Pathname("a"))
913
+ s = Pathname("l").lstat
914
+ assert_equal(true, s.symlink?)
915
+ }
916
+ end
917
+
918
+ def test_truncate
919
+ with_tmpchdir('rubytest-pathname') {|dir|
920
+ open("a", "w") {|f| f.write "abc" }
921
+ Pathname("a").truncate(2)
922
+ assert_equal("ab", File.read("a"))
923
+ }
924
+ end
925
+
926
+ def test_utime
927
+ with_tmpchdir('rubytest-pathname') {|dir|
928
+ open("a", "w") {|f| f.write "abc" }
929
+ atime = Time.utc(2000)
930
+ mtime = Time.utc(1999)
931
+ Pathname("a").utime(atime, mtime)
932
+ s = File.stat("a")
933
+ assert_equal(atime, s.atime)
934
+ assert_equal(mtime, s.mtime)
935
+ }
936
+ end
937
+
938
+ def test_basename
939
+ assert_equal(Pathname("basename"), Pathname("dirname/basename").basename)
940
+ assert_equal(Pathname("bar"), Pathname("foo/bar.x").basename(".x"))
941
+ end
942
+
943
+ def test_dirname
944
+ assert_equal(Pathname("dirname"), Pathname("dirname/basename").dirname)
945
+ end
946
+
947
+ def test_extname
948
+ assert_equal(".ext", Pathname("basename.ext").extname)
949
+ end
950
+
951
+ def test_expand_path
952
+ drv = DOSISH_DRIVE_LETTER ? Dir.pwd.sub(%r(/.*), '') : ""
953
+ assert_equal(Pathname(drv + "/a"), Pathname("/a").expand_path)
954
+ assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path("/"))
955
+ assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path(Pathname("/")))
956
+ assert_equal(Pathname(drv + "/b"), Pathname("/b").expand_path(Pathname("/a")))
957
+ assert_equal(Pathname(drv + "/a/b"), Pathname("b").expand_path(Pathname("/a")))
958
+ end
959
+
960
+ def test_split
961
+ assert_equal([Pathname("dirname"), Pathname("basename")], Pathname("dirname/basename").split)
962
+ end
963
+
964
+ def test_blockdev?
965
+ with_tmpchdir('rubytest-pathname') {|dir|
966
+ open("f", "w") {|f| f.write "abc" }
967
+ assert_equal(false, Pathname("f").blockdev?)
968
+ }
969
+ end
970
+
971
+ def test_chardev?
972
+ with_tmpchdir('rubytest-pathname') {|dir|
973
+ open("f", "w") {|f| f.write "abc" }
974
+ assert_equal(false, Pathname("f").chardev?)
975
+ }
976
+ end
977
+
978
+ def test_executable?
979
+ with_tmpchdir('rubytest-pathname') {|dir|
980
+ open("f", "w") {|f| f.write "abc" }
981
+ assert_equal(false, Pathname("f").executable?)
982
+ }
983
+ end
984
+
985
+ def test_executable_real?
986
+ with_tmpchdir('rubytest-pathname') {|dir|
987
+ open("f", "w") {|f| f.write "abc" }
988
+ assert_equal(false, Pathname("f").executable_real?)
989
+ }
990
+ end
991
+
992
+ def test_exist?
993
+ with_tmpchdir('rubytest-pathname') {|dir|
994
+ open("f", "w") {|f| f.write "abc" }
995
+ assert_equal(true, Pathname("f").exist?)
996
+ }
997
+ end
998
+
999
+ def test_grpowned?
1000
+ skip "Unix file owner test" if DOSISH
1001
+ with_tmpchdir('rubytest-pathname') {|dir|
1002
+ open("f", "w") {|f| f.write "abc" }
1003
+ File.chown(-1, Process.gid, "f")
1004
+ assert_equal(true, Pathname("f").grpowned?)
1005
+ }
1006
+ end
1007
+
1008
+ def test_directory?
1009
+ with_tmpchdir('rubytest-pathname') {|dir|
1010
+ open("f", "w") {|f| f.write "abc" }
1011
+ assert_equal(false, Pathname("f").directory?)
1012
+ Dir.mkdir("d")
1013
+ assert_equal(true, Pathname("d").directory?)
1014
+ }
1015
+ end
1016
+
1017
+ def test_file?
1018
+ with_tmpchdir('rubytest-pathname') {|dir|
1019
+ open("f", "w") {|f| f.write "abc" }
1020
+ assert_equal(true, Pathname("f").file?)
1021
+ Dir.mkdir("d")
1022
+ assert_equal(false, Pathname("d").file?)
1023
+ }
1024
+ end
1025
+
1026
+ def test_pipe?
1027
+ with_tmpchdir('rubytest-pathname') {|dir|
1028
+ open("f", "w") {|f| f.write "abc" }
1029
+ assert_equal(false, Pathname("f").pipe?)
1030
+ }
1031
+ end
1032
+
1033
+ def test_socket?
1034
+ with_tmpchdir('rubytest-pathname') {|dir|
1035
+ open("f", "w") {|f| f.write "abc" }
1036
+ assert_equal(false, Pathname("f").socket?)
1037
+ }
1038
+ end
1039
+
1040
+ def test_owned?
1041
+ with_tmpchdir('rubytest-pathname') {|dir|
1042
+ open("f", "w") {|f| f.write "abc" }
1043
+ assert_equal(true, Pathname("f").owned?)
1044
+ }
1045
+ end
1046
+
1047
+ def test_readable?
1048
+ with_tmpchdir('rubytest-pathname') {|dir|
1049
+ open("f", "w") {|f| f.write "abc" }
1050
+ assert_equal(true, Pathname("f").readable?)
1051
+ }
1052
+ end
1053
+
1054
+ def test_world_readable?
1055
+ skip "Unix file mode bit test" if DOSISH
1056
+ with_tmpchdir('rubytest-pathname') {|dir|
1057
+ open("f", "w") {|f| f.write "abc" }
1058
+ File.chmod(0400, "f")
1059
+ assert_equal(nil, Pathname("f").world_readable?)
1060
+ File.chmod(0444, "f")
1061
+ assert_equal(0444, Pathname("f").world_readable?)
1062
+ }
1063
+ end
1064
+
1065
+ def test_readable_real?
1066
+ with_tmpchdir('rubytest-pathname') {|dir|
1067
+ open("f", "w") {|f| f.write "abc" }
1068
+ assert_equal(true, Pathname("f").readable_real?)
1069
+ }
1070
+ end
1071
+
1072
+ def test_setuid?
1073
+ with_tmpchdir('rubytest-pathname') {|dir|
1074
+ open("f", "w") {|f| f.write "abc" }
1075
+ assert_equal(false, Pathname("f").setuid?)
1076
+ }
1077
+ end
1078
+
1079
+ def test_setgid?
1080
+ with_tmpchdir('rubytest-pathname') {|dir|
1081
+ open("f", "w") {|f| f.write "abc" }
1082
+ assert_equal(false, Pathname("f").setgid?)
1083
+ }
1084
+ end
1085
+
1086
+ def test_size
1087
+ with_tmpchdir('rubytest-pathname') {|dir|
1088
+ open("f", "w") {|f| f.write "abc" }
1089
+ assert_equal(3, Pathname("f").size)
1090
+ open("z", "w") {|f| }
1091
+ assert_equal(0, Pathname("z").size)
1092
+ assert_raise(Errno::ENOENT) { Pathname("not-exist").size }
1093
+ }
1094
+ end
1095
+
1096
+ def test_size?
1097
+ with_tmpchdir('rubytest-pathname') {|dir|
1098
+ open("f", "w") {|f| f.write "abc" }
1099
+ assert_equal(3, Pathname("f").size?)
1100
+ open("z", "w") {|f| }
1101
+ assert_equal(nil, Pathname("z").size?)
1102
+ assert_equal(nil, Pathname("not-exist").size?)
1103
+ }
1104
+ end
1105
+
1106
+ def test_sticky?
1107
+ skip "Unix file mode bit test" if DOSISH
1108
+ with_tmpchdir('rubytest-pathname') {|dir|
1109
+ open("f", "w") {|f| f.write "abc" }
1110
+ assert_equal(false, Pathname("f").sticky?)
1111
+ }
1112
+ end
1113
+
1114
+ def test_symlink?
1115
+ with_tmpchdir('rubytest-pathname') {|dir|
1116
+ open("f", "w") {|f| f.write "abc" }
1117
+ assert_equal(false, Pathname("f").symlink?)
1118
+ }
1119
+ end
1120
+
1121
+ def test_writable?
1122
+ with_tmpchdir('rubytest-pathname') {|dir|
1123
+ open("f", "w") {|f| f.write "abc" }
1124
+ assert_equal(true, Pathname("f").writable?)
1125
+ }
1126
+ end
1127
+
1128
+ def test_world_writable?
1129
+ skip "Unix file mode bit test" if DOSISH
1130
+ with_tmpchdir('rubytest-pathname') {|dir|
1131
+ open("f", "w") {|f| f.write "abc" }
1132
+ File.chmod(0600, "f")
1133
+ assert_equal(nil, Pathname("f").world_writable?)
1134
+ File.chmod(0666, "f")
1135
+ assert_equal(0666, Pathname("f").world_writable?)
1136
+ }
1137
+ end
1138
+
1139
+ def test_writable_real?
1140
+ with_tmpchdir('rubytest-pathname') {|dir|
1141
+ open("f", "w") {|f| f.write "abc" }
1142
+ assert_equal(true, Pathname("f").writable?)
1143
+ }
1144
+ end
1145
+
1146
+ def test_zero?
1147
+ with_tmpchdir('rubytest-pathname') {|dir|
1148
+ open("f", "w") {|f| f.write "abc" }
1149
+ assert_equal(false, Pathname("f").zero?)
1150
+ open("z", "w") {|f| }
1151
+ assert_equal(true, Pathname("z").zero?)
1152
+ assert_equal(false, Pathname("not-exist").zero?)
1153
+ }
1154
+ end
1155
+
1156
+ def test_s_glob
1157
+ with_tmpchdir('rubytest-pathname') {|dir|
1158
+ open("f", "w") {|f| f.write "abc" }
1159
+ Dir.mkdir("d")
1160
+ assert_equal([Pathname("d"), Pathname("f")], Pathname.glob("*").sort)
1161
+ a = []
1162
+ Pathname.glob("*") {|path| a << path }
1163
+ a.sort!
1164
+ assert_equal([Pathname("d"), Pathname("f")], a)
1165
+ }
1166
+ end
1167
+
1168
+ def test_s_getwd
1169
+ wd = Pathname.getwd
1170
+ assert_kind_of(Pathname, wd)
1171
+ end
1172
+
1173
+ def test_s_pwd
1174
+ wd = Pathname.pwd
1175
+ assert_kind_of(Pathname, wd)
1176
+ end
1177
+
1178
+ def test_entries
1179
+ with_tmpchdir('rubytest-pathname') {|dir|
1180
+ open("a", "w") {}
1181
+ open("b", "w") {}
1182
+ assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
1183
+ }
1184
+ end
1185
+
1186
+ def test_each_entry
1187
+ with_tmpchdir('rubytest-pathname') {|dir|
1188
+ open("a", "w") {}
1189
+ open("b", "w") {}
1190
+ a = []
1191
+ Pathname(".").each_entry {|v| a << v }
1192
+ assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
1193
+ }
1194
+ end
1195
+
1196
+ def test_mkdir
1197
+ with_tmpchdir('rubytest-pathname') {|dir|
1198
+ Pathname("d").mkdir
1199
+ assert(File.directory?("d"))
1200
+ Pathname("e").mkdir(0770)
1201
+ assert(File.directory?("e"))
1202
+ }
1203
+ end
1204
+
1205
+ def test_rmdir
1206
+ with_tmpchdir('rubytest-pathname') {|dir|
1207
+ Pathname("d").mkdir
1208
+ assert(File.directory?("d"))
1209
+ Pathname("d").rmdir
1210
+ assert(!File.exists?("d"))
1211
+ }
1212
+ end
1213
+
1214
+ def test_opendir
1215
+ with_tmpchdir('rubytest-pathname') {|dir|
1216
+ open("a", "w") {}
1217
+ open("b", "w") {}
1218
+ a = []
1219
+ Pathname(".").opendir {|d|
1220
+ d.each {|e| a << e }
1221
+ }
1222
+ assert_equal([".", "..", "a", "b"], a.sort)
1223
+ }
1224
+ end
1225
+
1226
+ def test_find
1227
+ with_tmpchdir('rubytest-pathname') {|dir|
1228
+ open("a", "w") {}
1229
+ open("b", "w") {}
1230
+ Dir.mkdir("d")
1231
+ open("d/x", "w") {}
1232
+ open("d/y", "w") {}
1233
+ a = []; Pathname(".").find {|v| a << v }; a.sort!
1234
+ assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1235
+ a = []; Pathname("d").find {|v| a << v }; a.sort!
1236
+ assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1237
+ a = Pathname(".").find.sort
1238
+ assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1239
+ a = Pathname("d").find.sort
1240
+ assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1241
+ }
1242
+ end
1243
+
1244
+ def test_mkpath
1245
+ with_tmpchdir('rubytest-pathname') {|dir|
1246
+ Pathname("a/b/c/d").mkpath
1247
+ assert(File.directory?("a/b/c/d"))
1248
+ }
1249
+ end
1250
+
1251
+ def test_rmtree
1252
+ with_tmpchdir('rubytest-pathname') {|dir|
1253
+ Pathname("a/b/c/d").mkpath
1254
+ assert(File.exist?("a/b/c/d"))
1255
+ Pathname("a").rmtree
1256
+ assert(!File.exist?("a"))
1257
+ }
1258
+ end
1259
+
1260
+ def test_unlink
1261
+ with_tmpchdir('rubytest-pathname') {|dir|
1262
+ open("f", "w") {|f| f.write "abc" }
1263
+ Pathname("f").unlink
1264
+ assert(!File.exist?("f"))
1265
+ Dir.mkdir("d")
1266
+ Pathname("d").unlink
1267
+ assert(!File.exist?("d"))
1268
+ }
1269
+ end
1270
+
1271
+ def test_matchop
1272
+ assert_raise(NoMethodError) { Pathname("a") =~ /a/ }
1273
+ end
1274
+
1275
+ def test_file_basename
1276
+ assert_equal("bar", File.basename(Pathname.new("foo/bar")))
1277
+ end
1278
+
1279
+ def test_file_dirname
1280
+ assert_equal("foo", File.dirname(Pathname.new("foo/bar")))
1281
+ end
1282
+
1283
+ def test_file_split
1284
+ assert_equal(["foo", "bar"], File.split(Pathname.new("foo/bar")))
1285
+ end
1286
+
1287
+ def test_file_extname
1288
+ assert_equal(".baz", File.extname(Pathname.new("bar.baz")))
1289
+ end
1290
+
1291
+ def test_file_fnmatch
1292
+ assert(File.fnmatch("*.*", Pathname.new("bar.baz")))
1293
+ end
1294
+
1295
+ def test_file_join
1296
+ assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar")))
1297
+ lambda {
1298
+ $SAFE = 1
1299
+ assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar").taint))
1300
+ }.call
1301
+ end
1302
+ end