Skip to content

Commit 10feaed

Browse files
committed
rubocop: resolve issues on rubocop 1.84.1
Signed-off-by: Philip Li <philip.li@intel.com>
1 parent c87c0b8 commit 10feaed

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

lib/bash.rb

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,47 @@ def run(*args, **options, &block)
5252

5353
cmd = TTY::Command.new(printer: verbose ? :pretty : :null, uuid: false, **options)
5454

55+
args = prepare_args(args, unsetenv_others)
56+
57+
result = cmd.run!(*args) do |out, err|
58+
# TTY::Command yields chunks, split them to match line-based expectation.
59+
(out || err).each_line { |line| block.call(line.chomp) } if stream
60+
end
61+
62+
handle_result(result, args, returns, options, stream, block)
63+
rescue TTY::Command::TimeoutExceeded
64+
raise TimeoutError, args.join(' ')
65+
end
66+
67+
private
68+
69+
def prepare_args(args, unsetenv_others)
5570
env = args.first.is_a?(Hash) ? args.shift : {}
5671

5772
if unsetenv_others
58-
# Manually construct `env -i` command because TTY::Command unsetenv_others is unreliable
59-
env_vars = env.map { |k, v| "#{k}=#{v}" }
60-
args = if args.size == 1
61-
['env', '-i'] + env_vars + ['bash', '-c', args.first]
62-
else
63-
['env', '-i'] + env_vars + args
64-
end
73+
construct_env_i_args(args, env)
6574
else
66-
# Normalize args to force bash for single strings (legacy support)
67-
args = ['bash', '-c', args.first] if args.size == 1
68-
args.unshift(env) unless env.empty?
75+
construct_bash_args(args, env)
6976
end
77+
end
7078

71-
result = cmd.run!(*args) do |out, err|
72-
next unless stream
73-
74-
# Streaming mode: yield lines to block
75-
# TTY::Command yields chunks, split them to match line-based expectation.
76-
(out || err).each_line { |line| block.call(line.chomp) }
79+
def construct_env_i_args(args, env)
80+
env_vars = env.map { |k, v| "#{k}=#{v}" }
81+
base = ['env', '-i'] + env_vars
82+
if args.size == 1
83+
base + ['bash', '-c', args.first]
84+
else
85+
base + args
7786
end
87+
end
88+
89+
def construct_bash_args(args, env)
90+
args = ['bash', '-c', args.first] if args.size == 1
91+
args.unshift(env) unless env.empty?
92+
args
93+
end
7894

95+
def handle_result(result, args, returns, options, stream, block)
7996
# Legacy Bash.run behavior (Post-run yield) if NOT streaming
8097
# Note: legacy behavior requires yielding (stdout, stderr, exit_status)
8198
if block && !stream
@@ -86,8 +103,6 @@ def run(*args, **options, &block)
86103
end
87104

88105
result.out.chomp
89-
rescue TTY::Command::TimeoutExceeded
90-
raise TimeoutError.new(args.join(' '))
91106
end
92107
end
93108
end

spec/bash_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@
9696
expect(out).to be_empty
9797
end
9898

99-
it 'clears environment when unsetenv_others: true (legacy alias)' do
100-
out = described_class.run('echo $LKP_TEST_ENV_VAR', unsetenv_others: true)
101-
expect(out).to be_empty
102-
end
103-
10499
it 'passes specific env vars even with unsetenv_others: true' do
105100
out = described_class.run({ 'PRESERVED' => 'yes' }, 'echo $PRESERVED', unsetenv_others: true)
106101
expect(out).to eq('yes')

stats/zoneinfo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ tag = ''
66
def puts_vals(tag, node, zone, line)
77
vals = line.split
88
if tag == 'pages' && vals.size >= 2
9-
puts "node#{node}.#{zone}.pages.#{vals[vals.size - 2]}: #{vals[vals.size - 1]}"
9+
puts "node#{node}.#{zone}.pages.#{vals[-2]}: #{vals[-1]}"
1010
elsif tag == 'node' && vals.size >= 2
11-
puts "node#{node}.#{vals[vals.size - 2]}: #{vals[vals.size - 1]}"
11+
puts "node#{node}.#{vals[-2]}: #{vals[-1]}"
1212
elsif line =~ /\s*([a-zA-Z].*):\s*([0-9]+)/
1313
puts "node#{node}.#{zone}.#{$1.tr(' ', '_')}: #{$2}"
1414
end

0 commit comments

Comments
 (0)