Skip to content

Add Rake test to run check and format commands #74

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions lib/syntax_tree/rake/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "rake"
require "rake/tasklib"

module SyntaxTree
module Rake
# A Rake task that runs check and format on a set of source files.
#
# Example:
#
# require 'syntax_tree/rake/task'
#
# SyntaxTree::Rake::Task.new do |t|
# t.source_files = '{app,config,lib}/**/*.rb'
# end
#
# This will create task that can be run with:
#
# rake syntax_tree:check_and_format
class Task < ::Rake::TaskLib
# Glob pattern to match source files.
# Defaults to 'lib/**/*.rb'.
attr_accessor :source_files

def initialize
@source_files = "lib/**/*.rb"

yield self if block_given?
define_task
end

private

def define_task
desc "Runs syntax_tree over source files"
task(:check_and_format) { run_task }
end

def run_task
%w[check format].each do |command|
SyntaxTree::CLI.run([command, source_files].compact)
end

# TODO: figure this out
# exit($?.exitstatus) if $?&.exited?
end
end
end
end
27 changes: 27 additions & 0 deletions test/task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative "test_helper"
require "syntax_tree/rake/task"

module SyntaxTree
class TaskTest < Minitest::Test
Invoke = Struct.new(:args)

def test_task
source_files = "{app,config,lib}/**/*.rb"

SyntaxTree::Rake::Task.new do |t|
t.source_files = source_files
end

invoke = []
SyntaxTree::CLI.stub(:run, ->(args) { invoke << Invoke.new(args) }) do
::Rake::Task["check_and_format"].invoke
end

assert_equal(
[["check", source_files], ["format", source_files]], invoke.map(&:args)
)
end
end
end