danger-warnings 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/warnings/helper/severity_util'
3
+
4
+ module Warnings
5
+ describe SeverityUtil do
6
+ context 'rcwef_short' do
7
+ it 'maps unknown to low' do
8
+ expect(SeverityUtil.rcwef_short('U000')).to eq(SeverityUtil::LOW)
9
+ expect(SeverityUtil.rcwef_short('u000')).to eq(SeverityUtil::LOW)
10
+ end
11
+
12
+ it 'maps R/r to low' do
13
+ expect(SeverityUtil.rcwef_short('R000')).to eq(SeverityUtil::LOW)
14
+ expect(SeverityUtil.rcwef_short('r000')).to eq(SeverityUtil::LOW)
15
+ end
16
+
17
+ it 'maps C/c to low' do
18
+ expect(SeverityUtil.rcwef_short('C000')).to eq(SeverityUtil::LOW)
19
+ expect(SeverityUtil.rcwef_short('c000')).to eq(SeverityUtil::LOW)
20
+ end
21
+
22
+ it 'maps W/w to medium' do
23
+ expect(SeverityUtil.rcwef_short('W000')).to eq(SeverityUtil::MEDIUM)
24
+ expect(SeverityUtil.rcwef_short('w000')).to eq(SeverityUtil::MEDIUM)
25
+ end
26
+
27
+ it 'maps E/e to high' do
28
+ expect(SeverityUtil.rcwef_short('E000')).to eq(SeverityUtil::HIGH)
29
+ expect(SeverityUtil.rcwef_short('e000')).to eq(SeverityUtil::HIGH)
30
+ end
31
+
32
+ it 'maps F/f to high' do
33
+ expect(SeverityUtil.rcwef_short('F000')).to eq(SeverityUtil::HIGH)
34
+ expect(SeverityUtil.rcwef_short('f000')).to eq(SeverityUtil::HIGH)
35
+ end
36
+ end
37
+
38
+ context 'rcwef_full' do
39
+ it 'maps unknown to low' do
40
+ expect(SeverityUtil.rcwef_short('Unknown')).to eq(SeverityUtil::LOW)
41
+ expect(SeverityUtil.rcwef_short('unknown')).to eq(SeverityUtil::LOW)
42
+ end
43
+
44
+ it 'maps Refactor/refactor to low' do
45
+ expect(SeverityUtil.rcwef_short('Refactor')).to eq(SeverityUtil::LOW)
46
+ expect(SeverityUtil.rcwef_short('refactor')).to eq(SeverityUtil::LOW)
47
+ end
48
+
49
+ it 'maps Convention/convention to low' do
50
+ expect(SeverityUtil.rcwef_short('Convention')).to eq(SeverityUtil::LOW)
51
+ expect(SeverityUtil.rcwef_short('convention')).to eq(SeverityUtil::LOW)
52
+ end
53
+
54
+ it 'maps Warning/warning to medium' do
55
+ expect(SeverityUtil.rcwef_short('Warning')).to eq(SeverityUtil::MEDIUM)
56
+ expect(SeverityUtil.rcwef_short('warning')).to eq(SeverityUtil::MEDIUM)
57
+ end
58
+
59
+ it 'maps Error/error to high' do
60
+ expect(SeverityUtil.rcwef_short('Error')).to eq(SeverityUtil::HIGH)
61
+ expect(SeverityUtil.rcwef_short('error')).to eq(SeverityUtil::HIGH)
62
+ end
63
+
64
+ it 'maps Fatal/fatal to high' do
65
+ expect(SeverityUtil.rcwef_short('Fatal')).to eq(SeverityUtil::HIGH)
66
+ expect(SeverityUtil.rcwef_short('fatal')).to eq(SeverityUtil::HIGH)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -3,31 +3,10 @@ require_relative '../../lib/warnings/parser/bandit_parser'
3
3
 
4
4
  module Warnings
5
5
  describe BanditParser do
6
- FIRST_ISSUE = {
7
- code: "2852 except ImportError:\n2853 import pickle\n2854 with open(filename, 'wb') as outf:\n",
8
- filename: 'example/ply/yacc_1.py',
9
- issue_confidence: 'HIGH',
10
- issue_severity: :low,
11
- issue_text: 'Consider possible security implications associated with pickle module.',
12
- line_number: 2853,
13
- line_range: [
14
- 2853
15
- ],
16
- more_info: 'https://bandit.readthedocs.io/en/latest/blacklists/blacklist_imports.html#b403-import-pickle',
17
- test_id: 'B403',
18
- test_name: 'blacklist'
19
- }.freeze
20
-
21
6
  before do
