Skip to content

Commit b39b1bf

Browse files
committed
Add Rake test to run check and format commands
1 parent 230a179 commit b39b1bf

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/syntax_tree/rake/task.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "rake"
4+
require "rake/tasklib"
5+
6+
module SyntaxTree
7+
module Rake
8+
# A Rake task that runs check and format on a set of source files.
9+
#
10+
# Example:
11+
#
12+
# require 'syntax_tree/rake/task'
13+
#
14+
# SyntaxTree::Rake::Task.new do |t|
15+
# t.source_files = '{app,config,lib}/**/*.rb'
16+
# end
17+
#
18+
# This will create task that can be run with:
19+
#
20+
# rake syntax_tree:check_and_format
21+
class Task < ::Rake::TaskLib
22+
# Glob pattern to match source files.
23+
# Defaults to 'lib/**/*.rb'.
24+
attr_accessor :source_files
25+
26+
def initialize
27+
@source_files = "lib/**/*.rb"
28+
29+
yield self if block_given?
30+
define_task
31+
end
32+
33+
private
34+
35+
def define_task
36+
desc "Runs syntax_tree over source files"
37+
task(:check_and_format) { run_task }
38+
end
39+
40+
def run_task
41+
%w[check format].each do |command|
42+
SyntaxTree::CLI.run([command, source_files].compact)
43+
end
44+
45+
# TODO: figure this out
46+
# exit($?.exitstatus) if $?&.exited?
47+
end
48+
end
49+
end
50+
end

test/task_test.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
require "syntax_tree/rake/task"
5+
6+
module SyntaxTree
7+
class TaskTest < Minitest::Test
8+
Invoke = Struct.new(:args)
9+
10+
def test_task
11+
source_files = "{app,config,lib}/**/*.rb"
12+
13+
SyntaxTree::Rake::Task.new do |t|
14+
t.source_files = source_files
15+
end
16+
17+
invoke = []
18+
SyntaxTree::CLI.stub(:run, ->(args) { invoke << Invoke.new(args) }) do
19+
::Rake::Task["check_and_format"].invoke
20+
end
21+
22+
assert_equal(
23+
[["check", source_files], ["format", source_files]], invoke.map(&:args)
24+
)
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)