# encoding: utf-8 require 'fedux_org_stdlib/core_ext/file/which' RSpec.describe 'File' do around :example do |example| with_env 'PATH' => absolute_path('.') do example.run end end context '#which' do it 'returns path to command' do touch_file 'vim' filesystem_permissions '0755', 'vim' expect(File.which('vim')).to eq absolute_path('vim') end it 'uses a different search path' do touch_file 'vim' filesystem_permissions '0755', 'vim' expect(File.which('vim', absolute_path('.'))).to eq absolute_path('vim') end it 'returns nil if command could not be found' do expect(File.which('vim')).to be nil end it 'returns nil if command is not executable' do touch_file 'vim' filesystem_permissions '0644', 'vim' expect(File.which('vim')).to be nil end it 'returns nil if command is not a file' do create_dir 'vim' expect(File.which('vim')).to be nil end it 'supports ~' do with_env 'HOME' => absolute_path('.') do touch_file 'vim' filesystem_permissions '0755', 'vim' expect(File.which('vim', '~/')).to eq absolute_path('vim') end end it 'returns nil if command path is blank' do expect(File.which('')).to eq nil expect(File.which(nil)).to eq nil expect(File.which([])).to eq nil end it 'fails if search path is blank' do expect { File.which('vim', '') }.to raise_error ArgumentError expect { File.which('vim', nil) }.to raise_error ArgumentError expect { File.which('vim', []) }.to raise_error ArgumentError end end end