Skip to content

Commit 5574bf6

Browse files
Merge pull request #23 from mudbugmedia/add-docker-test-env
Add docker test/dev env
2 parents 6ffb030 + 0239a1c commit 5574bf6

34 files changed

+169
-117
lines changed

.rubocop.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ AllCops:
66
- 'spec/*_helper.rb'
77
- 'Gemfile'
88
- 'Rakefile'
9-
- 'Vagrantfile'
109

1110
Documentation:
1211
Enabled: false

.rubocop_todo.yml

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

9-
# Offense count: 3
10-
# Configuration parameters: AllowURI, URISchemes.
11-
Metrics/LineLength:
12-
Max: 91
9+
# Offense count: 1
10+
Metrics/AbcSize:
11+
Max: 19
1312

1413
# Offense count: 1
15-
# Configuration parameters: Exclude.
16-
Style/FileName:
14+
# Configuration parameters: CountComments.
15+
Metrics/MethodLength:
16+
Max: 31
17+
18+
# Offense count: 1
19+
Naming/AccessorMethodName:
20+
Exclude:
21+
- 'spec/support/static_file_server.rb'
22+
23+
# Offense count: 1
24+
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
25+
# 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
26+
Naming/FileName:
1727
Exclude:
1828
- 'lib/critical-path-css-rails.rb'
29+
30+
# Offense count: 4
31+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
32+
# URISchemes: http, https
33+
Metrics/LineLength:
34+
Max: 96

BACKLOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# Backlog
22

3-
## Tests
4-
- Add a testing suite (preferably rspec)
5-
63
## Features
74
- Allow the user to give a single route for a Controller#Show route, instead of hard coding every unique Resource#Show URL
85
* Implementation should account for any route that allows variables/parameters in the URL
96
- Error reporting during CSS generation (404, 500 errors, etc.)
10-
- 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)
117
- Improve installation process, if possible
128
- Improve implementation. Is their a better solution then using Rails.cache?

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development, :test do
6+
gem 'actionpack'
67
gem 'byebug', platform: [:ruby], require: false
78
gem 'rubocop', require: false
9+
gem 'rspec-rails', '~> 3.6'
10+
gem 'capybara', '~> 2.16'
11+
gem 'pry-rails'
812
end
913

1014
# HACK: npm install on bundle

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ rails generate critical_path_css:install
126126

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

