ra10ke 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'ra10ke/dependencies'
5
+ RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
6
+
7
+ RSpec.describe 'Ra10ke::Dependencies::Verification' do
8
+ after(:each) do
9
+ # reset version formats
10
+ formats = Ra10ke::Dependencies.class_variable_get(:@@version_formats)
11
+ Ra10ke::Dependencies.class_variable_set(:@@version_formats, formats.select { |k, _v| k == :semver } )
12
+ end
13
+
14
+ context 'register_version_format' do
15
+ it 'default contains semver' do
16
+ expect(Ra10ke::Dependencies.class_variable_get(:@@version_formats)).to have_key(:semver)
17
+ end
18
+ it 'add new version format' do
19
+ Ra10ke::Dependencies.register_version_format(:test) do |tags|
20
+ nil
21
+ end
22
+ expect(Ra10ke::Dependencies.class_variable_get(:@@version_formats)).to have_key(:test)
23
+ end
24
+ end
25
+
26
+ context 'get_latest_ref' do
27
+ let(:instance) do
28
+ class DependencyDummy
29
+ include Ra10ke::Dependencies
30
+ end.new
31
+ end
32
+
33
+ context 'find latest semver tag' do
34
+ let(:latest_tag) do
35
+ 'v1.1.0'
36
+ end
37
+ let(:test_tags) do
38
+ {
39
+ 'v1.0.0' => nil,
40
+ latest_tag => nil,
41
+ }
42
+ end
43
+
44
+ it do
45
+ expect(instance.get_latest_ref({
46
+ 'tags' => test_tags,
47
+ })).to eq(latest_tag)
48
+ end
49
+ end
50
+
51
+ context 'find latest tag with custom version format' do
52
+ let(:latest_tag) do
53
+ 'latest'
54
+ end
55
+ let(:test_tags) do
56
+ {
57
+ 'dev' => nil,
58
+ latest_tag => nil,
59
+ }
60
+ end
61
+
62
+ it do
63
+ Ra10ke::Dependencies.register_version_format(:number) do |tags|
64
+ tags.detect { |tag| tag == latest_tag }
65
+ end
66
+ expect(instance.get_latest_ref({
67
+ 'tags' => test_tags,
68
+ })).to eq(latest_tag)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'ra10ke/deprecation'
5
+ RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
6
+
7
+ RSpec.describe 'Ra10ke::Deprecation::Validation' do
8
+ let(:instance) do
9
+ Ra10ke::Deprecation::Validation.new(puppetfile)
10
+ end
11
+
12
+ let(:puppetfile) do
13
+ File.join(fixtures_dir, 'Puppetfile')
14
+ end
15
+
16
+ it 'only checks forge modules' do
17
+ expect(PuppetForge::Module).to_not receive(:find).with('puppet')
18
+ allow(PuppetForge::Module).to receive(:find).and_raise(Faraday::ResourceNotFound.new(nil))
19
+ expect(instance.deprecated_modules.count).to eq(0)
20
+ end
21
+
22
+ it 'handles deprecated modules' do
23
+ expect(PuppetForge::Module).to receive(:find).with('puppetlabs-ruby').and_return(double(slug: 'puppetlabs-ruby', deprecated_at: '2021-04-22 10:29:42 -0700'))
24
+ allow(PuppetForge::Module).to receive(:find).and_return(double(slug: 'module-module', deprecated_at: nil))
25
+
26
+ expect(instance.bad_mods?).to eq(true)
27
+ expect(instance.deprecated_modules.first).to eq(name: 'puppetlabs-ruby', deprecated_at: Time.parse('2021-04-22 10:29:42 -0700'))
28
+ end
29
+
30
+ it 'handles missing modules' do
31
+ expect(PuppetForge::Module).to receive(:find).with('choria-choria').and_return(double(slug: 'choria-choria', deprecated_at: nil))
32
+ expect(PuppetForge::Module).to receive(:find).with('puppetlabs-ruby').and_raise(Faraday::ResourceNotFound.new(nil))
33
+ allow(PuppetForge::Module).to receive(:find).and_return(double(slug: 'module-module', deprecated_at: nil))
34
+
35
+ expect(instance.bad_mods?).to eq(false)
36
+ end
37
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'ra10ke/validate'
5
+
6
+ RSpec.describe 'Ra10ke::GitRepo' do
7
+ let(:url) { 'https://github.com/vshn/puppet-gitlab' }
8
+
9
+ it '#new' do
10
+ expect(Ra10ke::GitRepo.new(url)).to be_a Ra10ke::GitRepo
11
+ end
12
+
13
+ it 'should not run_command more than once' do
14
+ i = Ra10ke::GitRepo.new(url)
15
+ expect(i).to receive(:run_command).with("git ls-remote --symref #{url}").once
16
+ i.valid_url?
17
+ i.all_refs
18
+ end
19
+
20
+ describe 'good url' do
21
+ let(:url) { 'https://github.com/vshn/puppet-gitlab' }
22
+
23
+ let(:instance) do
24
+ Ra10ke::GitRepo.new(url)
25
+ end
26
+
27
+ let(:reflist) { File.read(File.join(fixtures_dir, 'refs', 'gitlab.txt')) }
28
+
29
+ before(:each) do
30
+ allow(instance).to receive(:run_command).with("git ls-remote --symref #{url}").and_return([reflist, true])
31
+ end
32
+
33
+ it '#remote_refs is array' do
34
+ expect(instance.remote_refs).to be_a Array
35
+ end
36
+
37
+ it '#remote_refs contains refs' do
38
+ expect(instance.remote_refs.first).to eq("1b3322d525e96bf7d0565b08703e2a44c90e7b4a\tHEAD\n")
39
+ end
40
+
41
+ it '#valid_ref?' do
42
+ expect(instance.valid_ref?('master')).to be true
43
+ end
44
+
45
+ it '#valid_commit?' do
46
+ expect(instance.valid_commit?('master')).to be true
47
+ end
48
+
49
+ it '#valid_commit? and nil sha' do
50
+ expect(instance.valid_commit?(nil)).to be false
51
+ end
52
+
53
+ it '#valid_commit? but invalid sha' do
54
+ allow(instance).to receive(:run_command).with(/clone/, silent: true).and_return([nil, true])
55
+ allow(instance).to receive(:run_command).with(/git\sshow.*/, silent: true).and_return([nil, false])
56
+ expect(instance.valid_commit?('invalid')).to be false
57
+ end
58
+
59
+ it '#valid_url?' do
60
+ expect(instance.valid_url?).to be true
61
+ end
62
+
63
+ it '#all_refs is a Array of Hashes' do
64
+ refs = instance.all_refs
65
+ expect(refs).to be_a Array
66
+ expect(refs.last).to eq(
67
+ { name: 'v5.0.0^{}', ref: 'refs/tags/v5.0.0^{}', sha: '1febd15f90d32e6b3d6c242a70db386b2ef1942c', subtype: nil, type: :tag }
68
+ )
69
+ end
70
+ end
71
+
72
+ describe 'bad url' do
73
+ let(:url) { 'https://github.com/nwops/typo' }
74
+
75
+ before(:each) do
76
+ allow(instance).to receive(:run_command).with("git ls-remote --symref #{url}").and_return(['', false])
77
+ end
78
+
79
+ let(:instance) do
80
+ Ra10ke::GitRepo.new(url)
81
+ end
82
+
83
+ it '#all_refs' do
84
+ expect(instance.all_refs).to be_a Array
85
+ expect(instance.all_refs).to eq []
86
+ end
87
+ end
88
+ end
@@ -9,7 +9,9 @@ RSpec.describe 'Ra10ke::PuppetfileParser' do
9
9
  end
