Skip to content

Commit

Permalink
Create custom adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mjacobus committed Mar 18, 2018
1 parent 488b5e8 commit 18c312f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 18 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,28 @@ bundle exec run_test some/file_spec.custom
# .test_runner.yml

adapters:
minitest:
adapter: Koine\TestRunner\Adapter\Ruby\Minitest
file_pattern: "*_test.rb"

rspec:
adapter: Koine\TestRunner\Adapter\Ruby\RSpec
adapter: Koine::TestRunner::Adapters::Ruby::RSpec
file_pattern: "*_spec.rb"

phpunit:
adapter: Koine\TestRunner\Adapter\Php\PHPUnit
adapter: Koine::TestRunner::Adapters::Php::PHPUnit
file_pattern: "*Test.php"

codeception:
adapter: Koine\TestRunner\Adapter\Php\Codeception
file_pattern: "*Spec.php"

jest:
adapter: Koine\TestRunner\Adapter\Javascript\Jest
file_pattern: "*_(test|spec).(js|jsx)"

adapter: Koine::TestRunner::Adapters::CustomAdapter
file_pattern: "client/.*.spec.js"
command: "./node_modules/.bin/jest"
commands:
all: "{command}"
file: "{command} {file}"
# oops, jest does not really filter by line
line: "{command} {file}"

# TODO
My::CustoAdapter:
require:
- some/file
adapter: MyCustomAdapter
suffix: '_spec.custom'
```
Expand Down
3 changes: 2 additions & 1 deletion lib/koine/test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'koine/test_runner/builder'
require 'koine/test_runner/adapters'
require 'koine/test_runner/adapters/base_adapter'
require 'koine/test_runner/adapters/base_regexp_adapter'
require 'koine/test_runner/adapters/custom_adapter'
require 'koine/test_runner/adapters/null_adapter'
require 'koine/test_runner/adapters/last_command_adapter'

Expand All @@ -12,7 +14,6 @@ class TestRunner
autoload :FileMatcher, 'koine/test_runner/file_matcher'

class Adapters
autoload :BaseRegexpAdapter, 'koine/test_runner/adapters/base_regexp_adapter'
autoload :Rspec, 'koine/test_runner/adapters/rspec'
autoload :Phpunit, 'koine/test_runner/adapters/phpunit'
end
Expand Down
41 changes: 41 additions & 0 deletions lib/koine/test_runner/adapters/custom_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Koine
class TestRunner
class Adapters
class CustomAdapter < BaseRegexpAdapter
def initialize(file_pattern:, command: nil, commands: {})
super(file_pattern: file_pattern)
@command = command
@commands = {}.tap do |hash|
commands.each do |key, value|
hash[key.to_sym] = value
end
end
end

private

def all_tests(config)
command(command_for(:all), config)
end

def single_file_command(config)
command(command_for(:file), config)
end

def file_line_command(config)
command(command_for(:line), config)
end

def command_for(type)
@commands.fetch(type).to_s
end

def command(template, config)
template.sub('{command}', @command)
.sub('{file}', config.file_path.to_s)
.sub('{line}', config.line.to_s)
end
end
end
end
end
13 changes: 9 additions & 4 deletions lib/koine/test_runner/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ def initialize_from_yaml_config(config_file)
end

def build_adapter(config)
adapter = config.delete('adapter')
adapter_name = config.delete('adapter')
adapter_class = adapter_name

if adapter.downcase == adapter.to_s
adapter_class = "Koine::TestRunner::Adapters::#{adapter.capitalize}"
if adapter_class.downcase == adapter_class.to_s
adapter_class = "Koine::TestRunner::Adapters::#{classify(adapter_class)}"
end

unless Object.const_defined?(adapter_class)
raise ArgumentError, "Cannot locate adapter #{adapter} => #{adapter_class}"
raise ArgumentError, "Cannot locate adapter #{adapter_name} => #{adapter_class}"
end

klass = Object.const_get(adapter_class)
klass.new(symbolize_keys(config))
end

def classify(klass)
klass.to_s.split('_').map(&:capitalize).join('')
end

def symbolize_keys(hash)
{}.tap do |new_hash|
hash.each do |key, value|
Expand Down
53 changes: 53 additions & 0 deletions spec/koine/test_runner/adapters/custom_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'spec_helper'

RSpec.describe Koine::TestRunner::Adapters::CustomAdapter do
let(:file) { 'foo/bar/baz/file_spec.rb' }

let(:adapter) do
described_class.new(
file_pattern: /_spec.rb$/,
command: './bin/rspec',
commands: {
all: '{command}',
'file' => '{command} {file}',
'line' => '{command} {file}:{line}'
}
)
end

subject { MockAdapter.succeed(adapter) }

it 'is a file pattern based adapter' do
expect(subject).to be_a(Koine::TestRunner::Adapters::BaseRegexpAdapter)
end

it 'inores files that is none of it\'s concern' do
config = Factory.config('some-other-file')

expect(subject.test_command(config)).to be_nil
end

it 'returns the command for all files' do
config = Factory.config(file, '--all')

command = subject.test_command(config)

expect(command).to eq('./bin/rspec')
end

it 'returns the command for single file' do
config = Factory.config(file)

command = subject.test_command(config)

expect(command).to eq("./bin/rspec #{file}")
end

it 'returns the command for single file filtered by line' do
config = Factory.config(file, '--line=10')

command = subject.test_command(config)

expect(command).to eq("./bin/rspec #{file}:10")
end
end
46 changes: 46 additions & 0 deletions spec/koine/test_runner/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,51 @@

expect(runner).to be_equal_to(expected_runner)
end

describe 'with snake_case_adapters' do
let(:file_config) do
{
'adapters' => {
'some' => {
'adapter' => 'custom_adapter',
'file_pattern' => 'client/.*.spec.js',
'commands' => { 'all' => 'bar' }
}
}
}
end

before do
allow(YAML).to receive(:load_file).and_return(file_config)
end

it 'creates a config based on snake_case_adapter_names' do
adapters = [
Koine::TestRunner::Adapters::CustomAdapter.new(
file_pattern: 'client/.*.spec.js',
commands: { 'all' => 'bar' }
)
]

expected_runner = Koine::TestRunner.new(adapters)

expect(runner).to be_equal_to(expected_runner)
end
end

it 'builds the runner based on the config file' do
adapters = [
Koine::TestRunner::Adapters::Rspec.new(
file_pattern: '.*_spec.rb$'
),
Koine::TestRunner::Adapters::Phpunit.new(
file_pattern: '.*Test.php$'
)
]

expected_runner = Koine::TestRunner.new(adapters)

expect(runner).to be_equal_to(expected_runner)
end
end
end
6 changes: 6 additions & 0 deletions spec/support/mock_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def null_adapter
Koine::TestRunner::Adapters::NullAdapter.new
end

def self.succeed(adapter)
mock = new(accept: true, command: nil)
adapter.next_adapter = mock
adapter
end

private

def single_file_command(_config)
Expand Down

0 comments on commit 18c312f

Please sign in to comment.