From 227d7f5e86ebc5cfb0121e824e1ee682c314ec6e Mon Sep 17 00:00:00 2001 From: pikesley Date: Thu, 18 Dec 2014 21:37:06 +0000 Subject: [PATCH 1/5] Some tests --- .rspec | 2 + Gemfile | 2 + Gemfile.lock | 39 ++++++++++++++++ Guardfile | 17 +++++++ Rakefile | 4 +- lib/github_badges.rb | 60 +++++++++++++++--------- spec/github_badges_spec.rb | 19 ++++++++ spec/spec_helper.rb | 93 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 214 insertions(+), 22 deletions(-) create mode 100644 .rspec create mode 100644 Guardfile create mode 100644 spec/github_badges_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index 44459fc..e2ad865 100644 --- a/Gemfile +++ b/Gemfile @@ -13,4 +13,6 @@ group :test do gem 'cucumber' gem 'capybara' gem 'rspec' + gem 'guard-cucumber' + gem 'guard-rspec' end diff --git a/Gemfile.lock b/Gemfile.lock index 4970a2b..474a1a3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,9 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + celluloid (0.16.0) + timers (~> 4.0.0) + coderay (1.1.0) coveralls (0.7.1) multi_json (~> 1.3) rest-client @@ -23,11 +26,35 @@ GEM curb (0.8.6) diff-lcs (1.2.5) docile (1.1.5) + ffi (1.9.6) + formatador (0.2.5) gherkin (2.12.2) multi_json (~> 1.3) + guard (2.10.2) + formatador (>= 0.2.4) + listen (~> 2.7) + lumberjack (~> 1.0) + pry (>= 0.9.12) + thor (>= 0.18.1) + guard-compat (1.1.0) + guard-cucumber (1.5.2) + cucumber (>= 1.3.0) + guard (>= 2.0.0) + guard-compat (~> 1.0) + guard-rspec (4.5.0) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) haml (4.0.6) tilt + hitimes (1.2.2) kramdown (1.5.0) + listen (2.8.3) + celluloid (>= 0.15.2) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) + lumberjack (1.0.9) + method_source (0.8.2) mime-types (2.4.3) mini_portile (0.6.1) multi_json (1.10.1) @@ -35,12 +62,19 @@ GEM netrc (0.10.1) nokogiri (1.6.5) mini_portile (~> 0.6.0) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) rack (1.5.2) rack-protection (1.5.3) rack rack-test (0.6.2) rack (>= 1.0) rake (10.4.2) + rb-fsevent (0.9.4) + rb-inotify (0.9.5) + ffi (>= 0.5.0) rest-client (1.7.2) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) @@ -65,10 +99,13 @@ GEM rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) + slop (3.6.0) term-ansicolor (1.3.0) tins (~> 1.0) thor (0.19.1) tilt (1.4.1) + timers (4.0.1) + hitimes tins (1.3.3) xpath (2.0.0) nokogiri (~> 1.3) @@ -81,6 +118,8 @@ DEPENDENCIES coveralls cucumber curb + guard-cucumber + guard-rspec haml kramdown rake diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..995278d --- /dev/null +++ b/Guardfile @@ -0,0 +1,17 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +## Uncomment and set this to only include directories you want to watch +# directories %(app lib config test spec feature) + +## Uncomment to clear the screen before every task +# clearing :on + +guard "cucumber" do + watch(%r{^features/.+\.feature$}) + watch(%r{^features/support/.+$}) { "features" } + + watch(%r{^features/step_definitions/(.+)_steps\.rb$}) do |m| + Dir[File.join("**/#{m[1]}.feature")][0] || "features" + end +end diff --git a/Rakefile b/Rakefile index 2b1011a..8e82252 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,10 @@ require File.join(File.dirname(__FILE__), 'lib/github_badges.rb') require 'cucumber/rake/task' +require 'rspec/core/rake_task' require 'coveralls/rake/task' Coveralls::RakeTask.new Cucumber::Rake::Task.new +RSpec::Core::RakeTask.new -task :default => [:cucumber, 'coveralls:push'] +task :default => [:spec, :cucumber, 'coveralls:push'] diff --git a/lib/github_badges.rb b/lib/github_badges.rb index c265141..7d8d4a5 100644 --- a/lib/github_badges.rb +++ b/lib/github_badges.rb @@ -11,6 +11,8 @@ } class GithubBadges < Sinatra::Base + attr_accessor :count + get '/' do haml :index, locals: { title: 'Github Badges', @@ -19,42 +21,58 @@ class GithubBadges < Sinatra::Base get '/:user/:repo/:thing' do thing_parts = params[:thing].split('.') - thing = thing_parts[0...-1].join('.') - extension = thing_parts[-1] + @thing = thing_parts[0...-1].join('.') + @extension = thing_parts[-1] - c = Curl::Easy.new("https://api.github.com/repos/#{params[:user]}/#{params[:repo]}/#{thing}") + c = Curl::Easy.new("https://api.github.com/repos/#{params[:user]}/#{params[:repo]}/#{@thing}") c.headers = { 'Accept' => 'application/json', 'User-agent' => 'GithubBadges' } c.perform j = JSON.parse c.body_str - count = j.count ||= 'NaN' - - colour = case count - when 0 - 'brightgreen' - when 1..3 - 'blue' - when 4..6 - 'orange' - else - 'red' - end + @count = j.count ||= 'NaN' + bounce + end + + # start the server if ruby file executed directly + run! if app_file == $0 + + def bounce + redirect target + end + + def target + "http://img.shields.io/badge/#{label}-#{@count}-#{Badgerise.colour @count}.#{@extension}" + end +end +module Badgerise + def Badgerise.colour count + case count + when 0 + 'brightgreen' + when 1..3 + 'blue' + when 4..6 + 'orange' + else + 'red' + end + end - thing = case params[:thing] + def Badgerise.label source + case source when 'issues' 'open%20issues' when 'pulls' 'pending%20pull--requests' else - thing + source end - - redirect "http://img.shields.io/badge/#{thing}-#{j.count}-#{colour}.#{extension}" end - # start the server if ruby file executed directly - run! if app_file == $0 + def target label, count, extension = 'svg' + "http://img.shields.io/badge/#{label}-#{@count}-#{Badgerise.colour @count}.#{@extension}" + end end diff --git a/spec/github_badges_spec.rb b/spec/github_badges_spec.rb new file mode 100644 index 0000000..c1cfc4e --- /dev/null +++ b/spec/github_badges_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper.rb' + +module Badgerise + context 'colour' do + it 'sets a colour' do + expect(Badgerise.colour 0).to eq 'brightgreen' + expect(Badgerise.colour 2).to eq 'blue' + expect(Badgerise.colour 4).to eq 'orange' + expect(Badgerise.colour 8).to eq 'red' + expect(Badgerise.colour 'derp').to eq 'red' + end + + it 'sets a label' do + expect(Badgerise.label 'issues').to eq 'open%20issues' + expect(Badgerise.label 'pulls').to eq 'pending%20pull--requests' + expect(Badgerise.label 'unchanged').to eq 'unchanged' + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..15624ab --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,93 @@ +require 'github_badges' + +# 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 + + config.order = :random + +# 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 + + # Limits the available syntax to the non-monkey patched syntax that is recommended. + # For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + 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 From 47e63f91f11b2182408e36e284f417434f3a1659 Mon Sep 17 00:00:00 2001 From: pikesley Date: Thu, 18 Dec 2014 21:42:14 +0000 Subject: [PATCH 2/5] I appear to know what I'm doing --- lib/github_badges.rb | 6 +++--- spec/github_badges_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/github_badges.rb b/lib/github_badges.rb index 7d8d4a5..2e76296 100644 --- a/lib/github_badges.rb +++ b/lib/github_badges.rb @@ -43,7 +43,7 @@ def bounce end def target - "http://img.shields.io/badge/#{label}-#{@count}-#{Badgerise.colour @count}.#{@extension}" + "http://img.shields.io/badge/#{}-#{@count}-#{Badgerise.colour @count}.#{@extension}" end end @@ -72,7 +72,7 @@ def Badgerise.label source end end - def target label, count, extension = 'svg' - "http://img.shields.io/badge/#{label}-#{@count}-#{Badgerise.colour @count}.#{@extension}" + def Badgerise.target type, count, extension = 'svg' + "http://img.shields.io/badge/#{label type}-#{count}-#{Badgerise.colour count}.#{extension}" end end diff --git a/spec/github_badges_spec.rb b/spec/github_badges_spec.rb index c1cfc4e..2f230b3 100644 --- a/spec/github_badges_spec.rb +++ b/spec/github_badges_spec.rb @@ -15,5 +15,10 @@ module Badgerise expect(Badgerise.label 'pulls').to eq 'pending%20pull--requests' expect(Badgerise.label 'unchanged').to eq 'unchanged' end + + it 'generates a target url' do + expect(Badgerise.target 'issues', 4).to eq 'http://img.shields.io/badge/open%20issues-4-orange.svg' + expect(Badgerise.target 'pulls', 1).to eq 'http://img.shields.io/badge/pending%20pull--requests-1-blue.svg' + end end end From ec2ed99513526831faf127d1279e16ca521b0cfe Mon Sep 17 00:00:00 2001 From: pikesley Date: Thu, 18 Dec 2014 21:44:39 +0000 Subject: [PATCH 3/5] Woop woop --- lib/github_badges.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/github_badges.rb b/lib/github_badges.rb index 2e76296..d71388e 100644 --- a/lib/github_badges.rb +++ b/lib/github_badges.rb @@ -39,11 +39,7 @@ class GithubBadges < Sinatra::Base run! if app_file == $0 def bounce - redirect target - end - - def target - "http://img.shields.io/badge/#{}-#{@count}-#{Badgerise.colour @count}.#{@extension}" + redirect Badgerise.target @thing, @count, @extension end end From 9cddc50a892e92ba3f4e0cb4a77d5b3eb1e54d65 Mon Sep 17 00:00:00 2001 From: pikesley Date: Thu, 18 Dec 2014 22:02:01 +0000 Subject: [PATCH 4/5] Hmm, this requires more thought than I can muster now --- lib/github_badges.rb | 6 ++++++ spec/github_badges_spec.rb | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/lib/github_badges.rb b/lib/github_badges.rb index d71388e..3f802d5 100644 --- a/lib/github_badges.rb +++ b/lib/github_badges.rb @@ -68,6 +68,12 @@ def Badgerise.label source end end + def Badgerise.get_extension text + parts = params[:thing].split('.') + @thing = thing_parts[0...-1].join('.') + @extension = thing_parts[-1] + end + def Badgerise.target type, count, extension = 'svg' "http://img.shields.io/badge/#{label type}-#{count}-#{Badgerise.colour count}.#{extension}" end diff --git a/spec/github_badges_spec.rb b/spec/github_badges_spec.rb index 2f230b3..39a6dbc 100644 --- a/spec/github_badges_spec.rb +++ b/spec/github_badges_spec.rb @@ -20,5 +20,9 @@ module Badgerise expect(Badgerise.target 'issues', 4).to eq 'http://img.shields.io/badge/open%20issues-4-orange.svg' expect(Badgerise.target 'pulls', 1).to eq 'http://img.shields.io/badge/pending%20pull--requests-1-blue.svg' end + + it 'extracts an extension' do + expect(Badgerise.get_extension 'this.svg').to eq 'svg' + end end end From 49974811ddcce5159c69d96d59237caa0c7f9dee Mon Sep 17 00:00:00 2001 From: pikesley Date: Thu, 18 Dec 2014 23:15:45 +0000 Subject: [PATCH 5/5] Extensions --- lib/github_badges.rb | 19 ++++++++++++++++--- spec/github_badges_spec.rb | 8 ++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/github_badges.rb b/lib/github_badges.rb index 3f802d5..1cbef15 100644 --- a/lib/github_badges.rb +++ b/lib/github_badges.rb @@ -69,9 +69,22 @@ def Badgerise.label source end def Badgerise.get_extension text - parts = params[:thing].split('.') - @thing = thing_parts[0...-1].join('.') - @extension = thing_parts[-1] + parts = text.split('.') + if ['svg', 'png'].include? parts[-1] + return parts[-1] + end + + 'svg' + end + + def Badgerise.without_extension text + parts = text.split('.') + + if ['svg', 'png'].include? parts[-1] + return parts[0...-1].join('.') + end + + text end def Badgerise.target type, count, extension = 'svg' diff --git a/spec/github_badges_spec.rb b/spec/github_badges_spec.rb index 39a6dbc..53bbd09 100644 --- a/spec/github_badges_spec.rb +++ b/spec/github_badges_spec.rb @@ -23,6 +23,14 @@ module Badgerise it 'extracts an extension' do expect(Badgerise.get_extension 'this.svg').to eq 'svg' + expect(Badgerise.get_extension 'that.png').to eq 'png' + expect(Badgerise.get_extension 'this.has.no.extension').to eq 'svg' + end + + it 'extracts the non-extension string' do + expect(Badgerise.without_extension 'some.name'). to eq 'some.name' + expect(Badgerise.without_extension 'some.other.name.png'). to eq 'some.other.name' + expect(Badgerise.without_extension 'more-name.svg'). to eq 'more-name' end end end