Skip to content

Commit

Permalink
Merge pull request #482 from bmjen/pdk-904
Browse files Browse the repository at this point in the history
(PDK-904) Warns users of pdk version compatibility
  • Loading branch information
rodjek committed Apr 20, 2018
2 parents 7df5f85 + bcf5c69 commit 101ef66
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pdk/cli/test/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module PDK::CLI
log_level: :info,
)

PDK::CLI::Util.module_version_check

report = nil

if opts[:list]
Expand Down
25 changes: 25 additions & 0 deletions lib/pdk/cli/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ def interactive?
true
end
module_function :interactive?

def module_version_check
module_pdk_ver = PDK::Util.module_pdk_version

# This means the module does not have a pdk-version tag in the metadata.json
# and will require a pdk convert.
if module_pdk_ver.nil?
PDK.logger.warn _('This module is not PDK compatible. Run `pdk convert` to make it compatible with your version of PDK.')
# This checks that the version of pdk in the module's metadata is older
# than 1.3.1, which means the module will need to run pdk convert to the
# new templates.
elsif Gem::Version.new(module_pdk_ver) < Gem::Version.new('1.3.1')
PDK.logger.warn _('This module template is out of date. Run `pdk convert` to make it compatible with your version of PDK.')
# This checks if the version of the installed PDK is older than the
# version in the module's metadata, and advises the user to upgrade to
# their install of PDK.
elsif Gem::Version.new(PDK::VERSION) < Gem::Version.new(module_pdk_ver)
PDK.logger.warn _('This module is compatible with a newer version of PDK. Upgrade your version of PDK to ensure compatibility.')
# This checks if the version listed in the module's metadata is older
# than the installed PDK, and advises the user to run pdk update.
elsif Gem::Version.new(PDK::VERSION) > Gem::Version.new(module_pdk_ver)
PDK.logger.warn _('This module is compatible with an older version of PDK. Run `pdk update` to update it to your version of PDK.')
end
end
module_function :module_version_check
end
end
end
2 changes: 2 additions & 0 deletions lib/pdk/cli/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module PDK::CLI
log_level: :info,
)

PDK::CLI::Util.module_version_check

if args[0]
# This may be a single validator, a list of validators, or a target.
if Util::OptionValidator.comma_separated_list?(args[0])
Expand Down
11 changes: 11 additions & 0 deletions lib/pdk/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,16 @@ def module_pdk_compatible?
['pdk-version', 'template-url'].any? { |key| module_metadata.key?(key) }
end
module_function :module_pdk_compatible?

def module_pdk_version
metadata = module_metadata

if !metadata.nil? && metadata.include?('pdk-version')
metadata['pdk-version'].split.first
else
nil
end
end
module_function :module_pdk_version
end
end
1 change: 1 addition & 0 deletions spec/unit/pdk/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
context 'when executing' do
before(:each) do
expect(PDK::CLI::Util).to receive(:ensure_in_module!).with(any_args).once
expect(PDK::Util).to receive(:module_pdk_version).and_return(PDK::VERSION)
end

context 'when listing tests' do
Expand Down
57 changes: 57 additions & 0 deletions spec/unit/pdk/cli/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,61 @@
it { is_expected.to be_falsey }
end
end

describe 'module_version_check' do
subject(:module_version_check) { described_class.module_version_check }

before(:each) do
stub_const('PDK::VERSION', '1.5.0')
allow(PDK::Util).to receive(:module_pdk_version).and_return(module_pdk_ver)
end

context 'if module doesn\'t have pdk-version in metadata' do
let(:module_pdk_ver) { nil }

before(:each) do
expect(logger).to receive(:warn).with(a_string_matching(%r{This module is not PDK compatible. Run `pdk convert` to make it compatible with your version of PDK.}i))
end

it 'does not raise an error' do
expect { module_version_check }.not_to raise_error
end
end

