Skip to content

Commit

Permalink
ADD specs
Browse files Browse the repository at this point in the history
Project lacks specs. At least offer some unit testing.

Details
* ADD `rspec`
* ADD specs
* REFACTOR
  • Loading branch information
MaximeD committed Feb 25, 2015
1 parent a38f4f7 commit f781d7f
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--require spec_helper
--format documentation
2 changes: 2 additions & 0 deletions gem_updater.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'json', '~> 1.8'
s.add_runtime_dependency 'nokogiri', '~> 1.6'

s.add_development_dependency 'rspec'

s.executables << 'gem_update'
end
4 changes: 2 additions & 2 deletions lib/gem_updater/source_page_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize( url: nil, version: nil )
def changelog
@changelog ||= begin
puts "Looking for a changelog in #{@uri}"
doc = Nokogiri::HTML(open( @uri ) )
doc = Nokogiri::HTML( open( @uri ) )

find_changelog( doc )

Expand Down Expand Up @@ -119,7 +119,7 @@ def find_changelog_link
# @param url [String] url of changelog
# @return [String, nil] anchor's href
def find_anchor( url )
changelog_page = Nokogiri::HTML(open( url ) )
changelog_page = Nokogiri::HTML( open( url ) )
anchor = changelog_page.css( %(a.anchor) ).find{ |element| element.attr( 'href' ).match( @version.gsub('.', '') ) }

if anchor
Expand Down
69 changes: 69 additions & 0 deletions spec/gem_updater/gem_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe GemUpdater::GemFile do
subject( :gemfile ) { GemUpdater::GemFile.new }
let( :bundler_cli ){ OpenStruct.new( update: true, compute_changes: true ) }

def old_gem_set
Bundler::SpecSet.new( [
Gem::Specification.new( 'gem_up_to_date', '0.1' ),
Gem::Specification.new( 'gem_to_update', '1.5' )
] )
end

def new_gem_set
Bundler::SpecSet.new( [
Gem::Specification.new( 'gem_up_to_date', '0.1' ),
Gem::Specification.new( 'gem_to_update', '2.3' )
] )
end

before do
allow( Bundler.definition ).to receive( :specs ).and_return( old_gem_set )
allow( Bundler::CLI ).to receive( :new ).and_return( bundler_cli )
allow( bundler_cli ).to receive( :update )
end

describe '#initialize' do
before { subject }
it 'gets current spec set' do
expect( Bundler.definition ).to have_received( :specs )
end
end

describe '#update!' do
before :each do
allow( subject ).to receive( :compute_changes )
subject.update!
end

it 'launched bundle update' do
expect( bundler_cli ).to have_received( :update )
end

it 'computes changes' do
expect( subject ).to have_received( :compute_changes )
end

it 'gets new spec set' do
expect( Bundler.definition ).to have_received( :specs ).twice
end
end

describe '#compute_changes' do
before :each do
subject
allow( Bundler.definition ).to receive( :specs ).and_return( new_gem_set )
subject.update!
subject.compute_changes
end

it 'skips gems that were not updated' do
expect( subject.changes ).to_not have_key( :gem_up_to_date )
end

it 'includes updated gems with old and new version number' do
expect( subject.changes[ 'gem_to_update' ] ).to eq( versions: { old: '1.5', new: '2.3' } )
end
end
end
51 changes: 51 additions & 0 deletions spec/gem_updater/ruby_gems_fetcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe GemUpdater::RubyGemsFetcher do
subject { GemUpdater::RubyGemsFetcher.new( 'gem_name' ) }

describe '#source_uri' do
context "when 'source_code_uri' is present" do
before do
allow( subject ).to receive_message_chain( :open, :read ) { { source_code_uri: 'source_code_uri' }.to_json }
subject.source_uri
end

it "returns 'source_code_uri' value" do
expect( subject.source_uri ).to eq 'source_code_uri'
end
end

context "when 'homepage_uri' is present" do
before do
allow( subject ).to receive_message_chain( :open, :read ) { { homepage_uri: 'homepage_uri' }.to_json }
subject.source_uri
end

it "returns 'homepage_uri' value" do
expect( subject.source_uri ).to eq 'homepage_uri'
end
end

context "when both 'source_code_uri' and 'homepage_uri' are present" do
before do
allow( subject ).to receive_message_chain( :open, :read ) { { source_code_uri: 'source_code_uri', homepage_uri: 'homepage_uri' }.to_json }
subject.source_uri
end

it "returns 'source_code_uri' value" do
expect( subject.source_uri ).to eq 'source_code_uri'
end
end

context 'none is present' do
before do
allow( subject ).to receive_message_chain( :open, :read ) { {}.to_json }
subject.source_uri
end

it 'is falsey' do
expect( subject.source_uri ).to be_falsey
end
end
end
end
41 changes: 41 additions & 0 deletions spec/gem_updater/source_page_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'
require 'pry'

describe GemUpdater::SourcePageParser do
subject { GemUpdater::SourcePageParser.new( url: 'https://github.com/fake_user/fake_gem', version: '0.2' ) }

describe '#changelog' do
context 'when gem is hosted on github' do
context 'when there is no changelog' do
before do
allow( subject ).to receive( :open ) { github_gem_without_changelog }
end

it 'is nil' do
expect( subject.changelog ).to be_nil
end
end

context 'when changelog is in raw text' do
before do
allow( subject ).to receive( :open ) { github_gem_with_raw_changelog }
end

it 'returns url of changelog' do
expect( subject.changelog ).to eq 'https://github.com/fake_user/fake_gem/blob/master/changelog.txt'
end
end

context 'when changelog may contain anchor' do
before do
allow( subject ).to receive( :open ).with( URI( "https://github.com/fake_user/fake_gem" ) ) { github_gem_with_changelog_with_anchor }
allow_any_instance_of( GemUpdater::SourcePageParser::GitHubParser ).to receive( :open ).with( "https://github.com/fake_user/fake_gem/blob/master/changelog.md" ) { github_changelog }
end

it 'returns url of changelog with anchor to version' do
expect( subject.changelog ).to eq 'https://github.com/fake_user/fake_gem/blob/master/changelog.md#02'
end
end
end
end
end
54 changes: 54 additions & 0 deletions spec/gem_updater_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'spec_helper'

describe GemUpdater::Updater do
let( :gemfile ){ OpenStruct.new( update!: true, changes: [] ) }

before :each do
allow( GemUpdater::GemFile ).to receive( :new ).and_return( gemfile )
end

describe '#update' do
before :each do
allow( gemfile ).to receive( :update! )
allow( gemfile ).to receive( :changes ).and_return( { fake_gem: { versions: { old: '0.1', new: '0.2' } } } )
allow( GemUpdater::RubyGemsFetcher ).to receive_message_chain( :new, :source_uri )
allow( GemUpdater::SourcePageParser ).to receive( :new ).and_return( @source_page = OpenStruct.new( changelog: 'fake_gem_changelog_url' ) )
subject.update!
end

it 'updates gemfile' do
expect( gemfile ).to have_received( :update! )
end

it 'gets changelogs' do
expect( gemfile.changes[ :fake_gem ][ :changelog ] ).to eq 'fake_gem_changelog_url'
end
end

describe '#format_diff' do
before :each do
allow( gemfile ).to receive( :changes ).and_return( {
fake_gem_1: { changelog: 'fake_gem_1_url', versions: { old: '1.0', new: '1.1' } },
fake_gem_2: { changelog: 'fake_gem_2_url', versions: { old: '0.4', new: '0.4.2' } }
} )
allow( STDOUT ).to receive( :puts )
subject.format_diff
end

it 'outputs changes' do
expect( STDOUT ).to have_received( :puts ).with( <<CHANGELOG
* fake_gem_1 1.0 → 1.1
[changelog](fake_gem_1_url)
CHANGELOG
)

expect( STDOUT ).to have_received( :puts ).with( <<CHANGELOG
* fake_gem_2 0.4 → 0.4.2
[changelog](fake_gem_2_url)
CHANGELOG
)
end
end
end
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'gem_updater'

Dir["#{File.expand_path('../support', __FILE__)}/*.rb"].each do |file|
require file
end

RSpec.configure do |config|
config.include Spec::Helpers

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.order = :random
end
22 changes: 22 additions & 0 deletions spec/support/fake_web/changelog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="readme" class="blob instapaper_body">
<article class="markdown-body entry-content" itemprop="mainContentOfPage">
<h3>
<a id="user-content-02" class="anchor" href="#02" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
Version 0.2
</h3>

<p>Details</p>


<h3>
<a id="user-content-01" class="anchor" href="#01" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
Version 0.1
</h3>

