Class: Train::Transports::Mock::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/mock.rb,
lib/train/transports/mock.rb,
lib/train/transports/mock.rb,
lib/train/transports/mock.rb

Defined Under Namespace

Classes: Command, File, OS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = nil) ⇒ Connection

Returns a new instance of Connection.



66
67
68
69
70
# File 'lib/train/transports/mock.rb', line 66

def initialize(conf = nil)
  super(conf)
  mock_os
  @commands = {}
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



63
64
65
# File 'lib/train/transports/mock.rb', line 63

def commands
  @commands
end

#filesObject

Returns the value of attribute files.



63
64
65
# File 'lib/train/transports/mock.rb', line 63

def files
  @files
end

#osObject (readonly)

Returns the value of attribute os.



64
65
66
# File 'lib/train/transports/mock.rb', line 64

def os
  @os
end

Instance Method Details

#command_not_found(cmd) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/train/transports/mock.rb', line 85

def command_not_found(cmd)
  if @options[:verbose]
    STDERR.puts('Command not mocked:')
    STDERR.puts('    '+cmd.to_s.split("\n").join("\n    "))
    STDERR.puts('    SHA: ' + Digest::SHA256.hexdigest(cmd.to_s))
  end
  # return a non-zero exit code
  mock_command(cmd, nil, nil, 1)
end

#file(path) ⇒ Object



106
107
108
# File 'lib/train/transports/mock.rb', line 106

def file(path)
  @files[path] ||= file_not_found(path)
end

#file_not_found(path) ⇒ Object



101
102
103
104
# File 'lib/train/transports/mock.rb', line 101

def file_not_found(path)
  STDERR.puts('File not mocked: '+path.to_s) if @options[:verbose]
  File.new(self, path)
end

#mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0) ⇒ Object



81
82
83
# File 'lib/train/transports/mock.rb', line 81

def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
  @commands[cmd] = Command.new(stdout || '', stderr || '', exit_status)
end

#mock_os(value = {}) ⇒ Object



76
77
78
79
# File 'lib/train/transports/mock.rb', line 76

def mock_os(value = {})
  os_params = { name: 'unknown', family: 'unknown', release: 'unknown', arch: 'unknown' }.merge(value)
  @os = OS.new(self, os_params)
end

#run_command(cmd) ⇒ Object



95
96
97
98
99
# File 'lib/train/transports/mock.rb', line 95

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

#to_sObject



110
111
112
# File 'lib/train/transports/mock.rb', line 110

def to_s
  'Mock Connection'
end

#uriObject



72
73
74
# File 'lib/train/transports/mock.rb', line 72

def uri
  'mock://'
end