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

(BKR-1656) Fixes for cat and file_exist? on PSWindows #1654

Merged
merged 1 commit into from
Jun 3, 2020
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
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