-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDiagnosticProviderTests.scala
189 lines (168 loc) · 5.1 KB
/
DiagnosticProviderTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package playground.language
import alloy.SimpleRestJson
import aws.protocols.AwsJson1_0
import aws.protocols.AwsJson1_1
import aws.protocols.AwsQuery
import aws.protocols.Ec2Query
import aws.protocols.RestJson1
import aws.protocols.RestXml
import cats.data.NonEmptyList
import cats.effect.IO
import cats.effect.kernel.Resource
import demo.smithy.DemoServiceGen
import noop.NoRunnerServiceGen
import org.http4s.HttpApp
import org.http4s.client.Client
import playground.Assertions.*
import playground.CompilationError
import playground.CompilationErrorDetails
import playground.CompilationFailed
import playground.DiagnosticSeverity
import playground.Diffs.given
import playground.FileCompiler
import playground.FileRunner
import playground.OperationCompiler
import playground.OperationRunner
import playground.PreludeCompiler
import playground.ServiceIndex
import playground.ServiceUtils.*
import playground.smithyql.QualifiedIdentifier
import playground.smithyql.SourceRange
import playground.smithyql.StringRangeUtils.*
import playground.std.ClockGen
import playground.std.RandomGen
import playground.std.Stdlib
import playground.std.StdlibRuntime
import smithy4s.HasId
import smithy4s.aws.AwsEnvironment
import weaver.*
object DiagnosticProviderTests extends SimpleIOSuite {
private val client = Client.fromHttpApp(HttpApp.notFound[IO])
private implicit val stdlib: StdlibRuntime[IO] = StdlibRuntime.instance[IO]
private val services = List(
wrapService(DemoServiceGen),
wrapService(RandomGen),
wrapService(ClockGen),
wrapService(NoRunnerServiceGen),
)
private val knownServiceIds = NonEmptyList.of(
QualifiedIdentifier.of("demo", "smithy", "DemoService"),
QualifiedIdentifier.of("playground", "std", "Random"),
QualifiedIdentifier.of("playground", "std", "Clock"),
QualifiedIdentifier.of("noop", "NoRunnerService"),
)
// note: this is plugin-aware, hence we're not defining it in any main files
private val knownProtocols = NonEmptyList
.of[HasId](
SimpleRestJson,
AwsJson1_0,
AwsJson1_1,
RestJson1,
AwsQuery,
RestXml,
Ec2Query,
Stdlib,
)
.map(_.id)
private val provider = DiagnosticProvider.instance(
compiler = FileCompiler
.instance(
PreludeCompiler.instance[CompilationError.InIorNel](ServiceIndex.fromServices(services)),
OperationCompiler.fromServices(services),
)
.mapK(CompilationFailed.wrapK),
fileRunner = FileRunner.instance(
OperationRunner.merge(
OperationRunner.forServices[IO](
services = services,
getSchema = _ => None,
client = client,
baseUri = IO.stub,
awsEnv = Resource.eval(IO.stub: IO[AwsEnvironment[IO]]),
plugins = Nil,
),
ServiceIndex.fromServices(services),
)
),
)
pureTest("empty file - no diagnostics") {
assertNoDiff(provider.getDiagnostics(""), Nil)
}
pureTest("file doesn't parse at all") {
val input = "horrendous <parsing mistake>"
assertNoDiff(
provider.getDiagnostics(input),
List(
CompilationError(
err = CompilationErrorDetails.ParseError(expectationString = "{"),
range = SourceRange.empty(input.positionOf("<")),
severity = DiagnosticSeverity.Error,
tags = Set(),
)
),
)
}
pureTest("file doesn't compile") {
val input = "AnyOp {}"
assertNoDiff(
provider.getDiagnostics(input),
List(
CompilationError(
err = CompilationErrorDetails.AmbiguousService(
workspaceServices = knownServiceIds.toList
),
range = input.rangeOf("AnyOp"),
severity = DiagnosticSeverity.Error,
tags = Set(),
)
),
)
}
pureTest("file parses with multiple queries, doesn't compile yet") {
val input =
"""op1 {}
|op2 {}""".stripMargin
assertNoDiff(
provider.getDiagnostics(
input
),
List(
CompilationError.error(
err = CompilationErrorDetails.AmbiguousService(
workspaceServices = knownServiceIds.toList
),
range = input.rangeOf("op1"),
),
CompilationError.error(
err = CompilationErrorDetails.AmbiguousService(
workspaceServices = knownServiceIds.toList
),
range = input.rangeOf("op2"),
),
),
)
}
pureTest("file parses and compiles with multiple queries, missing runner for one of them") {
val input =
"""playground.std#Random.NextUUID {}
|noop#NoRunnerService.Noop {}""".stripMargin
assertNoDiff(
provider.getDiagnostics(input),
List(
CompilationError.info(
err = CompilationErrorDetails.UnsupportedProtocols(
knownProtocols,
Nil,
),
range = input.rangeOf("noop#NoRunnerService.Noop"),
)
),
)
}
pureTest("file is correct with multiple queries") {
val input =
"""playground.std#Random.NextUUID {}
|playground.std#Clock.CurrentTimestamp {}""".stripMargin
assertNoDiff(provider.getDiagnostics(input), Nil)
}
}