solargraph_test_coverage 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5aba663e3f7dca496c3c527adc1be8024353932ef806cf6f9eed9c70bd44f120
4
- data.tar.gz: d4a031ffbb4e97f2d13eff2552760d3beae56413b1666e6e84edd1c182919c1e
3
+ metadata.gz: 74b455e9459cc2032d23bcb1acf41d4d31f9ee3b8148c67895598ace977a98e9
4
+ data.tar.gz: a0c2942f3973466af1d7782363415e5accd17fb1f65c9fcd6d930dedd8b363c0
5
5
  SHA512:
6
- metadata.gz: 71edec840ad30fb0e2852c0f6ad3928b961631f546d02060500001e4c8e6bc0ff9f5bb3f71f4e4261ed00d483d6d4c6c3e25fc7dca35cd970a386b77ba28df16
7
- data.tar.gz: 1abf9451d0fdcaca5f5f4ec52a6ffb918391bfdc092a09c77778b76dcc137b41c0e17719d7141182a92f472f2505d56f2f9b93fc0547748f681a1f1ba226bd78
6
+ metadata.gz: f8abcd10d5c816586cab20e0b20ca3925df48723d64c9442fe63d8cbcbfacdc51367da617bf77adb158c22653ca08bf8cc92005cd83ecf170216ccb19eefc3ad
7
+ data.tar.gz: 7d4fbef5094bb5ebc477daebfe20b70ca0c9e6fd6d3a04053138bafee245094bfe51b38e05251837a665b275b2a16cfd8f6f33ce4dc3839bc9726000eb499b94
data/README.md CHANGED
@@ -14,7 +14,7 @@ Currently there are four different diagnostics:
14
14
  Add this line to your application's Gemfile:
15
15
 
16
16
  ```ruby
17
- gem 'solargraph_test_coverage'
17
+ gem 'solargraph_test_coverage', require: false
18
18
  ```
19
19
 
20
20
  Then add this to your `.solargraph.yml` config:
@@ -57,6 +57,10 @@ module SolargraphTestCoverage
57
57
  plugin_config['test_framework']
58
58
  end
59
59
 
60
+ def full_test_dir
61
+ File.join(Dir.pwd, test_dir)
62
+ end
63
+
60
64
  def test_dir
61
65
  case test_framework
62
66
  when 'rspec'
@@ -102,9 +106,9 @@ module SolargraphTestCoverage
102
106
  def preload_rails!
103
107
  return if defined?(Rails) || !File.file?('spec/rails_helper.rb')
104
108
 
105
- $LOAD_PATH.unshift(test_path) unless $LOAD_PATH.include?(test_path)
109
+ $LOAD_PATH.unshift(full_test_dir) unless $LOAD_PATH.include?(full_test_dir)
106
110
 
107
- require File.join(test_path, 'rails_helper')
111
+ require File.join(full_test_dir, 'rails_helper')
108
112
  Coverage.result(stop: true, clear: true) if Coverage.running?
109
113
 
110
114
  true
@@ -126,9 +130,5 @@ module SolargraphTestCoverage
126
130
  def workspace_config
127
131
  Solargraph::Workspace::Config.new(Dir.pwd).raw_data.fetch('test_coverage', {})
128
132
  end
129
-
130
- def test_path
131
- FileHelpers.test_path
132
- end
133
133
  end
134
134
  end
@@ -2,64 +2,64 @@
2
2
 
3
3
  module SolargraphTestCoverage
4
4
  module DiagnosticMessages
5
- def line_coverage_warning(source, line)
5
+ def line_coverage_warning(line)
6
6
  return unless Config.line_coverage?
7
7
 
8
8
  {
9
- range: range(line, 0, line, source.code.lines[line].length),
9
+ range: range(line, 0, line, @source.code.lines[line].length),
10
10
  severity: Solargraph::Diagnostics::Severities::WARNING,
11
11
  source: 'TestCoverage',
12
12
  message: 'Line is missing test coverage'
13
13
  }
14
14
  end
15
15
 
16
- def branch_coverage_warning(source, report)
16
+ def branch_coverage_warning(report)
17
17
  return unless Config.branch_coverage?
18
18
 
19
19
  {
20
- range: range(report[:line] - 1, 0, report[:line] - 1, source.code.lines[report[:line] - 1].length),
20
+ range: range(report[:line] - 1, 0, report[:line] - 1, @source.code.lines[report[:line] - 1].length),
21
21
  severity: Solargraph::Diagnostics::Severities::WARNING,
22
22
  source: 'TestCoverage',
23
23
  message: "'#{report[:type].upcase}' branch is missing test coverage"
24
24
  }
25
25
  end
26
26
 
27
- def test_failing_error(source)
27
+ def test_failing_error
28
28
  return unless Config.test_failing_coverage?
29
29
 
30
30
  {
31
- range: range(0, 0, 0, source.code.lines[0].length),
31
+ range: range(0, 0, 0, @source.code.lines[0].length),
32
32
  severity: Solargraph::Diagnostics::Severities::ERROR,
33
33
  source: 'TestCoverage',
34
34
  message: 'Unit Test is currently failing.'
35
35
  }
