Skip to content

Commit 6d72cfb

Browse files
authored
Merge pull request #74 from wenderjean/feature/add-rake-task-to-check-and-format
Add Rake test to run check and format commands
2 parents dd1a0fe + 66f708b commit 6d72cfb

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

lib/syntax_tree/rake/check_task.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 on a set of source files.
9+
#
10+
# Example:
11+
#
12+
# require 'syntax_tree/rake/check_task'
13+
#
14+
# SyntaxTree::Rake::CheckTask.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 stree_check
21+
#
22+
class CheckTask < ::Rake::TaskLib
23+
# Name of the task.
24+
# Defaults to :stree_check.
25+
attr_accessor :name
26+
27+
# Glob pattern to match source files.
28+
# Defaults to 'lib/**/*.rb'.
29+
attr_accessor :source_files
30+
31+
def initialize(name = :stree_check)
32+
@name = name
33+
@source_files = "lib/**/*.rb"
34+
35+
yield self if block_given?
36+
define_task
37+
end
38+
39+
private
40+
41+
def define_task
42+
desc "Runs `stree check` over source files"
43+
task(name) { run_task }
44+
end
45+
46+
def run_task
47+
SyntaxTree::CLI.run(["check", source_files].compact)
48+
end
49+
end
50+
end
51+
end

lib/syntax_tree/rake/write_task.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 format on a set of source files.
9+
#
10+
# Example:
11+
#
12+
# require 'syntax_tree/rake/write_task'
13+
#
14+
# SyntaxTree::Rake::WriteTask.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 stree_write
21+
#
22+
class WriteTask < ::Rake::TaskLib
23+
# Name of the task.
24+
# Defaults to :stree_write.
25+
attr_accessor :name
26+
27+
# Glob pattern to match source files.
28+
# Defaults to 'lib/**/*.rb'.
29+
attr_accessor :source_files
30+
31+
def initialize(name = :stree_write)
32+
@name = name
33+
@source_files = "lib/**/*.rb"
34+
35+
yield self if block_given?
36+
define_task
37+
end
38+
39+
private
40+
41+
def define_task
42+
desc "Runs `stree write` over source files"
43+
task(name) { run_task }
44+
end
45+
46+
def run_task
47+
SyntaxTree::CLI.run(["write", source_files].compact)
48+
end
49+
end
50+
end
51+
end

test/check_task_test.rb

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

test/write_task_test.rb

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

0 commit comments

Comments
 (0)