10
10
 
11
11
  let(:puppetfile_modules) do
12
- [{:args=>{:version=>"2.2.0"}, :name=>"inifile", :namespace=>"puppetlabs"},
12
+ [{:args=>{:version=>'0.26.2'}, :name=>'choria', :namespace=>nil},
13
+ {:args=>{:version=>"2.2.0"}, :name=>"inifile", :namespace=>"puppetlabs"},
14
+ {:args=>{:version=>"1.0.1"}, :name=>"ruby", :namespace=>"puppetlabs"},
13
15
  {:args=>{:version=>"4.24.0"}, :name=>"stdlib", :namespace=>"puppetlabs"},
14
16
  {:args=>{:version=>"4.0.0"}, :name=>"concat", :namespace=>"puppetlabs"},
15
17
  {:args=>{:version=>"6.4.1"}, :name=>"ntp", :namespace=>"puppetlabs"},
@@ -9,40 +9,101 @@ RSpec.describe 'Ra10ke::Validate::Validation' do
9
9
  Ra10ke::Validate::Validation.new(puppetfile)
10
10
  end
11
11
 
12
- let(:result) do
13
- double
14
- end
15
-
16
- before(:each) do
17
- allow(result).to receive(:success?).and_return(true)
18
- # allow(instance).to receive(:`).with(anything).and_return(result)
19
- allow($CHILD_STATUS).to receive(:success?).and_return(true)
20
- allow(instance).to receive(:`).with(anything)
21
- .and_return(File.read(File.join(fixtures_dir, 'reflist.txt')))
22
- end
23
-
24
12
  let(:puppetfile) do
25
13
  File.join(fixtures_dir, 'Puppetfile')
26
14
  end
27
15
 
28
- it '#new' do
29
- expect(instance).to be_a Ra10ke::Validate::Validation
16
+ before(:each) do
17
+ allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_ref?).and_return(true)
18
+ allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_url?).and_return(true)
30
19
  end
31
20
 
32
- it '#valid_ref?' do
33
- expect(instance.valid_ref?('https://www.example.com', 'master')).to be true
21
+ describe 'bad url' do
22
+ let(:instance) do
23
+ Ra10ke::Validate::Validation.new(puppetfile)
24
+ end
25
+
26
+ let(:puppetfile) do
27
+ File.join(fixtures_dir, 'Puppetfile_with_bad_refs')
28
+ end
29
+
30
+ before(:each) do
31
+ working = double('Ra1ke::GitRepo', url: 'https://github.com/vshn/puppet-gitlab')
32
+ expect(working).to receive(:valid_ref?).with('00397b86dfb3487d9df768cbd3698d362132b5bf').and_return(false)
33
+ expect(working).to receive(:valid_commit?).with('00397b86dfb3487d9df768cbd3698d362132b5bf').and_return(true)
34
+ expect(working).to receive(:valid_url?).and_return(true)
35
+ bad_tag = double('Ra1ke::GitRepo', url: 'https://github.com/acidprime/r10k')
36
+ expect(bad_tag).to receive(:valid_ref?).with('bad').and_return(false)
37
+ expect(bad_tag).to receive(:valid_commit?).with(nil).and_return(false)
38
+ expect(bad_tag).to receive(:valid_url?).and_return(true)
39
+ bad_url = double('Ra1ke::GitRepo', url: 'https://github.com/nwops/typo')
40
+ expect(bad_url).to receive(:valid_ref?).with(nil).and_return(false)
41
+ expect(bad_url).to receive(:valid_commit?).with(nil).and_return(false)
42
+ expect(bad_url).to receive(:valid_url?).and_return(false)
43
+
44
+ expect(Ra10ke::GitRepo).to receive(:new).and_return(working, bad_tag, bad_url)
45
+ end
46
+
47
+ it 'details mods that are bad' do
48
+ expect(instance.all_modules.find { |m| !m[:valid_url?] }).to be_a Hash
49
+ expect(instance.all_modules.find_all { |m| !m[:valid_ref?] }.count).to eq(2)
50
+ end
34
51
  end
35
52
 
36
- it '#valid_commit?' do
37
- expect(instance.valid_commit?('https://www.example.com', 'master')).to be true
53
+ describe 'control_branch' do
54
+ before(:each) do
55
+ ENV.delete 'CONTROL_BRANCH'
56
+ ENV.delete 'CONTROL_BRANCH_FALLBACK'
57
+ end
58
+
59
+ let(:instance) do
60
+ Ra10ke::Validate::Validation.new(puppetfile)
61
+ end
62
+
63
+ let(:puppetfile) do
64
+ File.join(fixtures_dir, 'Puppetfile_with_control_branch')
65
+ end
66
+
67
+ it 'correctly replaces control branch' do
68
+ ENV['CONTROL_BRANCH'] = 'env-control_branch'
69
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_control' }[:ref]).to eq('env-control_branch')
70
+ end
71
+
72
+ it 'correctly detects current branch' do
73
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return('current_branch-control_branch')
74
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_control' }[:ref]).to eq('current_branch-control_branch')
75
+ end
76
+
77
+ it 'correctly falls back if no current branch' do
78
+ ENV['CONTROL_BRANCH_FALLBACK'] = 'env-control_branch_fallback'
79
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil)
80
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_control' }[:ref]).to eq('env-control_branch_fallback')
81
+ end
82
+
83
+ it 'correctly falls back to default_branch if no current branch' do
84
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil)
85
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_controlwithdefault' }[:ref]).to eq('master')
86
+ end
87
+
88
+ it 'correctly falls back to fallback if no current branch but default branch' do
89
+ ENV['CONTROL_BRANCH_FALLBACK'] = 'env-control_branch_fallback'
90
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil)
91
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_controlwithdefault' }[:ref]).to eq('env-control_branch_fallback')
92
+ end
93
+
94
+ it 'correctly falls back to default_branch if no current branch with override' do
95
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil)
96
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_controlwithdefaultoverride' }[:ref]).to eq('master')
97
+ end
98
+
99
+ it 'correctly falls back to main if no current branch and no fallback' do
100
+ allow(Ra10ke::GitRepo).to receive(:current_branch).and_return(nil)
101
+ expect(instance.all_modules.find { |m| m[:name] == 'hiera_control' }[:ref]).to eq('main')
102
+ end
38
103
  end
39
104
 
40
- it '#bad_mods?' do
41
- allow(instance).to receive(:`).with(anything)
42
- .and_return(File.read(File.join(fixtures_dir, 'reflist.txt')))
43
- # because we can't test every single module we return the same result set
44
- # which only passes for a single module, while others fail.
45
- expect(instance.bad_mods?).to be true
105
+ it '#new' do
106
+ expect(instance).to be_a Ra10ke::Validate::Validation
46
107
  end
47
108
 
48
109
  it '#all_modules is an array' do
@@ -59,22 +120,13 @@ RSpec.describe 'Ra10ke::Validate::Validation' do
59
120
 
60
121
  it '#data is a hash with keys' do
61
122
  keys = instance.all_modules.first.keys
62
- expect(keys).to eq(%i[name url ref valid_ref? status])
123
+ expect(keys).to eq(%i[name url ref valid_url? valid_ref? status])
63
124
  end
64
125
 
65
126
  it '#data is a hash with values' do
66
127
  keys = instance.all_modules.first.values
67
128
 
68
129
  expect(keys).to eq(['gitlab', 'https://github.com/vshn/puppet-gitlab',
69
- '00397b86dfb3487d9df768cbd3698d362132b5bf', true, '👍'])
70
- end
71
-
72
- it '#all_refs' do
73
- refs = instance.all_refs('https://www.example.com')
74
- expect(refs).to be_a Array
75
- expect(refs.first).to eq(
76
- {:sha=>"0ec707e431367bbe2752966be8ab915b6f0da754",:name=>"74110ac", :ref=>"refs/heads/74110ac", :subtype=>nil, :type=>:branch}
77
- )
78
-
130
+ '00397b86dfb3487d9df768cbd3698d362132b5bf', true, true, '👍'])
79
131
  end
