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.



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

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.



58
59
60
# File 'lib/train/transports/mock.rb', line 58

def options
  @options
end

Instance Method Details

#command_not_found(cmd) ⇒ Object



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

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



88
89
90
# File 'lib/train/transports/mock.rb', line 88

def commands
  @cache[:command]
end

#commands=(commands) ⇒ Object



84
85
86
# File 'lib/train/transports/mock.rb', line 84

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

#file_not_found(path) ⇒ Object



114
115
116
117
# File 'lib/train/transports/mock.rb', line 114

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

#filesObject



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

def files
  @cache[:file]
end

#files=(files) ⇒ Object



92
93
94
# File 'lib/train/transports/mock.rb', line 92

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

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



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

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



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

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



119
120
121
# File 'lib/train/transports/mock.rb', line 119

def to_s
  "Mock Connection"
end

#uriObject



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

def uri
  "mock://"
end