Skip to content

Commit be80e49

Browse files
author
Lisa Ugray
committed
Handle out of space errors
I would have thought that this would come up so rarely as to not be an issue, but I see bug reports for this semi-frequently. Let's deal with this one more properly.
1 parent efe16ec commit be80e49

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
cli-kit (3.2.0)
4+
cli-kit (3.3.0)
55
cli-ui (>= 1.1.4)
66

77
GEM
@@ -11,7 +11,7 @@ GEM
1111
ast (2.4.0)
1212
builder (3.2.3)
1313
byebug (9.0.6)
14-
cli-ui (1.1.4)
14+
cli-ui (1.2.3)
1515
metaclass (0.0.4)
1616
method_source (0.8.2)
1717
minitest (5.10.2)
@@ -53,4 +53,4 @@ DEPENDENCIES
5353
rubocop (~> 0.56.0)
5454

5555
BUNDLED WITH
56-
1.17.1
56+
1.17.3

lib/cli/kit/error_handler.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
module CLI
55
module Kit
66
class ErrorHandler
7-
def initialize(log_file:, exception_reporter:)
7+
def initialize(log_file:, exception_reporter:, tool_name: nil)
88
@log_file = log_file
99
@exception_reporter_or_proc = exception_reporter || NullExceptionReporter
10+
@tool_name = tool_name
1011
end
1112

1213
module NullExceptionReporter
@@ -84,6 +85,14 @@ def handle_abort
8485
rescue Interrupt
8586
$stderr.puts(format_error_message("Interrupt"))
8687
CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG
88+
rescue Errno::ENOSPC
89+
message = if @tool_name
90+
"Your disk is full - {{command:#{@tool_name}}} requires free space to operate"
91+
else
92+
"Your disk is full - free space is required to operate"
93+
end
94+
$stderr.puts(format_error_message(message))
95+
CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG
8796
end
8897

8998
def exception_reporter

lib/cli/kit/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CLI
22
module Kit
3-
VERSION = "3.2.0"
3+
VERSION = "3.3.0"
44
end
55
end

test/cli/kit/error_handler_test.rb

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ class ErrorHandlerTest < MiniTest::Test
77
def setup
88
@rep = Object.new
99
@tf = Tempfile.create('executor-log').tap(&:close)
10-
@eh = ErrorHandler.new(log_file: @tf.path, exception_reporter: @rep)
11-
class << @eh
12-
attr_reader :exit_handler
13-
# Prevent `install!` from actually installing the hook.
14-
def at_exit(&block)
15-
@exit_handler = block
16-
end
17-
end
10+
@eh = error_handler
1811
end
1912

2013
def teardown
@@ -77,6 +70,29 @@ def test_bug
7770
end
7871
end
7972

73+
def test_out_of_space
74+
run_test(
75+
expect_code: CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG,
76+
expect_out: "",
77+
expect_err: "\e[0;31mYour disk is full - free space is required to operate\e[0m\n",
78+
expect_report: false,
79+
) do
80+
raise(Errno::ENOSPC)
81+
end
82+
end
83+
84+
def test_out_of_space_with_name
85+
@eh = error_handler(tool_name: "foo")
86+
run_test(
87+
expect_code: CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG,
88+
expect_out: "",
89+
expect_err: "\e[0;31mYour disk is full - \e[0;31;36mfoo\e[0;31m requires free space to operate\e[0m\n",
90+
expect_report: false,
91+
) do
92+
raise(Errno::ENOSPC)
93+
end
94+
end
95+
8096
def test_interrupt
8197
run_test(
8298
expect_code: CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG,
@@ -126,6 +142,18 @@ def test_exit_1
126142

127143
private
128144

145+
def error_handler(tool_name: nil)
146+
ErrorHandler.new(log_file: @tf.path, exception_reporter: @rep, tool_name: tool_name).tap do |eh|
147+
class << eh
148+
attr_reader :exit_handler
149+
# Prevent `install!` from actually installing the hook.
150+
def at_exit(&block)
151+
@exit_handler = block
152+
end
153+
end
154+
end
155+
end
156+
129157
def with_handler
130158
code = nil
131159
out, err = capture_io do

0 commit comments

Comments
 (0)