Skip to content

Commit

Permalink
Merge pull request #1654 from sebastian-miclea/BKR-1656
Browse files Browse the repository at this point in the history
(BKR-1656) Fixes for cat and file_exist? on PSWindows
  • Loading branch information
mihaibuzgau authored Jun 3, 2020
2 parents 5c46cb3 + ce1e068 commit d7c991f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/beaker/host/pswindows/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def path_split(paths)
end

def cat(path)
exec(powershell("type #{path}"))
exec(powershell("type #{path}")).stdout
end

def file_exist?(path)
result = exec(Beaker::Command.new("if exist #{path} echo true"), :acceptable_exit_codes => [0, 1])
result.stdout =~ /true/
result = exec(Beaker::Command.new("if exist #{path} echo true"), accept_all_exit_codes: true)
result.stdout.strip == 'true'
end
end
27 changes: 23 additions & 4 deletions spec/beaker/host/pswindows/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ def to_s
let (:instance) { PSWindowsFileTest.new(opts, logger) }

describe '#cat' do
let(:path) { '/path/to/cat' }
let(:content) { 'file content' }
it 'reads output for file' do
path = '/path/to/delete'
expect(instance).to receive(:exec)
expect(instance).to receive(:exec).and_return(double(stdout: content))
expect(Beaker::Command).to receive(:new).with('powershell.exe', array_including("-Command type #{path}"))
instance.cat(path)
expect(instance.cat(path)).to eq(content)
end
end

describe '#file_exist?' do
let(:path) { '/path/to/test/file.txt' }
context 'file exists' do
it 'returns true' do
expect(instance).to receive(:exec).and_return(double(stdout: "true\n"))
expect(Beaker::Command).to receive(:new).with("if exist #{path} echo true")
expect(instance.file_exist?(path)).to eq(true)
end
end

context 'file does not exist' do
it 'returns false' do
expect(instance).to receive(:exec).and_return(double(stdout: ""))
expect(Beaker::Command).to receive(:new).with("if exist #{path} echo true")
expect(instance.file_exist?(path)).to eq(false)
end
end
end

Expand All @@ -39,7 +59,6 @@ def to_s

before do
allow(instance).to receive(:execute).with(anything)

end

context 'with dirname sent' do
Expand Down

0 comments on commit d7c991f

Please sign in to comment.