Skip to content

Commit

Permalink
Add generator test (#300)
Browse files Browse the repository at this point in the history
* Add base rails dummy app for test

* Add test for bin/setup and peer dependencies

* Add e2e test after installation

---------

Co-authored-by: Judah Meek <judah.meek@gmail.com>
  • Loading branch information
ahangarha and Judahmeek committed Jun 10, 2023
1 parent d192dfb commit 83c42e8
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 10 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/generator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Generator specs

on: [push]

jobs:
test:
name: Generator specs
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
ruby: ['2.6', '2.7', '3.0']
gemfile:
- gemfiles/Gemfile-rails.6.0.x
- gemfiles/Gemfile-rails.6.1.x
- gemfiles/Gemfile-rails.7.0.x
# Uncomment the following line only to ensure compatibility with the
# upcomming Rails versions, maybe before a release.
#- gemfiles/Gemfile-rails-edge
exclude:
- ruby: 2.6
os: ubuntu-latest
gemfile: gemfiles/Gemfile-rails.7.0.x
- ruby: 2.6
os: ubuntu-latest
gemfile: gemfiles/Gemfile-rails-edge
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}

steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install
- run: bundle exec rake run_spec:generator
14 changes: 8 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ For this, you need `yalc` to be installed on your local machine
bundle exec rake run_spec:dummy
```

## Testing the generator
If you change the generator, check that install instructions work.
#### 4.6 Testing the installer
To ensure that your installer works as expected, either you can run `bundle exec rake run_spec:install`, or take the following manual testing steps:

1. Update the Gemfile so that gem "shakapacker" has a line like this, pointing to your install of Shakapacker
1. Update the `Gemfile` so that gem `shakapacker` has a line like this, pointing to your developing Shakapacker:
```ruby
gem 'shakapacker', path: "~/shakacode/forks/shakapacker"
gem 'shakapacker', path: "relative_or_absolute_path_to_the_gem"
```
2. `bundle`
3. Run the generator to confirm that you got the right changes.
2. Run `bundle install` to install the updated gem.
3. Run `bundle exec rails shakapacker:install` to confirm that you got the right changes.

**Note:** Ensure that you use bundle exec otherwise the installed shakapacker gem will run and not the one you are working on.

## Find existing issues
You may look at the issues list to find existing known issues to be addressed. In this, we recommend looking at closed issues, particularly with the "[help wanted](https://github.com/shakacode/shakapacker/issues?q=is%3Aissue+label%3A%22help+wanted%22+is%3Aclosed+)" label.
13 changes: 9 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace :run_spec do
desc "Run shakapacker specs"
task :gem do
puts "Running Shakapacker gem specs"
system("bundle exec rspec spec/shakapacker/*_spec.rb")
sh("bundle exec rspec spec/shakapacker/*_spec.rb")
end

desc "Run backward compatibility specs"
task :gem_bc do
puts "Running Shakapacker gem specs for backward compatibility"
system("bundle exec rspec spec/backward_compatibility_specs/*_spec_bc.rb")
sh("bundle exec rspec spec/backward_compatibility_specs/*_spec_bc.rb")
end

desc "Run specs in the dummy app"
Expand All @@ -35,12 +35,17 @@ namespace :run_spec do
end
end

desc "Run generator specs"
task :generator do
sh("bundle exec rspec spec/generator_specs/*_spec.rb")
end

desc "Run all specs"
task all_specs: %i[gem gem_bc dummy] do
task all_specs: %i[gem gem_bc dummy generator] do
puts "Completed all RSpec tests"
end
end

def sh_in_dir(dir, *shell_commands)
shell_commands.flatten.each { |shell_command| sh %(cd #{dir} && #{shell_command.strip}) }
Shakapacker::Utils::Misc.sh_in_dir(dir, *shell_commands)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HomeController < ApplicationController
def index
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'

function App() {
return (
<div className="App">
<h1>App component</h1>
<p>Text from react component!</p>
</div>
)
}

export default App
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from '../components/App'

const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Sh7InstallTest</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application" %>
<%= javascript_pack_tag "application" %>
</head>

<body>
<%= yield %>
<div id="root"></div>
</body>
</html>
3 changes: 3 additions & 0 deletions spec/generator_specs/e2e_template/files/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
get "home/index"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"

RSpec.describe "home/index", type: :system, js: true do
before do
driven_by(:selenium_headless)
end

it "renders the App component" do
visit "home/index"

expect(page.body).to match "App component"
end
end
21 changes: 21 additions & 0 deletions spec/generator_specs/e2e_template/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# install react
system("yarn add react react-dom @babel/preset-react")

# update webpack presets for react
package_json_path = Rails.root.join("./package.json")
insert_into_file(
package_json_path,
%( "@babel/preset-react",\n),
after: /"presets": \[\n/
)

# install rspec-rails
system("bundle add rspec-rails --group development,test")
system("bundle exec rails g rspec:install")

# copy files
directory(
Rails.root.join("../e2e_template/files"),
Rails.root,
force: true
)
169 changes: 169 additions & 0 deletions spec/generator_specs/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
require "pathname"
require "rake"
require "json"
require "shakapacker/utils/misc"
require "shakapacker/utils/version_syntax_converter"

GEM_ROOT = Pathname.new(File.expand_path("../../..", __FILE__))
SPEC_PATH = Pathname.new(File.expand_path("../", __FILE__))
BASE_RAILS_APP_PATH = SPEC_PATH.join("base-rails-app")
TEMP_RAILS_APP_PATH = SPEC_PATH.join("temp-rails-app")

describe "Generator" do
before :all do
# Don't use --skip-git because we want .gitignore file to exist in the project
sh_in_dir(SPEC_PATH, %(
rails new base-rails-app --skip-javascript --skip-bundle --skip-spring
rm -rf base-rails-app/.git
))

Bundler.with_unbundled_env do
sh_in_dir(BASE_RAILS_APP_PATH, %(
gem update bundler
bundle add shakapacker --path "#{GEM_ROOT}"
))
end
end

after :all do
Dir.chdir(SPEC_PATH)
FileUtils.rm_rf(BASE_RAILS_APP_PATH)
end

describe "shakapacker:install" do
context "in a normal Rails project" do
before :all do
sh_in_dir(SPEC_PATH, "cp -r #{BASE_RAILS_APP_PATH} #{TEMP_RAILS_APP_PATH}")

Bundler.with_unbundled_env do
sh_in_dir(TEMP_RAILS_APP_PATH, "FORCE=true bundle exec rails shakapacker:install")
end
end

after :all do
Dir.chdir(SPEC_PATH)
FileUtils.rm_rf(TEMP_RAILS_APP_PATH)
end

it "creates `config/shakapacker.yml`" do
config_file_relative_path = "config/shakapacker.yml"
actual_content = read(path_in_the_app(config_file_relative_path))
expected_content = read(path_in_the_gem(config_file_relative_path))

expect(actual_content).to eq expected_content
end

it "replaces package.json with template file" do
actual_content = read(path_in_the_app("package.json"))

expect(actual_content).to match /"name": "app",/
end

it "creates webpack config directory and its files" do
expected_files = [
"webpack.config.js"
]

Dir.chdir(path_in_the_app("config/webpack")) do
exisiting_files_in_config_webpack_dir = Dir.glob("*")
expect(exisiting_files_in_config_webpack_dir).to eq expected_files
end
end

it "adds binstubs" do
expected_binstubs = []
Dir.chdir(File.join(GEM_ROOT, "lib/install/bin")) do
expected_binstubs = Dir.glob("bin/*")
end

Dir.chdir(File.join(TEMP_RAILS_APP_PATH, "bin")) do
actual_binstubs = Dir.glob("*")
expect(actual_binstubs).to include(*expected_binstubs)
end
end

it "modifies .gitignore" do
actual_content = read(path_in_the_app(".gitignore"))

expect(actual_content).to match ".yarn-integrity"
end

it 'adds <%= javascript_pack_tag "application" %>' do
actual_content = read(path_in_the_app("app/views/layouts/application.html.erb"))

expect(actual_content).to match '<%= javascript_pack_tag "application" %>'
end

it "updates `bin/setup" do
setup_file_content = read(path_in_the_app("bin/setup"))
expect(setup_file_content).to match %r(^\s*system!\(['"]bin/yarn['"]\))
end

it "adds relevant shakapacker version in package.json depending on gem version," do
npm_version = Shakapacker::Utils::VersionSyntaxConverter.new.rubygem_to_npm(Shakapacker::VERSION)

actual_content = read(path_in_the_app("package.json"))

expect(actual_content).to match /"shakapacker": "#{npm_version}",/
end

it "adds Shakapacker peer dependencies to package.json" do
package_json = JSON.parse(File.read(path_in_the_app("package.json")))
actual_dependencies = package_json["dependencies"]&.keys

expected_dependencies = %w(
@babel/core
@babel/plugin-transform-runtime
@babel/preset-env
@babel/runtime
babel-loader
compression-webpack-plugin
terser-webpack-plugin
webpack
webpack-assets-manifest
webpack-cli
webpack-merge
)

expect(actual_dependencies).to include(*expected_dependencies)
end

it "adds Shakapacker peer dev dependencies to package.json" do
package_json = JSON.parse(File.read(path_in_the_app("package.json")))
actual_dev_dependencies = package_json["devDependencies"]&.keys

expected_dev_dependencies = %w(
webpack-dev-server
)

expect(actual_dev_dependencies).to include(*expected_dev_dependencies)
end

context "with a basic react app setup" do
it "passes the test for rendering react component on the page" do
Bundler.with_unbundled_env do
sh_in_dir(TEMP_RAILS_APP_PATH, "./bin/rails app:template LOCATION=../e2e_template/template.rb")
expect(sh_in_dir(TEMP_RAILS_APP_PATH, "bundle exec rspec")).to be_truthy
end
end
end
end
end

private
def path_in_the_app(relative_path = nil)
Pathname.new(File.join([TEMP_RAILS_APP_PATH, relative_path].compact))
end

def path_in_the_gem(relative_path = nil)
Pathname.new(File.join([GEM_ROOT, "lib/install" , relative_path].compact))
end

def read(path)
File.read(path)
end

def sh_in_dir(dir, *shell_commands)
Shakapacker::Utils::Misc.sh_in_dir(dir, *shell_commands)
end
end

0 comments on commit 83c42e8

Please sign in to comment.