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

Defined Under Namespace

Classes: Command, File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = nil) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(conf = nil)
  super(conf)
  mock_os
  enable_cache(:file)
  enable_cache(:command)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



60
61
62
# File 'lib/train/transports/mock.rb', line 60

def options
  @options
end

Instance Method Details

#command_not_found(cmd) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/train/transports/mock.rb', line 106

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

#commandsObject



90
91
92
# File 'lib/train/transports/mock.rb', line 90

def commands
  @cache[:command]
end

#commands=(commands) ⇒ Object



86
87
88
# File 'lib/train/transports/mock.rb', line 86

def commands=(commands)
  @cache[:command] = commands
end

#file_not_found(path) ⇒ Object



116
117
118
119
# File 'lib/train/transports/mock.rb', line 116

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

#filesObject



98
99
100
# File 'lib/train/transports/mock.rb', line 98

def files
  @cache[:file]
end

#files=(files) ⇒ Object



94
95
96
# File 'lib/train/transports/mock.rb', line 94

def files=(files)
  @cache[:file] = files
end

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



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

def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
  @cache[:command][cmd] = Command.new(stdout || "", stderr || "", exit_status)
end

#mock_os(value = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/train/transports/mock.rb', line 73

def mock_os(value = {})
  # if a user passes a nil value, set to an empty hash so the merge still succeeds
  value ||= {}
  value.each { |k, v| value[k] = "unknown" if v.nil? }
  value = { name: "mock", family: "mock", release: "unknown", arch: "unknown" }.merge(value)

  platform = Train::Platforms.name(value[:name])
  platform.find_family_hierarchy
  platform.platform = value
  platform.add_platform_methods
  @platform = platform
end

#to_sObject



121
122
123
# File 'lib/train/transports/mock.rb', line 121

def to_s
  "Mock Connection"
end

#uriObject



69
70
71
# File 'lib/train/transports/mock.rb', line 69

def uri
  "mock://"
end