Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --create flag #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Or install it yourself as:
$ gem install turbo_tests
```

## Setup

Create test databases

```bash
$ bundle exec turbo_tests --create
```
Comment on lines +64 to +68
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create test databases before running tests

  1. Start TEST_ENV_NUMBER from 1
export PARALLEL_TEST_FIRST_IS_1=true
  1. Create databases
bundle exec rake db:create RAILS_ENV=test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't create the necessary test databases. That will just create a single "_test" database, not one database per cpu.

Here is your suggestion:

❯ export PARALLEL_TEST_FIRST_IS_1=true
❯ bundle exec rake db:create RAILS_ENV=test
Database 'myapp_test' already exists

Here is my suggestion with this PR:

❯ bundle exec turbo_tests --create
Database 'myapp_test5' already exists
Database 'myapp_test9' already exists
Database 'myapp_test10' already exists
Database 'myapp_test1' already exists
Database 'myapp_test3' already exists
Database 'myapp_test7' already exists
Database 'myapp_test8' already exists
Database 'myapp_test6' already exists
Database 'myapp_test2' already exists
Database 'myapp_test4' already exists

The way to do this without any changes to turbo_tests is first install the parallel_test gem as a direct dependency, and then run this:

❯ export PARALLEL_TEST_FIRST_IS_1=true
❯ bundle exec rake parallel:create
Database 'myapp_test3' already exists
Database 'myapp_test1' already exists
Database 'myapp_test9' already exists
Database 'myapp_test2' already exists
Database 'myapp_test8' already exists
Database 'myapp_test5' already exists
Database 'myapp_test6' already exists
Database 'myapp_test10' already exists
Database 'myapp_test4' already exists
Database 'myapp_test7' already exists

But the problem with this is it it requires having the parallel_tests gem installed as a direct dependency. And this is a direct dependency that is only used for the purpose of this one-time command. With my PR, the user is no longer required to install parallel_tests as a direct dependency. They can just install turbo_tests which acts as their gateway to parallel_tests.


## Usage

Execute tests:
Expand All @@ -85,6 +93,7 @@ Options:
--runtime-log FILE Location of previously recorded test runtimes
-v, --verbose More output
--fail-fast=[N]
--create Create test databases
```

## Development
Expand Down
9 changes: 9 additions & 0 deletions lib/turbo_tests/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def run
runtime_log = nil
verbose = false
fail_fast = nil
create = false

OptionParser.new { |opts|
opts.banner = <<~BANNER
Expand Down Expand Up @@ -76,8 +77,16 @@ def run
end
fail_fast = n.nil? || n < 1 ? 1 : n
end

opts.on("--create", "Create databases") do
create = true
end
}.parse!(@argv)

if create
return TurboTests::Runner.create(count)
end

requires.each { |f| require(f) }

if formatters.empty?
Expand Down
8 changes: 8 additions & 0 deletions lib/turbo_tests/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

require "json"
require "parallel_tests/rspec/runner"
require "parallel_tests/tasks"

require_relative "../utils/hash_extension"

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
Comment on lines +13 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it should be added to the Runner.

I understand it'd be nicer to reduce the number of steps on the user side. But in this case, it seems, that wrapping two commands in a bash/rake command fits better.


def self.run(opts = {})
files = opts[:files]
formatters = opts[:formatters]
Expand Down
22 changes: 22 additions & 0 deletions spec/turbo_tests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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