22
7
  @parser = BanditParser.new
23
8
  end
24
9
 
25
- context '#file_types' do
26
- it 'include json' do
27
- expect(@parser.file_types).to include(:json)
28
- end
29
- end
30
-
31
10
  context '#parse' do
32
11
  describe 'json' do
33
12
  context 'filled results' do
@@ -42,28 +21,24 @@ module Warnings
42
21
  expect(@parser.issues.count).to eq(3)
43
22
  end
44
23
 
45
- it 'maps name' do
46
- expect(@issue.file_name).to eq(FIRST_ISSUE[:filename])
24
+ it 'maps filename' do
25
+ expect(@issue.file_name).to eq(Assets::BANDIT_FIRST_ISSUE[:filename])
47
26
  end
48
27
 
49
- it 'maps id' do
50
- expect(@issue.id).to eq(FIRST_ISSUE[:test_id])
28
+ it 'maps id-name' do
29
+ expect(@issue.category).to eq("#{Assets::BANDIT_FIRST_ISSUE[:test_id]}-#{Assets::BANDIT_FIRST_ISSUE[:test_name]}")
51
30
  end
52
31
 
53
32
  it 'maps line' do
54
- expect(@issue.line).to eq(FIRST_ISSUE[:line_number])
33
+ expect(@issue.line).to eq(Assets::BANDIT_FIRST_ISSUE[:line_number])
55
34
  end
56
35
 
57
36
  it 'maps severity' do
58
- expect(@issue.severity).to eq(FIRST_ISSUE[:issue_severity])
37
+ expect(@issue.severity).to eq(Assets::BANDIT_FIRST_ISSUE[:issue_severity])
59
38
  end
60
39
 
61
40
  it 'maps message' do
62
- expect(@issue.message).to eq(FIRST_ISSUE[:issue_text])
63
- end
64
-
65
- it 'maps name' do
66
- expect(@issue.name).to eq(FIRST_ISSUE[:test_name])
41
+ expect(@issue.message).to eq(Assets::BANDIT_FIRST_ISSUE[:issue_text])
67
42
  end
68
43
  end
69
44
 
@@ -91,10 +66,7 @@ module Warnings
91
66
  describe 'unsupported type' do
92
67
  it 'raises error' do
93
68
  file_name = 'hello.txt'
94
- ext = File.extname(file_name).delete('.')
95
- expect { @parser.parse(file_name) }.to raise_error(format(Parser::ERROR_EXT_NOT_SUPPORTED,
96
- ext,
97
- @parser.class.name))
69
+ expect { @parser.parse(file_name) }.to raise_error(format(Parser::ERROR_EXT_NOT_JSON, file_name))
98
70
  end
99
71
  end
100
72
  end
@@ -16,18 +16,30 @@ module Warnings
16
16
  expect { ParserFactory.create('unknown') }.to raise_error('Parser \'unknown\' not supported.')
17
17
  end
18
18
 
19
- context 'bandit' do
20
- it 'symbol' do
21
- result = ParserFactory.create(:bandit)
22
- expect(result).not_to be_nil
23
- expect(result).to be_a(BanditParser)
24
- end
25
-
26
- it 'string' do
27
- result = ParserFactory.create('bandit')
28
- expect(result).not_to be_nil
29
- expect(result).to be_a(BanditParser)
30
- end
19
+ it 'known symbol' do
20
+ expect(ParserFactory.create(:bandit)).to be_a(BanditParser)
21
+ end
22
+
23
+ it 'known string' do
24
+ expect(ParserFactory.create('bandit')).to be_a(BanditParser)
25
+ end
26
+
27
+ it 'bandit' do
28
+ result = ParserFactory.create(:bandit)
29
+ expect(result).not_to be_nil
30
+ expect(result).to be_a(BanditParser)
31
+ end
32
+
33
+ it 'pylint' do
34
+ result = ParserFactory.create(:pylint)
35
+ expect(result).not_to be_nil
36
+ expect(result).to be_a(PylintParser)
37
+ end
38
+
39
+ it 'rubocop' do
40
+ result = ParserFactory.create(:rubocop)
41
+ expect(result).not_to be_nil
42
+ expect(result).to be_a(RubocopParser)
31
43
  end
32
44
  end
33
45
  end