<p>Details</p>
</article>
</div>
22 changes: 22 additions & 0 deletions spec/support/fake_web/changelog_with_anchor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="files" data-pjax="">
<tbody>
<tr>
<td class="icon">
<span class="octicon octicon-file-text"></span>
<img alt="" class="spinner" height="16" src="https://assets-cdn.github.com/assets/spinners/octocat-spinner-32-e513294efa576953719e4e2de888dd9cf929b7d62ed8d05f25e731d02452ab6c.gif" width="16">
</td>
<td class="content">
<span class="css-truncate css-truncate-target"><a href="/fake_user/fake_gem/blob/master/changelog.md" class="js-directory-link"
id="c28af15433806ee98e1fb1ae32619c17-f0a73dd5b17e914a016b0c326905d0f53f01067f" title="changelog.md">changelog.md</a></span>
</td>
<td class="message">
<span class="css-truncate css-truncate-target">
<a href="/fake_user/fake_gem/commit/43a5c088fe6622db46f357ff172b1e01b4cc06a1" class="message" data-pjax="true" title="Foo">Foo</a>
</span>
</td>
<td class="age">
<span class="css-truncate css-truncate-target"><time datetime="2014-08-08T17:37:39Z" is="time-ago" title="8 août 2014 18:37 UTC+1">7 months ago</time></span>
</td>
</tr>
</tbody>
</table>
21 changes: 21 additions & 0 deletions spec/support/fake_web/no_changelog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<table class="files" data-pjax="">
<tbody>
<tr>
<td class="icon">
<span class="octicon octicon-file-text"></span>
<img alt="" class="spinner" height="16" src="https://assets-cdn.github.com/assets/spinners/octocat-spinner-32-e513294efa576953719e4e2de888dd9cf929b7d62ed8d05f25e731d02452ab6c.gif" width="16">
</td>
<td class="content">
<span class="css-truncate css-truncate-target"><a href="/fake_user/fake_gem/blob/master/foo" class="js-directory-link" id="c28af15433806ee98e1fb1ae32619c17-f0a73dd5b17e914a016b0c326905d0f53f01067f" title="foo">foo</a></span>
</td>
<td class="message">
<span class="css-truncate css-truncate-target">
<a href="/fake_user/fake_gem/commit/43a5c088fe6622db46f357ff172b1e01b4cc06a1" class="message" data-pjax="true" title="Foo">Foo</a>
</span>
</td>
<td class="age">
<span class="css-truncate css-truncate-target"><time datetime="2014-08-08T17:37:39Z" is="time-ago" title="8 août 2014 18:37 UTC+1">7 months ago</time></span>
</td>
</tr>
</tbody>
</table>
22 changes: 22 additions & 0 deletions spec/support/fake_web/raw_changelog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="files" data-pjax="">
<tbody>
<tr>
<td class="icon">
<span class="octicon octicon-file-text"></span>
<img alt="" class="spinner" height="16" src="https://assets-cdn.github.com/assets/spinners/octocat-spinner-32-e513294efa576953719e4e2de888dd9cf929b7d62ed8d05f25e731d02452ab6c.gif" width="16">
</td>
<td class="content">
<span class="css-truncate css-truncate-target"><a href="/fake_user/fake_gem/blob/master/changelog.txt" class="js-directory-link"
id="c28af15433806ee98e1fb1ae32619c17-f0a73dd5b17e914a016b0c326905d0f53f01067f" title="changelog">changelog</a></span>
</td>
<td class="message">
<span class="css-truncate css-truncate-target">
<a href="/fake_user/fake_gem/commit/43a5c088fe6622db46f357ff172b1e01b4cc06a1" class="message" data-pjax="true" title="Foo">Foo</a>
</span>
</td>
<td class="age">
<span class="css-truncate css-truncate-target"><time datetime="2014-08-08T17:37:39Z" is="time-ago" title="8 août 2014 18:37 UTC+1">7 months ago</time></span>
</td>
</tr>
</tbody>
</table>
24 changes: 24 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Spec
module Helpers

def fake_web_dir
'../fake_web'
end

def github_gem_without_changelog
open( File.expand_path( "#{fake_web_dir}/no_changelog.html", __FILE__) )
end

def github_gem_with_raw_changelog
open( File.expand_path( "#{fake_web_dir}/raw_changelog.html", __FILE__) )
end

def github_gem_with_changelog_with_anchor
open( File.expand_path( "#{fake_web_dir}/changelog_with_anchor.html", __FILE__) )
end

def github_changelog
open( File.expand_path( "#{fake_web_dir}/changelog.html", __FILE__) )
end
end
end

0 comments on commit f781d7f

Please sign in to comment.