solargraph_test_coverage 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +1 -1
- data/README.md +0 -2
- data/lib/solargraph_test_coverage/helpers.rb +21 -33
- data/lib/solargraph_test_coverage/test_coverage_reporter.rb +30 -27
- data/lib/solargraph_test_coverage/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1038b1738247fe8ef8aa47e6675989d5d1e97e4e18c2f26fb9d6aafceca5f6a6
|
4
|
+
data.tar.gz: 9893901996387356d5547b57aa4ebb787998845976c8d7ae067bba3985acf49e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28ab19d5bfe978d8783bfd03f584b49228cc0d06bf310d8c1b8e5cdc20815d077c8587b3faac1ec1d6ddce6b2f7e7af74ace67a91ca94c0125269536607cad15
|
7
|
+
data.tar.gz: f91c21643e59dd1001712f2e62248adccb52a0032ac6d300177a7779e3283bddca769f67c24b307dcba8947b1b5279607c2e32e532bf1c44b11d419df064f5e7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,48 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module SolargraphTestCoverage
|
4
|
+
# Some helper functions for the diagnostics
|
4
5
|
module Helpers
|
5
|
-
#
|
6
6
|
# Determines if a file should be excluded from running diagnostics
|
7
|
-
#
|
8
7
|
# @return [Boolean]
|
9
8
|
#
|
10
9
|
def exclude_file?(source_filename)
|
11
|
-
return true if source_filename.start_with?
|
10
|
+
return true if source_filename.start_with? test_path
|
12
11
|
|
13
12
|
Config.exclude_paths.each { |path| return true if source_filename.sub(Dir.pwd, '').include? path }
|
14
13
|
|
15
14
|
false
|
16
15
|
end
|
17
16
|
|
18
|
-
#
|
19
17
|
# Attempts to find the corresponding unit test file
|
20
|
-
#
|
21
18
|
# @return [String]
|
22
19
|
#
|
23
20
|
def test_file(source)
|
24
|
-
|
25
|
-
|
26
|
-
relative_filepath[0] = Config.test_dir
|
21
|
+
relative_filepath = source.location.filename.sub(Dir.pwd, '').split('/').reject(&:empty?)
|
22
|
+
relative_filepath[0] = Config.test_dir
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
#
|
34
|
-
# Memoized wrapper for #run_test
|
35
|
-
#
|
36
|
-
# @return [Hash]
|
37
|
-
#
|
38
|
-
def results(source)
|
39
|
-
@results ||= run_test(source)
|
24
|
+
File.join(Dir.pwd, relative_filepath.join('/'))
|
25
|
+
.sub('.rb', Config.test_file_suffix)
|
40
26
|
end
|
41
27
|
|
42
|
-
#
|
43
|
-
# Runs RSpec on test file in a child process
|
28
|
+
# Runs test file in a child process with specified testing framework
|
44
29
|
# Returns coverage results for current working file
|
45
|
-
# RSpec::Core::Runner.run will return 0 if the test file passes, and 1 if it does not.
|
46
30
|
#
|
47
31
|
# @return [Hash]
|
48
32
|
#
|
@@ -54,7 +38,6 @@ module SolargraphTestCoverage
|
|
54
38
|
end
|
55
39
|
end
|
56
40
|
|
57
|
-
#
|
58
41
|
# Adapted from SingleCov
|
59
42
|
# Coverage returns nil for untestable lines (like do, end, if keywords)
|
60
43
|
# otherwise returns int showing how many times a line was called
|
@@ -66,12 +49,11 @@ module SolargraphTestCoverage
|
|
66
49
|
def uncovered_lines(results)
|
67
50
|
results.fetch(:lines)
|
68
51
|
.each_with_index
|
69
|
-
.select { |c, _| c
|
52
|
+
.select { |c, _| c&.zero? }
|
70
53
|
.map { |_, i| i }
|
71
54
|
.compact
|
72
55
|
end
|
73
56
|
|
74
|
-
#
|
75
57
|
# Builds a new Branch object for each branch tested from results hash
|
76
58
|
# Then removes branches which have test coverage
|
77
59
|
#
|
@@ -81,9 +63,14 @@ module SolargraphTestCoverage
|
|
81
63
|
Branch.build_from(results).reject(&:covered?)
|
82
64
|
end
|
83
65
|
|
66
|
+
# Builds a range for warnings/errors
|
67
|
+
# @return [Hash]
|
84
68
|
#
|
69
|
+
def range(start_line, start_column, end_line, end_column)
|
70
|
+
Solargraph::Range.from_to(start_line, start_column, end_line, end_column).to_hash
|
71
|
+
end
|
72
|
+
|
85
73
|
# requires the specified testing framework
|
86
|
-
#
|
87
74
|
# @return [Boolean]
|
88
75
|
#
|
89
76
|
def self.require_testing_framework!
|
@@ -97,7 +84,6 @@ module SolargraphTestCoverage
|
|
97
84
|
end
|
98
85
|
end
|
99
86
|
|
100
|
-
#
|
101
87
|
# Only called once, when gem is loaded
|
102
88
|
# Preloads rails via spec/rails_helper if Rails isn't already defined
|
103
89
|
# This gives us a nice speed-boost when running test in child process
|
@@ -117,13 +103,11 @@ module SolargraphTestCoverage
|
|
117
103
|
# @return [Boolean]
|
118
104
|
#
|
119
105
|
def self.preload_rails!
|
120
|
-
return if defined?(Rails)
|
121
|
-
return unless File.file?('spec/rails_helper.rb')
|
106
|
+
return if defined?(Rails) || !File.file?('spec/rails_helper.rb')
|
122
107
|
|
123
|
-
|
124
|
-
$LOAD_PATH.unshift(spec_path) unless $LOAD_PATH.include?(spec_path)
|
108
|
+
$LOAD_PATH.unshift(test_path) unless $LOAD_PATH.include?(test_path)
|
125
109
|
|
126
|
-
require File.
|
110
|
+
require File.join(test_path, 'rails_helper')
|
127
111
|
Coverage.result(stop: true, clear: true) if Coverage.running?
|
128
112
|
|
129
113
|
true
|
@@ -133,5 +117,9 @@ module SolargraphTestCoverage
|
|
133
117
|
|
134
118
|
false
|
135
119
|
end
|
120
|
+
|
121
|
+
def test_path
|
122
|
+
File.join(Dir.pwd, Config.test_dir)
|
123
|
+
end
|
136
124
|
end
|
137
125
|
end
|
@@ -5,65 +5,71 @@ module SolargraphTestCoverage
|
|
5
5
|
class TestCoverageReporter < Solargraph::Diagnostics::Base
|
6
6
|
include Helpers
|
7
7
|
|
8
|
-
#
|
9
8
|
# LSP Diagnostic method
|
10
|
-
#
|
11
9
|
# @return [Array]
|
12
10
|
#
|
13
11
|
def diagnose(source, _api_map)
|
14
12
|
return [] if source.code.empty? || exclude_file?(source.location.filename)
|
15
13
|
return [test_missing_error(source)] unless File.file?(test_file(source))
|
16
14
|
|
17
|
-
|
15
|
+
results = run_test(source)
|
16
|
+
messages(source, results)
|
18
17
|
rescue ChildFailedError
|
19
18
|
[]
|
20
19
|
end
|
21
20
|
|
22
21
|
private
|
23
22
|
|
24
|
-
|
23
|
+
# Compiles all diagnostic messages for source file
|
24
|
+
# @return [Array]
|
25
|
+
#
|
26
|
+
def messages(source, results)
|
25
27
|
messages = [
|
26
|
-
line_warnings(source),
|
27
|
-
branch_warnings(source),
|
28
|
-
test_passing_error(source)
|
28
|
+
line_warnings(source, results),
|
29
|
+
branch_warnings(source, results),
|
30
|
+
test_passing_error(source, results)
|
29
31
|
]
|
30
32
|
|
31
33
|
messages.flatten.compact
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
+
# Creates array of warnings for uncovered lines
|
37
|
+
# @return [Array]
|
38
|
+
#
|
39
|
+
def line_warnings(source, results)
|
40
|
+
uncovered_lines(results).map { |line| line_coverage_warning(source, line) }
|
36
41
|
end
|
37
42
|
|
38
|
-
|
39
|
-
|
43
|
+
# Creates array of warnings for uncovered branches
|
44
|
+
# @return [Array]
|
45
|
+
#
|
46
|
+
def branch_warnings(source, results)
|
47
|
+
uncovered_branches(results).map { |branch| branch_coverage_warning(source, branch.report) }
|
40
48
|
end
|
41
49
|
|
42
|
-
|
43
|
-
|
50
|
+
# Creates array containing error for failing spec
|
51
|
+
# @return [Array]
|
52
|
+
#
|
53
|
+
def test_passing_error(source, results)
|
54
|
+
results[:test_status] ? [] : [test_failing_error(source)]
|
44
55
|
end
|
45
56
|
|
46
|
-
#
|
47
57
|
# Creates LSP warning message for missing line coverage
|
48
|
-
#
|
49
58
|
# @return [Hash]
|
50
59
|
#
|
51
60
|
def line_coverage_warning(source, line)
|
52
61
|
return unless Config.line_coverage?
|
53
62
|
|
54
63
|
{
|
55
|
-
range:
|
64
|
+
range: range(line, 0, line, source.code.lines[line].length),
|
56
65
|
severity: Solargraph::Diagnostics::Severities::WARNING,
|
57
66
|
source: 'TestCoverage',
|
58
67
|
message: 'Line is missing test coverage'
|
59
68
|
}
|
60
69
|
end
|
61
70
|
|
62
|
-
#
|
63
71
|
# Creates LSP warning message for missing branch coverage
|
64
|
-
#
|
65
|
-
# starts counting lines at 1, while the LSP (source.code.line) is an array
|
66
|
-
# with an index starting at 0
|
72
|
+
# Line numbers are off by 1, since Branch Coverage starts counting at 1, not 0
|
67
73
|
#
|
68
74
|
# @return [Hash]
|
69
75
|
#
|
@@ -71,24 +77,21 @@ module SolargraphTestCoverage
|
|
71
77
|
return unless Config.branch_coverage?
|
72
78
|
|
73
79
|
{
|
74
|
-
range:
|
75
|
-
source.code.lines[report[:line] - 1].length).to_hash,
|
80
|
+
range: range(report[:line] - 1, 0, report[:line] - 1, source.code.lines[report[:line] - 1].length),
|
76
81
|
severity: Solargraph::Diagnostics::Severities::WARNING,
|
77
82
|
source: 'TestCoverage',
|
78
83
|
message: "'#{report[:type].upcase}' branch is missing test coverage"
|
79
84
|
}
|
80
85
|
end
|
81
86
|
|
82
|
-
#
|
83
|
-
# Creates LSP error message if test is failing
|
84
|
-
#
|
87
|
+
# Creates LSP error message if test file is failing
|
85
88
|
# @return [Hash]
|
86
89
|
#
|
87
90
|
def test_failing_error(source)
|
88
91
|
return unless Config.test_failing_coverage?
|
89
92
|
|
90
93
|
{
|
91
|
-
range:
|
94
|
+
range: range(0, 0, 0, source.code.lines[0].length),
|
92
95
|
severity: Solargraph::Diagnostics::Severities::ERROR,
|
93
96
|
source: 'TestCoverage',
|
94
97
|
message: 'Unit Test is currently failing.'
|
@@ -104,7 +107,7 @@ module SolargraphTestCoverage
|
|
104
107
|
return unless Config.test_missing_coverage?
|
105
108
|
|
106
109
|
{
|
107
|
-
range:
|
110
|
+
range: range(0, 0, 0, source.code.lines[0].length),
|
108
111
|
severity: Solargraph::Diagnostics::Severities::ERROR,
|
109
112
|
source: 'TestCoverage',
|
110
113
|
message: "No unit test file found at #{test_file(source)}"
|