@@ -0,0 +1,57 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/warnings/parser/pylint_parser'
3
+
4
+ module Warnings
5
+ describe PylintParser do
6
+ before do
7
+ @parser = PylintParser.new
8
+ end
9
+
10
+ context '#parse' do
11
+ context 'filled results' do
12
+ before do
13
+ @parser.parse(Assets::PYLINT_TXT)
14
+ @issue = @parser.issues.first
15
+ end
16
+
17
+ it 'parses issues' do
18
+ expect(@parser.issues).not_to be_empty
19
+ end
20
+
21
+ it 'maps filename' do
22
+ expect(@issue.file_name).to eq(Assets::PYLINT_FIRST_ISSUE[:filename])
23
+ end
24
+
25
+ it 'maps id' do
26
+ expect(@issue.category).to eq(Assets::PYLINT_FIRST_ISSUE[:category])
27
+ end
28
+
29
+ it 'maps line' do
30
+ expect(@issue.line).to eq(Assets::PYLINT_FIRST_ISSUE[:line])
31
+ end
32
+
33
+ it 'maps message' do
34
+ expect(@issue.message).to eq(Assets::PYLINT_FIRST_ISSUE[:message])
35
+ end
36
+
37
+ it 'maps severity' do
38
+ expect(@issue.severity).to eq(:high)
39
+ end
40
+ end
41
+
42
+ context 'empty file' do
43
+ it 'has no issues' do
44
+ @parser.parse(Assets::EMPTY_FILE)
45
+ expect(@parser.issues).to be_empty
46
+ end
47
+ end
48
+
49
+ context 'missing file' do
50
+ it 'raises error' do
51
+ file_name = 'invalid'
52
+ expect { @parser.parse(file_name) }.to raise_error(format(Parser::ERROR_FILE_NOT_EXIST, file_name))
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,94 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/warnings/parser/rubocop_parser'
3
+
4
+ module Warnings
5
+ describe RubocopParser do
6
+ before do
7
+ @parser = RubocopParser.new
8
+ end
9
+
10
+ context '#parse' do
11
+ context 'json' do
12
+ context 'default' do
13
+ before do
14
+ @parser.parse(Assets::RUBOCOP_JSON)
15
+ @issue = @parser.issues.first
16
+ @first_issue_offense = Assets::RUBOCOP_FIRST_ISSUE[:offenses].first
17
+ end
18
+
19
+ it 'parses issues' do
20
+ expect(@parser.issues).not_to be_empty
21
+ end
22
+
23
+ it 'maps path' do
24
+ expect(@issue.file_name).to eq(Assets::RUBOCOP_FIRST_ISSUE[:path])
25
+ end
26
+
27
+ it 'maps category' do
28
+ expect(@issue.category).to eq(@first_issue_offense[:cop_name])
29
+ end
30
+
31
+ it 'maps line' do
32
+ expect(@issue.line).to eq(@first_issue_offense[:location][:line])
33
+ end
34
+
35
+ it 'maps message' do
36
+ expect(@issue.message).to eq(@first_issue_offense[:message])
37
+ end
38
+
39
+ it 'maps severity' do
40
+ expect(@issue.severity).to eq(:low)
41
+ end
42
+ end
43
+
44
+ context 'multiple offenses' do
45
+ it 'parses multiple offenses' do
46
+ @parser.parse(Assets::RUBOCOP_MULTI_JSON)
47
+ expect(@parser.issues.count).to eq(11)
48
+ end
49
+ end
50
+
51
+ context 'empty issues' do
52
+ it 'has no issues' do
53
+ @parser.parse(Assets::EMPTY_FILE)
54
+ expect(@parser.issues).to be_empty
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'simple' do
60
+ context 'default' do
61
+ before do
62
+ @parser.parse(Assets::RUBOCOP_SIMPLE)
63
+ @issue = @parser.issues.first
64
+ @first_issue_offense = Assets::RUBOCOP_FIRST_ISSUE[:offenses].first
65
+ end
66
+
67
+ it 'parses issues' do
68
+ expect(@parser.issues).not_to be_empty
69
+ end
70
+
71
+ it 'maps path' do
72
+ expect(@issue.file_name).to eq(Assets::RUBOCOP_FIRST_ISSUE[:path])
73
+ end
74
+
75
+ it 'maps category' do
76
+ expect(@issue.category).to be_nil
77
+ end
78
+
79
+ it 'maps line' do
80
+ expect(@issue.line).to eq(@first_issue_offense[:location][:line])
81
+ end
82
+
83
+ it 'maps message' do
84
+ expect(@issue.message).to eq(@first_issue_offense[:message])
85
+ end
86
+
87
+ it 'maps severity' do
88
+ expect(@issue.severity).to eq(:low)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,5 +1,5 @@
1
- require_relative 'spec_helper'
2
- require_relative '../lib/warnings/reporter'
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/warnings/report/reporter'
3
3
  require 'danger'
4
4
 
5
5
  module Warnings
