@@ -2,10 +2,9 @@ package lsp
2
2
3
3
import (
4
4
"context"
5
- "io "
5
+ "maps "
6
6
"os"
7
7
"slices"
8
- "strings"
9
8
"testing"
10
9
11
10
rio "github.com/styrainc/regal/internal/io"
@@ -49,7 +48,9 @@ func TestEvalWorkspacePath(t *testing.T) {
49
48
ls .cache .SetModule ("file://policy1.rego" , module1 )
50
49
ls .cache .SetModule ("file://policy2.rego" , module2 )
51
50
52
- input := strings .NewReader (`{"exists": true}` )
51
+ input := map [string ]any {
52
+ "exists" : true ,
53
+ }
53
54
54
55
res , err := ls .EvalWorkspacePath (context .TODO (), "data.policy1.allow" , input )
55
56
if err != nil {
@@ -71,7 +72,7 @@ func TestEvalWorkspacePathInternalData(t *testing.T) {
71
72
& LanguageServerOptions {LogWriter : logger , LogLevel : log .LevelDebug },
72
73
)
73
74
74
- res , err := ls .EvalWorkspacePath (context .TODO (), "object.keys(data.internal)" , strings . NewReader ( "{}" ) )
75
+ res , err := ls .EvalWorkspacePath (context .TODO (), "object.keys(data.internal)" , map [ string ] any {} )
75
76
if err != nil {
76
77
t .Fatal (err )
77
78
}
@@ -104,35 +105,50 @@ func TestEvalWorkspacePathInternalData(t *testing.T) {
104
105
func TestFindInput (t * testing.T ) {
105
106
t .Parallel ()
106
107
107
- tmpDir := t .TempDir ()
108
+ cases := []struct {
109
+ fileType string
110
+ fileContent string
111
+ }{
112
+ {"json" , `{"x": true}` },
113
+ {"yaml" , "x: true" },
114
+ }
108
115
109
- workspacePath := tmpDir + "/workspace"
110
- file := tmpDir + "/workspace/foo/bar/baz.rego"
116
+ for _ , tc := range cases {
117
+ t .Run (tc .fileType , func (t * testing.T ) {
118
+ t .Parallel ()
111
119
112
- if err := os .MkdirAll (workspacePath + "/foo/bar" , 0o755 ); err != nil {
113
- t .Fatal (err )
114
- }
120
+ tmpDir := t .TempDir ()
115
121
116
- if readInputString (t , file , workspacePath ) != "" {
117
- t .Fatalf ("did not expect to find input.json" )
118
- }
122
+ workspacePath := tmpDir + "/workspace"
123
+ file := tmpDir + "/workspace/foo/bar/baz.rego"
119
124
120
- content := `{"x": 1}`
125
+ if err := os .MkdirAll (workspacePath + "/foo/bar" , 0o755 ); err != nil {
126
+ t .Fatal (err )
127
+ }
121
128
122
- createWithContent (t , tmpDir + "/workspace/foo/bar/input.json" , content )
129
+ path , content := rio .FindInput (file , workspacePath )
130
+ if path != "" || content != nil {
131
+ t .Fatalf ("did not expect to find input.%s" , tc .fileType )
132
+ }
123
133
124
- if res := readInputString (t , file , workspacePath ); res != content {
125
- t .Errorf ("expected input at %s, got %s" , content , res )
126
- }
134
+ createWithContent (t , tmpDir + "/workspace/foo/bar/input." + tc .fileType , tc .fileContent )
127
135
128
- if err := os .Remove (tmpDir + "/workspace/foo/bar/input.json" ); err != nil {
129
- t .Fatal (err )
130
- }
136
+ path , content = rio .FindInput (file , workspacePath )
137
+ if path != workspacePath + "/foo/bar/input." + tc .fileType || ! maps .Equal (content , map [string ]any {"x" : true }) {
138
+ t .Errorf (`expected input {"x": true} at, got %s` , content )
139
+ }
140
+
141
+ if err := os .Remove (tmpDir + "/workspace/foo/bar/input." + tc .fileType ); err != nil {
142
+ t .Fatal (err )
143
+ }
131
144
132
- createWithContent (t , tmpDir + "/workspace/input.json" , content )
145
+ createWithContent (t , tmpDir + "/workspace/input." + tc . fileType , tc . fileContent )
133
146
134
- if res := readInputString (t , file , workspacePath ); res != content {
135
- t .Errorf ("expected input at %s, got %s" , content , res )
147
+ path , content = rio .FindInput (file , workspacePath )
148
+ if path != workspacePath + "/input." + tc .fileType || ! maps .Equal (content , map [string ]any {"x" : true }) {
149
+ t .Errorf (`expected input {"x": true} at, got %s` , content )
150
+ }
151
+ })
136
152
}
137
153
}
138
154
@@ -150,24 +166,3 @@ func createWithContent(t *testing.T, path string, content string) {
150
166
t .Fatal (err )
151
167
}
152
168
}
153
-
154
- func readInputString (t * testing.T , file , workspacePath string ) string {
155
- t .Helper ()
156
-
157
- _ , input := rio .FindInput (file , workspacePath )
158
-
159
- if input == nil {
160
- return ""
161
- }
162
-
163
- bs , err := io .ReadAll (input )
164
- if err != nil {
165
- t .Fatal (err )
166
- }
167
-
168
- if bs == nil {
169
- return ""
170
- }
171
-
172
- return string (bs )
173
- }
0 commit comments