File: tasks_spec.rb

package info (click to toggle)
ruby-parallel-tests 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 760 kB
  • sloc: ruby: 5,446; javascript: 16; makefile: 4
file content (269 lines) | stat: -rw-r--r-- 9,049 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# frozen_string_literal: true
require 'spec_helper'
require 'parallel_tests/tasks'
require 'rspec/support/spec/shell_out'

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, nil, nil, nil])
    end

    it "should default to the prefix" do
      args = { count: "models" }
      expect(ParallelTests::Tasks.parse_args(args)).to eq([nil, "models", nil, nil])
    end

    it "should return the count and pattern" do
      args = { count: 2, pattern: "models" }
      expect(ParallelTests::Tasks.parse_args(args)).to eq([2, "models", nil, nil])
    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", nil])
    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", nil])
    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
    include RSpec::Support::ShellOut

    it "is test when nothing was set" do
      expect(ParallelTests::Tasks.rails_env).to eq("test")
    end

    it "ignores RAILS_ENV since that is often set when rake is executed" do
      with_env "RAILS_ENV" => "bar" do
        expect(ParallelTests::Tasks.rails_env).to eq("test")
      end
    end

    it "uses PARALLEL_RAILS_ENV" do
      with_env "PARALLEL_RAILS_ENV" => "bar" do
        expect(ParallelTests::Tasks.rails_env).to eq("bar")
      end
    end
  end

  describe ".run_in_parallel" do
    let(:full_path) { File.expand_path('../../bin/parallel_test', __dir__) }

    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(*ParallelTests.with_ruby_binary(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(*ParallelTests.with_ruby_binary(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(*ParallelTests.with_ruby_binary(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(*ParallelTests.with_ruby_binary(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)
      # Explicitly run as a parameter to /bin/bash to simulate how
      # the command will be run by parallel_test --exec
      # This also tests shell escaping of single quotes
      shell_command = [
        '/bin/bash',
        '-c',
        Shellwords.shelljoin(ParallelTests::Tasks.suppress_output(command, grep))
      ]
      result = IO.popen(shell_command, &:read)
      [result, $?.success?]
    end

    context "with pipefail supported" do
      before :all do
        unless system("/bin/bash", "-c", "set -o pipefail 2>/dev/null")
          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(['/bin/bash', '-c', 'echo 123 && false'], "123")).to eq(["", false])
      end

      it "should fail if command fails and the pattern fails" do
        expect(call(['/bin/bash', '-c', 'echo 124 && false'], "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'
        ).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(['/bin/bash', '-c', 'echo 123 && false'], "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 'ActiveRecord < 4.2.0' do
      before do
        stub_const('ActiveRecord', double(version: Gem::Version.new('3.2.1')))
      end

      it "should return nil for ActiveRecord < 4.2.0" do
        expect(ParallelTests::Tasks.purge_before_load).to eq nil
      end
    end

    context 'ActiveRecord > 4.2.0' do
      before do
        stub_const('ActiveRecord', double(version: Gem::Version.new('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

  describe ".build_run_command" do
    it "builds simple command" do
      command = ParallelTests::Tasks.build_run_command("test", {})
      command.shift 2 if command.include?("--") # windows prefixes ruby executable
      expect(command).to eq [
        "#{Dir.pwd}/bin/parallel_test", "test", "--type", "test"
      ]
    end

    it "fails on unknown" do
      expect { ParallelTests::Tasks.build_run_command("foo", {}) }.to raise_error(KeyError)
    end

    it "builds with all arguments" do
      command = ParallelTests::Tasks.build_run_command(
        "test",
        count: 1, pattern: "foo", options: "bar", pass_through: "baz baz"
      )
      command.shift 2 if command.include?("--") # windows prefixes ruby executable
      expect(command).to eq [
        "#{Dir.pwd}/bin/parallel_test", "test", "--type", "test",
        "-n", "1", "--pattern", "foo", "--test-options", "bar", "baz", "baz"
      ]
    end
  end
end