Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
54ca8d0d
Commit
54ca8d0d
authored
Jan 23, 2018
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fail static-analysis if there's output to stderr
TODO: fix offenders
parent
a0d57ee2
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
258 additions
and
24 deletions
+258
-24
lib/gitlab/popen.rb
lib/gitlab/popen.rb
+18
-5
lib/gitlab/popen/runner.rb
lib/gitlab/popen/runner.rb
+46
-0
scripts/static-analysis
scripts/static-analysis
+41
-16
spec/lib/gitlab/popen/runner_spec.rb
spec/lib/gitlab/popen/runner_spec.rb
+139
-0
spec/lib/gitlab/popen_spec.rb
spec/lib/gitlab/popen_spec.rb
+14
-2
spec/support/javascript_fixtures_helpers.rb
spec/support/javascript_fixtures_helpers.rb
+0
-1
No files found.
lib/gitlab/popen.rb
View file @
54ca8d0d
...
@@ -5,7 +5,17 @@ module Gitlab
...
@@ -5,7 +5,17 @@ module Gitlab
module
Popen
module
Popen
extend
self
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
)
unless
cmd
.
is_a?
(
Array
)
raise
"System commands must be given as an array of strings"
raise
"System commands must be given as an array of strings"
end
end
...
@@ -18,18 +28,21 @@ module Gitlab
...
@@ -18,18 +28,21 @@ module Gitlab
FileUtils
.
mkdir_p
(
path
)
FileUtils
.
mkdir_p
(
path
)
end
end
cmd_output
=
""
cmd_stdout
=
''
cmd_stderr
=
''
cmd_status
=
0
cmd_status
=
0
start
=
Time
.
now
Open3
.
popen3
(
vars
,
*
cmd
,
options
)
do
|
stdin
,
stdout
,
stderr
,
wait_thr
|
Open3
.
popen3
(
vars
,
*
cmd
,
options
)
do
|
stdin
,
stdout
,
stderr
,
wait_thr
|
yield
(
stdin
)
if
block_given?
yield
(
stdin
)
if
block_given?
stdin
.
close
stdin
.
close
cmd_
output
<<
stdout
.
read
cmd_
stdout
=
stdout
.
read
cmd_
output
<<
stderr
.
read
cmd_
stderr
=
stderr
.
read
cmd_status
=
wait_thr
.
value
.
exitstatus
cmd_status
=
wait_thr
.
value
.
exitstatus
end
end
[
cmd_output
,
cmd_status
]
Result
.
new
(
cmd
,
cmd_stdout
,
cmd_stderr
,
cmd_status
,
Time
.
now
-
start
)
end
end
end
end
end
end
lib/gitlab/popen/runner.rb
0 → 100644
View file @
54ca8d0d
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
scripts/static-analysis
View file @
54ca8d0d
#!/usr/bin/env ruby
#!/usr/bin/env ruby
require
::
File
.
expand_path
(
'../lib/gitlab/popen'
,
__dir__
)
# We don't have auto-loading here
require_relative
'../lib/gitlab/popen'
require_relative
'../lib/gitlab/popen/runner'
def
emit_warnings
(
static_analysis
)
static_analysis
.
warned_results
.
each
do
|
result
|
puts
puts
"****
#{
result
.
cmd
.
join
(
' '
)
}
had the following warnings:"
puts
puts
result
.
stdout
puts
result
.
stderr
puts
end
end
def
emit_errors
(
static_analysis
)
static_analysis
.
failed_results
.
each
do
|
result
|
puts
puts
"****
#{
result
.
cmd
.
join
(
' '
)
}
failed with the following error:"
puts
puts
result
.
stdout
puts
result
.
stderr
puts
end
end
tasks
=
[
tasks
=
[
%w[bundle exec rake config_lint]
,
%w[bundle exec rake config_lint]
,
...
@@ -17,18 +41,16 @@ tasks = [
...
@@ -17,18 +41,16 @@ tasks = [
%w[scripts/lint-rugged]
%w[scripts/lint-rugged]
]
]
failed_tasks
=
tasks
.
reduce
({})
do
|
failures
,
task
|
static_analysis
=
Gitlab
::
Popen
::
Runner
.
new
start
=
Time
.
now
puts
puts
"$
#{
task
.
join
(
' '
)
}
"
output
,
status
=
Gitlab
::
Popen
.
popen
(
task
)
static_analysis
.
run
(
tasks
)
do
|
cmd
,
&
run
|
puts
"==> Finished in
#{
Time
.
now
-
start
}
seconds"
puts
puts
puts
"$
#{
cmd
.
join
(
' '
)
}
"
failures
[
task
.
join
(
' '
)]
=
output
unless
status
.
zero?
result
=
run
.
call
failures
puts
"==> Finished in
#{
result
.
duration
}
seconds"
puts
end
end
puts
puts
...
@@ -36,17 +58,20 @@ puts '==================================================='
...
@@ -36,17 +58,20 @@ puts '==================================================='
puts
puts
puts
puts
if
failed_tasks
.
empty
?
if
static_analysis
.
all_good
?
puts
'All static analyses passed successfully.'
puts
'All static analyses passed successfully.'
elsif
static_analysis
.
all_status_zero?
puts
'All static analyses passed successfully, but we have warnings:'
puts
emit_warnings
(
static_analysis
)
exit
2
else
else
puts
'Some static analyses failed:'
puts
'Some static analyses failed:'
failed_tasks
.
each
do
|
failed_task
,
output
|
emit_warnings
(
static_analysis
)
puts
emit_errors
(
static_analysis
)
puts
"****
#{
failed_task
}
failed with the following error:"
puts
puts
output
end
exit
1
exit
1
end
end
spec/lib/gitlab/popen/runner_spec.rb
0 → 100644
View file @
54ca8d0d
require
'spec_helper'
describe
Gitlab
::
Popen
::
Runner
do
subject
{
described_class
.
new
}
describe
'#run'
do
it
'runs the command and returns the result'
do
run_command
expect
(
Gitlab
::
Popen
).
to
have_received
(
:popen_with_detail
)
end
end
describe
'#all_good?'
do
it
'returns true when exit status is 0 and stderr is empty'
do
run_command
expect
(
subject
).
to
be_all_good
end
it
'returns false when exit status is not 0'
do
run_command
(
exitstatus:
1
)
expect
(
subject
).
not_to
be_all_good
end
it
'returns false when exit stderr has something'
do
run_command
(
stderr:
'stderr'
)
expect
(
subject
).
not_to
be_all_good
end
end
describe
'#all_status_zero?'
do
it
'returns true when exit status is 0'
do
run_command
expect
(
subject
).
to
be_all_status_zero
end
it
'returns false when exit status is not 0'
do
run_command
(
exitstatus:
1
)
expect
(
subject
).
not_to
be_all_status_zero
end
it
'returns true'
do
run_command
(
stderr:
'stderr'
)
expect
(
subject
).
to
be_all_status_zero
end
end
describe
'#all_stderr_empty?'
do
it
'returns true when stderr is empty'
do
run_command
expect
(
subject
).
to
be_all_stderr_empty
end
it
'returns true when exit status is not 0'
do
run_command
(
exitstatus:
1
)
expect
(
subject
).
to
be_all_stderr_empty
end
it
'returns false when exit stderr has something'
do
run_command
(
stderr:
'stderr'
)
expect
(
subject
).
not_to
be_all_stderr_empty
end
end
describe
'#failed_results'
do
it
'returns [] when everything is passed'
do
run_command
expect
(
subject
.
failed_results
).
to
be_empty
end
it
'returns the result when exit status is not 0'
do
result
=
run_command
(
exitstatus:
1
)
expect
(
subject
.
failed_results
).
to
contain_exactly
(
result
)
end
it
'returns [] when exit stderr has something'
do
run_command
(
stderr:
'stderr'
)
expect
(
subject
.
failed_results
).
to
be_empty
end
end
describe
'#warned_results'
do
it
'returns [] when everything is passed'
do
run_command
expect
(
subject
.
warned_results
).
to
be_empty
end
it
'returns [] when exit status is not 0'
do
run_command
(
exitstatus:
1
)
expect
(
subject
.
warned_results
).
to
be_empty
end
it
'returns the result when exit stderr has something'
do
result
=
run_command
(
stderr:
'stderr'
)
expect
(
subject
.
warned_results
).
to
contain_exactly
(
result
)
end
end
def
run_command
(
command:
'command'
,
stdout:
'stdout'
,
stderr:
''
,
exitstatus:
0
,
status:
double
(
exitstatus:
exitstatus
,
success?:
exitstatus
.
zero?
),
duration:
0.1
)
result
=
Gitlab
::
Popen
::
Result
.
new
(
command
,
stdout
,
stderr
,
status
,
duration
)
allow
(
Gitlab
::
Popen
)
.
to
receive
(
:popen_with_detail
)
.
and_return
(
result
)
subject
.
run
([
command
])
do
|
cmd
,
&
run
|
expect
(
cmd
).
to
eq
(
command
)
cmd_result
=
run
.
call
expect
(
cmd_result
).
to
eq
(
result
)
end
subject
.
results
.
first
end
end
spec/lib/gitlab/popen_spec.rb
View file @
54ca8d0d
require
'spec_helper'
require
'spec_helper'
describe
'Gitlab::Popen'
do
describe
Gitlab
::
Popen
do
let
(
:path
)
{
Rails
.
root
.
join
(
'tmp'
).
to_s
}
let
(
:path
)
{
Rails
.
root
.
join
(
'tmp'
).
to_s
}
before
do
before
do
@klass
=
Class
.
new
(
Object
)
@klass
=
Class
.
new
(
Object
)
@klass
.
send
(
:include
,
Gitlab
::
Popen
)
@klass
.
send
(
:include
,
described_class
)
end
describe
'.popen_with_detail'
do
subject
{
@klass
.
new
.
popen_with_detail
(
cmd
)
}
let
(
:cmd
)
{
%W[
#{
Gem
.
ruby
}
-e $stdout.puts(1);$stderr.puts(2);exit(3)]
}
it
{
expect
(
subject
.
cmd
).
to
eq
(
cmd
)
}
it
{
expect
(
subject
.
stdout
).
to
eq
(
"1
\n
"
)
}
it
{
expect
(
subject
.
stderr
).
to
eq
(
"2
\n
"
)
}
it
{
expect
(
subject
.
status
).
to
eq
(
3
)
}
it
{
expect
(
subject
.
duration
).
to
be_kind_of
(
Numeric
)
}
end
end
context
'zero status'
do
context
'zero status'
do
...
...
spec/support/javascript_fixtures_helpers.rb
View file @
54ca8d0d
require
'action_dispatch/testing/test_request'
require
'action_dispatch/testing/test_request'
require
'fileutils'
require
'fileutils'
require
'gitlab/popen'
module
JavaScriptFixturesHelpers
module
JavaScriptFixturesHelpers
include
Gitlab
::
Popen
include
Gitlab
::
Popen
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment