Kouhei Sutou
null+****@clear*****
Wed Jan 1 23:23:41 JST 2014
Kouhei Sutou 2014-01-01 23:23:41 +0900 (Wed, 01 Jan 2014) New Revision: b60487e9e13e352faebb337b7059eb9b102c9aaf https://github.com/ranguba/chupa-text/commit/b60487e9e13e352faebb337b7059eb9b102c9aaf Message: Add tar decomposer Added files: test/decomposer/test-tar.rb test/fixture/tar/directory.tar test/fixture/tar/top-level.tar Copied files: lib/chupa-text/plugin/decomposer/tar.rb (from test/run-test.rb) test/helper.rb (from test/run-test.rb) Modified files: test/run-test.rb Copied: lib/chupa-text/plugin/decomposer/tar.rb (+23 -11) 58% Mode: 100755 -> 100644 =================================================================== --- test/run-test.rb 2014-01-01 23:06:40 +0900 (3699131) +++ lib/chupa-text/plugin/decomposer/tar.rb 2014-01-01 23:23:41 +0900 (c4b8979) @@ -1,5 +1,3 @@ -#!/usr/bin/env ruby -# # Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or @@ -16,16 +14,30 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -$VERBOSE = true - -require "pathname" +require "stringio" +require "rubygems/package" -require "test-unit" +require "chupa-text" -base_dir = Pathname(__FILE__).dirname -lib_dir = base_dir + "lib" -$LOAD_PATH.unshift(lib_dir.to_s) +module ChupaText + class TarDecomposer < Decomposer + registory.register(self) -require "chupa-text" + def target?(data) + data.extension == "tar" or + data.content_type == "application/x-tar" + end -exit(Test::Unit::AutoRunner.run(true)) + def decompose(data) + Gem::Package::TarReader.new(StringIO.new(data.body)) do |reader| + reader.each do |entry| + next unless entry.file? + extracted = Data.new + extracted.path = entry.full_name + extracted.body = entry.read + yield(extracted) + end + end + end + end +end Added: test/decomposer/test-tar.rb (+72 -0) 100644 =================================================================== --- /dev/null +++ test/decomposer/test-tar.rb 2014-01-01 23:23:41 +0900 (bc66d20) @@ -0,0 +1,72 @@ +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestTarDecomposer < Test::Unit::TestCase + include Helper + + def setup + @decomposer = ChupaText::TarDecomposer.new + end + + private + def decompose(data) + decomposed = [] + @decomposer.decompose(data) do |decomopsed_data| + decomposed << decomopsed_data + end + decomposed + end + + def fixture_path(*components) + super("tar", *components) + end + + sub_test_case("top-level") do + def setup + super + @data = ChupaText::Data.new + @data.path = fixture_path("top-level.tar") + end + + def test_decompose + decomposed = decompose(@data).collect do |data| + [data.path, data.body] + end + assert_equal([ + [Pathname("top-level.txt"), "top level\n"], + ], + decomposed) + end + end + + sub_test_case("directory") do + def setup + super + @data = ChupaText::Data.new + @data.path = fixture_path("directory.tar") + end + + def test_decompose + decomposed = decompose(@data).collect do |data| + [data.path, data.body] + end + assert_equal([ + [Pathname("directory/hello.txt"), "Hello in directory\n"], + ], + decomposed) + end + end +end Added: test/fixture/tar/directory.tar (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/tar/top-level.tar (+0 -0) 100644 =================================================================== (Binary files differ) Copied: test/helper.rb (+6 -13) 78% Mode: 100755 -> 100644 =================================================================== --- test/run-test.rb 2014-01-01 23:06:40 +0900 (3699131) +++ test/helper.rb 2014-01-01 23:23:41 +0900 (c46e2e6) @@ -1,5 +1,3 @@ -#!/usr/bin/env ruby -# # Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or @@ -16,16 +14,11 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -$VERBOSE = true - require "pathname" -require "test-unit" - -base_dir = Pathname(__FILE__).dirname -lib_dir = base_dir + "lib" -$LOAD_PATH.unshift(lib_dir.to_s) - -require "chupa-text" - -exit(Test::Unit::AutoRunner.run(true)) +module Helper + def fixture_path(*components) + base_path = Pathname(__FILE__).dirname + "fixture" + base_path.join(*components) + end +end Modified: test/run-test.rb (+4 -0) =================================================================== --- test/run-test.rb 2014-01-01 23:06:40 +0900 (3699131) +++ test/run-test.rb 2014-01-01 23:23:41 +0900 (f12bc4f) @@ -28,4 +28,8 @@ $LOAD_PATH.unshift(lib_dir.to_s) require "chupa-text" +ChupaText::Decomposer.load + +require_relative "helper" + exit(Test::Unit::AutoRunner.run(true)) -------------- next part -------------- HTML����������������������������...Download