Skip to content

Add docker test env #23

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

Merged
merged 12 commits into from
Jan 6, 2018
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
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ AllCops:
- 'spec/*_helper.rb'
- 'Gemfile'
- 'Rakefile'
- 'Vagrantfile'

Documentation:
Enabled: false
30 changes: 23 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2015-10-29 13:19:55 -0500 using RuboCop version 0.34.1.
# on 2017-12-29 15:04:25 +0000 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 91
# Offense count: 1
Metrics/AbcSize:
Max: 19

# Offense count: 1
# Configuration parameters: Exclude.
Style/FileName:
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 31

# Offense count: 1
Naming/AccessorMethodName:
Exclude:
- 'spec/support/static_file_server.rb'

# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Naming/FileName:
Exclude:
- 'lib/critical-path-css-rails.rb'

# Offense count: 4
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 96
4 changes: 0 additions & 4 deletions BACKLOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# Backlog

## Tests
- Add a testing suite (preferably rspec)

## Features
- Allow the user to give a single route for a Controller#Show route, instead of hard coding every unique Resource#Show URL
* Implementation should account for any route that allows variables/parameters in the URL
- Error reporting during CSS generation (404, 500 errors, etc.)
- Allow the user to pass arguments to Penthouse.js, i.e. Viewport size, etc. For a list of the configurable options, please see [Penthouse](https://github.com/pocketjoso/penthouse)
- Improve installation process, if possible
- Improve implementation. Is their a better solution then using Rails.cache?
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ source 'https://rubygems.org'
gemspec

group :development, :test do
gem 'actionpack'
gem 'byebug', platform: [:ruby], require: false
gem 'rubocop', require: false
gem 'rspec-rails', '~> 3.6'
gem 'capybara', '~> 2.16'
gem 'pry-rails'
end

# HACK: npm install on bundle
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ rails generate critical_path_css:install

Answer 'Y' when prompted to overwrite `critical_path_css.rake`. However, overwriting `critical_path_css.yml` is not necessary and not recommended.


## Testing

This gem is to be tested inside of docker/docker-compose. [Combustion](https://github.com/pat/combustion), alongside rspec-rails and capybara, are the primary components for testing. To run the test, you'll need to have [Docker](https://docs.docker.com/engine/installation) installed. Once installed, run the following commands in the gem's root to build, run, and shell into the docker container.

```Bash
docker-compose build
docker-compose up -d
docker exec -it $(cat app_container_name) /bin/bash
```

Once shell'd in, run `bundle exec rspec spec` to run the test. The test rails app lives in `spec/internal`, and it can be viewed locally at `http://localhost:9292/`


## Versions

The critical-path-css-rails gem follows these version guidelines:
Expand Down
1 change: 1 addition & 0 deletions app_container_name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
criticalpathcss_ruby
6 changes: 6 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rubygems'
require 'bundler'
require 'combustion'

Combustion.initialize! :action_controller, :action_view
run Combustion::Application
28 changes: 14 additions & 14 deletions critical-path-css-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require File.expand_path('../lib/critical_path_css/rails/version', __FILE__)

Gem::Specification.new do |s|
s.name = 'critical-path-css-rails'
s.version = CriticalPathCSS::Rails::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Michael Misshore']
s.email = 'mmisshore@gmail.com'
s.summary = 'Critical Path CSS for Rails!'
s.description = 'Only load the CSS you need for the initial viewport in Rails!'
s.license = 'MIT'
Gem::Specification.new do |gem|
gem.name = 'critical-path-css-rails'
gem.version = CriticalPathCSS::Rails::VERSION
gem.platform = Gem::Platform::RUBY
gem.authors = ['Michael Misshore']
gem.email = 'mmisshore@gmail.com'
gem.summary = 'Critical Path CSS for Rails!'
gem.description = 'Only load the CSS you need for the initial viewport in Rails!'
gem.license = 'MIT'

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_path = 'lib'
gem.files = `git ls-files`.split("\n")
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
gem.require_path = 'lib'

s.add_development_dependency 'rspec', '~> 3.6'
gem.add_development_dependency 'combustion', '~> 0.7.0'

s.extensions = ['ext/npm/extconf.rb']
gem.extensions = ['ext/npm/extconf.rb']
end
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '2'
services:
ruby:
build:
context: .
dockerfile: docker/ruby/Dockerfile
ports:
- 9292:9292
volumes:
- .:/app:rw
volumes_from:
- data
env_file: docker/ruby/.env
container_name: criticalpathcss_ruby
data:
build:
context: .
dockerfile: docker/ruby/Dockerfile
volumes:
- /gems
command: "true"
1 change: 1 addition & 0 deletions docker/ruby/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RAILS_ENV=development
18 changes: 18 additions & 0 deletions docker/ruby/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ruby:2.5.0

# Install Dependencies
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs npm

RUN npm cache clean -f
RUN npm install -g n
RUN n 8.9.3
RUN ln -sf /usr/local/n/versions/node/8.9.3/bin/node /usr/bin/nodejs

ENV BUNDLE_PATH /gems

WORKDIR /app

COPY docker/ruby/startup.dev /usr/local/bin/startup
RUN chmod 755 /usr/local/bin/startup
CMD "/usr/local/bin/startup"
5 changes: 5 additions & 0 deletions docker/ruby/startup.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

bundle check || bundle install

bundle exec rackup --host 0.0.0.0
1 change: 0 additions & 1 deletion ext/npm/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
File.write 'Makefile',
"make:\n\t\ninstall:\n\truby install.rb\nclean:\n\t\n"

2 changes: 1 addition & 1 deletion lib/critical-path-css-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'critical_path_css/rails/config_loader'

module CriticalPathCss
CACHE_NAMESPACE = 'critical-path-css'
CACHE_NAMESPACE = 'critical-path-css'.freeze

def self.generate(route)
::Rails.cache.write(
Expand Down
1 change: 0 additions & 1 deletion lib/critical_path_css/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'erb'
module CriticalPathCss
class Configuration

def initialize(config)
@config = config
end
Expand Down
2 changes: 1 addition & 1 deletion lib/critical_path_css/css_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def css_for_route(route)
# '^\.regexWorksToo'
],
# ms; abort critical CSS generation after this timeout
'timeout' => 30000,
'timeout' => 30_000,
# set to true to throw on CSS errors (will run faster if no errors)
'strict' => false,
# characters; strip out inline base64 encoded resources larger than this
Expand Down
4 changes: 2 additions & 2 deletions lib/critical_path_css/rails/config_loader.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module CriticalPathCss
module Rails
class ConfigLoader
CONFIGURATION_FILENAME = 'critical_path_css.yml'
CONFIGURATION_FILENAME = 'critical_path_css.yml'.freeze

def load
config = YAML.load(ERB.new(File.read(configuration_file_path)).result)[::Rails.env]
config = YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
config['css_path'] = "#{::Rails.root}/public" + (
config['css_path'] ||
ActionController::Base.helpers.stylesheet_path(
Expand Down
2 changes: 1 addition & 1 deletion lib/critical_path_css/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CriticalPathCSS
module Rails
VERSION = '1.0.1'
VERSION = '1.0.1'.freeze
end
end
5 changes: 2 additions & 3 deletions lib/npm_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

# NPM wrapper with helpful error messages
class NpmCommands

# @return [Boolean] whether the installation succeeded
def install(*args) # rubocop:disable Metrics/MethodLength
def install(*args)
return false unless check_nodejs_installed
STDERR.puts 'Installing npm dependencies...'
install_status = Dir.chdir File.expand_path('..', File.dirname(__FILE__)) do
Expand All @@ -24,7 +23,7 @@ def install(*args) # rubocop:disable Metrics/MethodLength

private

def check_nodejs_installed # rubocop:disable Metrics/MethodLength
def check_nodejs_installed
return true if executable?('node')
STDERR.puts(
'-' * 60,
Expand Down
25 changes: 0 additions & 25 deletions spec/css_fetcher_spec.rb

This file was deleted.

16 changes: 16 additions & 0 deletions spec/features/generate_and_fetch_critical_css_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper.rb'

RSpec.describe 'generate and fetch the critical css' do
before do
CriticalPathCss.generate_all
end

context 'on the root page' do
let(:route) { '/' }

it 'displays the correct critical CSS' do
visit route
expect(page).to have_content 'color: red;'
end
end
end
7 changes: 0 additions & 7 deletions spec/fixtures/static/test.html

This file was deleted.

3 changes: 3 additions & 0 deletions spec/internal/app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RootController < ActionController::Base
def index; end
end
4 changes: 4 additions & 0 deletions spec/internal/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<head></head>
<body><%= yield %></body>
</html>
1 change: 1 addition & 0 deletions spec/internal/app/views/root/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= CriticalPathCss.fetch(request.path) %></p>
11 changes: 11 additions & 0 deletions spec/internal/config/critical_path_css.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
css_path: /test.css
routes:
- /

development:
<<: *defaults

test:
<<: *defaults
3 changes: 3 additions & 0 deletions spec/internal/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
adapter: sqlite3
database: db/combustion_test.sqlite
3 changes: 3 additions & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
root 'root#index'
end
3 changes: 3 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ActiveRecord::Schema.define do
#
end
1 change: 1 addition & 0 deletions spec/internal/log/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
p {
color: red;
}
}
12 changes: 9 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'critical-path-css-rails'
require 'bundler'

require 'support/static_file_server'
Bundler.require :default, :development

Combustion.initialize! :action_controller, :action_view

require 'rspec/rails'
require 'capybara/rails'

RSpec.configure do |config|
config.use_transactional_fixtures = true

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

Expand Down
Loading