similatron 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad680fcf560bab8fa96a85ecc6f5cecf7d32053c
4
+ data.tar.gz: f5eb8e425ffdf0118d4e54da4ffce28e5acbba35
5
+ SHA512:
6
+ metadata.gz: 43f8ab420051ec7199ce5f678a17d3037e265b4c3f0f6b0dff6ab65bceed63d35645c284b63a9c4ae333cfb97ad17ecabd010f1c0b6dc9f4802fc5a3edaeb967
7
+ data.tar.gz: 52bc0875f0314d97f3de537fa28bba4a5bd8a3d080dc39cdcac29a8a095952083a2e5ab65113540d6baae740d2d08c9d90eb815aa84a3b5e92113125d59a2e75
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .vimrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ Style/Lambda:
2
+ Enabled: false
3
+
4
+ Style/StringLiterals:
5
+ Enabled: false
6
+
7
+ Style/EmptyLinesAroundClassBody:
8
+ Enabled: false
9
+
10
+ Style/EmptyLinesAroundBlockBody:
11
+ Enabled: false
12
+
13
+ Style/EmptyLinesAroundModuleBody:
14
+ Enabled: false
15
+
16
+ Style/PredicateName:
17
+ Enabled: false
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Metrics/LineLength:
23
+ Max: 120
24
+
25
+ Metrics/BlockLength:
26
+ Exclude:
27
+ - 'spec/**/*'
28
+
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in similatron.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Diego Guerra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Similatron
2
+
3
+ The laziest way to test generated artifacts.
4
+
5
+ `Similatron` compares files. It can make image, pdf, text or pure binary
6
+ comparisons.
7
+
8
+ It's designed to be used in tests, so:
9
+
10
+ * If the files are different, it'll raise an exception
11
+ * If the expected file doesn't yet exist, it'll copy the expected one over (so
12
+ you can build your tests post-hoc in one go)
13
+ * It can create html/json reports after running, so you'll be able to inspect
14
+ differences in the actual files, and, when available, have access to
15
+ a file with the differences as computed.
16
+
17
+ ## Usage
18
+
19
+ One of the simplest ways to use it is the following:
20
+
21
+ ```ruby
22
+
23
+ after :all do
24
+ Similatron.complete
25
+ end
26
+
27
+
28
+ it "creates an image for an ugly face" do
29
+ expected = "spec/assets/expected_face.jpg"
30
+ actual = face_builder.ugly_face(:path => "tmp/ugly.jpg")
31
+
32
+ Similatron.compare(expected: expected, actual: actual)
33
+ end
34
+
35
+ ```
36
+
37
+ Depending on the files...
38
+
39
+ * If the actual exists and is equal to the expected, the spec will pass.
40
+ * If the expected face doesn't exist, `tmp/ugly.jpg` will be copied over, and
41
+ the spec will not fail.
42
+ * Otherwise, the spec fails.
43
+
44
+ Whenever the images are not equal, an html report is generated so you can
45
+ check the reasons for the failure.
46
+
47
+
48
+ ## Installation
49
+
50
+ Add this line to your application's Gemfile:
51
+
52
+ ```ruby
53
+ gem 'similatron'
54
+ ```
55
+
56
+ And then execute:
57
+
58
+ $ bundle
59
+
60
+ Or install it yourself as:
61
+
62
+ $ gem install similatron
63
+
64
+ `Similatron` needs Imagemagick (to compare images), and a standard
65
+ implementation of `diff`.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
70
+ run `rake spec` to run the tests. You can also run `bin/console` for an
71
+ interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`.
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at
78
+ https://github.com/dgsuarez/similatron.
79
+
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT
84
+ License](http://opensource.org/licenses/MIT).
85
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "similatron"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ <html lang="en">
2
+ <head>
3
+ <meta charset="utf-8">
4
+
5
+ <title>Similatron report</title>
6
+ <meta name="description" content="Similatron report">
7
+ <meta name="author" content="Similatron">
8
+ </head>
9
+
10
+ <body>
11
+ <p>
12
+ Did <%= comparisons.length %> checks with <%= failed_comparisons.length %> failures and <%= overwrite_comparisons.length %> new files.
13
+ </p>
14
+ <ul>
15
+ <% overwrite_comparisons.each do |comparison| %>
16
+ <li>
17
+ Original
18
+ <a href="<%= comparison.expected %>">Original</a> not found, copied
19
+ <a href="<%= comparison.actual %>">actual</a>.
20
+ </li>
21
+ <% end %>
22
+ </ul>
23
+
24
+ <ul>
25
+ <% failed_comparisons.each do |comparison| %>
26
+ <li>
27
+ Failed comparison:
28
+ Expected <a href="<%= comparison.expected %>">expected</a>
29
+ to equal <a href="<%= comparison.actual %>">actual</a>.
30
+ <% if comparison.diff %>
31
+ <a href="<%= comparison.diff %>">Diff</a>.
32
+ <% end %>
33
+ </li>
34
+ <% end %>
35
+ </ul>
36
+ </body>
37
+ </html>
@@ -0,0 +1,31 @@
1
+ module Similatron
2
+ class BinaryDiffComparisonEngine < ComparisonEngine
3
+
4
+ def can_handle_mime?(_)
5
+ true
6
+ end
7
+
8
+ private
9
+
10
+ def command(expected, actual, _diff_path)
11
+ "#{executable_path} #{expected} #{actual}"
12
+ end
13
+
14
+ def default_executable_path
15
+ "diff"
16
+ end
17
+
18
+ def diff_extension
19
+ "notused"
20
+ end
21
+
22
+ def diff(_diff_path, _exec_result)
23
+ nil
24
+ end
25
+
26
+ def score(exec_result)
27
+ exec_result.status
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ module Similatron
2
+ class Comparison
3
+
4
+ attr_reader :expected, :actual, :diff, :score
5
+
6
+ def initialize(expected:, actual:, score:, diff: nil, overwrite: nil)
7
+ @expected = expected
8
+ @actual = actual
9
+ @diff = diff
10
+ @score = score
11
+ @overwrite = overwrite
12
+ end
13
+
14
+ def same?
15
+ @score.zero?
16
+ end
17
+
18
+ def overwrite?
19
+ @overwrite
20
+ end
21
+
22
+ def raise_when_different
23
+ return if same?
24
+
25
+ message_parts = [
26
+ "Found #{actual} different from #{expected}\n",
27
+ "Score: #{score}"
28
+ ]
29
+ message_parts << "\nDiff in #{diff}" if diff
30
+
31
+ raise StandardError, message_parts.join
32
+ end
33
+
34
+ def as_json
35
+ {
36
+ expected: expected,
37
+ actual: actual,
38
+ diff: diff,
39
+ score: score,
40
+ same: !!same?, # rubocop:disable Style/DoubleNegation
41
+ overwrite: !!overwrite? # rubocop:disable Style/DoubleNegation
42
+ }
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ module Similatron
2
+ class ComparisonEngine
3
+ def initialize(executable_path: nil, diffs_path:)
4
+ @executable_path = executable_path || "diff"
5
+ @diffs_path = diffs_path
6
+ end
7
+
8
+ def compare(expected:, actual:)
9
+ diff_path = next_diff_path
10
+
11
+ command = command(expected, actual, diff_path)
12
+
13
+ exec_result = run(command)
14
+
15
+ Comparison.new(
16
+ expected: expected,
17
+ actual: actual,
18
+ score: score(exec_result),
19
+ diff: diff(diff_path, exec_result)
20
+ )
21
+ end
22
+
23
+ protected
24
+
25
+ attr_reader :diffs_path, :diff_index
26
+
27
+ def run(command)
28
+ exec_info = Open3.capture3(command)
29
+ Open3Result.new(*exec_info)
30
+ end
31
+
32
+ def next_diff_path
33
+ @diff_index ||= 0
34
+ @diff_index += 1
35
+
36
+ File.join(diffs_path, "diff_#{diff_index}.#{diff_extension}")
37
+ end
38
+
39
+ def executable_path
40
+ default_executable_path || @executable_path
41
+ end
42
+
43
+ class Open3Result
44
+ attr_reader :out, :err, :status
45
+
46
+ def initialize(out, err, status)
47
+ @out = out
48
+ @err = err
49
+ @status = status.exitstatus
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,39 @@
1
+ module Similatron
2
+ class DiffComparisonEngine < ComparisonEngine
3
+
4
+ def can_handle_mime?(mime_type)
5
+ mime_type !~ /charset=binary/
6
+ end
7
+
8
+ private
9
+
10
+ def command(expected, actual, _diff_path)
11
+ "#{executable_path} #{expected} #{actual}"
12
+ end
13
+
14
+ def default_executable_path
15
+ "diff"
16
+ end
17
+
18
+ def diff_extension
19
+ "diff"
20
+ end
21
+
22
+ def diff(diff_path, exec_result)
23
+ if exec_result.status != 0
24
+ File.write(diff_path, exec_result.out)
25
+ diff_path
26
+ else # rubocop:disable Style/EmptyElse
27
+ nil
28
+ end
29
+ end
30
+
31
+ def score(exec_result)
32
+ return 0 if exec_result.status.zero?
33
+
34
+ lines = exec_result.out.split("\n")
35
+ lines.grep(/^>|</).size / 2.0
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ module Similatron
2
+ class ImagemagickComparisonEngine < ComparisonEngine
3
+
4
+ def can_handle_mime?(mime_type)
5
+ [/image/, /application.pdf/].any? do |format|
6
+ mime_type =~ format
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def default_executable_path
13
+ "compare"
14
+ end
15
+
16
+ def diff_extension
17
+ "jpg"
18
+ end
19
+
20
+ def command(expected, actual, diff_path)
21
+ "#{executable_path} -metric PSNR #{expected} #{actual} #{diff_path}"
22
+ end
23
+
24
+ def diff(diff_path, exec_result)
25
+ exec_result.status == 1 ? diff_path : nil
26
+ end
27
+
28
+ def score(exec_result)
29
+ if exec_result.status == 2
30
+ 100
31
+ else
32
+ exec_result.err.to_i
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,109 @@
1
+ module Similatron
2
+ class Run
3
+
4
+ attr_reader :comparisons
5
+
6
+ def start(base_path: "tmp")
7
+ run_id = SecureRandom.urlsafe_base64(8)
8
+ @run_path = File.join(base_path, "run_#{run_id}")
9
+
10
+ @engines = [
11
+ ImagemagickComparisonEngine.new(diffs_path: run_path),
12
+ DiffComparisonEngine.new(diffs_path: run_path),
13
+ BinaryDiffComparisonEngine.new(diffs_path: run_path)
14
+ ]
15
+
16
+ @comparisons = []
17
+ FileUtils.mkdir_p(run_path)
18
+ end
19
+
20
+ def complete
21
+ write_reports
22
+ end
23
+
24
+ def compare(expected:, actual:)
25
+ comparison = if File.exist?(expected)
26
+ real_comparison(expected, actual)
27
+ else
28
+ copy_comparison(expected, actual)
29
+ end
30
+ comparisons << comparison
31
+
32
+ comparison
33
+ end
34
+
35
+ def compare!(expected:, actual:)
36
+ comparison = compare(expected: expected, actual: actual)
37
+ comparison.raise_when_different
38
+ end
39
+
40
+ def summary
41
+ return "" if failed_comparisons.empty? && overwrite_comparisons.empty?
42
+ [
43
+ "Similatron report:",
44
+ "#{failed_comparisons.count} failures.",
45
+ "#{overwrite_comparisons.count} new files.",
46
+ "Report may be found in #{html_report_path}"
47
+ ].join("\n")
48
+ end
49
+
50
+ def json_report_path
51
+ File.join(run_path, "report.json")
52
+ end
53
+
54
+ def html_report_path
55
+ File.join(run_path, "report.html")
56
+ end
57
+
58
+ def failed_comparisons
59
+ comparisons.reject(&:same?)
60
+ end
61
+
62
+ def overwrite_comparisons
63
+ comparisons.select(&:overwrite?)
64
+ end
65
+
66
+ private
67
+
68
+ attr_reader :run_path, :engines
69
+
70
+ def copy_comparison(expected, actual)
71
+ FileUtils.cp(actual, expected)
72
+ Comparison.new(
73
+ overwrite: true,
74
+ expected: expected,
75
+ actual: actual,
76
+ score: 0
77
+ )
78
+ end
79
+
80
+ def best_engine(file)
81
+ mime_type = `file --mime -b #{file}`.chomp
82
+ engines.detect { |engine| engine.can_handle_mime?(mime_type) }
83
+ end
84
+
85
+ def real_comparison(expected, actual)
86
+ engine = best_engine(expected)
87
+
88
+ engine.compare(
89
+ expected: expected,
90
+ actual: actual
91
+ )
92
+ end
93
+
94
+ def to_json
95
+ comparisons.map(&:as_json).to_json
96
+ end
97
+
98
+ def to_html
99
+ template = ERB.new(File.read("lib/assets/report.html.erb"))
100
+ template.result(binding)
101
+ end
102
+
103
+ def write_reports
104
+ File.write(html_report_path, to_html)
105
+ File.write(json_report_path, to_json)
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Similatron
2
+ VERSION = "0.1.0"
3
+ end
data/lib/similatron.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "English"
2
+ require 'securerandom'
3
+ require 'open3'
4
+ require 'json'
5
+ require 'erb'
6
+
7
+ require "similatron/version"
8
+ require "similatron/comparison"
9
+ require "similatron/comparison_engine"
10
+ require "similatron/imagemagick_comparison_engine"
11
+ require "similatron/diff_comparison_engine"
12
+ require "similatron/binary_diff_comparison_engine"
13
+ require "similatron/run"
14
+
15
+ module Similatron
16
+ def self.compare(*args)
17
+ run.compare!(*args)
18
+ end
19
+
20
+ def self.complete
21
+ run.complete
22
+ puts run.summary
23
+ end
24
+
25
+ def self.run
26
+ @run ||= begin
27
+ run = Run.new
28
+ run.start
29
+ run
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'similatron/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "similatron"
9
+ spec.version = Similatron::VERSION
10
+ spec.authors = ["Diego Guerra"]
11
+ spec.email = ["diego.guerra.suarez@gmail.com"]
12
+
13
+ spec.summary = 'Check image similarities'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: similatron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Diego Guerra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - diego.guerra.suarez@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/assets/report.html.erb
73
+ - lib/similatron.rb
74
+ - lib/similatron/binary_diff_comparison_engine.rb
75
+ - lib/similatron/comparison.rb
76
+ - lib/similatron/comparison_engine.rb
77
+ - lib/similatron/diff_comparison_engine.rb
78
+ - lib/similatron/imagemagick_comparison_engine.rb
79
+ - lib/similatron/run.rb
80
+ - lib/similatron/version.rb
81
+ - similatron.gemspec
82
+ homepage:
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Check image similarities
106
+ test_files: []