Skip to content

Commit 1d69c78

Browse files
authored
Merge pull request #114 from Shopify/at-fix-bump
Fix bump command when called with a custom `--error-url-base`
2 parents 8d662de + 1d13b80 commit 1d69c78

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

lib/spoom/cli/bump.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ def bump(directory = ".")
8383
exit(files_to_bump.empty?)
8484
end
8585

86+
error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE
8687
output, status, exit_code = Sorbet.srb_tc(
8788
"--no-error-sections",
89+
"--error-url-base=#{error_url_base}",
8890
path: exec_path,
8991
capture_err: true,
9092
sorbet_bin: options[:sorbet]
@@ -104,7 +106,7 @@ def bump(directory = ".")
104106
exit(files_to_bump.empty?)
105107
end
106108

107-
errors = Sorbet::Errors::Parser.parse_string(output)
109+
errors = Sorbet::Errors::Parser.parse_string(output, error_url_base: error_url_base)
108110

109111
files_with_errors = errors.map do |err|
110112
path = File.expand_path(err.file)

lib/spoom/sorbet/errors.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Sorbet
66
module Errors
77
extend T::Sig
88

9+
DEFAULT_ERROR_URL_BASE = "https://srb.help/"
10+
911
# Parse errors from Sorbet output
1012
class Parser
1113
extend T::Sig
@@ -18,28 +20,16 @@ class Parser
1820
"or set SORBET_SILENCE_DEV_MESSAGE=1 in your shell environment.",
1921
]
2022

21-
ERROR_LINE_MATCH_REGEX = %r{
22-
^ # match beginning of line
23-
(\S[^:]*) # capture filename as something that starts with a non-space character
24-
# followed by anything that is not a colon character
25-
: # match the filename - line number seperator
26-
(\d+) # capture the line number
27-
:\s # match the line number - error message separator
28-
(.*) # capture the error message
29-
\shttps://srb.help/ # match the error code url prefix
30-
(\d+) # capture the error code
31-
$ # match end of line
32-
}x.freeze
33-
34-
sig { params(output: String).returns(T::Array[Error]) }
35-
def self.parse_string(output)
36-
parser = Spoom::Sorbet::Errors::Parser.new
23+
sig { params(output: String, error_url_base: String).returns(T::Array[Error]) }
24+
def self.parse_string(output, error_url_base: DEFAULT_ERROR_URL_BASE)
25+
parser = Spoom::Sorbet::Errors::Parser.new(error_url_base: error_url_base)
3726
parser.parse(output)
3827
end
3928

40-
sig { void }
41-
def initialize
29+
sig { params(error_url_base: String).void }
30+
def initialize(error_url_base: DEFAULT_ERROR_URL_BASE)
4231
@errors = []
32+
@error_line_match_regex = error_line_match_regexp(error_url_base)
4333
@current_error = nil
4434
end
4535

@@ -66,9 +56,26 @@ def parse(output)
6656

6757
private
6858

59+
sig { params(error_url_base: String).returns(Regexp) }
60+
def error_line_match_regexp(error_url_base)
61+
url = Regexp.escape(error_url_base)
62+
%r{
63+
^ # match beginning of line
64+
(\S[^:]*) # capture filename as something that starts with a non-space character
65+
# followed by anything that is not a colon character
66+
: # match the filename - line number seperator
67+
(\d+) # capture the line number
68+
:\s # match the line number - error message separator
69+
(.*) # capture the error message
70+
\s#{url} # match the error code url prefix
71+
(\d+) # capture the error code
72+
$ # match end of line
73+
}x
74+
end
75+
6976
sig { params(line: String).returns(T.nilable(Error)) }
7077
def match_error_line(line)
71-
match = line.match(ERROR_LINE_MATCH_REGEX)
78+
match = line.match(@error_line_match_regex)
7279
return unless match
7380

7481
file, line, message, code = match.captures

test/spoom/cli/bump_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,41 @@ def self.something_else; end
184184
assert_equal("true", Sorbet::Sigils.file_strictness("#{@project.path}/file.rb"))
185185
end
186186

187+
def test_bump_with_custom_error_url_base
188+
@project.sorbet_config(<<~CONFIG)
189+
.
190+
--error-url-base="https://docs.org#"
191+
CONFIG
192+
193+
@project.write("file.rb", <<~RB)
194+
# typed: true
195+
require "test_helper"
196+
197+
class Test
198+
def self.foo(*arg); end
199+
def self.something; end
200+
def self.something_else; end
201+
202+
foo "foo" do
203+
q = something do
204+
q = something_else.new
205+
end
206+
end
207+
end
208+
RB
209+
210+
out, err, status = @project.bundle_exec("spoom bump --no-color --from true --to strict")
211+
assert_empty(err)
212+
assert_equal(<<~OUT, out)
213+
Checking files...
214+
215+
No file to bump from `true` to `strict`
216+
OUT
217+
assert(status)
218+
219+
assert_equal("true", Sorbet::Sigils.file_strictness("#{@project.path}/file.rb"))
220+
end
221+
187222
def test_bump_preserve_file_encoding
188223
string = <<~RB
189224
# typed: false

test/spoom/sorbet/errors_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,30 @@ def test_parses_errors_with_multiple_blank_lines
248248
assert_equal([7003, 7001, 7002], errors.map(&:code))
249249
end
250250

251+
def test_parses_errors_with_custom_error_url_base
252+
errors = Spoom::Sorbet::Errors::Parser.parse_string(<<~ERR, error_url_base: "https://custom-url#")
253+
a.rb:80: unexpected token "end" https://custom-url#2001
254+
80 |end
255+
^^^
256+
257+
b.rb:28: Not enough arguments provided for method Foo#bar. Expected: 1..2, got: 1 https://custom-url#7004
258+
28 | bar "hello"
259+
^^^^^^^^^^^
260+
261+
c.rb:100: Method Foo#initialize redefined without matching argument count. Expected: 0, got: 2 https://custom-url#4010
262+
100 | class Foo < T::Struct
263+
101 | end
264+
265+
d.rb:105: Method foo does not exist on String https://custom-url#7003
266+
105 | printer.print "foo".light_black
267+
^^^^^^^^^^^^^^^^^
268+
ERR
269+
assert_equal(4, errors.size)
270+
assert_equal(["a.rb", "b.rb", "c.rb", "d.rb"], errors.map(&:file))
271+
assert_equal([80, 28, 100, 105], errors.map(&:line))
272+
assert_equal([2001, 7004, 4010, 7003], errors.map(&:code))
273+
end
274+
251275
def test_sort_errors
252276
errors = Spoom::Sorbet::Errors::Parser.parse_string(<<~ERR)
253277
z.rb:80: unexpected token "end" https://srb.help/2001

0 commit comments

Comments
 (0)