buildpack-support 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/LICENSE +202 -0
  2. data/NOTICE +2 -0
  3. data/docs/cache.md +77 -0
  4. data/docs/component.md +1 -0
  5. data/docs/configuration.md +27 -0
  6. data/docs/logging.md +54 -0
  7. data/docs/other.md +1 -0
  8. data/docs/rake.md +1 -0
  9. data/docs/repository.md +116 -0
  10. data/docs/test.md +1 -0
  11. data/lib/buildpack_support.rb +18 -0
  12. data/lib/buildpack_support/base_buildpack.rb +166 -0
  13. data/lib/buildpack_support/buildpack_version.rb +124 -0
  14. data/lib/buildpack_support/cache.rb +24 -0
  15. data/lib/buildpack_support/cache/application_cache.rb +41 -0
  16. data/lib/buildpack_support/cache/cached_file.rb +103 -0
  17. data/lib/buildpack_support/cache/download_cache.rb +280 -0
  18. data/lib/buildpack_support/cache/inferred_network_failure.rb +26 -0
  19. data/lib/buildpack_support/cache/internet_availability.rb +64 -0
  20. data/lib/buildpack_support/component.rb +24 -0
  21. data/lib/buildpack_support/component/application.rb +76 -0
  22. data/lib/buildpack_support/component/base_component.rb +78 -0
  23. data/lib/buildpack_support/component/base_droplet.rb +96 -0
  24. data/lib/buildpack_support/component/downloads.rb +88 -0
  25. data/lib/buildpack_support/component/services.rb +84 -0
  26. data/lib/buildpack_support/component/versioned_dependency_component.rb +71 -0
  27. data/lib/buildpack_support/component/versioned_downloads.rb +57 -0
  28. data/lib/buildpack_support/component/with_timing.rb +40 -0
  29. data/lib/buildpack_support/configuration_utils.rb +58 -0
  30. data/lib/buildpack_support/constantize.rb +46 -0
  31. data/lib/buildpack_support/dash_case.rb +29 -0
  32. data/lib/buildpack_support/directory_finder.rb +45 -0
  33. data/lib/buildpack_support/filtering_pathname.rb +227 -0
  34. data/lib/buildpack_support/format_duration.rb +57 -0
  35. data/lib/buildpack_support/logging.rb +22 -0
  36. data/lib/buildpack_support/logging/delegating_logger.rb +48 -0
  37. data/lib/buildpack_support/logging/logger_factory.rb +148 -0
  38. data/lib/buildpack_support/qualify_path.rb +36 -0
  39. data/lib/buildpack_support/rake.rb +22 -0
  40. data/lib/buildpack_support/rake/buildpack_stage_task.rb +86 -0
  41. data/lib/buildpack_support/rake/cached_artifact_finder.rb +99 -0
  42. data/lib/buildpack_support/rake/check_api_doc_task.rb +70 -0
  43. data/lib/buildpack_support/rake/dependency_cache_task.rb +87 -0
  44. data/lib/buildpack_support/rake/disable_remote_downloads_task.rb +80 -0
  45. data/lib/buildpack_support/rake/package_task.rb +133 -0
  46. data/lib/buildpack_support/rake/package_zip_task.rb +80 -0
  47. data/lib/buildpack_support/rake/repository_configuration_finder.rb +66 -0
  48. data/lib/buildpack_support/rake/write_version_file_task.rb +82 -0
  49. data/lib/buildpack_support/repository.rb +24 -0
  50. data/lib/buildpack_support/repository/configured_item.rb +81 -0
  51. data/lib/buildpack_support/repository/repository_index.rb +98 -0
  52. data/lib/buildpack_support/repository/wildcard_version_resolver.rb +75 -0
  53. data/lib/buildpack_support/shell.rb +41 -0
  54. data/lib/buildpack_support/snake_case.rb +30 -0
  55. data/lib/buildpack_support/space_case.rb +29 -0
  56. data/lib/buildpack_support/test/application_helper.rb +41 -0
  57. data/lib/buildpack_support/test/base_component_helper.rb +59 -0
  58. data/lib/buildpack_support/test/base_droplet_helper.rb +36 -0
  59. data/lib/buildpack_support/test/console_helper.rb +57 -0
  60. data/lib/buildpack_support/test/environment_helper.rb +32 -0
  61. data/lib/buildpack_support/test/internet_availability_helper.rb +29 -0
  62. data/lib/buildpack_support/test/logging_helper.rb +50 -0
  63. data/lib/buildpack_support/test/scratch_helper.rb +32 -0
  64. data/lib/buildpack_support/test/versioned_dependency_component_helper.rb +32 -0
  65. data/lib/buildpack_support/test/with_load_path_helper.rb +27 -0
  66. data/lib/buildpack_support/to_b.rb +38 -0
  67. data/lib/buildpack_support/tokenized_version.rb +157 -0
  68. data/lib/buildpack_support/version.rb +23 -0
  69. data/spec/buildpack_support/base_buildpack_spec.rb +112 -0
  70. data/spec/buildpack_support/buildpack_version_spec.rb +122 -0
  71. data/spec/buildpack_support/cache/application_cache_spec.rb +56 -0
  72. data/spec/buildpack_support/cache/cached_file_spec.rb +94 -0
  73. data/spec/buildpack_support/cache/download_cache_spec.rb +293 -0
  74. data/spec/buildpack_support/cache/internet_availability_spec.rb +57 -0
  75. data/spec/buildpack_support/cache/yield_file_with_content.rb +30 -0
  76. data/spec/buildpack_support/component/application_spec.rb +81 -0
  77. data/spec/buildpack_support/component/base_component_spec.rb +81 -0
  78. data/spec/buildpack_support/component/base_droplet_spec.rb +72 -0
  79. data/spec/buildpack_support/component/downloads_spec.rb +63 -0
  80. data/spec/buildpack_support/component/services_spec.rb +80 -0
  81. data/spec/buildpack_support/component/versioned_dependency_component_spec.rb +58 -0
  82. data/spec/buildpack_support/component/versioned_downloads_spec.rb +58 -0
  83. data/spec/buildpack_support/component/with_timing_spec.rb +30 -0
  84. data/spec/buildpack_support/configuration_utils_spec.rb +39 -0
  85. data/spec/buildpack_support/constantize_spec.rb +34 -0
  86. data/spec/buildpack_support/dash_case_spec.rb +41 -0
  87. data/spec/buildpack_support/directory_finder_spec.rb +41 -0
  88. data/spec/buildpack_support/filtering_pathname_spec.rb +443 -0
  89. data/spec/buildpack_support/format_duration_spec.rb +60 -0
  90. data/spec/buildpack_support/logging/delegating_logger_spec.rb +62 -0
  91. data/spec/buildpack_support/logging/logger_factory_spec.rb +262 -0
  92. data/spec/buildpack_support/qualify_path_spec.rb +42 -0
  93. data/spec/buildpack_support/rake/buildpack_stage_task_spec.rb +88 -0
  94. data/spec/buildpack_support/rake/cached_artifact_finder_spec.rb +73 -0
  95. data/spec/buildpack_support/rake/check_api_doc_task_spec.rb +69 -0
  96. data/spec/buildpack_support/rake/dependency_cache_task_spec.rb +133 -0
  97. data/spec/buildpack_support/rake/disable_remote_downloads_task_spec.rb +91 -0
  98. data/spec/buildpack_support/rake/package_task_spec.rb +335 -0
  99. data/spec/buildpack_support/rake/package_zip_task_spec.rb +91 -0
  100. data/spec/buildpack_support/rake/repository_configuration_finder_spec.rb +61 -0
  101. data/spec/buildpack_support/rake/write_version_file_task_spec.rb +96 -0
  102. data/spec/buildpack_support/repository/configured_item_spec.rb +78 -0
  103. data/spec/buildpack_support/repository/repository_index_spec.rb +118 -0
  104. data/spec/buildpack_support/repository/wildcard_version_resolver_spec.rb +73 -0
  105. data/spec/buildpack_support/shell_spec.rb +32 -0
  106. data/spec/buildpack_support/snake_case_spec.rb +45 -0
  107. data/spec/buildpack_support/space_case_spec.rb +41 -0
  108. data/spec/buildpack_support/to_b_spec.rb +41 -0
  109. data/spec/buildpack_support/tokenized_version_spec.rb +132 -0
  110. data/spec/fixtures/application/test-file +0 -0
  111. data/spec/fixtures/config/found-config.yml +2 -0
  112. data/spec/fixtures/droplet-resources/droplet-resource +0 -0
  113. data/spec/fixtures/stub-download-with-top-level.zip +0 -0
  114. data/spec/fixtures/stub-download.tar.gz +0 -0
  115. data/spec/fixtures/stub-download.zip +0 -0
  116. data/spec/fixtures/test-cache.yml +18 -0
  117. data/spec/fixtures/test-index.yml +2 -0
  118. data/spec/fixtures/test_component.rb +0 -0
  119. data/spec/fixtures/zip-contents/test-directory/test-deep-file +0 -0
  120. data/spec/fixtures/zip-contents/test-file +0 -0
  121. data/spec/spec_helper.rb +30 -0
  122. metadata +416 -0
