File: test_helper.rb

package info (click to toggle)
camping 3.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: ruby: 5,032; javascript: 2,362; makefile: 29
file content (161 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'camping/tools'
$VERBOSE = nil

begin
  require 'rubygems'
rescue LoadError
end

if ENV['ABRIDGED']
  require 'camping'
else
  require 'camping-unabridged'
end

require 'minitest/autorun'
require 'rack/test'
require 'minitest/reporters'
require 'minitest/hooks'
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)]

module CommandLineCommands

  def move_to_tmp
    @original_dir = Dir.pwd
    Dir.chdir "test"
    Dir.mkdir("tmp") unless Dir.exist?("tmp")
    Dir.chdir "tmp"
  end

  def leave_tmp
    Dir.chdir @original_dir
    `rm -rf test/tmp` if File.exist?('test/tmp')
  end

  # reloader helpers:
  # move_to_apps
  # moves to the apps directory in /test
  def move_to_reloader
    @original_dir = Dir.pwd
    Dir.chdir "test"
    Dir.chdir "apps"
    Dir.chdir "reloader"
   	Dir.mkdir("apps") unless Dir.exist?("apps")
    Dir.mkdir("lib") unless Dir.exist?("lib")
  end

  # deletes the temporary directories found in the /apps directory for reloader testing.
  def leave_reloader
    leave_dir
    `rm -rf test/apps/reloader/apps` if File.exist?('test/apps/reloader/apps')
    `rm -rf test/apps/reloader/lib` if File.exist?('test/apps/reloader/lib')
  end

  # Moves to the loader directory
  def move_to_loader
    @original_dir = Dir.pwd
    Dir.chdir "test"
    Dir.chdir "apps"
    Dir.chdir "loader"
   	Dir.mkdir("apps") unless Dir.exist?("apps")
    Dir.mkdir("lib") unless Dir.exist?("lib")
  end

  # generic move_to(dir) method
  def move_to(dir)
    @original_dir = Dir.pwd
    Dir.chdir dir
  end

  # generic leave_dir method
  def leave_dir = Dir.chdir @original_dir

  def write(file, content)
    raise "cannot write nil" unless file
    file = tmp_file(file)
    folder = File.dirname(file)
    `mkdir -p #{folder}` unless File.exist?(folder)
    File.open(file, 'w') { |f| f.write content }
  end

  def read(file)
    File.read(tmp_file(file))
  end

  def tmp_file(file)
    "#{file}"
  end

  def write_config
    write 'config.kdl', <<-TXT
// config.kdl
database {
  default adapter="sqlite3" host="localhost" max_connections=5 timeout=5000
  development
  production adapter="postgres" database="kow"
}
hostname "crickets.com"
friends "_why" "judofyr" "chunky bacon"
TXT
  end

  def trash_config
    `rm -rf config.kdl` if File.exist?('config.kdl')
  end

end

class TestCase < Minitest::Test
  include Rack::Test::Methods
  include CommandLineCommands
  include Minitest::Hooks

  def self.inherited(mod)
    mod.app = Object.const_get(mod.to_s[/\w+/])
    super
  end

  class << self
    attr_accessor :app
  end

  def setup
    super
    Camping.make_camp
  end

  def body() last_response.body end
  def app()  self.class.app     end

  # adding this because sometimes the response is wonky???
  def response_body() last_response.to_a end

  def assert_reverse
    begin
      yield
    rescue Exception
    else
      assert false, "Block didn't fail"
    end
  end

  def assert_body(str, message="")
    case str
    when Regexp
      assert_match(str, last_response.body.strip, message)
    else
      assert_equal(str.to_s, last_response.body.strip, message)
    end
  end

  def assert_log(str, message="")
    logs = File.read Dir["./**/logs/development.log"].first
    assert(logs.to_s.match?(str), message)
  end

  def assert_status(code, message="")
    assert_equal(code, last_response.status, message)
  end

  def test_silly; end
end