about summary refs log tree commit homepage
path: root/test/test_pipeline.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2017-04-28 18:15:20 +0000
committerEric Wong <e@80x24.org>2017-04-28 18:38:55 +0000
commit60d7f657f7922457c18b46f56bcd58b8d9e56bbf (patch)
tree0cf45e25f0ae5b8bb955c6fbce80d3e4915a723b /test/test_pipeline.rb
parent668eac36b811d9c321d9a48ad784807f43171882 (diff)
downloaddtas-60d7f657f7922457c18b46f56bcd58b8d9e56bbf.tar.gz
This should allow us easily to manipulate process pipelines
as an array of arrays.

Originally posted at
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/435624
Diffstat (limited to 'test/test_pipeline.rb')
-rw-r--r--test/test_pipeline.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/test_pipeline.rb b/test/test_pipeline.rb
new file mode 100644
index 0000000..3cc32cc
--- /dev/null
+++ b/test/test_pipeline.rb
@@ -0,0 +1,47 @@
+# Copyright (C) 2017 all contributors <dtas-all@nongnu.org>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+# frozen_string_literal: true
+require './test/helper'
+require 'dtas/pipeline'
+
+class TestPipeline < Testcase
+  include DTAS::Pipeline
+  def setup
+    @env = ENV.to_hash
+  end
+
+  def pipeline_result
+    IO.pipe do |rd, wr|
+      begin
+        pid = fork do
+          rd.close
+          $stdout.reopen(wr)
+          yield
+          exit!(0)
+        end
+        wr.close
+        return rd.read
+      ensure
+        _, status = Process.waitpid2(pid)
+        assert_predicate status, :success?
+      end
+    end
+    nil
+  end
+
+  def test_pipeline
+    assert_equal("BYYRU\n", pipeline_result do
+      run_pipeline(@env, [
+        %w(echo hello), # anything which generates something to stdout
+        %w(tr [a-z] [A-Z]), # upcase
+        # this lambda runs inside its own process
+        lambda do
+          $stdin.each_line { |l| $stdout.write("#{l.chomp.reverse}\n") }
+          exit!(0)
+        end,
+        # rot13
+        %w(tr [a-m][n-z][A-M][N-Z] [n-z][a-m][N-Z][A-M])
+      ])
+    end)
+  end
+end