test with elm-test

This commit is contained in:
Brian Hicks 2023-05-24 15:07:37 -05:00
parent 39981f0ec6
commit d046c17097
No known key found for this signature in database
GPG Key ID: C4F324B9CAAB0D50
6 changed files with 101 additions and 5 deletions

10
BUCK
View File

@ -1,11 +1,19 @@
# A list of available rules and their signatures can be found here: https://buck2.build/docs/api/rules/
load("@prelude-nri//:elm.bzl", "elm_docs", "elm_format_diffs")
load("@prelude-nri//:elm.bzl", "elm_docs", "elm_format_diffs", "elm_test")
load("@prelude-nri//:node.bzl", "node_modules", "npm_bin", "npm_script_test", "prettier_diffs")
elm_docs(
name = "docs.json",
elm_json = "elm.json",
src = "src",
tests = [
":elm_test",
],
)
elm_test(
name = "elm_test",
test_srcs = glob(["test/**/*.elm"]),
)
filegroup(

View File

@ -1,4 +1,4 @@
load("@prelude-nri//:elm.bzl", "elm_app", "elm_format_diffs")
load("@prelude-nri//:elm.bzl", "elm_app", "elm_format_diffs", "elm_test")
load("@prelude-nri//:node.bzl", "prettier_diffs")
elm_app(
@ -10,6 +10,14 @@ elm_app(
"../src": "//:src",
"src": "src",
},
tests = [
":elm_test",
],
)
elm_test(
name = "elm_test",
test_srcs = glob(["tests/**/*.elm"]),
)
filegroup(

View File

@ -154,4 +154,45 @@ elm_format_diffs = rule(
providers = [ElmToolchainInfo],
),
}
)
def _elm_test_impl(ctx: "context") -> [[DefaultInfo.type, ExternalRunnerTestInfo.type]]:
command = [
ctx.attrs._python_toolchain[PythonToolchainInfo].interpreter,
cmd_args(ctx.attrs._elm_toolchain[ElmToolchainInfo].elm_test_workdir[DefaultInfo].default_outputs),
"--elm-test-binary", "elm-test", # TODO parameterize
ctx.label.package or '.',
cmd_args(ctx.attrs._elm_toolchain.get(ElmToolchainInfo).elm, format="--compiler={}"),
cmd_args(str(ctx.attrs.fuzz), format="--fuzz={}"),
]
if ctx.attrs.seed:
command.append(cmd_args(ctx.attrs.seed, format="--seed={}"))
command.extend(ctx.attrs.test_srcs)
return [
DefaultInfo(),
ExternalRunnerTestInfo(
type = "elm_test",
command = command,
run_from_project_root = False,
),
]
elm_test = rule(
impl = _elm_test_impl,
attrs = {
"test_srcs": attrs.list(attrs.source()),
"fuzz": attrs.int(default=100),
"seed": attrs.option(attrs.string(), default=None),
"_elm_toolchain": attrs.toolchain_dep(
default = "toolchains//:elm",
providers = [ElmToolchainInfo],
),
"_python_toolchain": attrs.toolchain_dep(
default = "toolchains//:python",
providers = [PythonToolchainInfo],
),
},
)

View File

@ -3,6 +3,11 @@ export_file(
visibility = ["PUBLIC"],
)
export_file(
name = "elm_test_workdir.py",
visibility = ["PUBLIC"],
)
ELM_COMPILER_URL = select({
"config//os:linux": "https://github.com/elm/compiler/releases/download/0.19.1/binary-for-linux-64-bit.gz",
"config//os:macos": "https://github.com/elm/compiler/releases/download/0.19.1/binary-for-mac-64-bit.gz",
@ -24,6 +29,7 @@ genrule(
cmd = "gzip --decompress --stdout --keep $(location :elm_compiler_archive) > $OUT && chmod +x $OUT",
out = "elm",
executable = True,
remote = True,
visibility = ["PUBLIC"],
)
@ -54,5 +60,6 @@ genrule(
out = "elm-format",
cmd = "cp $(location :elm_format_archive)/elm-format $OUT",
executable = True,
remote = True,
visibility = ["PUBLIC"],
)

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python
import argparse
import subprocess
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"root", help="Where to change directory to before running elm-test"
)
parser.add_argument(
"--elm-test-binary",
default="elm-test",
help="The binary to invoke for elm-test",
)
parser.add_argument(
"elm_test_args",
nargs=argparse.REMAINDER,
help="Args to be passed on to elm-test",
)
args = parser.parse_args()
returncode = subprocess.call(
[args.elm_test_binary] + args.elm_test_args, cwd=args.root
)
sys.exit(returncode)

View File

@ -1,7 +1,8 @@
ElmToolchainInfo = provider(fields = [
"elm",
"isolated_compile",
"elm_format",
"isolated_compile",
"elm_test_workdir",
])
def _system_elm_toolchain_impl(ctx) -> [[DefaultInfo.type, ElmToolchainInfo.type]]:
@ -15,6 +16,7 @@ def _system_elm_toolchain_impl(ctx) -> [[DefaultInfo.type, ElmToolchainInfo.type
elm = RunInfo(args = ["elm"]),
elm_format = RunInfo(args = ["elm-format"]),
isolated_compile = ctx.attrs._isolated_compile,
elm_test_workdir = ctx.attrs._elm_test_workdir,
),
]
@ -22,6 +24,7 @@ system_elm_toolchain = rule(
impl = _system_elm_toolchain_impl,
attrs = {
"_isolated_compile": attrs.dep(default = "prelude-nri//elm:isolated_compile.py"),
"_elm_test_workdir": attrs.dep(default = "prelude-nri//elm:elm_test_workdir.py"),
},
is_toolchain_rule = True,
)
@ -37,21 +40,23 @@ def _elm_toolchain_impl(ctx: "context") -> [[DefaultInfo.type, ElmToolchainInfo.
elm = ctx.attrs.elm[RunInfo],
elm_format = ctx.attrs.elm_format[RunInfo],
isolated_compile = ctx.attrs._isolated_compile,
elm_test_workdir = ctx.attrs._elm_test_workdir,
),
]
elm_toolchain = rule(
impl = _elm_toolchain_impl,
attrs = {
"elm": attrs.dep(
"elm": attrs.exec_dep(
providers = [RunInfo],
default = "prelude-nri//elm:elm_compiler_binary",
),
"elm_format": attrs.dep(
"elm_format": attrs.exec_dep(
providers = [RunInfo],
default = "prelude-nri//elm:elm_format_binary",
),
"_isolated_compile": attrs.dep(default = "prelude-nri//elm:isolated_compile.py"),
"_elm_test_workdir": attrs.dep(default = "prelude-nri//elm:elm_test_workdir.py"),
},
is_toolchain_rule = True,
)