Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-1137) Determine module name from metadata when possible #265

Merged
merged 1 commit into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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