Skip to content

Commit

Permalink
Extract html report testing dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
mainej committed Jan 11, 2015
1 parent 5af9363 commit bb74e9c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 64 deletions.
9 changes: 5 additions & 4 deletions features/configure/approve_dependencies_spec.rb
Expand Up @@ -21,11 +21,12 @@
specify "include approval detail in reports" do
user.execute_command 'license_finder report --format html'

expect(user.html_formatting_of('manual_dep')).to include "approved"
html = user.view_html
expect(html).to be_approved 'manual_dep'

user.in_dep_html("manual_dep") do |manual_dep|
expect(manual_dep).to have_content "Julian"
expect(manual_dep).to have_content "We really need this"
html.in_dep("manual_dep") do |section|
expect(section).to have_content "Julian"
expect(section).to have_content "We really need this"
end
end
end
7 changes: 4 additions & 3 deletions features/configure/name_project_spec.rb
Expand Up @@ -10,15 +10,16 @@
before { user.create_empty_project }

specify "appears in the HTML report" do
user.execute_command 'license_finder project_name add my_proj'
user.execute_command 'license_finder project_name add changed_name'

user.execute_command 'license_finder report --format html'
expect(user.html_title).to have_content 'my_proj'
expect(user.view_html).to be_has_title 'changed_name'
end

specify "defaults to the directory name" do
user.execute_command 'license_finder report --format html'
expect(user.html_title).to have_content 'my_app'

expect(user.view_html).to be_has_title 'my_app'
end

specify "appears in the CLI" do
Expand Down
17 changes: 8 additions & 9 deletions features/report/html_spec.rb
Expand Up @@ -23,7 +23,7 @@

user.execute_command 'license_finder report --format html'

user.in_dep_html(gem_name) do |section|
user.view_html.in_dep(gem_name) do |section|
expect(section.find("a[href='#{gem_attributes[:homepage]}']", text: gem_name)).to be
expect(section).to have_content gem_attributes[:license]
expect(section).to have_content gem_attributes[:summary]
Expand All @@ -41,14 +41,13 @@

user.execute_command 'license_finder report --format html'

expect(user.html_formatting_of('gpl_dep')).to include "unapproved"
expect(user.html_formatting_of('mit_dep')).to include "approved"
html = user.view_html
expect(html).to be_unapproved 'gpl_dep'
expect(html).to be_approved 'mit_dep'

user.in_html do |page|
expect(page).to have_content '1 GPL'
action_items = page.find('.action-items')
expect(action_items).to have_content '(GPL)'
expect(action_items).not_to have_content 'MIT'
end
expect(html).to have_content '1 GPL'
action_items = html.find('.action-items')
expect(action_items).to have_content '(GPL)'
expect(action_items).not_to have_content 'MIT'
end
end
109 changes: 61 additions & 48 deletions features/support/testing_dsl.rb
@@ -1,7 +1,4 @@
require 'fileutils'
require 'pathname'
require 'bundler'
require 'capybara'

module LicenseFinder::TestingDSL
module Shell
Expand All @@ -23,6 +20,49 @@ def shell_out(command, allow_failures = false)
end
end

class User
include Shell

def run_license_finder
execute_command "license_finder --quiet"
end

def create_empty_project
EmptyProject.create
end

def create_ruby_app
RubyProject.create
end

def execute_command(command)
::Bundler.with_clean_env do
@output = shell_out("cd #{Paths.app} && bundle exec #{command}", true)
end
end

def seeing?(content)
@output.include? content
end

def seeing_line?(content)
seeing_something_like? /^#{Regexp.escape content}$/
end

def seeing_something_like?(regex)
@output =~ regex
end

def receiving_exit_code?(code)
@last_command_exit_status.exitstatus == code
end

def view_html
HtmlReport.new(@output)
end
end

require 'pathname'
module Paths
include Shell
extend self
Expand Down Expand Up @@ -235,62 +275,35 @@ def add_to_bundler(name, options = {})
end
end


class User
include Shell

def run_license_finder
execute_command "license_finder --quiet"
end

def create_empty_project
EmptyProject.create
end

def create_ruby_app
RubyProject.create
end

def execute_command(command)
::Bundler.with_clean_env do
@output = shell_out("cd #{Paths.app} && bundle exec #{command}", true)
end
end

def seeing?(content)
@output.include? content
require 'capybara'
require 'delegate'
class HtmlReport < SimpleDelegator
def initialize(str)
super(Capybara.string(str))
end

def seeing_line?(content)
seeing_something_like? /^#{Regexp.escape content}$/
def in_dep(dep_name)
result = find("##{dep_name}")
yield result if block_given?
result
end

def seeing_something_like?(regex)
@output =~ regex
def approved?(dep_name)
classes_of(dep_name).include? "approved"
end

def receiving_exit_code?(code)
@last_command_exit_status.exitstatus == code
def unapproved?(dep_name)
classes_of(dep_name).include? "unapproved"
end

def in_html
yield Capybara.string(@output)
def has_title?(title)
find("h1").has_content? title
end

def in_dep_html(dep_name)
in_html do |page|
yield page.find("##{dep_name}")
end
end

def html_formatting_of(dep_name)
in_dep_html(dep_name) do |dep|
dep[:class].split(' ')
end
end
private

def html_title
in_html { |page| page.find("h1") }
def classes_of(dep_name)
in_dep(dep_name)[:class].split(' ')
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -6,6 +6,7 @@
require 'rspec'
require 'webmock/rspec'
require 'rspec/its'
require 'fileutils'

ENV['test_run'] = true.to_s

Expand Down

0 comments on commit bb74e9c

Please sign in to comment.