Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 in WAFI/spectre from refactor-image-geom-method…
Browse files Browse the repository at this point in the history
…-to-class to master

* commit 'e8a0ba32b40783a2c56a4a8ac9678af1ff7bda6e':
  refactor to image geometry class
  add rspec
  use rails rubocop settings
  • Loading branch information
fionnbharra committed Feb 5, 2016
2 parents 24a8f11 + e8a0ba3 commit 2f6ca39
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
43 changes: 13 additions & 30 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'open3'
require "image_size"
#require 'image_size'
#require 'v8'
require 'image_size'
require 'image_geometry'

class TestsController < ApplicationController
skip_before_action :verify_authenticity_token
Expand Down Expand Up @@ -50,32 +48,32 @@ def create
@test.save!

# get the width/height of both the baseline and the test image
baseline_screenshot_details = identify_image_geometry(@test.screenshot_baseline.path)
test_screenshot_details = identify_image_geometry(@test.screenshot.path)

baseline_screenshot_details = ImageGeometry.new(@test.screenshot_baseline.path)
test_screenshot_details = ImageGeometry.new(@test.screenshot.path)
# create a canvas using the baseline's dimensions
canvas = {
width: baseline_screenshot_details[:width],
height: baseline_screenshot_details[:height]
width: baseline_screenshot_details.width,
height: baseline_screenshot_details.height
}


# enlarge the canvas to the wider of the two widths
if test_screenshot_details[:width] > canvas[:width]
canvas[:width] = test_screenshot_details[:width]
if test_screenshot_details.width > canvas[:width]
canvas[:width] = test_screenshot_details.width
@test.dimensions_changed = true
end

if test_screenshot_details[:width] < canvas[:width]
if test_screenshot_details.width < canvas[:width]
@test.dimensions_changed = true
end

# enlarge canvas to the wider of the two heights
if test_screenshot_details[:height] > canvas[:height]
canvas[:height] = test_screenshot_details[:height]
if test_screenshot_details.height > canvas[:height]
canvas[:height] = test_screenshot_details.height
@test.dimensions_changed = true
end

if test_screenshot_details[:height] < canvas[:height]
if test_screenshot_details.height < canvas[:height]
@test.dimensions_changed = true
end

Expand Down Expand Up @@ -129,21 +127,6 @@ def test_params
params.require(:test).permit(:name, :platform, :browser, :width, :screenshot, :run_id)
end

# taken from Peter's artefact comparison code
def identify_image_geometry(file_path)
stdout_str, status = Open3.capture2("identify -verbose #{file_path.shellescape}")
if status.exitstatus == 0
geometry = /Geometry: (.*)/.match(stdout_str)[1]
{
geometry: geometry,
width: /(\d+)x(\d+)/.match(geometry)[1],
height: /(\d+)x(\d+)/.match(geometry)[2],
}
else
return { file_path: file_path, unsupported: true }
end
end

def convert_image_command(input_file, output_file, canvas)
"convert #{input_file.shellescape} -background white -extent #{canvas[:width]}x#{canvas[:height]} #{output_file.shellescape}"
end
Expand Down
13 changes: 13 additions & 0 deletions lib/image_geometry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'open3'

class ImageGeometry
attr_reader :width, :height

def initialize(file_path)
stdout_str, status = Open3.capture2("identify -verbose #{file_path.shellescape}")
return unless status.exitstatus == 0
geometry = /Geometry: (.*)/.match(stdout_str)[1]
@width = /(\d+)x(\d+)/.match(geometry)[1].to_i
@height = /(\d+)x(\d+)/.match(geometry)[2].to_i
end
end
2 changes: 1 addition & 1 deletion rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
AllCops:
# Uncomment this line for rails projects
# RunRailsCops: true
RunRailsCops: true

# Output settings
DisplayCopNames: true
Expand Down
13 changes: 13 additions & 0 deletions spec/image_geometry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'image_geometry'

RSpec.describe ImageGeometry, '#image' do
it 'calculates the width of an image' do
image_geometry = ImageGeometry.new('spec/support/images/testcard.jpg')
expect(image_geometry.width).to eq 400
end

it 'calculates the height of an image' do
image_geometry = ImageGeometry.new('spec/support/images/testcard.jpg')
expect(image_geometry.height).to eq 300
end
end
96 changes: 96 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
Binary file added spec/support/images/testcard.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2f6ca39

Please sign in to comment.