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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2018-01-23 15:12:51 +0300
committerLin Jen-Shin <godfat@godfat.org>2018-01-26 14:42:48 +0300
commit54ca8d0d6c4744be53c7044b9bbfa05acc358090 (patch)
treeb4258bc91245eca92b2ef5321f18e67ee524c61c /lib
parenta0d57ee2b35f0e232aff930a94dfbcabe2860158 (diff)
Fail static-analysis if there's output to stderr
TODO: fix offenders
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/popen.rb23
-rw-r--r--lib/gitlab/popen/runner.rb46
2 files changed, 64 insertions, 5 deletions
diff --git a/lib/gitlab/popen.rb b/lib/gitlab/popen.rb
index 4bc5cda8cb5..05526a9ac94 100644
--- a/lib/gitlab/popen.rb
+++ b/lib/gitlab/popen.rb
@@ -5,7 +5,17 @@ module Gitlab
module Popen
extend self
- def popen(cmd, path = nil, vars = {})
+ Result = Struct.new(:cmd, :stdout, :stderr, :status, :duration)
+
+ # Returns [stdout + stderr, status]
+ def popen(cmd, path = nil, vars = {}, &block)
+ result = popen_with_detail(cmd, path, vars, &block)
+
+ [result.stdout << result.stderr, result.status]
+ end
+
+ # Returns Result
+ def popen_with_detail(cmd, path = nil, vars = {})
unless cmd.is_a?(Array)
raise "System commands must be given as an array of strings"
end
@@ -18,18 +28,21 @@ module Gitlab
FileUtils.mkdir_p(path)
end
- cmd_output = ""
+ cmd_stdout = ''
+ cmd_stderr = ''
cmd_status = 0
+ start = Time.now
+
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
yield(stdin) if block_given?
stdin.close
- cmd_output << stdout.read
- cmd_output << stderr.read
+ cmd_stdout = stdout.read
+ cmd_stderr = stderr.read
cmd_status = wait_thr.value.exitstatus
end
- [cmd_output, cmd_status]
+ Result.new(cmd, cmd_stdout, cmd_stderr, cmd_status, Time.now - start)
end
end
end
diff --git a/lib/gitlab/popen/runner.rb b/lib/gitlab/popen/runner.rb
new file mode 100644
index 00000000000..36284134707
--- /dev/null
+++ b/lib/gitlab/popen/runner.rb
@@ -0,0 +1,46 @@
+module Gitlab
+ module Popen
+ class Runner
+ attr_reader :results
+
+ def initialize
+ @results = []
+ end
+
+ def run(commands, &block)
+ commands.each do |cmd|
+ # yield doesn't support blocks, so we need to use a block variable
+ block.call(cmd) do # rubocop:disable Performance/RedundantBlockCall
+ cmd_result = Gitlab::Popen.popen_with_detail(cmd)
+
+ results << cmd_result
+
+ cmd_result
+ end
+ end
+ end
+
+ def all_good?
+ all_status_zero? && all_stderr_empty?
+ end
+
+ def all_status_zero?
+ results.all? { |result| result.status.zero? }
+ end
+
+ def all_stderr_empty?
+ results.all? { |result| result.stderr.empty? }
+ end
+
+ def failed_results
+ results.select { |result| result.status.nonzero? }
+ end
+
+ def warned_results
+ results.select do |result|
+ result.status.zero? && !result.stderr.empty?
+ end
+ end
+ end
+ end
+end