diff --git a/bin/translate_dependency_url b/bin/translate_dependency_url index 9901904..0fd7431 100755 --- a/bin/translate_dependency_url +++ b/bin/translate_dependency_url @@ -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 diff --git a/lib/compile_extensions.rb b/lib/compile_extensions.rb new file mode 100644 index 0000000..25edcb2 --- /dev/null +++ b/lib/compile_extensions.rb @@ -0,0 +1 @@ +require 'dependencies' diff --git a/lib/dependencies.rb b/lib/dependencies.rb index 5ed6257..e950835 100644 --- a/lib/dependencies.rb +++ b/lib/dependencies.rb @@ -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 diff --git a/spec/integration/translate_dependency_url_spec.rb b/spec/integration/translate_dependency_url_spec.rb index f741c6f..4d6efa6 100644 --- a/spec/integration/translate_dependency_url_spec.rb +++ b/spec/integration/translate_dependency_url_spec.rb @@ -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 @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1bcaab2..a1db453 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,3 @@ require 'rspec' require 'tmpdir' +require 'compile_extensions' diff --git a/spec/unit/compile_extensions/dependencies_spec.rb b/spec/unit/compile_extensions/dependencies_spec.rb new file mode 100644 index 0000000..a3cd98e --- /dev/null +++ b/spec/unit/compile_extensions/dependencies_spec.rb @@ -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