From 60d7f657f7922457c18b46f56bcd58b8d9e56bbf Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 28 Apr 2017 18:15:20 +0000 Subject: pipeline: new module for running process pipelines 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 --- test/test_pipeline.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/test_pipeline.rb (limited to 'test') 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 +# License: GPL-3.0+ +# 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 -- cgit v1.2.3-24-ge0c7