Class: RSpec::Bash::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/bash/script.rb

Constant Summary collapse

MAIN_SCRIPT_FILE =
File.expand_path('../controller.sh', __FILE__)
NOOP =
lambda { |*| '' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, path = 'Anonymous') ⇒ Script

Returns a new instance of Script.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec/bash/script.rb', line 13

def initialize(source, path = 'Anonymous')
  @conditional_stubs = []
  @conditional_stub_calls = []
  @source = source
  @source_file = path
  @stubs = {}
  @stub_calls = Hash.new { |h, k| h[k] = [] }
  @stdout = ""
  @stderr = ""
  @exit_code = nil
end

Instance Attribute Details

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



11
12
13
# File 'lib/rspec/bash/script.rb', line 11

def exit_code
  @exit_code
end

#sourceObject (readonly)

Returns the value of attribute source.



11
12
13
# File 'lib/rspec/bash/script.rb', line 11

def source
  @source
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



11
12
13
# File 'lib/rspec/bash/script.rb', line 11

def source_file
  @source_file
end

#stderrObject (readonly)

Returns the value of attribute stderr.



11
12
13
# File 'lib/rspec/bash/script.rb', line 11

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



11
12
13
# File 'lib/rspec/bash/script.rb', line 11

def stdout
  @stdout
end

Class Method Details

.load(path) ⇒ Object



7
8
9
# File 'lib/rspec/bash/script.rb', line 7

def self.load(path)
  new(File.read(path))
end

Instance Method Details

#calls_for(name) ⇒ Object



65
66
67
# File 'lib/rspec/bash/script.rb', line 65

def calls_for(name)
  @stub_calls[name.to_sym]
end

#conditional_calls_for(expr) ⇒ Object



69
70
71
# File 'lib/rspec/bash/script.rb', line 69

def conditional_calls_for(expr)
  @conditional_stub_calls.select { |x| x[:expr] == expr }
end

#has_conditional_stubs?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rspec/bash/script.rb', line 61

def has_conditional_stubs?
  @conditional_stubs.any?
end

#inspectObject



29
30
31
# File 'lib/rspec/bash/script.rb', line 29

def inspect
  "Script(\"#{File.basename(@source_file)}\")"
end

#stub(fn, call_original: false, subshell: true, &body) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rspec/bash/script.rb', line 33

def stub(fn, call_original: false, subshell: true, &body)
  @stubs[fn.to_sym] = {
    body: (call_original || !body) ? NOOP : body,
    subshell: subshell,
    call_original: call_original
  }
end

#stub_conditional(expr, &body) ⇒ Object



41
42
43
# File 'lib/rspec/bash/script.rb', line 41

def stub_conditional(expr, &body)
  @conditional_stubs << { expr: expr, body: body || NOOP }
end

#stubbed(name, args) ⇒ Object



45
46
47
48
49
# File 'lib/rspec/bash/script.rb', line 45

def stubbed(name, args)
  fail "#{name} is not stubbed" unless @stubs.key?(name.to_sym)

  @stubs[name.to_sym][:body].call(args)
end

#stubbed_conditional(expr, args) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/rspec/bash/script.rb', line 51

def stubbed_conditional(expr, args)
  conditional_stub = @conditional_stubs.detect { |x| x[:expr] == expr }

  if conditional_stub
    conditional_stub[:body].call(args)
  else
    ""
  end
end

#to_sObject



25
26
27
# File 'lib/rspec/bash/script.rb', line 25

def to_s
  to_bash_script
end

#track_call(name, args) ⇒ Object



73
74
75
76
77
# File 'lib/rspec/bash/script.rb', line 73

def track_call(name, args)
  fail "#{name} is not stubbed" unless @stubs.key?(name.to_sym)

  @stub_calls[name.to_sym].push({ args: args })
end

#track_conditional_call(expr, args) ⇒ Object



79
80
81
# File 'lib/rspec/bash/script.rb', line 79

def track_conditional_call(expr, args)
  @conditional_stub_calls.push({ expr: expr, args: args })
end

#track_exit_code(code) ⇒ Object



83
84
85
# File 'lib/rspec/bash/script.rb', line 83

def track_exit_code(code)
  @exit_code = code
end