80
132
  end
data/spec/ra10ke_spec.rb CHANGED
@@ -6,7 +6,7 @@ require 'spec_helper'
6
6
  RSpec.describe 'Ra10ke::RakeTask' do
7
7
  let(:instance) do
8
8
  Ra10ke::RakeTask.new do |t|
9
- t.puppetfile_path = puppetfile
9
+ t.puppetfile_path = puppetfile
10
10
  end
11
11
  end
12
12
 
@@ -50,6 +50,9 @@ RSpec.describe 'Ra10ke::RakeTask' do
50
50
 
51
51
  describe 'run tasks with good refs' do
52
52
  it '#run_validate_task' do
53
+ allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_ref?).and_return(true)
54
+ allow_any_instance_of(Ra10ke::GitRepo).to receive(:valid_url?).and_return(true)
55
+
53
56
  task = instance.define_task_validate(args)
54
57
  expect(task.invoke).to be_a Array
55
58
  end
@@ -59,14 +62,13 @@ RSpec.describe 'Ra10ke::RakeTask' do
59
62
  let(:puppetfile) do
60
63
  File.join(fixtures_dir, 'Puppetfile_with_bad_refs')
61
64
  end
62
-
65
+
63
66
  # I suspect rake is caching something here and the puppetfile is
64
67
  # not being sent correctly as it is not using the file I specify.