context 'if module version is older than 1.3.1' do
let(:module_pdk_ver) { '1.2.0' }

before(:each) do
expect(logger).to receive(:warn).with(a_string_matching(%r{This module template is out of date. Run `pdk convert` to make it compatible with your version of PDK.}i))
end

it 'does not raise an error' do
expect { module_version_check }.not_to raise_error
end
end

context 'if module version is newer than installed version' do
let(:module_pdk_ver) { '1.5.1' }

before(:each) do
expect(logger).to receive(:warn).with(a_string_matching(%r{This module is compatible with a newer version of PDK. Upgrade your version of PDK to ensure compatibility.}i))
end

it 'does not raise an error' do
expect { module_version_check }.not_to raise_error
end
end

context 'if module version is older than installed version' do
let(:module_pdk_ver) { '1.3.1' }

before(:each) do
expect(logger).to receive(:warn).with(a_string_matching(%r{This module is compatible with an older version of PDK. Run `pdk update` to update it to your version of PDK.}i))
end

it 'does not raise an error' do
expect { module_version_check }.not_to raise_error
end
end
end
end
1 change: 1 addition & 0 deletions spec/unit/pdk/cli/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
allow(PDK::Util::Bundler).to receive(:ensure_bundle!)
allow(PDK::Util).to receive(:module_root).and_return('/path/to/testmodule')
allow(PDK::Report).to receive(:new).and_return(report)
allow(PDK::Util).to receive(:module_pdk_version).and_return(PDK::VERSION)
end

context 'when no arguments or options are provided' do
Expand Down
78 changes: 78 additions & 0 deletions spec/unit/pdk/util_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
require 'spec_helper'

describe PDK::Util do
let(:pdk_version) { '1.2.3' }
let(:template_url) { 'https://github.com/puppetlabs/pdk-templates' }
let(:template_ref) { nil }
let(:mock_metadata) do
instance_double(
PDK::Module::Metadata,
data: {
'pdk-version' => pdk_version,
'template-url' => template_url,
'template-ref' => template_ref,
},
)
end

shared_context :with_version_file, version_file: true do
let(:version_file) { File.join('path', 'to', 'the', 'version', 'file') }

Expand Down Expand Up @@ -458,4 +472,68 @@
end
end
end

describe 'module_metadata' do
subject(:result) { described_class.module_metadata }

before(:each) do
allow(PDK::Module::Metadata).to receive(:from_file).with(File.join(described_class.module_root, 'metadata.json')).and_return(mock_metadata)
end

context 'when the metadata.json can be read' do
it 'returns the metadata object' do
is_expected.to eq(mock_metadata.data)
end
end

context 'when the metadata.json can not be read' do
before(:each) do
allow(PDK::Module::Metadata).to receive(:from_file).with(File.join(described_class.module_root, 'metadata.json')).and_raise(ArgumentError, 'some error')
end

it 'raises an ExitWithError exception' do
expect { -> { result }.call }.to raise_error(ArgumentError, %r{some error}i)
end
end
end

describe 'module_pdk_compatible?' do
subject(:result) { described_class.module_pdk_compatible? }

context 'is compatible' do
before(:each) do
allow(described_class).to receive(:module_metadata).and_return(mock_metadata.data)
end

it { is_expected.to be true }
end

context 'is not compatible' do
before(:each) do
allow(described_class).to receive(:module_metadata).and_return({})
end

it { is_expected.to be false }
end
end

describe 'module_pdk_version' do
subject(:result) { described_class.module_pdk_version }

context 'is in metadata' do
before(:each) do
allow(described_class).to receive(:module_metadata).and_return(mock_metadata.data)
end

it { is_expected.to match(pdk_version) }
end

context 'is not in metadata' do
before(:each) do
allow(described_class).to receive(:module_metadata).and_return({})
end

it { is_expected.to be nil }
end
end
end

0 comments on commit 101ef66

Please sign in to comment.