Skip to content

Commit ea12e88

Browse files
author
Konstantin Gredeskoul
committed
Introducing ruby_rspec_test and a bunch of others
* refactoring rules * moving some definitions to constants
1 parent 10a116c commit ea12e88

File tree

15 files changed

+317
-185
lines changed

15 files changed

+317
-185
lines changed

.circleci/config.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,25 @@ jobs:
6868
- run:
6969
name: "Bazel Simple Script Example Build"
7070
command: |
71-
cd examples/simple_script && bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} -- //...
71+
cd examples/simple_script && bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} -- :all
72+
73+
- run:
74+
name: "Bazel Simple Script Example Rubocop Check"
75+
command: |
76+
cd examples/simple_script && bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} -- :rubocop || true
7277
7378
- run:
7479
name: "Bazel Simple Script Example Test"
7580
command: |
76-
cd examples/simple_script && bazel ${BAZEL_OPTS} test ${BAZEL_BUILD_OPTS} ${BAZEL_TEST_OPTS} -- //...
81+
cd examples/simple_script
82+
bazel ${BAZEL_OPTS} test -s --verbose_failures --sandbox_debug \
83+
--test_output=streamed --test_verbose_timeout_warnings :all-specs
7784
7885
- run:
79-
name: "Bazel Simple Script Example Rubocop Check"
86+
name: "Print output log"
8087
command: |
81-
cd examples/simple_script && bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} -- :rubocop
88+
cat /home/circleci/.cache/bazel/_bazel_circleci/86ef9d8d260271208b18f4c01debfa13/execroot/bazelruby_ruby_rules_example/bazel-out/k8-fastbuild/testlogs/all-specs/test.log
89+
8290
8391
buildifier:
8492
<<: *bazel_defaults

WORKSPACE

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
workspace(name = "bazelruby_ruby_rules")
22

3-
load("@//ruby:deps.bzl", "ruby_register_toolchains", "ruby_rules_dependencies")
3+
load("@//ruby/private:dependencies.bzl", "ruby_rules_dependencies")
44

55
ruby_rules_dependencies()
66

7-
ruby_register_toolchains()
8-
97
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
108

119
bazel_skylib_workspace()
@@ -14,6 +12,10 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
1412

1513
versions.check("1.2.1")
1614

15+
load("@//ruby:deps.bzl", "ruby_register_toolchains")
16+
17+
ruby_register_toolchains()
18+
1719
local_repository(
1820
name = "bazelruby_ruby_rules_ruby_tests_testdata_another_workspace",
1921
path = "ruby/tests/testdata/another_workspace",

bin/test-suite

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ test::simple-script() {
148148
__test::exec simple-script "
149149
cd examples/simple_script
150150
${Bazel__BuildSteps}
151-
bazel ${BAZEL_OPTS} run ${BAZEL_BUILD_OPTS} -- :rubocop ;
151+
bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} :all;
152+
bazel ${BAZEL_OPTS} test ${BAZEL_BUILD_OPTS} :all-specs
152153
"
153154
}
154155

examples/simple_script/BUILD

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load(
44
"@bazelruby_ruby_rules//ruby:defs.bzl",
55
"ruby_binary",
66
"ruby_library",
7+
"ruby_rspec_test",
78
"ruby_test",
89
)
910

@@ -32,24 +33,46 @@ ruby_binary(
3233
)
3334

