Sha256: ae72f13a8a5096146bcb06efa2bee96d52780c9844d2232bcda92b444e8e1f9a
Contents?: true
Size: 1.94 KB
Versions: 2
Compression:
Stored size: 1.94 KB
Contents
module Test end unless defined?(Test) ## # test-cmd.rb is a library for accessing the output streams # (both stdout and stderr) of a spawned process. The library was # first realized in a test environment, where it provided a path # for verifying that when code examples are run they produce the # expected output. The library can be generally useful outside a # test environment, too. module Test::Cmd class Result require "tempfile" ## # @return [String] # Returns the contents of stdout attr_reader :stdout ## # @return [String] # Returns the contents of stderr attr_reader :stderr ## @return [Process::Status] # Returns the status of a process attr_reader :status ## # @param [Tempfile] stdout # @param [Tempfile] stderr # @param [Process::Status] pstatus # @return [Test::Cmd::Result] def initialize(stdout, stderr, pstatus) @stdout = stdout.tap(&:rewind).read @stderr = stderr.tap(&:rewind).read @status = pstatus end ## # @return [Integer] # Returns the exit status of a process def exit_status @status.exitstatus end ## # Yields each line of stdout when the command # was successful, or each line of stderr when # the command was not successful. # # @return [Enumerator] # Returns an Enumerator when a block is not given. def each_line return enum_for(:each_line) unless block_given? io = @status.success? ? @stdout : @stderr io.each_line.each { yield(_1.chomp) } end end ## # @param [String] cmd # A command to execute # # @return [Test::Cmd::Result] # Returns an instance of {Test::Cmd::Result Test::Cmd::Result} def cmd(cmd) out = Tempfile.new("cmd-stdout").tap(&:unlink) err = Tempfile.new("cmd-stderr").tap(&:unlink) Process.wait spawn(cmd, {err:, out:}) Result.new(out, err, $?) ensure out.close err.close end module_function :cmd end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
test-cmd.rb-0.4.2 | lib/test-cmd.rb |
test-cmd.rb-0.4.1 | lib/test-cmd.rb |