Skip to content

Commit

Permalink
Add Rails integration test for compiled template
Browse files Browse the repository at this point in the history
We want to be confident that the changes we make to govuk_template don't
break the compiled templates. The only way we can really do this automatically
is to have code that can process each template.

The spec is currently wired up to anything and is hardcoded for the filename
generated by the Rails integration code. The expectations should be portable,
however.

This needs some scripting to trigger the sub-build within each integration
testing app and then run the spec. For the languages that aren't Ruby, we'll
need to alter the travis.yml.
  • Loading branch information
jamiecobbett committed May 22, 2015
1 parent 708a45d commit a53e58b
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.DS_Store
.bundle
.sass-cache
Gemfile.lock
app
/Gemfile.lock
/app
pkg

integration_tests/html_for_testing/*.html
2 changes: 2 additions & 0 deletions govuk_template.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'govuk_frontend_toolkit', '3.5.1'
spec.add_development_dependency 'gem_publisher', '1.3.0'
spec.add_development_dependency 'rspec', '3.2.0'
spec.add_development_dependency 'rspec-html-matchers', '0.7.0'
spec.add_development_dependency 'mustache', '0.99.7'
spec.add_development_dependency 'nokogiri', '1.6.6.2'
spec.add_development_dependency 'octokit', '3.4.2'
end
10 changes: 10 additions & 0 deletions integration_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Integration Tests

When we make changes to govuk_template, we want to know that the changes don't break integration with clients.

This directory contains example integrations for compilation targets of govuk_template so that we can test two things:

* is the compiled template still syntactically valid?
* has the API changed - has it required any changes in the app?

This is achieved by getting each app to render HTML from the template, saving it into `./html_for_testing` and then running assertions against each of those HTML files.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'
require 'rspec-html-matchers'

describe "HTML rendered by each integration testing app" do
include RSpecHtmlMatchers

let(:filename) { "#{PROJECT_ROOT}/integration_tests/html_for_testing/rails_integration_test_app.html" }

subject { File.open(filename).read }

it "should allow overriding the lang attribute on the html tag" do
expect(subject).to have_tag("html", with: { lang: 'rb' })
end

it "should allow overriding the page title" do
expect(subject).to have_tag("title", text: "This is a custom page title")
end

it "should allows inserting content into the head" do
expect(subject).to have_tag("head") do
with_tag "inserted-into-head"
end
end

it "should allow setting_body_classes" do
expect(subject).to have_tag("body", with: { class: "custom_body_class" })
end

it "should allow inserting content into body_start" do
expect(subject).to have_tag("body") do
with_tag "inserted-into-body-start"
end
end

it "should allow overriding the cookie message" do
expect(subject).to have_tag("div#global-cookie-message", text: /Custom cookie message/)
end

it "should allow setting_global_header_class" do
expect(subject).to have_tag("header", with: { class: "custom_header_class" })
end

it "should allow inserting content into the header" do
expect(subject).to have_tag("header") do
with_tag("inside-header")
end
end

it "should allow inserting_a_proposition_header" do
expect(subject).to have_tag("header") do
with_tag("proposition-header")
end
end

it "should allow inserting_after_header" do
expect(subject).to have_tag("body") do
with_tag("after-header")
end
end

it "should content" do
expect(subject).to have_tag("body", text: /The page content/)
end

it "should footer_top" do
expect(subject).to have_tag("footer") do
with_tag "footer-top"
end
end

it "should footer_support_links" do
expect(subject).to have_tag("footer") do
with_tag "footer-support-links"
end
end

it "should body_end" do
expect(subject).to have_tag("body") do
with_tag "body-end"
end
end
end
3 changes: 3 additions & 0 deletions integration_tests/integrations/rails/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem "actionpack", "4.2.1"
52 changes: 52 additions & 0 deletions integration_tests/integrations/rails/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (4.2.1)
actionview (= 4.2.1)
activesupport (= 4.2.1)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
actionview (4.2.1)
activesupport (= 4.2.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
activesupport (4.2.1)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
builder (3.2.2)
erubis (2.7.0)
i18n (0.7.0)
json (1.8.2)
loofah (2.0.2)
nokogiri (>= 1.5.9)
mini_portile (0.6.2)
minitest (5.6.1)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
rack (1.6.1)
rack-test (0.6.3)
rack (>= 1.0)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.6)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
actionpack (= 4.2.1)
6 changes: 6 additions & 0 deletions integration_tests/integrations/rails/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash
set -e

bundle install

bundle exec ruby simplicity.rb
26 changes: 26 additions & 0 deletions integration_tests/integrations/rails/simplicity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'action_controller'

class TemplateRenderer < AbstractController::Base
include AbstractController::Rendering
include ActionView::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths

self.view_paths = [
"views",
File.absolute_path(File.expand_path(File.join(__FILE__, "../../../../app/views"))) # Hack to manually add the compiled template path
]

layout "govuk_template"

def show
render template: "show"
end
end

rendered_template = TemplateRenderer.new.show

output_filepath = File.absolute_path(File.expand_path(File.join(__FILE__, "../../../html_for_testing/rails_integration_test_app.html")))

File.write(output_filepath, rendered_template)
27 changes: 27 additions & 0 deletions integration_tests/integrations/rails/views/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<% content_for(:html_lang, 'rb') %>
<% content_for(:page_title, "This is a custom page title") %>
<% content_for(:head) do %><inserted-into-head></inserted-into-head><% end %>
<% content_for(:body_classes, "custom_body_class") %>
<% content_for(:body_start) do %><inserted-into-body-start></inserted-into-body-start><% end %>
<% content_for(:cookie_message, "Custom cookie message") %>
<% content_for(:header_class, "custom_header_class") %>
<% content_for(:inside_header) do %><inside-header></inside-header><% end %>
<% content_for(:proposition_header) do %><proposition-header></proposition-header><% end %>
<% content_for(:after_header) do %><after-header></after-header><% end %>

The page content

<% content_for(:footer_top) do %><footer-top></footer-top><% end %>
<% content_for(:footer_support_links) do %><footer-support-links></footer-support-links><% end %>
<% content_for(:body_end) do %><body-end></body-end><% end %>

0 comments on commit a53e58b

Please sign in to comment.