Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #68 from diffux/upload-new
Browse files Browse the repository at this point in the history
Upload new baselines
  • Loading branch information
trotzig committed Nov 29, 2015
2 parents 82f4c05 + 774edd1 commit ecc95a4
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 7 deletions.
3 changes: 3 additions & 0 deletions example-project/.diffux_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ viewports:
# http://stackoverflow.com/questions/12463643/mobile-safari-on-iphone-5-visible-area-size
width: 320
height: 444
s3_access_key_id: <%= ENV['S3_ACCESS_KEY_ID'] %>
s3_secret_access_key: <%= ENV['S3_SECRET_ACCESS_KEY'] %>
s3_bucket_name: <%= ENV['S3_BUCKET_NAME'] %>
11 changes: 11 additions & 0 deletions lib/diffux_ci-diffs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
<h1>Diffux-CI diffs</h1>
<p>File generated: <%= Time.now %></p>

<h2>Diffs</h2>
<% diff_images.each do |diff| %>
<h3>
<%= Rack::Utils.escape_html(diff[:description]) %> @ <%= diff[:viewport] %>
</h3>
<p><img src="<%= diff[:url] %>"></p>
<% end %>

<hr>

<h2>New examples</h2>
<% new_images.each do |image| %>
<h3>
<%= Rack::Utils.escape_html(image[:description]) %> @ <%= image[:viewport] %>
</h3>
<p><img src="<%= image[:url] %>"></p>
<% end %>
</body>
</html>
26 changes: 26 additions & 0 deletions lib/diffux_ci_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'oily_png'
require 'diffux_ci_utils'
require 'fileutils'
require 'yaml'

def resolve_viewports(example)
configured_viewports = DiffuxCIUtils.config['viewports']
Expand Down Expand Up @@ -50,6 +51,13 @@ def init_driver
fail "JavaScript errors found during initialization: \n#{errors.inspect}"
end

# Initialize a hash to store a summary of the results from the run
result_summary = {
new_examples: [],
diff_examples: [],
okay_examples: []
}

all_examples = driver.execute_script('return window.diffux.getAllExamples()')

# To avoid the overhead of resizing the window all the time, we are going to
Expand Down Expand Up @@ -169,10 +177,18 @@ def init_driver
print '.'

puts " #{comparison[:diff_in_percent].round(1)}% (#{candidate_path})"
result_summary[:diff_examples] << {
description: description,
viewport: viewport['name']
}
else
# No visual difference was found, so we don't need to do any more
# work.
puts ' No diff.'
result_summary[:okay_examples] << {
description: description,
viewport: viewport['name']
}
end
else
# There was no baseline image yet, so we want to start by saving a new
Expand All @@ -185,9 +201,19 @@ def init_driver
screenshot.save(baseline_path, :fast_rgba)
print '.'
puts " First snapshot created (#{baseline_path})"
result_summary[:new_examples] << {
description: description,
viewport: viewport['name']
}
end
end
end

result_summary_file = File.join(DiffuxCIUtils.config['snapshots_folder'],
'result_summary.yaml')
File.open(result_summary_file, 'w') do |file|
file.write result_summary.to_yaml
end
ensure
driver.quit
end
20 changes: 18 additions & 2 deletions lib/diffux_ci_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ def upload_diffs

dir = SecureRandom.uuid

diff_images = current_snapshots[:diffs].map do |diff|
result_summary = YAML.load(File.read(File.join(
DiffuxCIUtils.config['snapshots_folder'], 'result_summary.yaml')))
diff_images = result_summary[:diff_examples].map do |diff|
image = bucket.objects.build(
"#{dir}/#{diff[:description]}_#{diff[:viewport]}.png")
image.content = open(diff[:file])
image.content = open(DiffuxCIUtils.path_to(diff[:description],
diff[:viewport],
'diff.png'))
image.content_type = 'image/png'
image.save
diff[:url] = image.url
diff
end

new_images = result_summary[:new_examples].map do |example|
image = bucket.objects.build(
"#{dir}/#{example[:description]}_#{example[:viewport]}.png")
image.content = open(DiffuxCIUtils.path_to(example[:description],
example[:viewport],
'baseline.png'))
image.content_type = 'image/png'
image.save
example[:url] = image.url
example
end

html = bucket.objects.build("#{dir}/index.html")
path = File.expand_path(
File.join(File.dirname(__FILE__), 'diffux_ci-diffs.html.erb'))
Expand Down
2 changes: 1 addition & 1 deletion lib/diffux_ci_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.config_from_file
end

def self.normalize_description(description)
Base64.encode64(description)
Base64.encode64(description).strip
end

def self.path_to(description, viewport_name, file_name)
Expand Down
43 changes: 41 additions & 2 deletions spec/diffux_ci_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def run_diffux
def snapshot_file_exists?(description, size, file_name)
File.exist?(
File.join(@tmp_dir, 'snapshots',
Base64.encode64(description), size, file_name)
Base64.encode64(description).strip, size, file_name)
)
end

Expand All @@ -63,14 +63,27 @@ def snapshot_file_exists?(description, size, file_name)
expect(run_diffux[:exit_status]).to eq(0)
end

it 'generates a baseline, but no diff' do
it 'generates a new baseline, but no diff' do
run_diffux
expect(snapshot_file_exists?(description, '@large', 'baseline.png'))
.to eq(true)
expect(snapshot_file_exists?(description, '@large', 'diff.png'))
.to eq(false)
expect(snapshot_file_exists?(description, '@large', 'candidate.png'))
.to eq(false)
expect(
YAML.load(File.read(File.join(
@tmp_dir, 'snapshots', 'result_summary.yaml')))
).to eq(
new_examples: [
{
description: description,
viewport: 'large'
}
],
diff_examples: [],
okay_examples: []
)
end
end

Expand All @@ -92,6 +105,19 @@ def snapshot_file_exists?(description, size, file_name)
.to eq(false)
expect(snapshot_file_exists?(description, '@large', 'candidate.png'))
.to eq(false)
expect(
YAML.load(File.read(File.join(
@tmp_dir, 'snapshots', 'result_summary.yaml')))
).to eq(
okay_examples: [
{
description: description,
viewport: 'large'
}
],
new_examples: [],
diff_examples: []
)
end
end

Expand Down Expand Up @@ -124,6 +150,19 @@ def snapshot_file_exists?(description, size, file_name)
.to eq(true)
expect(snapshot_file_exists?(description, '@large', 'candidate.png'))
.to eq(true)
expect(
YAML.load(File.read(File.join(
@tmp_dir, 'snapshots', 'result_summary.yaml')))
).to eq(
diff_examples: [
{
description: description,
viewport: 'large'
}
],
new_examples: [],
okay_examples: []
)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/diffux_ci_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

context 'with special characters' do
let(:description) { '<MyComponent> something interesting' }
it { should eq(Base64.encode64(description)) }
it { should eq(Base64.encode64(description).strip) }
end
end

Expand All @@ -57,6 +57,6 @@
let(:description) { '<MyComponent>' }
let(:viewport_name) { 'large' }
let(:file_name) { 'diff.png' }
it { should eq("./snapshots/#{Base64.encode64(description)}/@large/diff.png") }
it { should eq("./snapshots/#{Base64.encode64(description).strip}/@large/diff.png") }
end
end

0 comments on commit ecc95a4

Please sign in to comment.