65
68
  # The output should be different.
66
69
  # Testing this by itself works
67
70
  it '#run_validate_task' do
68
- t = Ra10ke::RakeTask.new
69
- task2 = t.define_task_validate(args)
71
+ task2 = instance.define_task_validate(args)
70
72
  expect(task2.invoke).to be nil
71
73
  end
72
74
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'simplecov'
5
+ require 'simplecov-console'
6
+ require 'codecov'
7
+ rescue LoadError
8
+ else
9
+ SimpleCov.start do
10
+ track_files 'lib/**/*.rb'
11
+
12
+ add_filter '/spec'
13
+
14
+ enable_coverage :branch
15
+
16
+ # do not track vendored files
17
+ add_filter '/vendor'
18
+ add_filter '/.vendor'
19
+ end
20
+
21
+ SimpleCov.formatters = [
22
+ SimpleCov::Formatter::Console,
23
+ SimpleCov::Formatter::Codecov,
24
+ ]
25
+ end
26
+
27
+ require 'rspec/core'
28
+
1
29
  def fixtures_dir
2
30
  File.join(__dir__, 'fixtures')
3
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ra10ke
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Chatzimichos
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-02-08 00:00:00.000000000 Z
12
+ date: 2021-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -123,6 +123,34 @@ dependencies:
123
123
  - - "~>"
