Skip to content

Commit 1756e6c

Browse files
committed
feat: validate ruby version (+ better config loading validation)
1 parent d9b3bc1 commit 1756e6c

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

lib/appmap/service/validator/config_validator.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ def initialize(config_file)
1515
end
1616

1717
def valid?
18+
validate_ruby_version
1819
validate_config_presence
19-
@violations.length == 0
20+
@violations.empty?
2021
end
2122

2223
private
@@ -26,14 +27,22 @@ def present?
2627
end
2728

2829
def load_config
30+
YAML.parse_file(@config_file) if present?
2931
AppMap::Config.load_from_file(@config_file) if present?
30-
rescue StandardError => e
32+
rescue Psych::SyntaxError => e
3133
@violations << Violation.error(
3234
filename: @config_file,
3335
message: 'AppMap configuration is not valid YAML',
3436
detailed_message: e.message
3537
)
3638
nil
39+
rescue StandardError => e
40+
@violations << Violation.error(
41+
filename: @config_file,
42+
message: 'AppMap configuration could not be loaded.',
43+
detailed_message: e.message
44+
)
45+
nil
3746
end
3847

3948
def validate_config_presence
@@ -44,6 +53,15 @@ def validate_config_presence
4453
)
4554
end
4655
end
56+
57+
def validate_ruby_version
58+
unless RUBY_VERSION =~ AppMap::SUPPORTED_RUBY_VERSIONS_REGEX
59+
@violations << Violation.error(
60+
message: "AppMap does not support Ruby #{RUBY_VERSION}. " \
61+
"Supported versions are: #{AppMap::SUPPORTED_RUBY_VERSIONS.join(', ')}."
62+
)
63+
end
64+
end
4765
end
4866
end
4967
end

lib/appmap/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module AppMap
77

88
APPMAP_FORMAT_VERSION = '1.5.1'
99

10+
SUPPORTED_RUBY_VERSIONS_REGEX = /^2\.[567]\./.freeze
11+
SUPPORTED_RUBY_VERSIONS = %w[2.5 2.6 2.7].freeze
12+
1013
DEFAULT_APPMAP_DIR = 'tmp/appmap'.freeze
1114
DEFAULT_CONFIG_FILE_PATH = 'appmap.yml'.freeze
1215
end
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
name:
2-
-
3-
packages: "[]" asd
1+
name: name
2+
packages: not_array
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name:
2+
-
3+
packages: "[]" asd

spec/service/config_analyzer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
end
5252

5353
context 'with invalid YAML config' do
54-
let(:config_file) { 'spec/fixtures/config/invalid_config.yml'}
54+
let(:config_file) { 'spec/fixtures/config/invalid_yaml_config.yml'}
5555

5656
describe '.app_name' do
5757
it 'returns app name value from config' do

test/agent_setup_validate_test.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class AgentSetupValidateTest < Minitest::Test
77
NON_EXISTING_CONFIG_FILENAME = '123.yml'
8+
INVALID_YAML_CONFIG_FILENAME = 'spec/fixtures/config/invalid_yaml_config.yml'
89
INVALID_CONFIG_FILENAME = 'spec/fixtures/config/invalid_config.yml'
910

1011
def test_init_when_config_exists
@@ -26,15 +27,30 @@ def test_init_with_non_existing_config_file
2627
assert_equal expected, output.strip
2728
end
2829

29-
def test_init_with_custom_invalid_YAML
30+
def test_init_with_invalid_YAML
31+
output = `./exe/appmap-agent-validate -c #{INVALID_YAML_CONFIG_FILENAME}`
32+
assert_equal 0, $CHILD_STATUS.exitstatus
33+
expected = JSON.pretty_generate([
34+
{
35+
level: :error,
36+
filename: INVALID_YAML_CONFIG_FILENAME,
37+
message: 'AppMap configuration is not valid YAML',
38+
detailed_message: "(#{INVALID_YAML_CONFIG_FILENAME}): " \
39+
'did not find expected key while parsing a block mapping at line 1 column 1'
40+
}
41+
])
42+
assert_equal expected, output.strip
43+
end
44+
45+
def test_init_with_invalid_data_config
3046
output = `./exe/appmap-agent-validate -c #{INVALID_CONFIG_FILENAME}`
3147
assert_equal 0, $CHILD_STATUS.exitstatus
3248
expected = JSON.pretty_generate([
3349
{
3450
level: :error,
3551
filename: INVALID_CONFIG_FILENAME,
36-
message: 'AppMap configuration is not valid YAML',
37-
detailed_message: "(<unknown>): did not find expected key while parsing a block mapping at line 1 column 1"
52+
message: 'AppMap configuration could not be loaded.',
53+
detailed_message: "undefined method `map' for \"not_array\":String\nDid you mean? tap"
3854
}
3955
])
4056
assert_equal expected, output.strip

0 commit comments

Comments
 (0)