@@ -251,5 +251,45 @@ module Warnings
251
251
  end
252
252
  end
253
253
  end
254
+
255
+ context 'bandit' do
256
+ it 'runs markdown' do
257
+ @reporter.inline = false
258
+ @reporter.filter = false
259
+ @reporter.parser = :bandit
260
+ @reporter.file = Assets::BANDIT_JSON
261
+ @reporter.report
262
+ expect(@dangerfile.status_report[:markdowns]).not_to be_empty
263
+ end
264
+
265
+ it 'runs inline' do
266
+ @reporter.inline = true
267
+ @reporter.filter = false
268
+ @reporter.parser = :bandit
269
+ @reporter.file = Assets::BANDIT_JSON
270
+ @reporter.report
271
+ expect(@dangerfile.status_report[:warnings]).not_to be_empty
272
+ end
273
+ end
274
+
275
+ context 'pylint' do
276
+ it 'runs markdown' do
277
+ @reporter.inline = false
278
+ @reporter.filter = false
279
+ @reporter.parser = :pylint
280
+ @reporter.file = Assets::PYLINT_TXT
281
+ @reporter.report
282
+ expect(@dangerfile.status_report[:markdowns]).not_to be_empty
283
+ end
284
+
285
+ it 'runs inline' do
286
+ @reporter.inline = true
287
+ @reporter.filter = false
288
+ @reporter.parser = :pylint
289
+ @reporter.file = Assets::PYLINT_TXT
290
+ @reporter.report
291
+ expect(@dangerfile.status_report[:warnings]).not_to be_empty
292
+ end
293
+ end
254
294
  end
255
295
  end
@@ -11,7 +11,7 @@ require 'danger'
11
11
  require 'mocha'
12
12
  require 'simplecov'
13
13
 
14
- require_relative 'assets/assets'
14
+ require_relative 'spec_helper/assets'
15
15
 
16
16
  SimpleCov.start
17
17
 
@@ -0,0 +1,53 @@
1
+ module Warnings
2
+ module Assets
3
+ ASSETS_DIR = Pathname.new(File.expand_path('../assets', __dir__))
4
+ EMPTY_FILE = "#{ASSETS_DIR}/empty.txt".freeze
5
+
6
+ BANDIT_JSON = "#{ASSETS_DIR}/bandit.json".freeze
7
+ BANDIT_EMPTY = "#{ASSETS_DIR}/bandit_empty.json".freeze
8
+ BANDIT_MISSING_RESULTS = "#{ASSETS_DIR}/bandit_missing_results.json".freeze
9
+ PYLINT_TXT = "#{ASSETS_DIR}/pylint.txt".freeze
10
+ RUBOCOP_JSON = "#{ASSETS_DIR}/rubocop.json".freeze
11
+ RUBOCOP_MULTI_JSON = "#{ASSETS_DIR}/rubocop_multi_offenses.json".freeze
12
+ RUBOCOP_SIMPLE = "#{ASSETS_DIR}/rubocop.txt".freeze
13
+
14
+ BANDIT_FIRST_ISSUE = {
15
+ code: "2852 except ImportError:\n2853 import pickle\n2854 with open(filename, 'wb') as outf:\n",
16
+ filename: 'example/ply/yacc_1.py',
17
+ issue_confidence: 'HIGH',
18
+ issue_severity: :low,
19
+ issue_text: 'Consider possible security implications associated with pickle module.',
20
+ line_number: 2853,
21
+ line_range: [
22
+ 2853
23
+ ],
24
+ more_info: 'https://bandit.readthedocs.io/en/latest/blacklists/blacklist_imports.html#b403-import-pickle',
25
+ test_id: 'B403',
26
+ test_name: 'blacklist'
27
+ }.freeze
28
+
29
+ PYLINT_FIRST_ISSUE = {
30
+ filename: 'test_project/__init__.py',
31
+ line: '1',
32
+ category: 'F403',
33
+ message: "'from test_project import *' used; unable to detect undefined names"
34
+ }.freeze
35
+
36
+ RUBOCOP_FIRST_ISSUE = {
37
+ path: 'spec/lib/danger/danger_core/plugins/dangerfile_gitlab_plugin_spec.rb',
38
+ offenses: [
39
+ {
40
+ severity: 'convention',
41
+ message: 'Do not use semicolons to terminate expressions.',
42
+ cop_name: 'Style/Semicolon',
43
+ corrected: false,
44
+ location: {
45
+ line: 82,
46
+ column: 65,
47
+ length: 1
48
+ }
49
+ }
50
+ ]
51
+ }.freeze
52
+ end
53
+ end