124
124
  - !ruby/object:Gem::Version
125
125
  version: '3.6'
126
+ - !ruby/object:Gem::Dependency
127
+ name: pry
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: simplecov
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
126
154
  description: R10K and Puppetfile rake tasks
127
155
  email:
128
156
  - voxpupuli@groups.io
@@ -130,17 +158,23 @@ executables: []
130
158
  extensions: []
131
159
  extra_rdoc_files: []
132
160
  files:
161
+ - ".github/dependabot.yml"
162
+ - ".github/workflows/release.yml"
163
+ - ".github/workflows/test.yml"
133
164
  - ".gitignore"
134
165
  - ".ruby-version"
135
166
  - ".travis.yml"
136
167
  - CHANGELOG.md
137
168
  - Gemfile
169
+ - HISTORY.md
138
170
  - LICENSE.txt
139
171
  - README.md
140
172
  - Rakefile
141
173
  - lib/ra10ke.rb
142
174
  - lib/ra10ke/dependencies.rb
175
+ - lib/ra10ke/deprecation.rb
143
176
  - lib/ra10ke/duplicates.rb
177
+ - lib/ra10ke/git_repo.rb
144
178
  - lib/ra10ke/install.rb
145
179
  - lib/ra10ke/monkey_patches.rb
146
180
  - lib/ra10ke/puppetfile_parser.rb
@@ -152,9 +186,16 @@ files:
152
186
  - spec/fixtures/Puppetfile
153
187
  - spec/fixtures/Puppetfile_test
154
188
  - spec/fixtures/Puppetfile_with_bad_refs
189
+ - spec/fixtures/Puppetfile_with_control_branch
155
190
  - spec/fixtures/Puppetfile_with_duplicates
156
- - spec/fixtures/reflist.txt
191
+ - spec/fixtures/refs/debug.txt
192
+ - spec/fixtures/refs/gitlab.txt
193
+ - spec/fixtures/refs/r10k.txt
194
+ - spec/fixtures/refs/reflist.txt
195
+ - spec/ra10ke/dependencies_spec.rb
196
+ - spec/ra10ke/deprecation_spec.rb
157
197
  - spec/ra10ke/duplicates_spec.rb
198
+ - spec/ra10ke/git_repo_spec.rb
158
199
  - spec/ra10ke/puppetfile_parser_spec.rb
159
200
  - spec/ra10ke/validate_spec.rb
160
201
  - spec/ra10ke_spec.rb
@@ -171,15 +212,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
212
  requirements:
172
213
  - - ">="
173
214
  - !ruby/object:Gem::Version
174
- version: 2.1.0
215
+ version: 2.4.0
175
216
  required_rubygems_version: !ruby/object:Gem::Requirement
176
217
  requirements:
177
218
  - - ">="
178
219
  - !ruby/object:Gem::Version
179
220
  version: '0'
180
221
  requirements: []
181
- rubyforge_project:
182
- rubygems_version: 2.7.7
222
+ rubygems_version: 3.2.22
183
223
  signing_key:
184
224
  specification_version: 4
185
225
  summary: Syntax check for the Puppetfile, check for outdated installed puppet modules