Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Write unit test for dependencies class
Browse files Browse the repository at this point in the history
[#83883822]
  • Loading branch information
Rasheed Abdul-Aziz and Sai To Yeung committed Dec 9, 2014
1 parent b7cf21f commit 08154be
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 20 deletions.
17 changes: 13 additions & 4 deletions bin/translate_dependency_url
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'dependencies'
require 'compile_extensions'
require 'yaml'

uri = ARGV[0]
cache_path = File.join(`pwd`.chomp, 'dependencies')

dependencies = Dependencies.new
manifest = YAML.load_file('manifest.yml')
dependencies = CompileExtensions::Dependencies.new(manifest)

translated_uri = dependencies.find_translated_dependency(uri)

if translated_uri.nil?
puts "DEPENDENCY_MISSING_IN_MANIFEST: #{uri}"
exit 1
end

if File.exist? cache_path
file_path = File.join(cache_path, dependencies.find_translated_dependency(uri).gsub(/[\/:]/, '_'))
file_path = File.join(cache_path, translated_uri.gsub(/[\/:]/, '_'))

puts "file://#{file_path}"
else
puts dependencies.find_translated_dependency(uri)
puts translated_uri
end


1 change: 1 addition & 0 deletions lib/compile_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'dependencies'
28 changes: 14 additions & 14 deletions lib/dependencies.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
require 'yaml'
module CompileExtensions
class Dependencies
def initialize(manifest)
@manifest = manifest
end

class Dependencies
def find_matching_dependency(uri)
manifest = YAML.load_file('manifest.yml')
manifest['dependencies'].find do |dependency|
dependency['original'] == uri
def find_matching_dependency(uri)
@manifest['dependencies'].find do |dependency|
dependency['original'] == uri
end
end
end

def find_translated_dependency(uri)
matching_dependency = find_matching_dependency(uri)
def find_translated_dependency(uri)
dependency = find_matching_dependency(uri)

if matching_dependency.nil?
puts "DEPENDENCY_MISSING_IN_MANIFEST: #{uri}"
exit 1
end
return nil if dependency.nil?

matching_dependency['xlated']
dependency['xlated']
end
end
end
4 changes: 2 additions & 2 deletions spec/integration/translate_dependency_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def run_translate
end

specify do
translated_url, _, _ = run_translate
translated_url, stderr, _ = run_translate
puts stderr

expect(translated_url).to eq("file://#{buildpack_dir}/dependencies/https___another_location_a_file_elsewhere.zip\n")
end
Expand All @@ -68,7 +69,6 @@ def run_translate
expect(translated_url).to eq "DEPENDENCY_MISSING_IN_MANIFEST: #{original_url}\n"
expect(status).not_to be_success
end

end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'rspec'
require 'tmpdir'
require 'compile_extensions'
66 changes: 66 additions & 0 deletions spec/unit/compile_extensions/dependencies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'spec_helper'

module CompileExtensions
describe Dependencies do

let(:manifest) {
{
'dependencies' => [
{
'original' => 'https://google.com/backdoor',
'xlated' => 'my_dog_has_fleas'
},
{
'original' => 'https://google.com/frontdoor',
'xlated' => 'i_do_not_like_green_eggs_and_ham'
}
]
}
}

subject(:dependencies) { CompileExtensions::Dependencies.new(manifest) }

describe 'find matching dependency hash' do
let(:matching_dependency) { dependencies.find_matching_dependency(original_url) }

context 'a key that should match the first item' do
let(:original_url) { 'https://google.com/backdoor' }

specify do
expect(matching_dependency['original']).to eql('https://google.com/backdoor')
expect(matching_dependency['xlated']).to eql('my_dog_has_fleas')
end
end

context 'a key that should match another item' do
let(:original_url) { 'https://google.com/frontdoor' }

specify do
expect(matching_dependency['original']).to eql('https://google.com/frontdoor')
expect(matching_dependency['xlated']).to eql('i_do_not_like_green_eggs_and_ham')
end
end

end

describe 'find matching dependency translated url' do
let(:translated_url) { dependencies.find_translated_dependency(original_url) }

context 'a matching dependency' do
let(:original_url) { 'https://google.com/backdoor' }

specify do
expect(translated_url).to eql('my_dog_has_fleas')
end
end

context 'no matching dependency' do
let(:original_url) { 'https://notthere.com' }

specify do
expect(translated_url).to be_nil
end
end
end
end
end

0 comments on commit 08154be

Please sign in to comment.