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 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
51 changes: 51 additions & 0 deletions lib/syntax_tree/rake/check_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "rake"
require "rake/tasklib"

module SyntaxTree
module Rake
# A Rake task that runs check on a set of source files.
#
# Example:
#
# require 'syntax_tree/rake/check_task'
#
# SyntaxTree::Rake::CheckTask.new do |t|
# t.source_files = '{app,config,lib}/**/*.rb'
# end
#
# This will create task that can be run with:
#
# rake stree_check
#
class CheckTask < ::Rake::TaskLib
# Name of the task.
# Defaults to :stree_check.
attr_accessor :name

# Glob pattern to match source files.
# Defaults to 'lib/**/*.rb'.
attr_accessor :source_files

def initialize(name = :stree_check)
@name = name
@source_files = "lib/**/*.rb"

yield self if block_given?
define_task
end

private

def define_task
desc "Runs `stree check` over source files"
task(name) { run_task }
end

def run_task
SyntaxTree::CLI.run(["check", source_files].compact)
end
end
end
end
51 changes: 51 additions & 0 deletions lib/syntax_tree/rake/write_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "rake"
require "rake/tasklib"

module SyntaxTree
module Rake
# A Rake task that runs format on a set of source files.
#
# Example:
#
# require 'syntax_tree/rake/write_task'
#
# SyntaxTree::Rake::WriteTask.new do |t|
# t.source_files = '{app,config,lib}/**/*.rb'
# end
#
# This will create task that can be run with:
#
# rake stree_write
#
class WriteTask < ::Rake::TaskLib
# Name of the task.
# Defaults to :stree_write.
attr_accessor :name

# Glob pattern to match source files.
# Defaults to 'lib/**/*.rb'.
attr_accessor :source_files

def initialize(name = :stree_write)
@name = name
@source_files = "lib/**/*.rb"

yield self if block_given?
define_task
end

private

def define_task
desc "Runs `stree write` over source files"
task(name) { run_task }
end

def run_task
SyntaxTree::CLI.run(["write", source_files].compact)
end
end
end
end
23 changes: 23 additions & 0 deletions test/check_task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative "test_helper"
require "syntax_tree/rake/check_task"

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

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

SyntaxTree::Rake::CheckTask.new { |t| t.source_files = source_files }

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

assert_equal(["check", source_files], invoke.args)
end
end
end
23 changes: 23 additions & 0 deletions test/write_task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative "test_helper"
require "syntax_tree/rake/write_task"

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

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

SyntaxTree::Rake::WriteTask.new { |t| t.source_files = source_files }

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

assert_equal(["write", source_files], invoke.args)
end
end
end