moar work on acceptance tests

This commit is contained in:
Sven Fuchs 2012-10-02 19:10:29 +02:00
parent 9851d1b276
commit 7eceeb2842
15 changed files with 134 additions and 15 deletions

View File

@ -6,7 +6,7 @@ class Travis::Api::App
# artifacts
class Artifacts < Endpoint
# Fetches an artifact by it's *id*.
get('/:id') do |id|
get '/:id' do |id|
body one(params).run
end
end

View File

@ -3,7 +3,7 @@ require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Branches < Endpoint
get('/') do
get '/' do
body all(params).run, type: :branches
end
end

View File

@ -3,11 +3,11 @@ require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Hooks < Endpoint
get('/', scope: :private) do
get '/', scope: :private do
body all(params).run, type: :hooks
end
put('/:id?', scope: :private) do
put '/:id?', scope: :private do
update(id: params[:id] || params[:hook][:id], active: params[:hook][:active]).run
204
end

View File

@ -3,11 +3,11 @@ require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Jobs < Endpoint
get('/') do
get '/' do
body all(params).run
end
get('/:id') do
get '/:id' do
body one(params).run
end
end

View File

@ -7,13 +7,9 @@ class Travis::Api::App
body all(params).run
end
get('/:id') do
get '/:id' do
body one(params).run
end
# TODO make sure status images and cc.xml work
# rescue ActiveRecord::RecordNotFound
# raise unless params[:format] == 'png'
end
end
end

View File

@ -0,0 +1,13 @@
require 'travis/api/app'
class Travis::Api::App
class Endpoint
class ResultImage < Endpoint
set(:prefix) { '/' }
get '/:owner_name/:name.png' do
result_image service(:repositories, :one, params).run(:raise => false)
end
end
end
end

View File

@ -3,11 +3,11 @@ require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Stats < Endpoint
get('/repos') do
get '/repos' do
{ :stats => service(:daily_repos) }
end
get('/tests') do
get '/tests' do
{ :stats => service(:daily_tests) }
end
end

View File

@ -3,7 +3,7 @@ require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Workers < Endpoint
get('/') do
get '/' do
body all(params).run
end
end

View File

@ -0,0 +1,27 @@
require 'travis/api/app'
require 'cgi'
module Travis::Api::App::Helpers
module ResultImage
RESULT_NAMES = { nil => 'unknown', 0 => 'passing', 1 => 'failing' }
def result_image(resource)
headers['Expires'] = CGI.rfc1123_date(Time.now.utc)
filename = filename(resource)
env['travis.sending-file'] = filename
send_file filename, type: :png, disposition: :inline
end
protected
def filename(resource)
root = File.expand_path("#{settings.root}/../../../../../") # TODO wat.
"#{root}/public/images/result/#{result(resource)}.png"
end
def result(resource)
RESULT_NAMES[resource.try(:last_build_result_on, branch: params[:branch])]
end
end
end

View File

@ -23,7 +23,7 @@ class Travis::Api::App
enable :raise_errors
# disable :dump_errors
register :subclass_tracker
helpers :json_renderer
helpers :json_renderer, :result_image
end
configure :development do

Binary file not shown.

After

(image error) Size: 1.5 KiB

Binary file not shown.

After

(image error) Size: 1.7 KiB

Binary file not shown.

After

(image error) Size: 1.6 KiB

View File

@ -0,0 +1,62 @@
require 'spec_helper'
describe 'Result images' do
let!(:repo) { Factory(:repository, owner_name: 'svenfuchs', name: 'minimal') }
describe 'GET /svenfuchs/minimal.png' do
it '"unknown" when the repository does not exist' do
get('/svenfuchs/does-not-exist.png').should deliver_result_image_for('unknown')
end
it '"unknown" when it only has one build that is not finished' do
repo.update_attributes!(last_build_result: nil)
get('/svenfuchs/minimal.png').should deliver_result_image_for('unknown')
end
it '"failing" when the last build has failed' do
repo.update_attributes!(last_build_result: 1)
get('/svenfuchs/minimal.png').should deliver_result_image_for('failing')
end
it '"passing" when the last build has passed' do
repo.update_attributes!(last_build_result: 0)
get('/svenfuchs/minimal.png').should deliver_result_image_for('passing')
end
it '"passing" when there is a running build but the previous one has passed' do
Factory(:build, repository: repo, state: :finished, result: nil, previous_result: 0)
repo.update_attributes!(last_build_result: nil)
get('/svenfuchs/minimal.png').should deliver_result_image_for('passing')
end
end
describe 'GET /svenfuchs/minimal.png' do
let(:commit) { Factory(:commit, branch: 'dev') }
it '"unknown" when the repository does not exist' do
get('/svenfuchs/does-not-exist.png?branch=dev').should deliver_result_image_for('unknown')
end
it '"unknown" when it only has a build that is not finished' do
Factory(:build, repository: repo, state: :started, result: nil, commit: commit)
get('/svenfuchs/minimal.png?branch=dev').should deliver_result_image_for('unknown')
end
it '"failing" when the last build has failed' do
Factory(:build, repository: repo, state: :finished, result: 1, commit: commit)
get('/svenfuchs/minimal.png?branch=dev').should deliver_result_image_for('failing')
end
it '"passing" when the last build has passed' do
Factory(:build, repository: repo, state: :finished, result: 0, commit: commit)
get('/svenfuchs/minimal.png?branch=dev').should deliver_result_image_for('passing')
end
it '"passing" when there is a running build but the previous one has passed' do
Factory(:build, repository: repo, state: :finished, result: 0, commit: commit)
Factory(:build, repository: repo, state: :started, result: nil, commit: commit)
repo.update_attributes!(last_build_result: nil)
get('/svenfuchs/minimal.png?branch=dev').should deliver_result_image_for('passing')
end
end
end

View File

@ -28,6 +28,27 @@ RSpec::Matchers.define :deliver_json_for do |resource, options = {}|
end
end
RSpec::Matchers.define :deliver_result_image_for do |name|
match do |response|
actual = files.detect do |(name, content)|
response.body.force_encoding('ascii') == content.force_encoding('ascii') # TODO ummmmmmmm?
end
actual = actual && actual[0]
failure_message_for_should do
"expected #{actual.inspect} to equal #{name.inspect}"
end
actual == name
end
def files
files = Hash[*Dir['public/images/result/*.png'].map do |file|
[File.basename(file, '.png'), File.read(file)]
end.flatten]
end
end
Travis.logger = Logger.new(StringIO.new)
Travis::Api::App.setup