Skip to content

Commit

Permalink
(PDK-804) Fixes error in build without ignore file
Browse files Browse the repository at this point in the history
pdk will now make sure that if there is no ignore file and if the pkg
target-dir is in the module_dir, then it will add the target-dir
to the ignores list.
  • Loading branch information
bmjen committed Feb 13, 2018
1 parent e12d456 commit 90ac514
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
11 changes: 9 additions & 2 deletions lib/pdk/module/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def self.invoke(options = {})
attr_reader :force

def initialize(options = {})
@module_dir = options[:module_dir] || Dir.pwd
@target_dir = options[:target_dir] || File.join(module_dir, 'pkg')
@module_dir = File.expand_path(options[:module_dir] || Dir.pwd)
@target_dir = File.expand_path(options[:'target-dir'] || File.join(module_dir, 'pkg'))
end

# Read and parse the values from metadata.json for the module that is
Expand Down Expand Up @@ -196,6 +196,13 @@ def ignored_files

PathSpec.new(data)
end

# Also ignore the target directory if it is in the module dir and not already ignored
if Find.find(@module_dir).include?(target_dir) && !@ignored_files.match(File.basename(target_dir) + '/')
@ignored_files = @ignored_files.add("\/#{File.basename(target_dir)}\/")
end

@ignored_files
end
end
end
Expand Down
40 changes: 22 additions & 18 deletions spec/unit/pdk/module/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
subject { described_class.new(initialize_options) }

let(:initialize_options) { {} }
let(:root_dir) { Gem.win_platform? ? 'C:/' : '/' }

shared_context 'with mock metadata' do
let(:mock_metadata) { PDK::Module::Metadata.new('name' => 'my-module') }
Expand All @@ -30,7 +31,7 @@
allow(Dir).to receive(:pwd).and_return(pwd)
end

let(:pwd) { '/path/to/my/module' }
let(:pwd) { File.join(root_dir, 'path', 'to', 'module') }

context 'by default' do
it 'uses the current working directory as the module directory' do
Expand All @@ -45,7 +46,7 @@
context 'if module_dir has been customised' do
let(:initialize_options) do
{
module_dir: '/some/other/module',
module_dir: File.join(root_dir, 'some', 'other', 'module'),
}
end

Expand All @@ -61,7 +62,7 @@
context 'if target_dir has been customised' do
let(:initialize_options) do
{
target_dir: '/tmp',
:'target-dir' => File.join(root_dir, 'tmp'),
}
end

Expand All @@ -70,15 +71,15 @@
end

it 'places the built packages in the provided path' do
is_expected.to have_attributes(target_dir: initialize_options[:target_dir])
is_expected.to have_attributes(target_dir: initialize_options[:'target-dir'])
end
end

context 'if both module_dir and target_dir have been customised' do
let(:initialize_options) do
{
target_dir: '/var/cache',
module_dir: '/tmp/git/my-module',
:'target-dir' => File.join(root_dir, 'var', 'cache'),
module_dir: File.join(root_dir, 'tmp', 'git', 'my-module'),
}
end

Expand All @@ -87,7 +88,7 @@
end

it 'places the built packages in the provided target_dir path' do
is_expected.to have_attributes(target_dir: initialize_options[:target_dir])
is_expected.to have_attributes(target_dir: initialize_options[:'target-dir'])
end
end
end
Expand All @@ -110,19 +111,19 @@
end

describe '#package_file' do
subject { described_class.new(target_dir: target_dir).package_file }
subject { described_class.new(:'target-dir' => target_dir).package_file }

let(:target_dir) { '/tmp' }
let(:target_dir) { File.join(root_dir, 'tmp') }

include_context 'with mock metadata'

it { is_expected.to eq(File.join(target_dir, 'my-module-0.1.0.tar.gz')) }
end

describe '#build_dir' do
subject { described_class.new(target_dir: target_dir).build_dir }
subject { described_class.new(:'target-dir' => target_dir).build_dir }

let(:target_dir) { '/tmp' }
let(:target_dir) { File.join(root_dir, 'tmp') }

include_context 'with mock metadata'

Expand All @@ -131,7 +132,7 @@

describe '#stage_module_in_build_dir' do
let(:instance) { described_class.new(module_dir: module_dir) }
let(:module_dir) { '/tmp/my-module' }
let(:module_dir) { File.join(root_dir, 'tmp', 'my-module') }

before(:each) do
allow(instance).to receive(:ignored_files).and_return(PathSpec.new("/spec/\n"))
Expand Down Expand Up @@ -170,7 +171,7 @@

describe '#stage_path' do
let(:instance) { described_class.new(module_dir: module_dir) }
let(:module_dir) { '/tmp/my-module' }
let(:module_dir) { File.join(root_dir, 'tmp', 'my-module') }
let(:path_to_stage) { File.join(module_dir, 'test') }
let(:path_in_build_dir) { File.join(module_dir, 'pkg', release_name, 'test') }
let(:release_name) { 'my-module-0.0.1' }
Expand Down Expand Up @@ -227,7 +228,7 @@
'foo',
]
end
let(:module_dir) { '/tmp/my-module' }
let(:module_dir) { File.join(root_dir, 'tmp', 'my-module') }

before(:each) do
allow(instance).to receive(:ignored_files).and_return(PathSpec.new(ignore_patterns.join("\n")))
Expand All @@ -249,7 +250,7 @@
describe '#ignore_file' do
subject { described_class.new(module_dir: module_dir).ignore_file }

let(:module_dir) { '/tmp/my-module' }
let(:module_dir) { File.join(root_dir, 'tmp', 'my-module') }
let(:possible_files) do
[
'.pdkignore',
Expand Down Expand Up @@ -307,17 +308,19 @@
describe '#ignored_files' do
subject { instance.ignored_files }

let(:module_dir) { '/tmp/my-module' }
let(:module_dir) { File.join(root_dir, 'tmp', 'my-module') }
let(:instance) { described_class.new(module_dir: module_dir) }

context 'when no ignore file is present in the module' do
before(:each) do
allow(Find).to receive(:find).with(module_dir).and_return([File.join(module_dir, 'pkg')])
allow(instance).to receive(:ignore_file).and_return(nil)
end

it 'returns a empty PathSpec object' do
it 'returns a PathSpec object with the target dir' do
is_expected.to be_a(PathSpec)
is_expected.to have_attributes(specs: [])
is_expected.not_to be_empty
is_expected.to match('pkg/')
end
end

Expand All @@ -328,6 +331,7 @@

allow(instance).to receive(:ignore_file).and_return(ignore_file_path)
allow(File).to receive(:open).with(ignore_file_path, 'rb:UTF-8').and_return(ignore_file_content)
allow(Find).to receive(:find).with(module_dir).and_return([File.join(module_dir, 'pkg')])
end

it 'returns a PathSpec object populated by the ignore file' do
Expand Down

0 comments on commit 90ac514

Please sign in to comment.