36
36
  end
37
37
 
38
- def test_missing_error(source)
38
+ def test_missing_error
39
39
  return unless Config.test_missing_coverage?
40
40
 
41
41
  {
42
- range: range(0, 0, 0, source.code.lines[0].length),
42
+ range: range(0, 0, 0, @source.code.lines[0].length),
43
43
  severity: Solargraph::Diagnostics::Severities::HINT,
44
44
  source: 'TestCoverage',
45
- message: "No test file found at '#{FileHelpers.test_file(source).sub("#{Dir.pwd}/", '')}'"
45
+ message: "No test file found at '#{FileHelpers.relative_test_file(@filename)}'"
46
46
  }
47
47
  end
48
48
 
49
- def example_failing_error(example, source)
49
+ def example_failing_error(example)
50
50
  return unless Config.example_failing_coverage?
51
51
 
52
52
  {
53
- range: range(example[:line_number], 0, example[:line_number], source.code.lines[example[:line_number]].length),
53
+ range: range(example[:line_number], 0, example[:line_number], @source.code.lines[example[:line_number]].length),
54
54
  severity: Solargraph::Diagnostics::Severities::ERROR,
55
55
  source: 'ExampleStatus',
56
56
  message: example[:message]
57
57
  }
58
58
  end
59
59
 
