-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
12 changed files
with
305 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
spec/generator_specs/e2e_template/files/app/controllers/home_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class HomeController < ApplicationController | ||
def index | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/generator_specs/e2e_template/files/app/javascript/components/App.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
spec/generator_specs/e2e_template/files/app/javascript/packs/application.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
2 changes: 2 additions & 0 deletions
2
spec/generator_specs/e2e_template/files/app/views/home/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
spec/generator_specs/e2e_template/files/app/views/layouts/application.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Rails.application.routes.draw do | ||
get "home/index" | ||
end |
13 changes: 13 additions & 0 deletions
13
spec/generator_specs/e2e_template/files/spec/system/test_react_component_renders_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |