[前][次][番号順一覧][スレッド一覧]

ruby-changes:29806

From: knu <ko1@a...>
Date: Tue, 9 Jul 2013 18:05:41 +0900 (JST)
Subject: [ruby-changes:29806] knu:r41858 (trunk): * test/fileutils/test_fileutils.rb

knu	2013-07-09 18:03:29 +0900 (Tue, 09 Jul 2013)

  New Revision: 41858

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41858

  Log:
    * test/fileutils/test_fileutils.rb
      (TestFileUtils#assert_output_lines): New utility assertion
      method for testing verbose output.

  Modified files:
    trunk/ChangeLog
    trunk/test/fileutils/test_fileutils.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41857)
+++ ChangeLog	(revision 41858)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jul  9 17:58:26 2013  Akinori MUSHA  <knu@i...>
+
+	* test/fileutils/test_fileutils.rb
+	  (TestFileUtils#assert_output_lines): New utility assertion
+	  method for testing verbose output.
+
 Tue Jul  9 17:43:57 2013  Koichi Sasada  <ko1@a...>
 
 	* test/test_tracer.rb: catch up recent rubygems changes.
Index: test/fileutils/test_fileutils.rb
===================================================================
--- test/fileutils/test_fileutils.rb	(revision 41857)
+++ test/fileutils/test_fileutils.rb	(revision 41858)
@@ -9,6 +9,21 @@ require 'test/unit' https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L9
 class TestFileUtils < Test::Unit::TestCase
   TMPROOT = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
   include Test::Unit::FileAssertions
+
+  def assert_output_lines(expected, fu = self, message=nil)
+    old = fu.instance_variable_get(:@fileutils_output)
+    read, write = IO.pipe
+    fu.instance_variable_set(:@fileutils_output, write)
+    th = Thread.new { read.read }
+
+    yield
+
+    write.close
+    lines = th.value.lines.map {|l| l.chomp }
+    assert_equal(expected, lines)
+  ensure
+    fu.instance_variable_set(:@fileutils_output, old) if old
+  end
 end
 
 prevdir = Dir.pwd
@@ -1024,38 +1039,21 @@ class TestFileUtils https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L1039
   def test_chmod_verbose
     check_singleton :chmod
 
-    stderr_back = $stderr
-    read, $stderr = IO.pipe
-    th = Thread.new { read.read }
-
-    touch 'tmp/a'
-    chmod 0700, 'tmp/a', verbose: true
-    assert_equal 0700, File.stat('tmp/a').mode & 0777
-    chmod 0500, 'tmp/a', verbose: true
-    assert_equal 0500, File.stat('tmp/a').mode & 0777
-
-    $stderr.close
-    lines = th.value.lines.map {|l| l.chomp }
-    assert_equal(["chmod 700 tmp/a", "chmod 500 tmp/a"], lines)
-  ensure
-    $stderr = stderr_back if stderr_back
+    assert_output_lines(["chmod 700 tmp/a", "chmod 500 tmp/a"]) {
+      touch 'tmp/a'
+      chmod 0700, 'tmp/a', verbose: true
+      assert_equal 0700, File.stat('tmp/a').mode & 0777
+      chmod 0500, 'tmp/a', verbose: true
+      assert_equal 0500, File.stat('tmp/a').mode & 0777
+    }
   end if have_file_perm?
 
   def test_s_chmod_verbose
-    output_back = FileUtils.instance_variable_get(:@fileutils_output)
-    read, write = IO.pipe
-    FileUtils.instance_variable_set(:@fileutils_output, write)
-    th = Thread.new { read.read }
-
-    touch 'tmp/a'
-    FileUtils.chmod 0700, 'tmp/a', verbose: true
-    assert_equal 0700, File.stat('tmp/a').mode & 0777
-
-    write.close
-    lines = th.value.lines.map {|l| l.chomp }
-    assert_equal(["chmod 700 tmp/a"], lines)
-  ensure
-    FileUtils.instance_variable_set(:@fileutils_output, output_back) if output_back
+    assert_output_lines(["chmod 700 tmp/a"], FileUtils) {
+      touch 'tmp/a'
+      FileUtils.chmod 0700, 'tmp/a', verbose: true
+      assert_equal 0700, File.stat('tmp/a').mode & 0777
+    }
   end if have_file_perm?
 
   # FIXME: How can I test this method?

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]