Skip to content

Commit c97f88c

Browse files
authored
lsp: Implement LSP opaTestProvider feature in language server (#1888)
* Start impl of server testing with test locations Add test locations worker and notification system for LSP clients. Signed-off-by: Charlie Egan <charlie_egan@apple.com> * Add test for test locations on server Signed-off-by: Charlie Egan <charlie_egan@apple.com> * lsp: Testing, reduce scope to just single test The clients are able to run all tests in file etc. This make the impl more simple. Signed-off-by: Charlie Egan <charlie_egan@apple.com> --------- Signed-off-by: Charlie Egan <charlie_egan@apple.com>
1 parent effb43d commit c97f88c

20 files changed

Lines changed: 706 additions & 237 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- markdownlint-disable MD041 -->
99

1010
[![Build Status](https://github.com/open-policy-agent/regal/workflows/Build/badge.svg)](https://github.com/open-policy-agent/regal/actions)
11-
![OPA v1.13.1](https://www.openpolicyagent.org/badge/v1.13.1)
11+
![OPA v1.13.1-0.20260224105757-9b2143cdf99c](https://www.openpolicyagent.org/badge/v1.13.1-0.20260224105757-9b2143cdf99c)
1212
[![codecov](https://codecov.io/github/open-policy-agent/regal/graph/badge.svg?token=EQK01YF3X3)](https://codecov.io/github/StyraInc/regal)
1313
[![Downloads](https://img.shields.io/github/downloads/open-policy-agent/regal/total.svg)](https://github.com/open-policy-agent/regal/releases)
1414

bundle/regal/ast/ast.rego

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ rules := [rule |
103103
tests := [rule |
104104
some rule in rules
105105

106-
startswith(rule.head.ref[0].value, "test_")
106+
some part in rule.head.ref
107+
startswith(part.value, "test_")
107108
]
108109

109110
# METADATA
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# METADATA
2+
# description: |
3+
# This returns a set of test_ rule locations in a given module.
4+
# Used by the regal/testLocations method in the LSP to have clients know
5+
# where tests are.
6+
package regal.lsp.testlocations
7+
8+
import data.regal.ast
9+
import data.regal.result as rs
10+
11+
# METADATA
12+
# description: |
13+
# result contains a list of locations. A location is test name, package and
14+
# the location (which has start and end char range too).
15+
result contains object.union(loc, {
16+
"package": _package_ref_string,
17+
"name": ast.ref_static_to_string(test.head.ref),
18+
}) if {
19+
some test in ast.tests
20+
21+
loc := rs.location(test.head)
22+
}
23+
24+
_package_ref_string := ast.ref_to_string(input.package.path)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package regal.lsp.testlocations_test
2+
3+
import data.regal.lsp.testlocations
4+
5+
test_multiple_test_rules if {
6+
policy := `package foo_test
7+
8+
test_1 if {
9+
1 == 2
10+
}
11+
12+
test_2 if true
13+
14+
test_3 if {
15+
1 == 2
16+
2 == 1
17+
}
18+
`
19+
20+
result := testlocations.result with input as regal.parse_module("file://foo_test.rego", policy)
21+
22+
{
23+
{
24+
"package": "data.foo_test",
25+
"name": "test_1",
26+
"location": {
27+
"col": 1,
28+
"row": 3,
29+
"end": {"col": 7, "row": 3},
30+
"file": "file://foo_test.rego",
31+
"text": "test_1 if {",
32+
},
33+
},
34+
{
35+
"package": "data.foo_test",
36+
"name": "test_2",
37+
"location": {
38+
"col": 1,
39+
"row": 7,
40+
"end": {"col": 7, "row": 7},
41+
"file": "file://foo_test.rego",
42+
"text": "test_2 if true",
43+
},
44+
},
45+
{
46+
"package": "data.foo_test",
47+
"name": "test_3",
48+
"location": {
49+
"col": 3,
50+
"row": 9,
51+
"end": {"col": 9, "row": 9},
52+
"file": "file://foo_test.rego",
53+
"text": " test_3 if {",
54+
},
55+
},
56+
} == result
57+
}
58+
59+
test_no_test_rules if {
60+
policy := `package foo_test`
61+
62+
result := testlocations.result with input as regal.parse_module("file://foo_test.rego", policy)
63+
64+
set() == result
65+
}
66+
67+
test_non_test_package if {
68+
policy := `package foo
69+
70+
test_foo if true
71+
`
72+
73+
result := testlocations.result with input as regal.parse_module("file://foo.rego", policy)
74+
75+
{{
76+
"package": "data.foo",
77+
"name": "test_foo",
78+
"location": {
79+
"col": 1,
80+
"row": 3,
81+
"end": {"col": 9, "row": 3},
82+
"file": "file://foo.rego",
83+
"text": "test_foo if true",
84+
},
85+
}} == result
86+
}
87+
88+
test_funny_test_names_package if {
89+
policy := `package foo
90+
91+
foo.bar.test_me if {
92+
false
93+
}
94+
95+
foo.bar.test_me.baz if {
96+
false
97+
}
98+
`
99+
100+
result := testlocations.result with input as regal.parse_module("file://foo.rego", policy)
101+
102+
{
103+
{
104+
"package": "data.foo",
105+
"name": "foo.bar.test_me",
106+
"location": {
107+
"col": 1,
108+
"row": 3,
109+
"end": {"col": 16, "row": 3},
110+
"file": "file://foo.rego",
111+
"text": "foo.bar.test_me if {",
112+
},
113+
},
114+
{
115+
"package": "data.foo",
116+
"name": "foo.bar.test_me.baz",
117+
"location": {
118+
"col": 1,
119+
"row": 7,
120+
"end": {"col": 20, "row": 7},
121+
"file": "file://foo.rego",
122+
"text": "foo.bar.test_me.baz if {",
123+
},
124+
},
125+
} == result
126+
}

cmd/languageserver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func init() {
5858

5959
go ls.StartDiagnosticsWorker(ctx)
6060
go ls.StartHoverWorker(ctx)
61+
go ls.StartTestLocationsWorker(ctx)
6162
go ls.StartCommandWorker(ctx)
6263
go ls.StartConfigWorker(ctx)
6364
go ls.StartWorkspaceStateWorker(ctx)

docs/readme-sections/badges.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22

33
[![Build Status](https://github.com/open-policy-agent/regal/workflows/Build/badge.svg)](https://github.com/open-policy-agent/regal/actions)
4-
![OPA v1.13.1](https://www.openpolicyagent.org/badge/v1.13.1)
4+
![OPA v1.13.1-0.20260224105757-9b2143cdf99c](https://www.openpolicyagent.org/badge/v1.13.1-0.20260224105757-9b2143cdf99c)
55
[![codecov](https://codecov.io/github/open-policy-agent/regal/graph/badge.svg?token=EQK01YF3X3)](https://codecov.io/github/StyraInc/regal)
66
[![Downloads](https://img.shields.io/github/downloads/open-policy-agent/regal/total.svg)](https://github.com/open-policy-agent/regal/releases)

e2e/testbuild/go.mod

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.26
55
replace github.com/open-policy-agent/regal => ../../
66

77
require (
8-
github.com/open-policy-agent/opa v1.13.1
8+
github.com/open-policy-agent/opa v1.13.1-0.20260224105757-9b2143cdf99c
99
github.com/open-policy-agent/regal v0.0.0-00010101000000-000000000000
1010
)
1111

@@ -19,18 +19,18 @@ require (
1919
github.com/bytecodealliance/wasmtime-go/v39 v39.0.1 // indirect
2020
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
2121
github.com/cespare/xxhash/v2 v2.3.0 // indirect
22-
github.com/clipperhouse/stringish v0.1.1 // indirect
23-
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
22+
github.com/clipperhouse/displaywidth v0.11.0 // indirect
23+
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
2424
github.com/cloudflare/circl v1.6.1 // indirect
2525
github.com/containerd/containerd/v2 v2.2.1 // indirect
2626
github.com/containerd/errdefs v1.0.0 // indirect
2727
github.com/containerd/log v0.1.0 // indirect
2828
github.com/containerd/platforms v1.0.0-rc.2 // indirect
2929
github.com/containerd/typeurl/v2 v2.2.3 // indirect
3030
github.com/cyphar/filepath-securejoin v0.5.1 // indirect
31-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
32-
github.com/dgraph-io/badger/v4 v4.9.0 // indirect
33-
github.com/dgraph-io/ristretto/v2 v2.3.0 // indirect
31+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.1 // indirect
32+
github.com/dgraph-io/badger/v4 v4.9.1 // indirect
33+
github.com/dgraph-io/ristretto/v2 v2.4.0 // indirect
3434
github.com/dustin/go-humanize v1.0.1 // indirect
3535
github.com/emirpasic/gods v1.18.1 // indirect
3636
github.com/fatih/color v1.18.0 // indirect
@@ -49,42 +49,42 @@ require (
4949
github.com/goccy/go-json v0.10.5 // indirect
5050
github.com/gogo/protobuf v1.3.2 // indirect
5151
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
52-
github.com/google/flatbuffers v25.9.23+incompatible // indirect
52+
github.com/google/flatbuffers v25.12.19+incompatible // indirect
5353
github.com/google/go-dap v0.12.0 // indirect
5454
github.com/google/pprof v0.0.0-20251007162407-5df77e3f7d1d // indirect
5555
github.com/google/uuid v1.6.0 // indirect
5656
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
57-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect
57+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 // indirect
5858
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
5959
github.com/huandu/go-clone v1.7.3 // indirect
60-
github.com/huandu/go-sqlbuilder v1.39.0 // indirect
60+
github.com/huandu/go-sqlbuilder v1.39.1 // indirect
6161
github.com/huandu/xstrings v1.5.0 // indirect
6262
github.com/inconshreveable/mousetrap v1.1.0 // indirect
6363
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
6464
github.com/json-iterator/go v1.1.12 // indirect
6565
github.com/jstemmer/go-junit-report/v2 v2.1.0 // indirect
6666
github.com/kevinburke/ssh_config v1.4.0 // indirect
67-
github.com/klauspost/compress v1.18.2 // indirect
67+
github.com/klauspost/compress v1.18.4 // indirect
6868
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
6969
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
7070
github.com/lestrrat-go/dsig v1.0.0 // indirect
7171
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
7272
github.com/lestrrat-go/httpcc v1.0.1 // indirect
73-
github.com/lestrrat-go/httprc/v3 v3.0.2 // indirect
73+
github.com/lestrrat-go/httprc/v3 v3.0.4 // indirect
7474
github.com/lestrrat-go/jwx/v3 v3.0.13 // indirect
7575
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
7676
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
7777
github.com/mattn/go-colorable v0.1.14 // indirect
7878
github.com/mattn/go-isatty v0.0.20 // indirect
79-
github.com/mattn/go-runewidth v0.0.19 // indirect
79+
github.com/mattn/go-runewidth v0.0.20 // indirect
8080
github.com/moby/locker v1.0.1 // indirect
8181
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
8282
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
8383
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
8484
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
85-
github.com/olekukonko/errors v1.1.0 // indirect
86-
github.com/olekukonko/ll v0.1.2 // indirect
87-
github.com/olekukonko/tablewriter v1.1.0 // indirect
85+
github.com/olekukonko/errors v1.2.0 // indirect
86+
github.com/olekukonko/ll v0.1.7 // indirect
87+
github.com/olekukonko/tablewriter v1.1.3 // indirect
8888
github.com/opencontainers/go-digest v1.0.0 // indirect
8989
github.com/opencontainers/image-spec v1.1.1 // indirect
9090
github.com/owenrumney/go-sarif/v2 v2.3.3 // indirect
@@ -95,10 +95,9 @@ require (
9595
github.com/pkg/profile v1.7.0 // indirect
9696
github.com/prometheus/client_golang v1.23.2 // indirect
9797
github.com/prometheus/client_model v0.6.2 // indirect
98-
github.com/prometheus/common v0.67.2 // indirect
98+
github.com/prometheus/common v0.67.5 // indirect
9999
github.com/prometheus/procfs v0.19.2 // indirect
100100
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
101-
github.com/rivo/uniseg v0.4.7 // indirect
102101
github.com/sagikazarmark/locafero v0.12.0 // indirect
103102
github.com/segmentio/asm v1.2.1 // indirect
104103
github.com/sergi/go-diff v1.4.0 // indirect
@@ -112,34 +111,34 @@ require (
112111
github.com/spf13/viper v1.21.0 // indirect
113112
github.com/subosito/gotenv v1.6.0 // indirect
114113
github.com/tchap/go-patricia/v2 v2.3.3 // indirect
115-
github.com/valyala/fastjson v1.6.7 // indirect
116-
github.com/vektah/gqlparser/v2 v2.5.31 // indirect
114+
github.com/valyala/fastjson v1.6.10 // indirect
115+
github.com/vektah/gqlparser/v2 v2.5.32 // indirect
117116
github.com/xanzy/ssh-agent v0.3.3 // indirect
118117
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
119118
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
120119
github.com/yashtewari/glob-intersection v0.2.0 // indirect
121120
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
122-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.64.0 // indirect
123-
go.opentelemetry.io/otel v1.39.0 // indirect
124-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.39.0 // indirect
125-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.39.0 // indirect
126-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.39.0 // indirect
127-
go.opentelemetry.io/otel/metric v1.39.0 // indirect
128-
go.opentelemetry.io/otel/sdk v1.39.0 // indirect
129-
go.opentelemetry.io/otel/trace v1.39.0 // indirect
121+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0 // indirect
122+
go.opentelemetry.io/otel v1.40.0 // indirect
123+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 // indirect
124+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.40.0 // indirect
125+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0 // indirect
126+
go.opentelemetry.io/otel/metric v1.40.0 // indirect
127+
go.opentelemetry.io/otel/sdk v1.40.0 // indirect
128+
go.opentelemetry.io/otel/trace v1.40.0 // indirect
130129
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
131130
go.uber.org/automaxprocs v1.6.0 // indirect
132131
go.yaml.in/yaml/v2 v2.4.3 // indirect
133132
go.yaml.in/yaml/v3 v3.0.4 // indirect
134-
golang.org/x/crypto v0.47.0 // indirect
135-
golang.org/x/net v0.49.0 // indirect
133+
golang.org/x/crypto v0.48.0 // indirect
134+
golang.org/x/net v0.50.0 // indirect
136135
golang.org/x/sync v0.19.0 // indirect
137-
golang.org/x/sys v0.40.0 // indirect
138-
golang.org/x/text v0.33.0 // indirect
136+
golang.org/x/sys v0.41.0 // indirect
137+
golang.org/x/text v0.34.0 // indirect
139138
golang.org/x/time v0.14.0 // indirect
140-
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217 // indirect
141-
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 // indirect
142-
google.golang.org/grpc v1.78.0 // indirect
139+
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 // indirect
140+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect
141+
google.golang.org/grpc v1.79.1 // indirect
143142
google.golang.org/protobuf v1.36.11 // indirect
144143
gopkg.in/ini.v1 v1.67.1 // indirect
145144
gopkg.in/warnings.v0 v0.1.2 // indirect

0 commit comments

Comments
 (0)