Skip to content

Commit ed745b4

Browse files
restingbullclaude
andcommitted
test: add unit tests for kotlinc/javac opts flag mapping
Tests all three code paths in convert._to_flags: - value_to_flag dict lookup (warn, include_stdlibs, release) - map_value_to_flag fn (jvm_target, x_backend_threads, x_optin, x_warning_level) - derive.info path (x_suppress_warning, x_lint, add_exports, annotation_processor_options) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5aafd49 commit ed745b4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/test/starlark/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
load("//src/test/starlark/internal/jvm:resource_strip_prefix_test.bzl", "strip_resource_prefix_test_suite")
22
load("//src/test/starlark/internal/lint:ktlint_fix_runfiles_test.bzl", "ktlint_fix_runfiles_test_suite")
33
load(":convert_test.bzl", "convert_test_suite")
4+
load(":opts_test.bzl", "opts_test_suite")
45

56
convert_test_suite(name = "convert_tests")
67

8+
opts_test_suite(name = "opts_tests")
9+
710
strip_resource_prefix_test_suite(name = "resource_strip_prefix_tests")
811

912
ktlint_fix_runfiles_test_suite(name = "ktlint_fix_runfiles_tests")
@@ -13,6 +16,7 @@ test_suite(
1316
tests = [
1417
":convert_tests",
1518
":ktlint_fix_runfiles_tests",
19+
":opts_tests",
1620
":resource_strip_prefix_tests",
1721
"//src/test/starlark/ksp:ksp_tests",
1822
],

src/test/starlark/opts_test.bzl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
2+
load("//src/main/starlark/core/options:opts.javac.bzl", "javac_options_to_flags")
3+
load("//src/main/starlark/core/options:opts.kotlinc.bzl", "kotlinc_options_to_flags")
4+
5+
# x_backend_threads and x_optin require non-None values (map_value_to_flag fails with None)
6+
def _kopts(**kwargs):
7+
base = {"x_backend_threads": 1, "x_optin": []}
8+
base.update(kwargs)
9+
return struct(**base)
10+
11+
def _kotlinc_flags_impl(ctx):
12+
env = unittest.begin(ctx)
13+
14+
# map_value_to_flag path
15+
asserts.equals(env, ["-jvm-target=17"], kotlinc_options_to_flags(_kopts(jvm_target = "17")))
16+
asserts.equals(env, ["-Xbackend-threads=4"], kotlinc_options_to_flags(_kopts(x_backend_threads = 4)))
17+
asserts.equals(env, ["-opt-in=A", "-opt-in=B"], kotlinc_options_to_flags(_kopts(x_optin = ["A", "B"])))
18+
asserts.equals(env, ["-Xwarning-level=UNUSED:warning"], kotlinc_options_to_flags(_kopts(x_warning_level = {"UNUSED": "warning"})))
19+
20+
# value_to_flag dict path
21+
asserts.equals(env, ["-Werror"], kotlinc_options_to_flags(_kopts(warn = "error")))
22+
asserts.equals(env, ["-nowarn"], kotlinc_options_to_flags(_kopts(warn = "off")))
23+
asserts.equals(env, ["-no-stdlib"], kotlinc_options_to_flags(_kopts(include_stdlibs = "none")))
24+
25+
# derive.info path (repeated_values_for)
26+
asserts.equals(env, ["-Xsuppress-warning=SOME"], kotlinc_options_to_flags(_kopts(x_suppress_warning = ["SOME"])))
27+
28+
# defaults produce no flags; None provider is falsy
29+
asserts.equals(env, [], kotlinc_options_to_flags(_kopts()))
30+
asserts.true(env, not kotlinc_options_to_flags(None))
31+
32+
return unittest.end(env)
33+
34+
kotlinc_flags_test = unittest.make(_kotlinc_flags_impl)
35+
36+
def _javac_flags_impl(ctx):
37+
env = unittest.begin(ctx)
38+
39+
# value_to_flag dict path
40+
asserts.equals(env, ["--release 17"], javac_options_to_flags(struct(release = "17")))
41+
asserts.equals(env, ["-Werror"], javac_options_to_flags(struct(warn = "error")))
42+
asserts.equals(env, ["-nowarn"], javac_options_to_flags(struct(warn = "off")))
43+
44+
# derive.repeated_values_for path
45+
asserts.equals(env, ["-Xlint:all", "-Xlint:cast"], javac_options_to_flags(struct(x_lint = ["all", "cast"])))
46+
asserts.equals(env, ["--add-exports=java.base/sun.nio.ch"], javac_options_to_flags(struct(add_exports = ["java.base/sun.nio.ch"])))
47+
48+
# derive.format_key_value_for path
49+
asserts.equals(env, ["-Akey=v"], javac_options_to_flags(struct(annotation_processor_options = {"key": "v"})))
50+
51+
# None provider is falsy
52+
asserts.true(env, not javac_options_to_flags(None))
53+
54+
return unittest.end(env)
55+
56+
javac_flags_test = unittest.make(_javac_flags_impl)
57+
58+
def opts_test_suite(name):
59+
unittest.suite(name, kotlinc_flags_test, javac_flags_test)

0 commit comments

Comments
 (0)