Skip to content
This repository was archived by the owner on Dec 24, 2021. It is now read-only.

Commit e75e9ca

Browse files
Bumps version v2.0.1 -> v2.0.2
- Streams to STDOUT for commands executed by backticks - Fixes `rake dependency:update` not updating due to metadata.rb pins - Fixes borked tests
1 parent 6b024f8 commit e75e9ca

File tree

8 files changed

+144
-70
lines changed

8 files changed

+144
-70
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
We follow [Semantic Versioning](http://semver.org/) as a way of measuring stability of an update. This
44
means we will never make a backwards-incompatible change within a major version of the project.
55

6+
## v2.0.2 (2018-09-17)
7+
8+
- Adds missing dependencies to gemspec for berkshelf and chef (dependency management)
9+
- [BUG] `rake dependency:update` leaves dependencies in wrapper cookbook on older than latest allowable version, per constraints defined in `Berksfile`
10+
- [BUG] `berks` commands seem unresponsive due to no output to STDOUT
11+
612
## v2.0.1 (2018-09-17)
713

814
- Adds missing spec for minimum ruby version supported (v2.3.0 or higher in the v2.x release)

lib/rordan_gramsay.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ def self.silence_warnings
88
yield
99
$VERBOSE = original_verbosity
1010
end
11+
12+
def self.stream_command(cmd)
13+
require 'pty'
14+
PTY.spawn(cmd) do |stdout, _stdin, _pid|
15+
begin
16+
stdout.each { |line| $stdout.puts(line) }
17+
rescue Errno::EIO
18+
nil # noop
19+
end
20+
end
21+
rescue PTY::ChildExited
22+
nil # noop
23+
end
1124
end

lib/rordan_gramsay/chef_tasks/dependencies.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
11
require 'paint'
22
require_relative '../dependencies'
3+
require_relative '../../rordan_gramsay'
34

45
# rubocop:disable Lint/UnneededCopDisableDirective
56

67
file 'Berksfile.lock' do
7-
`berks install`
8+
RordanGramsay.stream_command('berks install')
89
end
910

1011
task dependency: ['dependency:check']
1112

1213
namespace :dependency do
1314
desc 'Check if version pinning has been done correctly'
1415
task :check do
15-
$stdout.puts(Paint['Inspecting version constraints for errors', :yellow, :bold])
1616
obj = RordanGramsay::Dependencies::Pinning.new
17-
obj.check
17+
obj.check do
18+
$stdout.puts(Paint['Inspecting version constraints for errors', :yellow, :bold])
19+
end
1820
abort if obj.failure?
1921
end
2022

2123
desc 'Remove pinned dependencies from metadata.rb'
2224
task :clean do
23-
$stdout.puts(Paint['Removing dependency entries from metadata.rb', :yellow, :bold])
2425
obj = RordanGramsay::Dependencies::Pinning.new
25-
obj.clean
26+
obj.clean do
27+
$stdout.puts(Paint['Removing dependency entries from metadata.rb', :yellow, :bold])
28+
end
2629
abort if obj.failure?
2730
end
2831

2932
desc '[EXPERIMENTAL] Migrate cookbook dependencies from metadata.rb to Berksfile (for use with dependency:pin task)'
3033
task :migrate do
31-
$stdout.puts(Paint['Attempting to migrate dependencies from metadata.rb to Berksfile', :yellow, :bold])
3234
obj = RordanGramsay::Dependencies::Pinning.new
33-
obj.migrate
35+
obj.migrate do
36+
$stdout.puts(Paint['Attempting to migrate dependencies from metadata.rb to Berksfile', :yellow, :bold])
37+
end
3438
abort if obj.failure?
3539
$stdout.puts(Paint[' This may or may not have worked. Please audit the dependencies in Berksfile', :yellow, :bold])
3640
$stdout.puts(Paint % [' to ensure they look correct, then run %{cmd}', :yellow, :bold, cmd: Paint['rake dependency:update', :white, :bold]])
3741
end
3842

3943
desc 'Update dependency graph'
4044
task :update do
41-
$stdout.puts(Paint['Updating Berksfile.lock', :yellow, :bold])
42-
`berks update`
43-
44-
$stdout.puts(Paint['Pinning dependencies from Berksfile to metadata.rb', :yellow, :bold])
4545
obj = RordanGramsay::Dependencies::Pinning.new
46-
obj.call
46+
obj.update do
47+
$stdout.puts(Paint['Updating Berksfile.lock', :yellow, :bold])
48+
RordanGramsay.stream_command('berks update')
49+
$stdout.puts(Paint['Pinning dependencies from Berksfile to metadata.rb', :yellow, :bold])
50+
end
4751
abort if obj.failure?
4852
end
4953

5054
desc 'Pin dependencies'
5155
task :pin do
52-
$stdout.puts(Paint['Pinning dependencies from Berksfile to metadata.rb', :yellow, :bold])
5356
obj = RordanGramsay::Dependencies::Pinning.new
54-
obj.call
57+
obj.call do
58+
$stdout.puts(Paint['Pinning dependencies from Berksfile to metadata.rb', :yellow, :bold])
59+
end
5560
abort if obj.failure?
5661
end
5762
end

lib/rordan_gramsay/chef_tasks/test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
require_relative 'lint'
1414
require_relative 'kitchen'
15+
require_relative 'dependencies'

lib/rordan_gramsay/dependencies.rb

Lines changed: 100 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'berkshelf'
33
require 'chef/cookbook/metadata'
44
require_relative 'exceptions'
5+
require_relative '../rordan_gramsay'
56

67
module RordanGramsay
78
module Dependencies
@@ -36,13 +37,67 @@ def berksfile_lock
3637
end
3738
rescue FileMissing
3839
$stdout.puts('berks install')
39-
out = `berks install`
40+
RordanGramsay.stream_command('berks install')
4041
retry if $CHILD_STATUS.success?
41-
raise out
4242
end
4343
end
4444

4545
# :nodoc:
46+
module Helpers
47+
def initialize
48+
super
49+
@is_fail = false
50+
end
51+
52+
def failure?
53+
@is_fail
54+
end
55+
56+
private
57+
58+
def version_from_constraint(constraint)
59+
constraint.strip.gsub(/[^0-9.]+/, '')
60+
end
61+
62+
def corrected_constraint(cookbook, constraint)
63+
# Correct constraints `~> 0.1.0` or `~> 4.9`
64+
return constraint if constraint =~ /^\s*~>\s*[1-9][0-9]*\.\d+\s*$/
65+
return constraint if constraint =~ /^\s*~>0\.\d+\.\d+\s*$/
66+
67+
version = version_from_constraint(constraint).split('.')
68+
major = version.shift || '0'
69+
minor = version.shift || '0'
70+
patch = version.shift || '0'
71+
72+
if major == '0' && minor == '0' && patch == '0'
73+
# Pull in the latest version from berksfile_lock and "correct"
74+
# that version as if it is a constraint. This is so we can apply
75+
# some sort of reasonable version constraint on `0.0.0` dependencies
76+
version = berksfile_lock.graph.find(cookbook).version
77+
78+
corrected_constraint(cookbook, "~> #{version}")
79+
elsif major == '0'
80+
"~> #{major}.#{minor}.#{patch}"
81+
else
82+
"~> #{major}.#{minor}"
83+
end
84+
end
85+
86+
def report_change(cookbook, previous, now)
87+
return if previous == now
88+
$stderr.puts(Paint % [' %{cookbook} : %{previous} -> %{now}', cookbook: Paint[cookbook, :white, :bold], now: Paint[now, :green], previous: Paint[previous, :yellow]])
89+
end
90+
91+
def log_error(msg, opts = {})
92+
@is_fail = true
93+
$stderr.puts(msg)
94+
raise NextIteration if opts[:next]
95+
end
96+
end
97+
98+
# This is the main entry point if you don't know/care what cookbook
99+
# type whose dependencies you're managing. It should match all methods
100+
# on the public interface of both `WrapperPinning` or `CookbookPinning`
46101
class Pinning
47102
extend Forwardable
48103
include FileAccessor
@@ -55,7 +110,7 @@ def initialize
55110
end
56111
end
57112

58-
def_delegators :@obj, :call, :check, :clean, :migrate, :dependencies, :failure?
113+
def_delegators :@obj, :call, :check, :clean, :migrate, :update, :dependencies, :failure?
59114
end
60115

61116
# Most cookbooks should use this, unless you desire a layout
@@ -65,16 +120,11 @@ def initialize
65120
# `WrapperPinning` class.
66121
class CookbookPinning
67122
include FileAccessor
68-
69-
def initialize
70-
@is_fail = false
71-
end
72-
73-
def failure?
74-
@is_fail
75-
end
123+
include Helpers
76124

77125
def call
126+
yield if block_given?
127+
78128
File.open('metadata.new.rb', 'w') do |fd|
79129
File.readlines(metadata.source_file).each do |line|
80130
next if line =~ /\bdepends\b/i
@@ -98,6 +148,8 @@ def call
98148
end
99149

100150
def clean
151+
yield if block_given?
152+
101153
File.open('metadata.new.rb', 'w') do |fd|
102154
File.readlines(metadata.source_file).each do |line|
103155
next if line =~ /\bdepends\b/i
@@ -110,6 +162,8 @@ def clean
110162
end
111163

112164
def check
165+
yield if block_given?
166+
113167
berksfile
114168
metadata.dependencies.each do |(cookbook, constraint)|
115169
next if constraint =~ /^\s*~>\s*[1-9][0-9]*\.\d+\s*$/
@@ -150,11 +204,43 @@ def migrate
150204
@berksfile = nil
151205
File.unlink(berksfile_lock.source_file)
152206
@berksfile_lock = nil
153-
154207
clean
208+
209+
yield if block_given?
155210
call
156211
end
157212

213+
def update
214+
deps = metadata.dependencies.dup
215+
clean
216+
if block_given?
217+
yield
218+
else
219+
RordanGramsay.stream_command('berks update')
220+
end
221+
222+
File.open('metadata.new.rb', 'w') do |fd|
223+
File.readlines(metadata.source_file).each do |line|
224+
next if line =~ /\bdepends\b/i
225+
fd.puts(line)
226+
end
227+
228+
dependencies.each do |(cookbook, constraint)|
229+
old = deps[cookbook]
230+
if old
231+
report_change(cookbook, old, constraint)
232+
else
233+
report_change(cookbook, '<missing>', constraint)
234+
end
235+
236+
fd.puts(%(depends '#{cookbook}', '#{constraint}'))
237+
end
238+
end
239+
240+
File.rename('metadata.new.rb', metadata.source_file)
241+
@metadata = nil # Because we just modified it, clear cache
242+
end
243+
158244
def dependencies
159245
@dependencies ||= begin
160246
berksfile.dependencies.each_with_object({}) do |item, deps|
@@ -167,53 +253,13 @@ def dependencies
167253
end
168254
end
169255
end
170-
171-
private
172-
173-
def version_from_constraint(constraint)
174-
constraint.strip.gsub(/[^0-9.]+/, '')
175-
end
176-
177-
def corrected_constraint(cookbook, constraint)
178-
# Correct constraints `~> 0.1.0` or `~> 4.9`
179-
return constraint if constraint =~ /^\s*~>\s*[1-9][0-9]*\.\d+\s*$/
180-
return constraint if constraint =~ /^\s*~>0\.\d+\.\d+\s*$/
181-
182-
version = version_from_constraint(constraint).split('.')
183-
major = version.shift || '0'
184-
minor = version.shift || '0'
185-
patch = version.shift || '0'
186-
187-
if major == '0' && minor == '0' && patch == '0'
188-
# Pull in the latest version from berksfile_lock and "correct"
189-
# that version as if it is a constraint. This is so we can apply
190-
# some sort of reasonable version constraint on `0.0.0` dependencies
191-
version = berksfile_lock.graph.find(cookbook).version
192-
193-
corrected_constraint(cookbook, "~> #{version}")
194-
elsif major == '0'
195-
"~> #{major}.#{minor}.#{patch}"
196-
else
197-
"~> #{major}.#{minor}"
198-
end
199-
end
200-
201-
def report_change(cookbook, previous, now)
202-
return if previous == now
203-
$stderr.puts(Paint % [' %{cookbook} : %{previous} -> %{now}', cookbook: Paint[cookbook, :white, :bold], now: Paint[now, :green], previous: Paint[previous, :yellow]])
204-
end
205-
206-
def log_error(msg, opts = {})
207-
@is_fail = true
208-
$stderr.puts(msg)
209-
raise NextIteration if opts[:next]
210-
end
211256
end
212257

213258
# Pinning specifics for role- and wrapper-cookbooks
214259
class WrapperPinning < CookbookPinning
215260
def check
216-
@is_fail = false
261+
yield if block_given?
262+
217263
deps = metadata.dependencies.each_with_object({}) do |(cookbook, constraint), dep|
218264
dep[cookbook] = constraint
219265
end

lib/rordan_gramsay/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module RordanGramsay
2-
VERSION = '2.0.1'.freeze
2+
VERSION = '2.0.2'.freeze
33
EXECUTABLE = 'gramsay'.freeze
44
GEM_NAME = 'rordan_gramsay'.freeze
55
end

rordan_gramsay.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
2121

2222
spec.required_ruby_version = '~> 2.3'
2323

24+
spec.add_runtime_dependency 'berkshelf'
25+
spec.add_runtime_dependency 'chef'
2426
spec.add_runtime_dependency 'cookstyle', '~> 3.0'
2527
spec.add_runtime_dependency 'foodcritic', '~> 14.0'
2628
spec.add_runtime_dependency 'paint', '~> 2.0'

spec/rake/test_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
expect(all_tasks).to include 'test:quick'
4646
end
4747

48-
it "should defer to 'lint:all'" do
48+
it "should defer to 'lint:all' and 'dependency:check'" do
4949
out = run_command('rake --dry-run test:quick')
5050
expect(out).to include 'lint:all'
51+
expect(out).to include 'dependency:check'
5152
end
5253
end
5354

0 commit comments

Comments
 (0)