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 { |*| '' }
NOOP_BEHAVIOR =
StubBehavior.new(body: NOOP, charges: Float::INFINITY)

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.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspec/bash/script.rb', line 17

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.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def exit_code
  @exit_code
end

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def source
  @source
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def source_file
  @source_file
end

#stderrObject (readonly)

Returns the value of attribute stderr.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def stdout
  @stdout
end

#stubsObject (readonly)

Returns the value of attribute stubs.



15
16
17
# File 'lib/rspec/bash/script.rb', line 15

def stubs
  @stubs
end

Class Method Details

.load(path) ⇒ Object



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

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

Instance Method Details

#calls_for(name) ⇒ Object



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

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

#conditional_calls_for(expr) ⇒ Object



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

def conditional_calls_for(expr)
  @conditional_stub_calls.select { |x| x.index(expr) == 0 }
end

#exact_conditional_calls_for(fullexpr) ⇒ Object



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

def exact_conditional_calls_for(fullexpr)
  @conditional_stub_calls.select { |x| x == fullexpr }
end

#has_conditional_stubs?Boolean

Returns:

  • (Boolean)


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

def has_conditional_stubs?
  @conditional_stubs.any?
end

#has_stub?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_stub?(name)
  @stubs.key?(name.to_sym)
end

#inspectObject



33
34
35
# File 'lib/rspec/bash/script.rb', line 33

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

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



37
38
39
40
41
42
43
# File 'lib/rspec/bash/script.rb', line 37

def stub(fn, behaviors:, call_original: false, subshell: true)
  @stubs[fn.to_sym] = {
    behaviors: behaviors.map { |x| StubBehavior.new(x) },
    subshell: subshell,
    call_original: call_original
  }
end

#stub_conditional(expr, behaviors:) ⇒ Object



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

def stub_conditional(expr, behaviors:)
  @conditional_stubs << {
    behaviors: behaviors.map { |x| StubBehavior.new(x) },
    expr: expr,
  }
end

#stubbed(name, args) ⇒ Object



52
53
54
# File 'lib/rspec/bash/script.rb', line 52

def stubbed(name, args)
  apply_matching_behavior @stubs[name.to_sym], args
end

#stubbed_conditional(fullexpr) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/rspec/bash/script.rb', line 56

def stubbed_conditional(fullexpr)
  conditional_stub = @conditional_stubs.detect { |x| fullexpr.index(x[:expr]) == 0 }

  if conditional_stub
    apply_matching_behavior conditional_stub, fullexpr
  else
    ""
  end
end

#to_sObject



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

def to_s
  ScriptGenerator.generate(self)
end

#track_call(name, args) ⇒ Object



86
87
88
89
90
# File 'lib/rspec/bash/script.rb', line 86

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(fullexpr) ⇒ Object



92
93
94
# File 'lib/rspec/bash/script.rb', line 92

def track_conditional_call(fullexpr)
  @conditional_stub_calls.push(fullexpr)
end

#track_exit_code(code) ⇒ Object



96
97
98
# File 'lib/rspec/bash/script.rb', line 96

def track_exit_code(code)
  @exit_code = code
end