60
- def debug_message(exception, source)
60
+ def debug_message(exception)
61
61
  {
62
- range: range(0, 0, 0, source.code.lines[0].length),
62
+ range: range(0, 0, 0, @source.code.lines[0].length),
63
63
  severity: Solargraph::Diagnostics::Severities::ERROR,
64
64
  source: 'SolargraphTestCoverage Plugin',
65
65
  message: "DEBUG: (ChildFailedError) #{exception.message}"
@@ -8,13 +8,16 @@ module SolargraphTestCoverage
8
8
  include DiagnosticMessages
9
9
 
10
10
  def diagnose(source, _api_map)
11
- if source.code.empty? || using_debugger?(source) || !is_test_file?(source) || is_test_support_file?(source)
12
- return []
13
- end
11
+ @source = source
12
+ @filename = source.location.filename
14
13
 
15
- example_failing_errors(source, run_test(source, source.location.filename))
14
+ return [] if source.code.empty? || using_debugger? || !in_test_dir? || test_support_file?
15
+
16
+ @results = run_test(@filename)
17
+
18
+ example_failing_errors
16
19
  rescue ChildFailedError => e
17
- Config.debug? ? [debug_message(e, source)] : []
20
+ Config.debug? ? [debug_message(e)] : []
18
21
  end
19
22
  end
20
23
  end
@@ -4,19 +4,27 @@ module SolargraphTestCoverage
4
4
  module FileHelpers
5
5
  extend self
6
6
 
7
- def test_file(source)
8
- relative_filepath = source.location.filename.sub(Dir.pwd, '').split('/').reject(&:empty?)
7
+ def test_file(filename)
8
+ return filename if test_file?(filename)
9
9
 
10
- if relative_filepath.first == Config.test_dir && relative_filepath.last.end_with?(Config.test_file_suffix)
11
- return source.location.filename
12
- end
10
+ path = relative_path(filename).split('/')
11
+ path[0] = Config.test_dir
12
+ path[-1] = path.last.sub(/\.rb$/, Config.test_file_suffix)
13
13
 
14
- relative_filepath[0] = Config.test_dir
15
- File.join(Dir.pwd, relative_filepath.join('/')).sub('.rb', Config.test_file_suffix)
14
+ File.join(Dir.pwd, path.join('/'))
16
15
  end
17
16
 
18
- def test_path
19
- File.join(Dir.pwd, Config.test_dir)
17
+ def relative_path(path)
18
+ path.sub("#{Dir.pwd}/", '')
19
+ end
20
+
21
+ def relative_test_file(filename)
22
+ relative_path test_file(filename)
23
+ end
24
+
25
+ def test_file?(filename)
26
+ filename.start_with?(Config.full_test_dir) &&
27
+ filename.end_with?(Config.test_file_suffix)
20
28
  end
21
29
  end
22
30
  end
@@ -3,26 +3,24 @@
3
3
  module SolargraphTestCoverage
4
4
  # Some guard functions for the diagnostics
5
5
  module ReporterGuards
6
- def has_test_file?(source)
7
- File.file? FileHelpers.test_file(source)
6
+ def test_file_exists?
7
+ File.file? FileHelpers.test_file(@filename)
8
8
  end
9
9
 
10
- def is_test_file?(source)
11
- source.location.filename.start_with? FileHelpers.test_path
10
+ def in_test_dir?
11
+ @filename.start_with? Config.full_test_dir
12
12
  end
13
13
 
14
- def is_test_support_file?(source)
15
- is_test_file?(source) && !source.location.filename.end_with?(Config.test_file_suffix)
14
+ def test_support_file?
15
+ in_test_dir? && !@filename.end_with?(Config.test_file_suffix)
16
16
  end
17
17
 
18
- def exclude_file?(source)
19
- Config.exclude_paths.any? { |path| source.location.filename.sub(Dir.pwd, '').include? path }
18
+ def exclude_file?
19
+ Config.exclude_paths.any? { |path| FileHelpers.relative_path(@filename).include? path }
20
20
  end
21
21
 
22
- def using_debugger?(source)
23
- source.code.include?('binding.pry') ||
24
- source.code.include?('byebug') ||
25
- source.code.include?('debugger')
22
+ def using_debugger?
23
+ @source.code.match?(/(binding\.pry|byebug|debugger)/)
26
24
  end
27
25
  end
28
26
  end
@@ -4,32 +4,33 @@ module SolargraphTestCoverage
4
4
  # Some helper functions for the diagnostics
5
5
  module ReporterHelpers
6
6
  # @return [Hash]
7
- def run_test(source, test_file)
7
+ def run_test(test_file)
8
8
  ForkProcess.call do
9
9
  Coverage.start(lines: true, branches: true)
10
10
  runner = TestRunner.with(test_file).run!
11
+ extra = { test_status: runner.passed?, failed_examples: runner.failed_examples }
11
12
 
12
- Coverage.result
13
- .fetch(source.location.filename, {})
14
- .merge({ test_status: runner.passed?, failed_examples: runner.failed_examples })
13
+ Coverage.result.fetch(@filename, {}).merge(extra)
15
14
  end
16
15
  end
17
16
 
18
- def line_warnings(source, results)
19
- uncovered_lines(results).map { |line| line_coverage_warning(source, line) }
17
+ def branch_warnings
18
+ Branch.build_from(@results)
19
+ .reject(&:covered?)
20
+ .map { |branch| branch_coverage_warning(branch.report) }
20
21
  end
21
22
 
22
- def branch_warnings(source, results)
23
- uncovered_branches(results).map { |branch| branch_coverage_warning(source, branch.report) }
23
+ def test_passing_error
24
+ @results[:test_status] ? [] : [test_failing_error]
24
25
  end
25
26
 
26
- def test_passing_error(source, results)
27
- results[:test_status] ? [] : [test_failing_error(source)]
27
+ def example_failing_errors
28
+ @results.fetch(:failed_examples, [])
29
+ .map { |example| example_failing_error(example) }
28
30
  end
29
31
 
30
- def example_failing_errors(source, results)
31
- results.fetch(:failed_examples, [])
32
- .map { |example| example_failing_error(example, source) }
32
+ def line_warnings
33
+ uncovered_lines.map { |line| line_coverage_warning(line) }
33
34
  end
34
35
 
35
36
  # Adapted from SingleCov
@@ -38,17 +39,13 @@ module SolargraphTestCoverage
38
39
  #
39
40
  # [nil, 1, 0, 1, 0] -> [3, 5]
40
41
  # Returns array of line numbers with 0 coverage
41
- def uncovered_lines(results)
42
- return [] unless results[:lines]
42
+ def uncovered_lines
43
+ return [] unless @results[:lines]
43
44
 
44
- results[:lines].each_with_index
45
- .select { |c, _| c&.zero? }
46
- .map { |_, i| i }
47
- .compact
48
- end
49
-
50
- def uncovered_branches(results)
51
- Branch.build_from(results).reject(&:covered?)
45
+ @results[:lines].each_with_index
46
+ .select { |c, _| c&.zero? }
47
+ .map { |_, i| i }
48
+ .compact
52
49
  end
53
50
  end
54
51
  end
@@ -8,18 +8,17 @@ module SolargraphTestCoverage
8
8
  include DiagnosticMessages
9
9
 
10
10
  def diagnose(source, _api_map)
11
- return [] if source.code.empty? || using_debugger?(source) || exclude_file?(source) || is_test_file?(source)
12
- return [test_missing_error(source)] unless has_test_file?(source)
11
+ @source = source
12
+ @filename = source.location.filename
13
13
 
14
- results = run_test(source, FileHelpers.test_file(source))
14
+ return [] if source.code.empty? || using_debugger? || exclude_file? || in_test_dir?
15
+ return [test_missing_error] unless test_file_exists?
15
16
 
16
- [
17
- line_warnings(source, results),
18
- branch_warnings(source, results),
19
- test_passing_error(source, results)
20
- ].flatten.compact
17
+ @results = run_test(FileHelpers.test_file(@filename))
18
+
19
+ [line_warnings, branch_warnings, test_passing_error].flatten.compact
21
20
  rescue ChildFailedError => e
22
- Config.debug? ? [debug_message(e, source)] : []
21
+ Config.debug? ? [debug_message(e)] : []
23
22
  end
24
23
  end
25
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolargraphTestCoverage
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solargraph_test_coverage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Kolkey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-23 00:00:00.000000000 Z
11
+ date: 2021-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solargraph