Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename CypressDev to CypressOnRails #19

Merged
merged 4 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.4.0
* Accept an options argument for scenarios

### Tasks
* renamed to CypressOnRails

## 1.3.0
* send any arguments to simple rails factory, not only hashes
### Tasks
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CypressDev
# CypressOnRails

[![Build Status](https://travis-ci.org/grantspeelman/cypress-on-rails.svg?branch=master)](https://travis-ci.org/grantspeelman/cypress-on-rails)

Expand Down Expand Up @@ -33,17 +33,17 @@ end

The generate the boilerplate code using:
```
bin/rails g cypress_dev:install
bin/rails g cypress_on_rails:install
# if you have/want a different cypress folder (default is spec/cypress)
bin/rails g cypress_dev:install --cypress_folder=test/cypress
bin/rails g cypress_on_rails:install --cypress_folder=test/cypress
# if you want to install cypress with npm
bin/rails g cypress_dev:install --install_cypress_with=npm
bin/rails g cypress_on_rails:install --install_cypress_with=npm
```

The generator adds the following files/directory to your application:
* `config/initializers/cypress_dev` used to configure CypressDev
* `config/initializers/cypress_on_rails` used to configure CypressDev
* `spec/cypress/integrations/` contains your cypress tests
* `spec/cypress/support/on-rails.js` contains CypressDev support code
* `spec/cypress/app_commands/scenarios/` contains your CypressDev scenario definitions
Expand Down Expand Up @@ -76,9 +76,9 @@ You can run your [factory_bot](https://github.com/thoughtbot/factory_bot) direct

```ruby
# spec/cypress/app_commands/factory_bot.rb
require 'cypress_dev/smart_factory_wrapper'
require 'cypress_on_rails/smart_factory_wrapper'

CypressDev::SmartFactoryWrapper.configure(
CypressOnRails::SmartFactoryWrapper.configure(
always_reload: !Rails.configuration.cache_classes,
factory: FactoryBot,
files: Dir['./spec/factories/**/*.rb']
Expand Down Expand Up @@ -141,7 +141,7 @@ You define a scenario in the `spec/cypress/app_commands/scenarios` directory:
Profile.create name: "Cypress Hill"

# or if you have factory_bot enabled in your cypress_helper
CypressDev::SmartFactoryWrapper.create(:profile, name: "Cypress Hill")
CypressOnRails::SmartFactoryWrapper.create(:profile, name: "Cypress Hill")
```

Then reference the scenario in your test:
Expand Down Expand Up @@ -183,17 +183,17 @@ describe('My First Test', function() {

## Usage with other rack applications

Add CypressDev to your config.ru
Add CypressOnRails to your config.ru

```ruby
# an example config.ru
require File.expand_path('my_app', File.dirname(__FILE__))

require 'cypress_dev/middleware'
CypressDev.configure do |c|
require 'cypress_on_rails/middleware'
CypressOnRails.configure do |c|
c.cypress_folder = File.expand_path("#{__dir__}/test/cypress")
end
use CypressDev::Middleware
use CypressOnRails::Middleware

run MyApp
```
Expand All @@ -202,7 +202,7 @@ add the following file to cypress

```js
// test/cypress/support/on-rails.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the file itself be called cypress-on-rails?

// CypressDev: dont remove these command
// CypressOnRails: dont remove these command
Cypress.Commands.add('appCommands', function (body) {
cy.request({
method: 'POST',
Expand All @@ -224,7 +224,7 @@ Cypress.Commands.add('appScenario', function (name) {
Cypress.Commands.add('appFactories', function (options) {
cy.app('factory_bot', options)
});
// CypressDev: end
// CypressOnRails: end

// The next is optional
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/cypress/*_spec.rb'
t.pattern = 'spec/cypress_on_rails/*_spec.rb'
end

task default: %w[spec build]
5 changes: 3 additions & 2 deletions cypress-on-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
require "cypress_dev/version"
require "cypress_on_rails/version"

Gem::Specification.new do |s|
s.name = "cypress-on-rails"
s.version = CypressDev::VERSION
s.version = CypressOnRails::VERSION
s.author = ["miceportal team", 'Grant Petersen-Speelman']
s.email = ["[email protected]", '[email protected]']
s.homepage = "http://github.com/grantspeelman/cypress-on-rails"
s.summary = "Integrates cypress with rails or rack applications"
s.description = "Integrates cypress with rails or rack applications"
s.post_install_message = 'The CypressDev constant is being deprecated and will be completely removed and replaced with CypressOnRails.'
s.rubyforge_project = s.name
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {spec}/*`.split("\n")
Expand Down
6 changes: 5 additions & 1 deletion lib/cypress-on-rails.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'cypress_dev'
require 'cypress_on_rails/version'
require 'cypress_on_rails/configuration'
require_relative './cypress_on_rails/railtie' if defined?(Rails)

# maintain backward compatibility
CypressDev = CypressOnRails
Cypress = CypressDev
2 changes: 1 addition & 1 deletion lib/cypress/smart_factory_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'cypress-on-rails'
require 'cypress_dev/smart_factory_wrapper'
require 'cypress_on_rails/smart_factory_wrapper'
# for backward compatibility
5 changes: 1 addition & 4 deletions lib/cypress_dev.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
require 'cypress_dev/version'
require 'cypress_dev/configuration'
require_relative './cypress_dev/railtie' if defined?(Rails)

require 'cypress-on-rails'
67 changes: 2 additions & 65 deletions lib/cypress_dev/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,2 @@
require 'json'
require 'rack'
require 'cypress_dev/configuration'
require 'cypress_dev/command_executor'

module CypressDev
# Middleware to handle cypress commands and eval
class Middleware
def initialize(app, command_executor = CommandExecutor, file = ::File)
@app = app
@command_executor = command_executor
@file = file
end

def call(env)
request = Rack::Request.new(env)
if request.path.start_with?('/__cypress__/command')
configuration.tagged_logged { handle_command(request) }
else
@app.call(env)
end
end

private

def configuration
CypressDev.configuration
end

def logger
configuration.logger
end

Command = Struct.new(:name, :options, :cypress_folder) do
# @return [Array<Cypress::Middleware::Command>]
def self.from_body(body, configuration)
if body.is_a?(Array)
command_params = body
else
command_params = [body]
end
command_params.map do |params|
new(params.fetch('name'), params['options'], configuration.cypress_folder)
end
end

def file_path
"#{cypress_folder}/app_commands/#{name}.rb"
end
end

def handle_command(req)
body = JSON.parse(req.body.read)
logger.info "handle_command: #{body}"
commands = Command.from_body(body, configuration)
missing_command = commands.find {|command| !@file.exists?(command.file_path) }
if missing_command.nil?
commands.each { |command| @command_executor.load(command.file_path, command.options) }
[201, {}, ['success']]
else
[404, {}, ["could not find command file: #{missing_command.file_path}"]]
end
end
end
end
warn "cypress_dev is being deprecated, please require \"cypress_on_rails/middleware\" instead"
require 'cypress_on_rails/middleware'
93 changes: 2 additions & 91 deletions lib/cypress_dev/smart_factory_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,2 @@
require 'cypress_dev/configuration'
require 'cypress_dev/simple_rails_factory'

module CypressDev
class SmartFactoryWrapper
module FactoryCleaner
def self.clean(f = FactoryBot)
f.factories.clear if f.respond_to?(:factories)
f.traits.clear if f.respond_to?(:traits)
f.callbacks.clear if f.respond_to?(:callbacks)
f.sequences.clear if f.respond_to?(:sequences)
end
end

def self.instance
@instance ||= new(files: [], factory: SimpleRailsFactory)
end

def self.configure(files:, factory:, always_reload: true)
@instance = new(files: files, factory: factory, always_reload: always_reload)
end

def self.create(*args)
instance.create(*args)
end

def self.create_list(*args)
instance.create_list(*args)
end

# @return [Array]
attr_accessor :factory
attr_accessor :always_reload

def initialize(files:, factory:, always_reload: false,
factory_cleaner: FactoryCleaner, kernel: Kernel, file_system: File,
dir_system: Dir)
self.files = files
self.factory = factory
self.always_reload = always_reload
@kernel = kernel
@file_system = file_system
@factory_cleaner = factory_cleaner
@latest_mtime = nil
@dir_system = dir_system
end

def create(*args)
load_files
factory.create(*args)
end

def create_list(*args)
load_files
factory.create_list(*args)
end

private

# @param [String,Array] arg
def files=(array)
array = [array] if array.is_a?(String)
@dir_array = array
end

# @return [Array<String>]
def files
Dir[*@dir_array]
end

def logger
CypressDev.configuration.logger
end

def load_files
current_latest_mtime = files.map{|file| @file_system.mtime(file) }.max
return unless should_reload?(current_latest_mtime)
logger.info 'Loading Factories'
@latest_mtime = current_latest_mtime
@factory_cleaner.clean(factory)
files.each do |file|
logger.debug "-- Loading: #{file}"
@kernel.load(file)
end
end

def should_reload?(current_latest_mtime)
@always_reload || @latest_mtime.nil? || @latest_mtime < current_latest_mtime
end
end
end
warn "cypress_dev is being deprecated, please require \"cypress_on_rails/smart_factory_wrapper\" instead"
require 'cypress_on_rails/smart_factory_wrapper'
3 changes: 0 additions & 3 deletions lib/cypress_dev/version.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'cypress_dev/configuration'
require 'cypress_on_rails/configuration'

module CypressDev
module CypressOnRails
# loads and evals the command files
class CommandExecutor
def self.load(file,command_options = nil)
Expand All @@ -27,7 +27,7 @@ def self.logger
end

def self.configuration
CypressDev.configuration
CypressOnRails.configuration
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'logger'

module CypressDev
module CypressOnRails
class Configuration
attr_accessor :cypress_folder
attr_accessor :use_middleware
Expand Down
Loading