Skip to content

Commit

Permalink
Merge pull request #9 from chef/mock-cmd-sha
Browse files Browse the repository at this point in the history
allow mock command to support sha256 mocking of commands
  • Loading branch information
chris-rock committed Oct 14, 2015
2 parents a994a37 + f2a8898 commit 7516102
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/train/transports/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
end

def run_command(cmd)
@commands[cmd] || mock_command(cmd)
@commands[cmd] ||
@commands[Digest::SHA256.hexdigest cmd] ||
mock_command(cmd)
end

def file(path)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/transports/mock_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'helper'
require 'train/transports/mock'
require 'digest/sha2'

describe 'mock transport' do
let(:transport) { Train::Transports::Mock.new }
Expand Down Expand Up @@ -44,6 +45,14 @@
connection.mock_command(cmd, nil, nil, code)
connection.run_command(cmd).exit_status.must_equal(code)
end

it 'can mock a command via its SHA2 sum' do
out = rand.to_s
cmd = rand.to_s
shacmd = Digest::SHA256.hexdigest cmd
connection.mock_command(shacmd, out)
connection.run_command(cmd).stdout.must_equal(out)
end
end

describe 'when accessing a mocked os' do
Expand Down

0 comments on commit 7516102

Please sign in to comment.