diff --git a/interpreter/ruby/ruby.go b/interpreter/ruby/ruby.go index b84d4acdd..22af461f8 100644 --- a/interpreter/ruby/ruby.go +++ b/interpreter/ruby/ruby.go @@ -97,9 +97,9 @@ const ( var ( // regex to identify the Ruby interpreter executable - rubyRegex = regexp.MustCompile(`^(?:.*/)?libruby(?:-.*)?\.so\.(\d)\.(\d)\.(\d)$`) + rubyRegex = regexp.MustCompile(`^(?:.*/)?libruby(?:-.*)?\.so\.(\d+)\.(\d+)\.(\d+)$`) // regex to extract a version from a string - rubyVersionRegex = regexp.MustCompile(`^(\d)\.(\d)\.(\d)$`) + rubyVersionRegex = regexp.MustCompile(`^(\d+)\.(\d+)\.(\d+)$`) unknownCfunc = libpf.Intern("") cfuncDummyFile = libpf.Intern("") diff --git a/interpreter/ruby/ruby_test.go b/interpreter/ruby/ruby_test.go index 767641c0b..3e89c89e9 100644 --- a/interpreter/ruby/ruby_test.go +++ b/interpreter/ruby/ruby_test.go @@ -11,6 +11,119 @@ import ( "github.com/stretchr/testify/assert" ) +func TestRubyRegex(t *testing.T) { + tests := []struct { + name string + input string + match bool + major string + minor string + release string + }{ + { + name: "single_digit_version", + input: "libruby.so.3.2.1", + match: true, + major: "3", + minor: "2", + release: "1", + }, + { + // https://github.com/ruby/ruby/releases/tag/v3_3_10 + name: "multi_digit_release", + input: "libruby.so.3.3.10", + match: true, + major: "3", + minor: "3", + release: "10", + }, + { + name: "with_path", + input: "/usr/lib/libruby.so.3.3.10", + match: true, + major: "3", + minor: "3", + release: "10", + }, + { + name: "with_suffix", + input: "libruby-3.2.so.3.2.1", + match: true, + major: "3", + minor: "2", + release: "1", + }, + { + name: "no_match", + input: "libpython.so.3.9", + match: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + matches := rubyRegex.FindStringSubmatch(tt.input) + if !tt.match { + assert.Nil(t, matches) + return + } + if assert.NotNil(t, matches) { + assert.Equal(t, tt.major, matches[1]) + assert.Equal(t, tt.minor, matches[2]) + assert.Equal(t, tt.release, matches[3]) + } + }) + } +} + +func TestRubyVersionRegex(t *testing.T) { + tests := []struct { + name string + input string + match bool + major string + minor string + release string + }{ + { + name: "single_digit_version", + input: "3.2.1", + match: true, + major: "3", + minor: "2", + release: "1", + }, + { + name: "multi_digit_release", + input: "3.3.10", + match: true, + major: "3", + minor: "3", + release: "10", + }, + { + name: "no_match_partial", + input: "3.9", + match: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + matches := rubyVersionRegex.FindStringSubmatch(tt.input) + if !tt.match { + assert.Nil(t, matches) + return + } + if assert.NotNil(t, matches) { + assert.Equal(t, tt.major, matches[1]) + assert.Equal(t, tt.minor, matches[2]) + assert.Equal(t, tt.release, matches[3]) + } + }) + } +} + func TestQualifiedMethodName(t *testing.T) { tests := []struct { name string