Skip to content

Commit fa16636

Browse files
committed
Allow to run the test suite when rspec is used:
- `bundle exec rake test` don't work in some gems. While I plan to allow users to configure this command, I'd like Easy Compile to support `bundle exec rake spec` which is quite common.
1 parent ac7f9b2 commit fa16636

File tree

10 files changed

+120
-7
lines changed

10 files changed

+120
-7
lines changed

.github/actions/easy_compile/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ runs:
7070
if: "${{ startsWith(inputs.step, 'test') }}"
7171
working-directory: ${{ inputs.working-directory }}
7272
shell: bash
73-
run: bundle exec rake test # TODO This should be configurable
73+
run: easy_compile test # TODO This should be configurable
7474
- name: "Download tarball for platform"
7575
if: "${{ inputs.step == 'install' }}"
7676
uses: actions/download-artifact@v5

lib/easy_compile/cli.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def self.exit_on_failure?
2525
to the compilation.
2626
MSG
2727
def compile
28-
run_rake_tasks!(:compile)
28+
run_rake_tasks!("easy_compile:setup", :compile)
2929
end
3030

3131
desc "package", "Compile and package a 'fat gem'.", hide: true
@@ -44,12 +44,20 @@ def compile
4444
def package
4545
ENV["RUBY_CC_VERSION"] ||= compilation_task.ruby_cc_version
4646

47-
run_rake_tasks!(:cross, :native, :gem)
47+
run_rake_tasks!("easy_compile:setup", :cross, :native, :gem)
48+
end
49+
50+
desc "test", "Run the test suites of the target gem"
51+
long_desc <<~EOM
52+
EasyCompile will run the test suite of the gem. It either expects a `spec` or `test` task defined.
53+
EOM
54+
def test
55+
run_rake_tasks!(:test)
4856
end
4957

5058
desc "copy_from_staging_to_lib", "Copy the staging binary. For internal usage.", hide: true
5159
def copy_from_staging_to_lib
52-
run_rake_tasks!("copy:stage:lib")
60+
run_rake_tasks!("easy_compile:setup", "copy:stage:lib")
5361
end
5462

5563
desc "clean", "Cleanup temporary compilation artifacts."
@@ -60,7 +68,7 @@ def copy_from_staging_to_lib
6068
to cleanup by adding files to the vanilla CLEAN rake list.
6169
MSG
6270
def clean
63-
run_rake_tasks!(:clean)
71+
run_rake_tasks!("easy_compile:setup", :clean)
6472
end
6573

6674
desc "clobber", "Remove compiled binaries."
@@ -71,7 +79,7 @@ def clean
7179
to remove by adding files to the vanilla CLOBBER rake list.
7280
MSG
7381
def clobber
74-
run_rake_tasks!(:clobber)
82+
run_rake_tasks!("easy_compile:setup", :clobber)
7583
end
7684

7785
desc "ci_template", "Generate CI template files."

lib/easy_compile/tasks/wrapper.rake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
require_relative "../compilation_tasks"
44

55
task = EasyCompile::CompilationTasks.new(!Rake::Task.task_defined?(:package))
6-
task.setup
6+
7+
task "easy_compile:setup" do
8+
task.setup
9+
end
710

811
task "copy:stage:lib" do
912
version = RUBY_VERSION.match(/(\d\.\d)/)[1]
@@ -12,3 +15,11 @@ task "copy:stage:lib" do
1215

1316
cp_r(src, dest, remove_destination: true)
1417
end
18+
19+
unless Rake::Task.task_defined?(:test)
20+
task(:test) do
21+
raise(RuntimeError, "Don't know how to build task 'test'") unless Rake::Task.task_defined?(:spec)
22+
23+
Rake::Task[:spec].invoke
24+
end
25+
end

test/cli_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ def test_when_cli_runs_in_project_with_no_native_extension
150150
MSG
151151
end
152152

153+
def test_cli_test_command_when_a_test_rake_task_is_defined
154+
out = nil
155+
156+
Dir.chdir("test/fixtures/test_task_defined") do
157+
out, _ = capture_subprocess_io do
158+
CLI.start(["test"])
159+
end
160+
end
161+
162+
assert_equal("The test task was called.", out)
163+
end
164+
165+
def test_cli_test_command_when_a_spec_rake_task_is_defined
166+
out = nil
167+
168+
Dir.chdir("test/fixtures/spec_task_defined") do
169+
out, _ = capture_subprocess_io do
170+
CLI.start(["test"])
171+
end
172+
end
173+
174+
assert_equal("The spec task was called.", out)
175+
end
176+
177+
def test_cli_test_command_when_no_test_or_spec_rake_task_is_defined
178+
Dir.chdir("test/fixtures/no_test_task_defined") do
179+
capture_subprocess_io do
180+
assert_raises(RuntimeError) do
181+
CLI.start(["test"])
182+
end
183+
end
184+
end
185+
end
186+
153187
private
154188

155189
def raise_instead_of_exit(&block)

test/fixtures/no_test_task_defined/Rakefile

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "foo"
5+
spec.version = '0.0.1'
6+
spec.authors = ["John Doe"]
7+
spec.email = ["john@example.com"]
8+
9+
spec.summary = "Not important"
10+
spec.description = "Not important"
11+
spec.homepage = "https://github.com"
12+
spec.license = "MIT"
13+
spec.required_ruby_version = ">= 3.0.0"
14+
15+
spec.files = []
16+
spec.require_paths = ["lib"]
17+
spec.extensions = ["ext/foo.rb"]
18+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
task :spec do
2+
print "The spec task was called."
3+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "foo"
5+
spec.version = '0.0.1'
6+
spec.authors = ["John Doe"]
7+
spec.email = ["john@example.com"]
8+
9+
spec.summary = "Not important"
10+
spec.description = "Not important"
11+
spec.homepage = "https://github.com"
12+
spec.license = "MIT"
13+
spec.required_ruby_version = ">= 3.0.0"
14+
15+
spec.files = []
16+
spec.require_paths = ["lib"]
17+
spec.extensions = ["ext/foo.rb"]
18+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
task :test do
2+
print "The test task was called."
3+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "foo"
5+
spec.version = '0.0.1'
6+
spec.authors = ["John Doe"]
7+
spec.email = ["john@example.com"]
8+
9+
spec.summary = "Not important"
10+
spec.description = "Not important"
11+
spec.homepage = "https://github.com"
12+
spec.license = "MIT"
13+
spec.required_ruby_version = ">= 3.0.0"
14+
15+
spec.files = []
16+
spec.require_paths = ["lib"]
17+
spec.extensions = ["ext/foo.rb"]
18+
end

0 commit comments

Comments
 (0)