/
tasks_spec.rb
210 lines (172 loc) · 7.11 KB
/
tasks_spec.rb
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require 'spec_helper'
require 'parallel_tests/tasks'
describe ParallelTests::Tasks do
describe ".parse_args" do
it "should return the count" do
args = {:count => 2}
expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "", "", ""])
end
it "should default to the prefix" do
args = {:count => "models"}
expect(ParallelTests::Tasks.parse_args(args)).to eq([nil, "models", "", ""])
end
it "should return the count and pattern" do
args = {:count => 2, :pattern => "models"}
expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "models", "", ""])
end
it "should return the count, pattern, and options" do
args = {:count => 2, :pattern => "plain", :options => "-p default"}
expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "plain", "-p default", ""])
end
it "should return the count, pattern, and options" do
args = {:count => 2, :pattern => "plain", :options => "-p default --group-by steps"}
expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "plain", "-p default --group-by steps", ""])
end
it "should return the count, pattern, test options, and pass-through options" do
args = {:count => 2, :pattern => "plain", :options => "-p default --group-by steps", :pass_through => "--runtime-log /path/to/log"}
expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "plain", "-p default --group-by steps", "--runtime-log /path/to/log"])
end
end
describe ".rails_env" do
it "should be test" do
expect(ParallelTests::Tasks.rails_env).to eq("test")
end
it "should disregard whatever was set" do
ENV["RAILS_ENV"] = "foo"
expect(ParallelTests::Tasks.rails_env).to eq("test")
end
end
describe ".run_in_parallel" do
let(:full_path){ File.expand_path("../../../bin/parallel_test", __FILE__) }
it "has the executable" do
expect(File.file?(full_path)).to eq(true)
expect(File.executable?(full_path)).to eq(true) unless Gem.win_platform?
end
it "runs command in parallel" do
expect(ParallelTests::Tasks).to receive(:system).with(/#{full_path} --exec 'echo'$/).and_return true
ParallelTests::Tasks.run_in_parallel("echo")
end
it "runs command with :count option" do
expect(ParallelTests::Tasks).to receive(:system).with(/#{full_path} --exec 'echo' -n 123$/).and_return true
ParallelTests::Tasks.run_in_parallel("echo", :count => 123)
end
it "runs without -n with blank :count option" do
expect(ParallelTests::Tasks).to receive(:system).with(/#{full_path} --exec 'echo'$/).and_return true
ParallelTests::Tasks.run_in_parallel("echo", :count => "")
end
it "runs command with :non_parallel option" do
expect(ParallelTests::Tasks).to receive(:system).with(/#{full_path} --exec 'echo' --non-parallel$/).and_return true
ParallelTests::Tasks.run_in_parallel("echo", :non_parallel => true)
end
it "runs aborts if the command fails" do
expect(ParallelTests::Tasks).to receive(:system).and_return false
expect(ParallelTests::Tasks).to receive(:abort).and_return false
ParallelTests::Tasks.run_in_parallel("echo")
end
end
describe ".suppress_output", unless: Gem.win_platform? do
def call(command, grep)
# Explictly run as a parameter to /bin/sh to simulate how
# the command will be run by parallel_test --exec
# This also tests shell escaping of single quotes
result = `/bin/sh -c '#{ParallelTests::Tasks.suppress_output(command, grep)}'`
[result, $?.success?]
end
context "with pipefail supported" do
before :all do
if not system("/bin/bash", "-c", "set -o pipefail 2>/dev/null && test 1")
skip "pipefail is not supported on your system"
end
end
it "should hide offending lines" do
expect(call("echo 123", "123")).to eq(["", true])
end
it "should not hide other lines" do
expect(call("echo 124", "123")).to eq(["124\n", true])
end
it "should fail if command fails and the pattern matches" do
expect(call("echo 123 && test", "123")).to eq(["", false])
end
it "should fail if command fails and the pattern fails" do
expect(call("echo 124 && test", "123")).to eq(["124\n", false])
end
end
context "without pipefail supported" do
before do
expect(ParallelTests::Tasks).to receive(:system).with('/bin/bash', '-c', 'set -o pipefail 2>/dev/null && test 1').and_return false
end
it "should not filter and succeed" do
expect(call("echo 123", "123")).to eq(["123\n", true])
end
it "should not filter and fail" do
expect(call("echo 123 && test", "123")).to eq(["123\n", false])
end
end
end
describe ".suppress_schema_load_output" do
before do
allow(ParallelTests::Tasks).to receive(:suppress_output)
end
it 'should call suppress output with command' do
ParallelTests::Tasks.suppress_schema_load_output('command')
expect(ParallelTests::Tasks).to have_received(:suppress_output).with('command', "^ ->\\|^-- ")
end
end
describe ".check_for_pending_migrations" do
after do
Rake.application.instance_variable_get('@tasks').delete("db:abort_if_pending_migrations")
Rake.application.instance_variable_get('@tasks').delete("app:db:abort_if_pending_migrations")
end
it "should do nothing if pending migrations is no defined" do
ParallelTests::Tasks.check_for_pending_migrations
end
it "should run pending migrations is task is defined" do
foo = 1
Rake::Task.define_task("db:abort_if_pending_migrations") do
foo = 2
end
ParallelTests::Tasks.check_for_pending_migrations
expect(foo).to eq(2)
end
it "should run pending migrations is app task is defined" do
foo = 1
Rake::Task.define_task("app:db:abort_if_pending_migrations") do
foo = 2
end
ParallelTests::Tasks.check_for_pending_migrations
expect(foo).to eq(2)
end
it "should not execute the task twice" do
foo = 1
Rake::Task.define_task("db:abort_if_pending_migrations") do
foo += 1
end
ParallelTests::Tasks.check_for_pending_migrations
ParallelTests::Tasks.check_for_pending_migrations
expect(foo).to eq(2)
end
end
describe '.purge_before_load' do
context 'Rails < 4.2.0' do
before do
stub_const('Rails', double(version: '3.2.1'))
end
it "should return nil for Rails < 4.2.0" do
expect(ParallelTests::Tasks.purge_before_load).to eq nil
end
end
context 'Rails > 4.2.0' do
before do
stub_const('Rails', double(version: '4.2.8'))
end
it "should return db:purge when defined" do
allow(Rake::Task).to receive(:task_defined?).with('db:purge') { true }
expect(ParallelTests::Tasks.purge_before_load).to eq 'db:purge'
end
it "should return app:db:purge when db:purge is not defined" do
allow(Rake::Task).to receive(:task_defined?).with('db:purge') { false }
expect(ParallelTests::Tasks.purge_before_load).to eq 'app:db:purge'
end
end
end
end