Skip to content

Commit

Permalink
Merge pull request #265 from rodjek/pdk-1137
Browse files Browse the repository at this point in the history
(PDK-1137) Determine module name from metadata when possible
  • Loading branch information
bmjen authored Nov 26, 2018
2 parents ef83ccd + b62449b commit 879ce61
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/puppetlabs_spec_helper/tasks/fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'yaml'
require 'open3'
require 'json'

module PuppetlabsSpecHelper; end
module PuppetlabsSpecHelper::Tasks; end
Expand All @@ -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
Expand All @@ -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)
Expand Down
79 changes: 79 additions & 0 deletions spec/unit/puppetlabs_spec_helper/tasks/fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 879ce61

Please sign in to comment.