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

Constant Summary collapse

DEFAULTS =
{
  name: "mock",
  family: "mock",
  release: "unknown",
  arch: "unknown",
}.freeze

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



112
113
114
115
116
117
118
119
120
# File 'lib/train/transports/mock.rb', line 112

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



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

def commands
  @cache[:command]
end

#commands=(commands) ⇒ Object



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

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

#file_not_found(path) ⇒ Object



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

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

#filesObject



104
105
106
# File 'lib/train/transports/mock.rb', line 104

def files
  @cache[:file]
end

#files=(files) ⇒ Object



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

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

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



108
109
110
# File 'lib/train/transports/mock.rb', line 108

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/train/transports/mock.rb', line 80

def mock_os(value = {})
  value = DEFAULTS
    .merge(value || {})
    .transform_values { |v| v || "unknown" }

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

#to_sObject



127
128
129
# File 'lib/train/transports/mock.rb', line 127

def to_s
  "Mock Connection"
end

#uriObject



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

def uri
  "mock://"
end