Rakefile

 1# frozen_string_literal: true
 2
 3require "rake/testtask"
 4require "rubocop/rake_task"
 5require "json"
 6require "tempfile"
 7
 8
 9Rake::TestTask.new(:unit) do |t|
10	ENV["ENV"] = "test"
11	ENV["COVERAGE_NAME"] = "unit"
12
13	t.libs << "test"
14	t.libs << "lib"
15	t.test_files = FileList["test/**/test_*.rb"].exclude("test/property/**/**")
16	t.warning = false
17end
18
19Rake::TestTask.new(:_run_property) do |t|
20	t.libs << "test"
21	t.libs << "test/property"
22	t.libs << "lib"
23	t.test_files = FileList["test/property/**/test_*.rb"] +
24		FileList["test/stubs/seed_*.rb"]
25	t.ruby_opts << "-r failure_reporter"
26	t.warning = false
27end
28Rake::Task[:_run_property].clear_comments
29
30desc "Run property tests (optional seed: rake property[SEED,no-stubs])"
31task :property, [:seed, :no_stubs] do |_t, args|
32	ENV["RANTLY_VERBOSE"] = ""
33	ENV["SEED"] = (args[:seed] || rand(0xFFFF)).to_i.to_s
34	ENV["COVERAGE_NAME"] = "property"
35	srand(ENV["SEED"].to_i)
36
37	warn "Property seed: #{ENV['SEED']}"
38
39	stub_path = "test/stubs/seed_#{ENV['SEED']}.rb"
40	if args[:seed] && File.exist?(stub_path)
41		ENV["TESTOPTS"] = "--seed=#{ENV['SEED']}"
42		Rake::TestTask.new(:_run_stub) do |t|
43			t.libs << "test"
44			t.libs << "test/property"
45			t.libs << "lib"
46			t.test_files = [stub_path]
47			t.warning = false
48		end
49		Rake::Task[:_run_stub].clear_comments
50		Rake::Task[:_run_stub].invoke
51		next
52	end
53
54	report = Tempfile.new(["failures", ".json"])
55	begin
56		ENV["FAILURE_REPORT_PATH"] = report.path
57		ENV["TESTOPTS"] = "--seed=#{ENV['SEED']}"
58
59		Rake::Task[:_run_property].invoke
60	rescue RuntimeError
61		unless args[:no_stubs]
62			failing = JSON.parse(File.read(report.path))
63
64			mkdir_p "test/stubs"
65			comment = failing.map { |n| "# - #{n}" }.join("\n")
66			File.write(stub_path, <<~RUBY)
67				# frozen_string_literal: true
68
69				# Failing tests:
70				#{comment}
71
72				srand(#{ENV['SEED']})
73				require_relative "../property/test_webhook_handler"
74			RUBY
75			warn "\nStub written: #{stub_path}"
76			warn "Reproduce:    bundle exec ruby -Itest -Itest/property -Ilib #{stub_path}"
77		end
78		exit 1
79	ensure
80		report&.close
81		report&.unlink
82	end
83end
84
85task :test => [:unit, :property]
86
87RuboCop::RakeTask.new(:lint)
88
89task :entr do
90	sh "sh", "-c", "git ls-files | entr -s 'rake test && rubocop'"
91end
92
93task default: :test