From b62449bc388cfc62d06630eac9f70b1b7492999e Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Mon, 26 Nov 2018 16:49:40 +1100 Subject: [PATCH] (PDK-1137) Determine module name from metadata when possible --- lib/puppetlabs_spec_helper/tasks/fixtures.rb | 16 +++- .../tasks/fixtures_spec.rb | 79 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/puppetlabs_spec_helper/tasks/fixtures.rb b/lib/puppetlabs_spec_helper/tasks/fixtures.rb index 87874f21..e2b20683 100644 --- a/lib/puppetlabs_spec_helper/tasks/fixtures.rb +++ b/lib/puppetlabs_spec_helper/tasks/fixtures.rb @@ -1,5 +1,6 @@ require 'yaml' require 'open3' +require 'json' module PuppetlabsSpecHelper; end module PuppetlabsSpecHelper::Tasks; end @@ -9,6 +10,19 @@ def source_dir Dir.pwd end + def module_name + raise ArgumentError unless File.file?('metadata.json') && File.readable?('metadata.json') + + metadata = JSON.parse(File.read('metadata.json')) + metadata_name = metadata.fetch('name', nil) || '' + + raise ArgumentError if metadata_name.empty? + + metadata_name.split('-').last + rescue JSON::ParserError, ArgumentError + File.basename(Dir.pwd).split('-').last + end + # cache the repositories and return a hash object def repositories unless @repositories @@ -18,7 +32,7 @@ def repositories end def auto_symlink - { File.basename(Dir.pwd).split('-').last => '#{source_dir}' } + { module_name => '#{source_dir}' } end def fixtures(category) diff --git a/spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb b/spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb index 4ab3e77f..f64766a2 100644 --- a/spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb +++ b/spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb @@ -2,6 +2,85 @@ require 'puppetlabs_spec_helper/tasks/fixtures' describe PuppetlabsSpecHelper::Tasks::FixtureHelpers do + describe '.module_name' do + subject(:module_name) { described_class.module_name } + + before(:each) do + allow(Dir).to receive(:pwd).and_return(File.join('path', 'to', 'my-awsome-module_from_pwd')) + end + + shared_examples 'module name from working directory' do + it 'determines the module name from the working directory name' do + expect(module_name).to eq('module_from_pwd') + end + end + + shared_examples 'module name from metadata' do + it 'determines the module name from the module metadata' do + expect(module_name).to eq('module_from_metadata') + end + end + + context 'when metadata.json does not exist' do + before(:each) do + allow(File).to receive(:file?).with('metadata.json').and_return(false) + end + + it_behaves_like 'module name from working directory' + end + + context 'when metadata.json does exist' do + before(:each) do + allow(File).to receive(:file?).with('metadata.json').and_return(true) + end + + context 'but it is not readable' do + before(:each) do + allow(File).to receive(:readable?).with('metadata.json').and_return(false) + end + + it_behaves_like 'module name from working directory' + end + + context 'and it is readable' do + before(:each) do + allow(File).to receive(:readable?).with('metadata.json').and_return(true) + allow(File).to receive(:read).with('metadata.json').and_return(metadata_content) + end + + context 'but it contains invalid JSON' do + let(:metadata_content) { '{ "name": "my-awesome-module_from_metadata", }' } + + it_behaves_like 'module name from working directory' + end + + context 'and it contains a name value' do + let(:metadata_content) { '{ "name": "my-awesome-module_from_metadata" }' } + + it_behaves_like 'module name from metadata' + end + + context 'but it does not contain a name value' do + let(:metadata_content) { '{}' } + + it_behaves_like 'module name from working directory' + end + + context 'but the name has a null value' do + let(:metadata_content) { '{ "name": null }' } + + it_behaves_like 'module name from working directory' + end + + context 'but the name is blank' do + let(:metadata_content) { '{ "name": "" }' } + + it_behaves_like 'module name from working directory' + end + end + end + end + describe '.fixtures' do before :each do # Unstub the fixtures "helpers"