3435
ruby_test(
35-
name = "rspec",
36+
name = "all-specs",
3637
timeout = "short",
37-
srcs = [
38+
srcs = glob([
3839
"script.rb",
39-
"spec/lib/foo_spec.rb",
40-
"spec/script_spec.rb",
41-
"spec/spec_helper.rb",
42-
"@bundle//:bin/rspec",
43-
],
40+
"spec/**/*.rb",
41+
"lib/**/*.rb",
42+
]),
4443
args = [
45-
"spec",
44+
"--format documentation",
45+
"--force-color",
46+
"spec/spec_helper.rb",
47+
"spec/script_spec.rb",
48+
"spec/lib/foo_spec.rb",
4649
],
4750
includes = [
4851
".",
4952
"lib",
5053
"spec",
5154
],
5255
main = "spec/spec_helper.rb",
56+
rubyopt = ["-rrspec/autorun"],
57+
deps = [
58+
"//lib:foo",
59+
"@bundle//:awesome_print",
60+
"@bundle//:colored2",
61+
"@bundle//:rspec",
62+
"@bundle//:rspec-its",
63+
],
64+
)
65+
66+
ruby_rspec_test(
67+
name = "all-rspecs",
68+
srcs = [
69+
"script.rb",
70+
"spec/lib/foo_spec.rb",
71+
"spec/script_spec.rb",
72+
"spec/spec_helper.rb",
73+
],
74+
rubyopt = ["-rrspec/autorun"],
75+
spec_target = "spec/script_spec.rb",
5376
deps = [
5477
"//lib:foo",
5578
"@bundle//:awesome_print",
@@ -59,7 +82,6 @@ ruby_test(
5982
],
6083
)
6184

62-
# TODO: not the best way of defining this, should make `rubocop` rule like `buildifier`
6385
ruby_binary(
6486
name = "rubocop",
6587
srcs = [

examples/simple_script/WORKSPACE

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ local_repository(
88
)
99

1010
load(
11-
"@bazelruby_ruby_rules//ruby:deps.bzl",
12-
"ruby_register_toolchains",
11+
"@bazelruby_ruby_rules//ruby/private:dependencies.bzl",
1312
"ruby_rules_dependencies",
1413
)
1514

1615
ruby_rules_dependencies()
1716

18-
ruby_register_toolchains(version = "2.6.5")
19-
2017
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
2118

2219
bazel_skylib_workspace()
2320

21+
load(
22+
"@bazelruby_ruby_rules//ruby:deps.bzl",
23+
"ruby_register_toolchains",
24+
)
25+
26+
ruby_register_toolchains(version = "2.6.5")
27+
2428
load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
2529

2630
bundle_install(

examples/simple_script/script.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ def oss_rand
1111
end
1212

1313
puts Foo.aha + ' ' + oss_rand
14-
15-
ap Class.to_s

ruby/defs.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ load(
1515
"@bazelruby_ruby_rules//ruby/private:bundle.bzl",
1616
_ruby_bundle = "ruby_bundle",
1717
)
18+
load(
19+
"@bazelruby_ruby_rules//ruby/private:rspec.bzl",
20+
_ruby_rspec_test = "ruby_rspec_test",
21+
)
1822

1923
ruby_toolchain = _toolchain
2024
ruby_library = _library
2125
ruby_binary = _binary
2226
ruby_test = _test
27+
ruby_rspec_test = _ruby_rspec_test
2328
bundle_install = _ruby_bundle
2429
ruby_bundle = _ruby_bundle

ruby/private/binary.bzl

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load(":constants.bzl", "TOOLCHAIN_TYPE_NAME")
2-
load(":providers.bzl", "RubyLibrary")
1+
load(":constants.bzl", "RUBY_ATTRS", "TOOLCHAIN_TYPE_NAME")
32
load(
43
"//ruby/private/tools:deps.bzl",
54
_transitive_deps = "transitive_deps",
@@ -11,14 +10,19 @@ def _to_manifest_path(ctx, file):
1110
else:
1211
return ("%s/%s" % (ctx.workspace_name, file.short_path))
1312

14-
def _ruby_binary_impl(ctx):
13+
# Having this function allows us to override otherwise frozen attributes
14+
# such as main, srcs and deps. We use this in ruby_rspec_test rule by
15+
# adding rspec as a main, and sources, and rspec gem as a dependency.
16+
#
17+
# There could be similar situations in the future where we might want
18+
# to create a rule (eg, rubocop) that does exactly the same.
19+
def ruby_binary_macro(ctx, main, srcs, deps, args):
1520
sdk = ctx.toolchains[TOOLCHAIN_TYPE_NAME].ruby_runtime
1621
interpreter = sdk.interpreter[DefaultInfo].files_to_run.executable
1722

18-
main = ctx.file.main
1923
if not main:
2024
expected_name = "%s.rb" % ctx.attr.name
21-
for f in ctx.attr.srcs:
25+
for f in srcs:
2226
if f.label.name == expected_name:
2327
main = f.files.to_list()[0]
2428
break
@@ -30,6 +34,7 @@ def _ruby_binary_impl(ctx):
3034
)
3135

3236
executable = ctx.actions.declare_file(ctx.attr.name)
37+
3338
deps = _transitive_deps(
3439
ctx,
3540
extra_files = [executable],
@@ -55,41 +60,25 @@ def _ruby_binary_impl(ctx):
5560
data_runfiles = deps.data_files,
5661
)]
5762

58-
_ATTRS = {
59-
"srcs": attr.label_list(
60-
allow_files = True,
61-
),
62-
"deps": attr.label_list(
63-
providers = [RubyLibrary],
64-
),
65-
"includes": attr.string_list(),
66-
"rubyopt": attr.string_list(),
67-
"data": attr.label_list(
68-
allow_files = True,
69-
),
70-
"main": attr.label(
71-
allow_single_file = True,
72-
),
73-
"_wrapper_template": attr.label(
74-
allow_single_file = True,
75-
default = "binary_wrapper.tpl",
76-
),
77-
"_misc_deps": attr.label_list(
78-
allow_files = True,
79-
default = ["@bazel_tools//tools/bash/runfiles"],
80-
),
81-
}
63+
def ruby_binary_impl(ctx):
64+
return ruby_binary_macro(
65+
ctx,
66+
ctx.file.main,
67+
ctx.attr.srcs,
68+
ctx.attr.deps,
69+
ctx.attr.args,
70+
)
8271

8372
ruby_binary = rule(
84-
implementation = _ruby_binary_impl,
85-
attrs = _ATTRS,
73+
implementation = ruby_binary_impl,
74+
attrs = RUBY_ATTRS,
8675
executable = True,
8776
toolchains = [TOOLCHAIN_TYPE_NAME],
8877
)
8978

9079
ruby_test = rule(
91-
implementation = _ruby_binary_impl,
92-
attrs = _ATTRS,
80+
implementation = ruby_binary_impl,
81+
attrs = RUBY_ATTRS,
9382
test = True,
9483
toolchains = [TOOLCHAIN_TYPE_NAME],
9584
)

0 commit comments

Comments
 (0)