@@ -0,0 +1,38 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # A mixin that adds the ability to turn a +String+ into a boolean
17
+ class String
18
+
19
+ # Converts a +String+ to a boolean
20
+ #
21
+ # @return [Boolean] +true+ if +<STRING>.downcase == 'true'+. +false+ otherwise
22
+ def to_b
23
+ downcase == 'true'
24
+ end
25
+
26
+ end
27
+
28
+ # A mixin that adds the ability to turn a +nil+ into a boolean
29
+ class NilClass
30
+
31
+ # Converts a +nil+ to a boolean
32
+ #
33
+ # @return [Boolean] +false+ always
34
+ def to_b
35
+ false
36
+ end
37
+
38
+ end
@@ -0,0 +1,157 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support'
17
+
18
+ module BuildpackSupport
19
+
20
+ # A utility for manipulating JRE version numbers.
21
+ class TokenizedVersion < Array
22
+ include Comparable
23
+
24
+ # The wildcard component.
25
+ WILDCARD = '+'
26
+
27
+ # Create a tokenized version based on the input string.
28
+ #
29
+ # @param [String] version a version string
30
+ # @param [Boolean] allow_wildcards whether or not to allow '+' as the last component to represent a wildcard
31
+ def initialize(version, allow_wildcards = true)
32
+ @version = version
33
+ @version = WILDCARD if !@version && allow_wildcards
34
+
35
+ major, tail = major_or_minor_and_tail @version
36
+ minor, tail = major_or_minor_and_tail tail
37
+ micro, qualifier = micro_and_qualifier tail
38
+
39
+ concat [major, minor, micro, qualifier]
40
+ validate allow_wildcards
41
+ end
42
+
43
+ # Compare this to another array
44
+ #
45
+ # @return [Integer] A numerical representation of the comparison between two instances
46
+ def <=>(other)
47
+ comparison = 0
48
+ i = 0
49
+ while comparison == 0 && i < 3
50
+ comparison = self[i].to_i <=> other[i].to_i
51
+ i += 1
52
+ end
53
+ comparison = qualifier_compare(non_nil_qualifier(self[3]), non_nil_qualifier(other[3])) if comparison == 0
54
+
55
+ comparison
56
+ end
57
+
58
+ # Convert this to a string
59
+ #
60
+ # @return [String] a string representation of this tokenized version
61
+ def to_s
62
+ @version
63
+ end
64
+
65
+ # Check that this version has at most the given number of components.
66
+ #
67
+ # @param [Integer] maximum_components the maximum number of components this version is allowed to have
68
+ # @raise if this version has more than the given number of components
69
+ def check_size(maximum_components)
70
+ fail "Malformed version #{self}: too many version components" if self[maximum_components]
71
+ end
72
+
73
+ private
74
+
75
+ COLLATING_SEQUENCE = (['-', '.'] + ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a).freeze
76
+
77
+ private_constant :COLLATING_SEQUENCE
78
+
79
+ def char_compare(c1, c2)
80
+ COLLATING_SEQUENCE.index(c1) <=> COLLATING_SEQUENCE.index(c2)
81
+ end
82
+
83
+ def major_or_minor_and_tail(s)
84
+ if s.nil? || s.empty?
85
+ major_or_minor, tail = nil, nil
86
+ else
87
+ fail "Invalid version '#{s}': must not end in '.'" if s[-1] == '.'
88
+ fail "Invalid version '#{s}': missing component" if s =~ /\.[\._]/
89
+ tokens = s.match(/^([^\.]+)(?:\.(.*))?/)
90
+
91
+ major_or_minor, tail = tokens[1..-1]
92
+
93
+ fail "Invalid major or minor version '#{major_or_minor}'" unless valid_major_minor_or_micro major_or_minor
94
+ end
95
+
96
+ [major_or_minor, tail]
97
+ end
98
+
99
+ def micro_and_qualifier(s)
100
+ if s.nil? || s.empty?
101
+ micro, qualifier = nil, nil
102
+ else
103
+ fail "Invalid version '#{s}': must not end in '_'" if s[-1] == '_'
104
+ tokens = s.match(/^([^\_]+)(?:_(.*))?/)
105
+
106
+ micro, qualifier = tokens[1..-1]
107
+
108
+ fail "Invalid micro version '#{micro}'" unless valid_major_minor_or_micro micro
109
+ fail "Invalid qualifier '#{qualifier}'" unless valid_qualifier qualifier
110
+ end
111
+
112
+ [micro, qualifier]
113
+ end
114
+
115
+ def minimum_qualifier_length(a, b)
116
+ [a.length, b.length].min
117
+ end
118
+
119
+ def qualifier_compare(a, b)
120
+ comparison = 0
121
+
122
+ i = 0
123
+ until comparison != 0 || i == minimum_qualifier_length(a, b)
124
+ comparison = char_compare(a[i], b[i])
125
+ i += 1
126
+ end
127
+
128
+ comparison = a.length <=> b.length if comparison == 0
129
+
130
+ comparison
131
+ end
132
+
133
+ def non_nil_qualifier(qualifier)
134
+ qualifier.nil? ? '' : qualifier
135
+ end
136
+
137
+ def validate(allow_wildcards)
138
+ wildcarded = false
139
+ each do |value|
140
+ fail "Invalid version '#{@version}': wildcards are not allowed this context" if value == WILDCARD && !allow_wildcards
141
+
142
+ fail "Invalid version '#{@version}': no characters are allowed after a wildcard" if wildcarded && value
143
+ wildcarded = true if value == WILDCARD
144
+ end
145
+ fail "Invalid version '#{@version}': missing component" if !wildcarded && compact.length < 3
146
+ end
147
+
148
+ def valid_major_minor_or_micro(major_minor_or_micro)
149
+ major_minor_or_micro =~ /^[\d]*$/ || major_minor_or_micro =~ /^\+$/
150
+ end
151
+
152
+ def valid_qualifier(qualifier)
153
+ qualifier.nil? || qualifier.empty? || qualifier =~ /^[-\.a-zA-Z\d]*$/ || qualifier =~ /^\+$/
154
+ end
155
+ end
156
+
157
+ end
@@ -0,0 +1,23 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'buildpack_support'
17
+
18
+ module BuildpackSupport
19
+
20
+ # The version of the gem
21
+ VERSION = '1.0.0'
22
+
23
+ end
@@ -0,0 +1,112 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'spec_helper'
17
+ require 'buildpack_support/base_buildpack'
18
+ require 'buildpack_support/test/console_helper'
19
+ require 'buildpack_support/test/logging_helper'
20
+ require 'buildpack_support/test/scratch_helper'
21
+
22
+ describe BuildpackSupport::BaseBuildpack do
23
+ include_context 'console_helper'
24
+ include_context 'logging_helper'
25
+ include_context 'scratch_helper'
26
+
27
+ let(:component1) { double 'component1', class: 'Component1Class', detect: nil }
28
+ let(:component2) { double 'component2', class: 'Component2Class', detect: %w(tag1 tag2) }
29
+ let(:component3) { double 'component3', class: 'Component3Class', detect: %w(tag3 tag4) }
30
+
31
+ let(:buildpack) do
32
+ buildpack = nil
33
+ StubBuildpackClass.with_buildpack(scratch_dir, 'Error %s') { |b| buildpack = b }
34
+ buildpack
35
+ end
36
+
37
+ it 'should fail to detect' do
38
+ expect { buildpack.detect }.to raise_error("Method 'detect' must be defined")
39
+ end
40
+
41
+ it 'should fail to compile' do
42
+ expect { buildpack.compile }.to raise_error("Method 'compile' must be defined")
43
+ end
44
+
45
+ it 'should fail to release' do
46
+ expect { buildpack.release }.to raise_error("Method 'release' must be defined")
47
+ end
48
+
49
+ it 'handles exceptions correctly' do
50
+ expect { with_buildpack { |_buildpack| fail 'an exception' } }.to raise_error SystemExit
51
+ expect(stderr.string).to match(/an exception/)
52
+ end
53
+
54
+ it 'should require files needed for components' do
55
+ expect(buildpack).to receive(:require).with('test_component')
56
+ buildpack.require_component Pathname.new('spec/fixtures'), 'test-component'
57
+ end
58
+
59
+ it 'should not require files needed for components if they do not exist',
60
+ log_level: 'debug' do
61
+ expect(buildpack).not_to receive(:require).with('another_test_component')
62
+ buildpack.require_component Pathname.new('spec/fixtures'), 'another-test-component'
63
+ expect(stderr.string).to match('Cannot require another-test-component because spec/fixtures/another_test_component.rb does not exist')
64
+ end
65
+
66
+ it 'should return detected components and their tags' do
67
+ components, tags = buildpack.detection 'test', [component1, component2, component3], false
68
+ expect(components).not_to include(component1)
69
+ expect(components).to include(component2, component3)
70
+ expect(tags).to include(%w(tag1 tag2), %w(tag3 tag4))
71
+ end
72
+
73
+ it 'should return no components or tags when no components are detected and unique is true' do
74
+ components, tags = buildpack.detection 'test', [component1], true
75
+ expect(components).to eq([])
76
+ expect(tags).to eq([])
77
+ end
78
+
79
+ it 'should return a component and its tags when unique is true' do
80
+ components, tags = buildpack.detection 'test', [component2], true
81
+ expect(components).to include(component2)
82
+ expect(tags).to include(%w(tag1 tag2))
83
+ end
84
+
85
+ it 'should raise an error when more than one component is detected and unique is true' do
86
+ expect { buildpack.detection 'test', [component1, component2, component3], true }.to raise_error('Application can be run by more than one test: Component2 Class, Component3 Class')
87
+ end
88
+
89
+ it 'should return detected components' do
90
+ components = buildpack.component_detection 'test', [component1, component2, component3], false
91
+ expect(components).not_to include(component1)
92
+ expect(components).to include(component2, component3)
93
+ end
94
+
95
+ it 'should return detected tags' do
96
+ tags = buildpack.tag_detection 'test', [component1, component2, component3], false
97
+ expect(tags).to include(%w(tag1 tag2), %w(tag3 tag4))
98
+ end
99
+
100
+ def with_buildpack
101
+ described_class.with_buildpack(scratch_dir, 'Error %s') do |buildpack|
102
+ yield buildpack
103
+ end
104
+ end
105
+
106
+ end
107
+
108
+ class StubBuildpackClass < BuildpackSupport::BaseBuildpack
109
+
110
+ public :component_detection, :detection, :require_component, :tag_detection
111
+
112
+ end
@@ -0,0 +1,122 @@
1
+ # Encoding: utf-8
2
+ # Copyright 2013-2014 the original author or authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ require 'spec_helper'
16
+ require 'buildpack_support/buildpack_version'
17
+ require 'buildpack_support/test/environment_helper'
18
+ require 'buildpack_support/test/logging_helper'
19
+ require 'pathname'
20
+
21
+ describe BuildpackSupport::BuildpackVersion do
22
+ include_context 'environment_helper'
23
+ include_context 'logging_helper'
24
+
25
+ let(:buildpack_version) { described_class.new }
26
+
27
+ before do |example|
28
+ configuration = example.metadata[:configuration] || {}
29
+ allow_any_instance_of(BuildpackSupport::ConfigurationUtils).to receive(:load).with('version')
30
+ .and_return(configuration)
31
+ end
32
+
33
+ it 'should create offline version string from config/version.yml',
34
+ log_level: 'DEBUG',
35
+ configuration: { 'hash' => 'test-hash', 'offline' => true,
36
+ 'remote' => 'test-remote', 'version' => 'test-version' } do
37
+
38
+ expect(buildpack_version.to_s).to match(/test-version (offline) | test-remote#test-hash/)
39
+ expect(buildpack_version.to_s(false)).to match(/test-version-offline-test-remote#test-hash/)
40
+ expect(stderr.string).to match(/test-version (offline) | test-remote#test-hash/)
41
+ end
42
+
43
+ it 'should create online version string from config/version.yml',
44
+ log_level: 'DEBUG',
45
+ configuration: { 'hash' => 'test-hash', 'offline' => false,
46
+ 'remote' => 'test-remote', 'version' => 'test-version' } do
47
+
48
+ expect(buildpack_version.to_s).to match(/test-version | test-remote#test-hash/)
49
+ expect(buildpack_version.to_s(false)).to match(/test-version-test-remote#test-hash/)
50
+ expect(stderr.string).to match(/test-version | test-remote#test-hash/)
51
+ end
52
+
53
+ it 'should create version string from git repository if no config/version.yml exists',
54
+ log_level: 'DEBUG' do
55
+
56
+ git_dir = Pathname.new('.git').expand_path
57
+
58
+ allow_any_instance_of(described_class).to receive(:system)
59
+ .with('which git > /dev/null')
60
+ .and_return(true)
61
+ allow_any_instance_of(described_class).to receive(:`)
62
+ .with("git --git-dir=#{git_dir} rev-parse --short HEAD")
63
+ .and_return('test-hash')
64
+ allow_any_instance_of(described_class).to receive(:`)
65
+ .with("git --git-dir=#{git_dir} config --get remote.origin.url")
66
+ .and_return('test-remote')
67
+
68
+ expect(buildpack_version.to_s).to match(/test-remote#test-hash/)
69
+ expect(buildpack_version.to_s(false)).to match(/test-remote#test-hash/)
70
+ expect(stderr.string).to match(/test-remote#test-hash/)
71
+ end
72
+
73
+ it 'should create unknown version string if no config/version.yml exists and it is not in a git repository',
74
+ log_level: 'DEBUG' do
75
+
76
+ allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
77
+
78
+ expect(buildpack_version.to_s).to match(/unknown/)
79
+ expect(buildpack_version.to_s(false)).to match(/unknown/)
80
+ expect(stderr.string).to match(/unknown/)
81
+ end
82
+
83
+ it 'should create a has from the values',
84
+ configuration: { 'hash' => 'test-hash', 'offline' => true,
85
+ 'remote' => 'test-remote', 'version' => 'test-version' } do |example|
86
+
87
+ allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
88
+
89
+ expect(buildpack_version.to_hash).to eq(example.metadata[:configuration])
90
+ end
91
+
92
+ it 'should exclude non-populated values from the hash' do
93
+ allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
94
+
95
+ expect(buildpack_version.to_hash).to eq({})
96
+ end
97
+
98
+ context do
99
+
100
+ let(:environment) { { 'OFFLINE' => 'true' } }
101
+
102
+ it 'should pick up offline from the environment' do
103
+ allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
104
+
105
+ expect(buildpack_version.offline).to be
106
+ end
107
+
108
+ end
109
+
110
+ context do
111
+
112
+ let(:environment) { { 'VERSION' => 'test-version' } }
113
+
114
+ it 'should pick up version from the environment' do
115
+ allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
116
+
117
+ expect(buildpack_version.version).to match(/test-version/)
118
+ end
119
+
120
+ end
121
+
122
+ end