From 6eae1265b64442fec1f032423aabe13accec53e2 Mon Sep 17 00:00:00 2001 From: Peter Boling Date: Sat, 11 Jan 2025 09:16:06 +0700 Subject: [PATCH] add --create flag --- .rubocop_gradual.lock | 19 ++++++++++++------- README.md | 9 +++++++++ lib/turbo_tests/cli.rb | 9 +++++++++ lib/turbo_tests/runner.rb | 8 ++++++++ spec/turbo_tests_spec.rb | 22 ++++++++++++++++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/.rubocop_gradual.lock b/.rubocop_gradual.lock index 0a6ba3c..24ec251 100644 --- a/.rubocop_gradual.lock +++ b/.rubocop_gradual.lock @@ -25,13 +25,14 @@ "lib/turbo_tests/reporter.rb:1386902608": [ [7, 5, 400, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2092085575] ], - "lib/turbo_tests/runner.rb:816710941": [ - [12, 5, 798, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2396047272], - [190, 11, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [208, 47, 6, "Style/GlobalStdStream: Use `$stderr` instead of `STDERR`.", 3356712163], - [210, 21, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [219, 7, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [279, 9, 6, "Style/GlobalStdStream: Use `$stdout` instead of `STDOUT`.", 3356722952] + "lib/turbo_tests/runner.rb:3148178217": [ + [13, 5, 271, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 21989008], + [20, 5, 798, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2396047272], + [198, 11, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [216, 47, 6, "Style/GlobalStdStream: Use `$stderr` instead of `STDERR`.", 3356712163], + [218, 21, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [227, 7, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [287, 9, 6, "Style/GlobalStdStream: Use `$stdout` instead of `STDOUT`.", 3356722952] ], "spec/cli_spec.rb:43879232": [ [1, 1, 30, "RSpec/SpecFilePathFormat: Spec path should end with `turbo_tests/cli*_spec.rb`.", 965721356], @@ -56,6 +57,10 @@ "spec/doc_formatter_spec.rb:233381593": [ [5, 16, 19, "RSpec/DescribeClass: The first argument to describe should be the class or module being tested.", 879596202] ], + "spec/turbo_tests_spec.rb:811756577": [ + [10, 15, 7, "RSpec/MessageSpies: Prefer `have_received` for setting message expectations. Setup `ParallelTests::Tasks` as a spy using `allow` or `instance_spy`.", 1384559950], + [20, 15, 7, "RSpec/MessageSpies: Prefer `have_received` for setting message expectations. Setup `ParallelTests::Tasks` as a spy using `allow` or `instance_spy`.", 1384559950] + ], "turbo_tests.gemspec:2105011563": [ [30, 16, 36, "ThreadSafety/DirChdir: Avoid using `Dir.chdir` due to its process-wide effect.", 3576345059], [31, 5, 19, "Packaging/GemspecGit: Avoid using git to produce lists of files. Downstreams often need to build your package in an environment that does not have git (on purpose). Use some pure Ruby alternative, like `Dir` or `Dir.glob`.", 3879951891] diff --git a/README.md b/README.md index 79ba6aa..47cc3b8 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,14 @@ Or install it yourself as: $ gem install turbo_tests ``` +## Setup + +Create test databases + +```bash +$ bundle exec turbo_tests --create +``` + ## Usage Execute tests: @@ -97,6 +105,7 @@ Options: -v, --verbose More output --fail-fast=[N] --seed SEED Seed for rspec + --create Create test databases ``` ## Development diff --git a/lib/turbo_tests/cli.rb b/lib/turbo_tests/cli.rb index bd8364e..89c04bb 100644 --- a/lib/turbo_tests/cli.rb +++ b/lib/turbo_tests/cli.rb @@ -17,6 +17,7 @@ def run verbose = false fail_fast = nil seed = nil + create = false OptionParser.new do |opts| opts.banner = <<~BANNER @@ -85,8 +86,16 @@ def run opts.on("--seed SEED", "Seed for rspec") do |s| seed = s end + + opts.on("--create", "Create databases") do + create = true + end end.parse!(@argv) + if create + return TurboTests::Runner.create(count) + end + requires.each { |f| require(f) } if formatters.empty? diff --git a/lib/turbo_tests/runner.rb b/lib/turbo_tests/runner.rb index bcd745e..c2b406d 100644 --- a/lib/turbo_tests/runner.rb +++ b/lib/turbo_tests/runner.rb @@ -2,6 +2,7 @@ require "json" require "parallel_tests/rspec/runner" +require "parallel_tests/tasks" require_relative "../utils/hash_extension" @@ -9,6 +10,13 @@ module TurboTests class Runner using CoreExtensions + def self.create(count) + ENV["PARALLEL_TEST_FIRST_IS_1"] = "true" + command = ["bundle", "exec", "rake", "db:create", "RAILS_ENV=#{ParallelTests::Tasks.rails_env}"] + args = {count: count.to_s} + ParallelTests::Tasks.run_in_parallel(command, args) + end + def self.run(opts = {}) files = opts[:files] formatters = opts[:formatters] diff --git a/spec/turbo_tests_spec.rb b/spec/turbo_tests_spec.rb index 611fdb6..438ccb1 100644 --- a/spec/turbo_tests_spec.rb +++ b/spec/turbo_tests_spec.rb @@ -2,4 +2,26 @@ it "has a version number" do expect(TurboTests::VERSION).not_to be_nil end + + describe "create" do + context "with nil count" do + it "creates databases" do + expect(ParallelTests::Tasks) + .to receive(:run_in_parallel) + .with(["bundle", "exec", "rake", "db:create", "RAILS_ENV=test"], {count: ""}) + + TurboTests::Runner.create(nil) + end + end + + context "with count" do + it "creates databases" do + expect(ParallelTests::Tasks) + .to receive(:run_in_parallel) + .with(["bundle", "exec", "rake", "db:create", "RAILS_ENV=test"], {count: "4"}) + + TurboTests::Runner.create(4) + end + end + end end