Module: Lucent::API

Includes:
RSpec::Matchers
Defined in:
lib/lucent/api.rb,
lib/lucent/cli.rb,
lib/lucent/assert.rb

Instance Method Summary collapse

Instance Method Details

#all_outputObject



46
47
48
# File 'lib/lucent/api.rb', line 46

def all_output
  all_stdout << all_stderr
end

#all_stderrObject



55
56
57
58
# File 'lib/lucent/api.rb', line 55

def all_stderr
  stop_processes
  only_processes.inject('') { |out, ps| out << ps.stderr }
end

#all_stdoutObject



50
51
52
53
# File 'lib/lucent/api.rb', line 50

def all_stdout
  stop_processes
  only_processes.inject('') { |out, ps| out << ps.stdout }
end

#assert_exact_output(expected, actual) ⇒ Object



33
34
35
# File 'lib/lucent/assert.rb', line 33

def assert_exact_output(expected, actual)
  unescape(actual).should == unescape(expected)
end

#assert_exit_status(value) ⇒ Object



37
38
39
# File 'lib/lucent/assert.rb', line 37

def assert_exit_status(value)
  last_exit_status.should eq(value), "Exit status was #{last_exit_status} but expected it to be #{value}."
end

#assert_exit_status_and_output(expect_pass, expected, expect_exact) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/lucent/assert.rb', line 7

def assert_exit_status_and_output(expect_pass, expected, expect_exact)
  assert_success(expect_pass)
  if expect_exact
    assert_exact_output(expected, all_output)
  else
    assert_partial_output(expected, all_output)
  end
end

#assert_failing_with(expected) ⇒ Object



16
17
18
19
# File 'lib/lucent/assert.rb', line 16

def assert_failing_with(expected)
  assert_success(false)
  assert_output(expected, all_output)
end

#assert_not_exit_status(value) ⇒ Object



41
42
43
# File 'lib/lucent/assert.rb', line 41

def assert_not_exit_status(value)
  last_exit_status.should_not eq(value), "Exit status was #{last_exit_status} which was not expected."
end

#assert_output(expected, actual) ⇒ Object



25
26
27
# File 'lib/lucent/assert.rb', line 25

def assert_output(expected, actual)
  actual.should include(expected)
end

#assert_partial_output(expected, actual) ⇒ Object



29
30
31
# File 'lib/lucent/assert.rb', line 29

def assert_partial_output(expected, actual)
  unescape(actual).should include(unescape(expected))
end

#assert_success(expect_success) ⇒ Object



21
22
23
# File 'lib/lucent/assert.rb', line 21

def assert_success(expect_success)
  expect_success ? assert_exit_status(0) : assert_not_exit_status(0)
end

#create_file(name, content) ⇒ Object



8
9
10
11
12
13
# File 'lib/lucent/cli.rb', line 8

def create_file(name, content)
  in_lucent_directory do
    create_path_for(File.dirname(name))
    File.open(name, 'w') { |f| f << content }
  end
end

#create_path_for(name) ⇒ Object



100
101
102
# File 'lib/lucent/api.rb', line 100

def create_path_for(name)
  FileUtils.mkdir_p(name) unless File.directory?(name)
end

#create_ruby_command(command) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/lucent/api.rb', line 109

def create_ruby_command(command)
  if command =~ /^ruby\s/
    command.gsub(/^ruby\s/, "#{system_ruby} ")
  else
    command
  end
end

#in_lucent_directory(&block) ⇒ Object



83
84
85
86
# File 'lib/lucent/api.rb', line 83

def in_lucent_directory(&block)
  create_path_for(lucent_directory)
  Dir.chdir(lucent_directory, &block)
end

#last_exit_statusObject



40
41
42
43
44
# File 'lib/lucent/api.rb', line 40

def last_exit_status
  return @last_exit_status if @last_exit_status
  stop_processes
  @last_exit_status
end

#lucent_directoryObject



89
90
91
# File 'lib/lucent/api.rb', line 89

def lucent_directory
  File.join(*lucent_path)
end

#lucent_pathObject

Note: @path was @dirs



95
96
97
# File 'lib/lucent/api.rb', line 95

def lucent_path
  @path ||= %w(tmp lucent)
end

#only_processesObject



78
79
80
# File 'lib/lucent/api.rb', line 78

def only_processes
  processes.collect { |command, process| process }
end

#processesObject



60
61
62
# File 'lib/lucent/api.rb', line 60

def processes
  @processes ||= []
end

#register_process(command, process) ⇒ Object



64
65
66
# File 'lib/lucent/api.rb', line 64

def register_process(command, process)
  processes << [command, process]
end

#run(command) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lucent/api.rb', line 16

def run(command)
  @commands ||= []
  @commands << command
  puts "DEBUG: @commands = #{@commands}"

  command = create_ruby_command(command)
  puts "DEBUG: (ruby) command = #{command}"

  puts "DEBUG: Lucent.process = #{Lucent.process}"

  in_lucent_directory do
    process = Lucent.process.new(command)

    puts "DEBUG: Process = #{process.inspect}"

    register_process(command, process)
    process.run

    puts "DEBUG: Block given for run standard? #{block_given?}"

    block_given? ? yield(process) : process
  end
end

#run_standard(command) ⇒ Object



10
11
12
13
14
# File 'lib/lucent/api.rb', line 10

def run_standard(command)
  run(command) do |process|
    stop_process(process)
  end
end

#stop_process(process) ⇒ Object



68
69
70
# File 'lib/lucent/api.rb', line 68

def stop_process(process)
  @last_exit_status = process.stop
end

#stop_processesObject



72
73
74
75
76
# File 'lib/lucent/api.rb', line 72

def stop_processes
  processes.each do |command, process|
    stop_process(process)
  end
end

#system_rubyObject



117
118
119
# File 'lib/lucent/api.rb', line 117

def system_ruby
  File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
end

#unescape(string) ⇒ Object



104
105
106
107
# File 'lib/lucent/api.rb', line 104

def unescape(string)
  string = string.gsub(/\e\[\d+(?>(;\d+)*)m/, '')
  string
end

#write_file(name, content) ⇒ Object



4
5
6
# File 'lib/lucent/cli.rb', line 4

def write_file(name, content)
  create_file(name, content)
end