Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shellout.rb « service « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6efe50c4ae2baf9a69fa2f63566de1133379862b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

require 'open3'

module QA
  module Service
    module Shellout
      CommandError = Class.new(StandardError)

      module_function

      ##
      # TODO, make it possible to use generic QA framework classes
      # as a library - gitlab-org/gitlab-qa#94
      #
      def shell(command, stdin_data: nil)
        puts "Executing `#{command}`"

        Open3.popen2e(*command) do |stdin, out, wait|
          stdin.puts(stdin_data) if stdin_data
          stdin.close if stdin_data

          if block_given?
            out.each do |line|
              yield line
            end
          end

          out.each_char { |char| print char }

          if wait.value.exited? && wait.value.exitstatus.nonzero?
            raise CommandError, "Command `#{command}` failed!"
          end
        end
      end
    end
  end
end