From 42b941c9c813c7b0f3dcaca753b8beb1799fa1e3 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Sun, 8 Sep 2024 10:46:48 +0200 Subject: [PATCH] Changes cleanup --- lib/cypress_on_rails/configuration.rb | 4 +-- .../vcr/use_cassette_middleware.rb | 33 ++++++++++++------- .../initializers/cypress_on_rails.rb.erb | 2 +- .../spec/cypress/support/on-rails.js | 2 +- .../vcr/use_cassette_middleware_spec.rb | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/cypress_on_rails/configuration.rb b/lib/cypress_on_rails/configuration.rb index 8d10510..c7f42b4 100644 --- a/lib/cypress_on_rails/configuration.rb +++ b/lib/cypress_on_rails/configuration.rb @@ -9,7 +9,7 @@ class Configuration attr_accessor :use_vcr_use_cassette_middleware attr_accessor :before_request attr_accessor :logger - attr_accessor :vcr_use_cassette_mode + attr_accessor :vcr_record_mode # Attributes for backwards compatibility def cypress_folder @@ -37,7 +37,7 @@ def reset self.use_vcr_use_cassette_middleware = false self.before_request = -> (request) {} self.logger = Logger.new(STDOUT) - self.vcr_use_cassette_mode = :new_episodes + self.vcr_record_mode = :new_episodes end def tagged_logged diff --git a/lib/cypress_on_rails/vcr/use_cassette_middleware.rb b/lib/cypress_on_rails/vcr/use_cassette_middleware.rb index dca75b0..79d0f48 100644 --- a/lib/cypress_on_rails/vcr/use_cassette_middleware.rb +++ b/lib/cypress_on_rails/vcr/use_cassette_middleware.rb @@ -10,27 +10,38 @@ def initialize(app, vcr = nil) end def call(env) - vcr_initialized = vcr_defined? && - VCR.configuration.cassette_library_dir.present? && - VCR.configuration.cassette_library_dir != cassette_library_dir - return @app.call(env) if vcr_initialized + return @app.call(env) if should_not_use_vcr? + initialize_vcr + handle_request_with_vcr(env) + end + + private + + def vcr_defined? + defined?(VCR) != nil + end + + def should_not_use_vcr? + vcr_defined? && + VCR.configuration.cassette_library_dir.present? && + VCR.configuration.cassette_library_dir != cassette_library_dir + end + + def initialize_vcr WebMock.enable! if defined?(WebMock) vcr.turn_on! + end + + def handle_request_with_vcr(env) request = Rack::Request.new(env) cassette_name = fetch_request_cassette(request) - vcr.use_cassette(cassette_name, { record: configuration.vcr_use_cassette_mode }) do + vcr.use_cassette(cassette_name, { record: configuration.vcr_record_mode }) do logger.info "Handle request with cassette name: #{cassette_name}" @app.call(env) end end - private - - def vcr_defined? - defined?(VCR) != nil - end - def fetch_request_cassette(request) if request.path.start_with?('/graphql') && request.params.key?('operation') "#{request.path}/#{request.params['operation']}" diff --git a/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb b/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb index da6fd4f..2ec6534 100644 --- a/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb +++ b/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb @@ -8,7 +8,7 @@ if defined?(CypressOnRails) <% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production? # Use this if you want to use use_cassette wrapper instead of manual insert/eject # c.use_vcr_use_cassette_middleware = !Rails.env.production? - # c.vcr_use_cassette_mode = :once # Use to choose VCR record mode (:new_episodes by default) + # c.vcr_record_mode = :once # Use to choose VCR record mode c.logger = Rails.logger # If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc. diff --git a/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js b/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js index adfb3b4..a3bbaef 100644 --- a/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js +++ b/lib/generators/cypress_on_rails/templates/spec/cypress/support/on-rails.js @@ -47,7 +47,7 @@ Cypress.Commands.add('appFixtures', function (options) { // The next is optional // beforeEach(() => { // cy.app('clean') // have a look at cypress/app_commands/clean.rb -// cy.mockGraphQL() // for GraphQL usage, see cypress/support/commands.rb +// cy.mockGraphQL() // for GraphQL usage with use_cassette, see cypress/support/commands.rb // }); // comment this out if you do not want to attempt to log additional info on test fail diff --git a/spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb b/spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb index 8688333..e18c8f9 100644 --- a/spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb +++ b/spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb @@ -31,7 +31,7 @@ def rack_input(json_value) end it 'returns the application response using default request path cassette' do - allow(CypressOnRails).to receive(:configuration).and_return(double(vcr_use_cassette_mode: :once, + allow(CypressOnRails).to receive(:configuration).and_return(double(vcr_record_mode: :once, logger: Logger.new(nil))) env['PATH_INFO'] = '/test/path'