Class: RailsCodeAuditor::SimpleCovRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_code_auditor/simplecov_runner.rb

Class Method Summary collapse

Class Method Details

.runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails_code_auditor/simplecov_runner.rb', line 6

def self.run
  setup_file = ".simplecov_setup.rb"

  unless simplecov_installed_in_project?
    return {
      status: "Skipped",
      success: false,
      error: "simplecov gem not found in the target Rails app",
      details: "simplecov gem not found in the target Rails app"
    }
  end

  File.write(setup_file, "    require 'simplecov'\n\n    SimpleCov.start 'rails' do\n      enable_coverage :branch\n      add_filter '/test/'\n      add_filter '/spec/'\n    end\n\n    SimpleCov.command_name ENV.fetch(\"SIMPLECOV_COMMAND_NAME\", \"Rails Tests\")\n    puts \"[SimpleCov] started\"\n  RUBY\n\n  test_cmd =\n    if Dir.exist?(\"spec\")\n      \"SIMPLECOV_COMMAND_NAME='RSpec Tests' bundle exec ruby -r./\#{setup_file} -S rspec\"\n    elsif Dir.exist?(\"test\")\n      \"SIMPLECOV_COMMAND_NAME='Rails Tests' bundle exec ruby -r./\#{setup_file} -S rails test\"\n    else\n      return { status: \"Coverage: 0%\", error: \"No test directory found\", success: false,\n               details: \"No test directory found\" }\n    end\n\n  success = system(test_cmd)\n\n  coverage_path = \"coverage/.last_run.json\"\n  if File.exist?(coverage_path)\n    json = JSON.parse(File.read(coverage_path))\n    percent = json[\"result\"][\"covered_percent\"] || json[\"result\"][\"line\"]\n    if percent\n      percent = percent.round(2)\n      {\n        status: \"Coverage: \#{percent}%\",\n        success: success,\n        raw_result: json,\n        details: json\n      }\n    else\n      {\n        status: \"Coverage: N/A\",\n        success: false,\n        error: \"Coverage percentage not found in report\",\n        details: json\n      }\n    end\n  else\n    {\n      status: \"Coverage: 0%\",\n      success: success,\n      error: \"Coverage file not generated\",\n      details: \"Coverage file not generated\"\n    }\n  end\nensure\n  File.delete(setup_file) if setup_file && File.exist?(setup_file)\nend\n")

.simplecov_installed_in_project?Boolean



75
76
77
78
79
80
# File 'lib/rails_code_auditor/simplecov_runner.rb', line 75

def self.simplecov_installed_in_project?
  spec = Bundler.locked_gems.specs.find { |s| s.name == "simplecov" }
  !spec.nil?
rescue StandardError
  false
end