129+
130+
## Testing
131+
132+
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.
133+
134+
```Bash
135+
docker-compose build
136+
docker-compose up -d
137+
docker exec -it $(cat app_container_name) /bin/bash
138+
```
139+
140+
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/`
141+
142+
129143
## Versions
130144

131145
The critical-path-css-rails gem follows these version guidelines:

app_container_name

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
criticalpathcss_ruby

config.ru

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rubygems'
2+
require 'bundler'
3+
require 'combustion'
4+
5+
Combustion.initialize! :action_controller, :action_view
6+
run Combustion::Application

critical-path-css-rails.gemspec

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
require File.expand_path('../lib/critical_path_css/rails/version', __FILE__)
22

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

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

17-
s.add_development_dependency 'rspec', '~> 3.6'
17+
gem.add_development_dependency 'combustion', '~> 0.7.0'
1818

19-
s.extensions = ['ext/npm/extconf.rb']
19+
gem.extensions = ['ext/npm/extconf.rb']
2020
end

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '2'
2+
services:
3+
ruby:
4+
build:
5+
context: .
6+
dockerfile: docker/ruby/Dockerfile
7+
ports:
8+
- 9292:9292
9+
volumes:
10+
- .:/app:rw
11+
volumes_from:
12+
- data
13+
env_file: docker/ruby/.env
14+
container_name: criticalpathcss_ruby
15+
data:
16+
build:
17+
context: .
18+
dockerfile: docker/ruby/Dockerfile
19+
volumes:
20+
- /gems
21+
command: "true"

docker/ruby/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RAILS_ENV=development

docker/ruby/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM ruby:2.5.0
2+
3+
# Install Dependencies
4+
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
5+
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs npm
6+
7+
RUN npm cache clean -f
8+
RUN npm install -g n
9+
RUN n 8.9.3
10+
RUN ln -sf /usr/local/n/versions/node/8.9.3/bin/node /usr/bin/nodejs
11+
12+
ENV BUNDLE_PATH /gems
13+
14+
WORKDIR /app
15+
16+
COPY docker/ruby/startup.dev /usr/local/bin/startup
17+
RUN chmod 755 /usr/local/bin/startup
18+
CMD "/usr/local/bin/startup"

docker/ruby/startup.dev

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
bundle check || bundle install
4+
5+
bundle exec rackup --host 0.0.0.0

ext/npm/extconf.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
File.write 'Makefile',
22
"make:\n\t\ninstall:\n\truby install.rb\nclean:\n\t\n"
3-

lib/critical-path-css-rails.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'critical_path_css/rails/config_loader'
44

55
module CriticalPathCss
6-
CACHE_NAMESPACE = 'critical-path-css'
6+
CACHE_NAMESPACE = 'critical-path-css'.freeze
77

88
def self.generate(route)
99
::Rails.cache.write(

lib/critical_path_css/configuration.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'erb'
22
module CriticalPathCss
33
class Configuration
4-
54
def initialize(config)
65
@config = config
76
end

lib/critical_path_css/css_fetcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def css_for_route(route)
3333
# '^\.regexWorksToo'
3434
],
3535
# ms; abort critical CSS generation after this timeout
36-
'timeout' => 30000,
36+
'timeout' => 30_000,
3737
# set to true to throw on CSS errors (will run faster if no errors)
3838
'strict' => false,
3939
# characters; strip out inline base64 encoded resources larger than this

lib/critical_path_css/rails/config_loader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module CriticalPathCss
22
module Rails
33
class ConfigLoader
4-
CONFIGURATION_FILENAME = 'critical_path_css.yml'
4+
CONFIGURATION_FILENAME = 'critical_path_css.yml'.freeze
55

66
def load
7-
config = YAML.load(ERB.new(File.read(configuration_file_path)).result)[::Rails.env]
7+
config = YAML.safe_load(ERB.new(File.read(configuration_file_path)).result, [], [], true)[::Rails.env]
88
config['css_path'] = "#{::Rails.root}/public" + (
99
config['css_path'] ||
1010
ActionController::Base.helpers.stylesheet_path(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CriticalPathCSS
22
module Rails
3-
VERSION = '1.0.1'
3+
VERSION = '1.0.1'.freeze
44
end
55
end

lib/npm_commands.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
# NPM wrapper with helpful error messages
44
class NpmCommands
5-
65
# @return [Boolean] whether the installation succeeded
7-
def install(*args) # rubocop:disable Metrics/MethodLength
6+
def install(*args)
87
return false unless check_nodejs_installed
98
STDERR.puts 'Installing npm dependencies...'
109
install_status = Dir.chdir File.expand_path('..', File.dirname(__FILE__)) do
@@ -24,7 +23,7 @@ def install(*args) # rubocop:disable Metrics/MethodLength
2423

2524
private
2625

27-
def check_nodejs_installed # rubocop:disable Metrics/MethodLength
26+
def check_nodejs_installed
2827
return true if executable?('node')
2928
STDERR.puts(
3029
'-' * 60,

spec/css_fetcher_spec.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper.rb'
2+
3+
RSpec.describe 'generate and fetch the critical css' do
4+
before do
5+
CriticalPathCss.generate_all
6+
end
7+
8+
context 'on the root page' do
9+
let(:route) { '/' }
10+
11+
it 'displays the correct critical CSS' do
12+
visit route
13+
expect(page).to have_content 'color: red;'
14+
end
15+
end
16+
end

spec/fixtures/static/test.html

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class RootController < ActionController::Base
2+
def index; end
3+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html>
2+
<head></head>
3+
<body><%= yield %></body>
4+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><%= CriticalPathCss.fetch(request.path) %></p>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defaults: &defaults
2+
base_url: http://0.0.0.0:9292
3+
css_path: /test.css
4+
routes:
5+
- /
6+
7+
development:
8+
<<: *defaults
9+
10+
test:
11+
<<: *defaults

spec/internal/config/database.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
adapter: sqlite3
3+
database: db/combustion_test.sqlite

spec/internal/config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rails.application.routes.draw do
2+
root 'root#index'
3+
end

spec/internal/db/schema.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ActiveRecord::Schema.define do
2+
#
3+
end

spec/internal/log/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.log

spec/internal/public/favicon.ico

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
p {
22
color: red;
3-
}
3+
}

spec/spec_helper.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# frozen_string_literal: true
22

3-
require 'bundler/setup'
4-
require 'critical-path-css-rails'
3+
require 'bundler'
54

6-
require 'support/static_file_server'
5+
Bundler.require :default, :development
6+
7+
Combustion.initialize! :action_controller, :action_view
8+
9+
require 'rspec/rails'
10+
require 'capybara/rails'
711

812
RSpec.configure do |config|
13+
config.use_transactional_fixtures = true
14+
915
# Enable flags like --only-failures and --next-failure
1016
config.example_status_persistence_file_path = '.rspec_status'
1117

0 commit comments

Comments
 (0)