Class: Capydash::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/capydash/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/capydash/install_generator.rb', line 10

def create_initializer
  create_file "config/initializers/capydash.rb", <<~RUBY
    require 'capydash'

    # Configure CapyDash
    CapyDash.configure do |config|
      config.port = 4000
      config.screenshot_path = "tmp/capydash_screenshots"
    end

    # Subscribe to events for test data collection
    CapyDash::EventEmitter.subscribe do |event|
      # Collect test data for report generation
      CapyDash::TestDataAggregator.handle_event(event)
    end
  RUBY
end

#create_rake_tasksObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/capydash/install_generator.rb', line 28

def create_rake_tasks
  create_file "lib/tasks/capydash.rake", <<~RUBY
    namespace :capydash do
      desc "Generate static HTML test report"
      task :report => :environment do
        CapyDash::ReportGenerator.generate_report
      end

      desc "Start local server to view static HTML report"
      task :server => :environment do
        CapyDash::DashboardServer.start
      end
    end
  RUBY
end

#show_instructionsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/generators/capydash/install_generator.rb', line 107

def show_instructions
  say "\n" + "="*60, :green
  say "CapyDash has been successfully installed!", :green
  say "="*60, :green
  say "\nNext steps:", :yellow
  say "1. Run your tests: bundle exec rails test"
  say "2. Generate report: bundle exec rake capydash:report"
  say "3. View report: open capydash_report/index.html"
  say "\nImportant:", :yellow
  say "- CapyDash only captures system tests that use Capybara methods (visit, click, fill_in, etc.)"
  say "- Unit tests and integration tests without Capybara won't appear in the report"
  say "- Works with parallel testing - no configuration needed"
  say "\nFor more information, see the README.md file."
  say "="*60, :green
end

#update_test_helperObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/capydash/install_generator.rb', line 44

def update_test_helper
  test_helper_path = "test/test_helper.rb"

  if File.exist?(test_helper_path)
    # Read existing test helper
    content = File.read(test_helper_path)

    # Check if CapyDash is already configured
    unless content.include?("require 'capydash'")
      # Add CapyDash configuration
      capydash_config = <<~RUBY

        # CapyDash configuration
        require 'capydash'

        # Start test run data collection
        CapyDash::TestDataCollector.start_test_run

        # Hook into test execution to set current test name and manage test runs
        module CapyDash
          module TestHooks
            def run(&block)
              # Set the current test name for CapyDash
              CapyDash.current_test = self.name

              # Start test run data collection if not already started
              CapyDash::TestDataAggregator.start_test_run unless CapyDash::TestDataAggregator.instance_variable_get(:@current_run)

              super
            end
          end
        end

        # Apply the hook to the test case
        class ActiveSupport::TestCase
          prepend CapyDash::TestHooks
        end

        # Hook to finish test run when all tests are done
        Minitest.after_run do
          CapyDash::TestDataCollector.finish_test_run
          CapyDash::TestDataAggregator.finish_test_run
        end
      RUBY

      # Insert after the last require statement
      if content.match(/require.*\n/)
        content = content.gsub(/(require.*\n)/, "\\1#{capydash_config}")
      else
        content = capydash_config + content
      end

      File.write(test_helper_path, content)
      say "Updated test/test_helper.rb with CapyDash configuration"
    else
      say "CapyDash already configured in test/test_helper.rb", :yellow
    end
  else
    say "test/test_helper.rb not found. Please add CapyDash configuration manually.", :red
  end
end