# encoding: utf-8 require 'spec_helper' describe Actions::CreateFile do context '#initialize' do it 'requires a path' do repository = double('TemplateRepository') data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end Actions::CreateFile.new(:template, ::File.join(working_directory, 'file'), {}, data, engine_klass, repository) end end context '#run' do it 'runs the action' do repository = double('TemplateRepository') expect(repository).to receive(:find).with(:template) data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end action = Actions::CreateFile.new(:template, ::File.join(working_directory, 'file'), data, {}, engine_klass, repository) silence(:stderr) do action.run end expect(path_exists?('file')).to be_true end it 'creates sub directories' do repository = double('TemplateRepository') expect(repository).to receive(:find).with(:template) data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end action = Actions::CreateFile.new(:template, ::File.join(working_directory, 'data', 'file'), data, {create_directories: true}, engine_klass, repository) silence(:stderr) do action.run end expect(path_exists?('data/file')).to be_true end it 'respects existing path' do repository = double('TemplateRepository') data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end file = create_file('file') action = Actions::CreateFile.new(:template, file, data, {}, engine_klass, repository) result = capture(:stderr) do LocalPac.ui_logger.level = ::Logger::INFO action.run end expect(result).to include('already') end it 'does not respect existing path if forced to' do repository = double('TemplateRepository') expect(repository).to receive(:find).with(:template) data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end file = create_file('file') action = Actions::CreateFile.new(:template, file, data, { force: true }, engine_klass, repository) result = capture(:stderr) do LocalPac.ui_logger.level = ::Logger::INFO action.run end expect(result).to include('Creating file') end it 'resolves ~' do repository = double('TemplateRepository') expect(repository).to receive(:find).with(:template) data = double('Data') engine_klass = Class.new do def initialize(data) end def compile(template, destination) end end action = with_environment 'HOME' => working_directory do Actions::CreateFile.new(:template, '~/file', data, { force: true }, engine_klass, repository) end silence(:stderr) do action.run end expect(path_exists?('file')).to be_true end end end