@@ -78,6 +78,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
78
78
attempts : Int = if (TestUtil .isCI) 3 else 1 ,
79
79
pauseDuration : FiniteDuration = 5 .seconds,
80
80
bspOptions : List [String ] = List .empty,
81
+ bspEnvs : Map [String , String ] = Map .empty,
81
82
reuseRoot : Option [os.Path ] = None ,
82
83
stdErrOpt : Option [os.RelPath ] = None ,
83
84
extraOptionsOverride : Seq [String ] = extraOptions
@@ -96,7 +97,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
96
97
val stderr : os.ProcessOutput = stdErrPathOpt.getOrElse(os.Inherit )
97
98
98
99
val proc = os.proc(TestUtil .cli, " bsp" , bspOptions ++ extraOptionsOverride, args)
99
- .spawn(cwd = root, stderr = stderr)
100
+ .spawn(cwd = root, stderr = stderr, env = bspEnvs )
100
101
var remoteServer : b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer =
101
102
null
102
103
@@ -2114,6 +2115,190 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
2114
2115
}
2115
2116
}
2116
2117
2118
+ for {
2119
+ setPowerByLauncherOpt <- Seq (true , false )
2120
+ setPowerBySubCommandOpt <- Seq (true , false )
2121
+ setPowerByEnv <- Seq (true , false )
2122
+ setPowerByConfig <- Seq (true , false )
2123
+ powerIsSet =
2124
+ setPowerByLauncherOpt || setPowerBySubCommandOpt || setPowerByEnv || setPowerByConfig
2125
+ powerSettingDescription = {
2126
+ val launcherSetting = if (setPowerByLauncherOpt) " launcher option" else " "
2127
+ val subCommandSetting = if (setPowerBySubCommandOpt) " setup-ide option" else " "
2128
+ val envSetting = if (setPowerByEnv) " environment variable" else " "
2129
+ val configSetting = if (setPowerByConfig) " config" else " "
2130
+ List (launcherSetting, subCommandSetting, envSetting, configSetting)
2131
+ .filter(_.nonEmpty)
2132
+ .mkString(" , " )
2133
+ }
2134
+ testDescription =
2135
+ if (powerIsSet)
2136
+ s " BSP respects --power mode set by $powerSettingDescription (example: using python directive) "
2137
+ else
2138
+ " BSP fails when --power mode is not set for experimental directives (example: using python directive)"
2139
+ } test(testDescription) {
2140
+ val scriptName = " requires-power.sc"
2141
+ val inputs = TestInputs (os.rel / scriptName ->
2142
+ s """ //> using python
2143
+ |println("scalapy is experimental") """ .stripMargin)
2144
+ inputs.fromRoot { root =>
2145
+ val configFile = os.rel / " config" / " config.json"
2146
+ val configEnvs = Map (" SCALA_CLI_CONFIG" -> configFile.toString())
2147
+ val setupIdeEnvs : Map [String , String ] =
2148
+ if (setPowerByEnv) Map (" SCALA_CLI_POWER" -> " true" ) ++ configEnvs
2149
+ else configEnvs
2150
+ val launcherOpts =
2151
+ if (setPowerByLauncherOpt) List (" --power" )
2152
+ else List .empty
2153
+ val subCommandOpts =
2154
+ if (setPowerBySubCommandOpt) List (" --power" )
2155
+ else List .empty
2156
+ val args = launcherOpts ++ List (" setup-ide" , scriptName) ++ subCommandOpts
2157
+ os.proc(TestUtil .cli, args).call(cwd = root, env = setupIdeEnvs)
2158
+ if (setPowerByConfig)
2159
+ os.proc(TestUtil .cli, " config" , " power" , " true" )
2160
+ .call(cwd = root, env = configEnvs)
2161
+ val ideOptionsPath = root / Constants .workspaceDirName / " ide-options-v2.json"
2162
+ expect(ideOptionsPath.toNIO.toFile.exists())
2163
+ val ideLauncherOptsPath = root / Constants .workspaceDirName / " ide-launcher-options.json"
2164
+ expect(ideLauncherOptsPath.toNIO.toFile.exists())
2165
+ val ideEnvsPath = root / Constants .workspaceDirName / " ide-envs.json"
2166
+ expect(ideEnvsPath.toNIO.toFile.exists())
2167
+ val jsonOptions = List (
2168
+ " --json-options" ,
2169
+ ideOptionsPath.toString,
2170
+ " --json-launcher-options" ,
2171
+ ideLauncherOptsPath.toString,
2172
+ " --envs-file" ,
2173
+ ideEnvsPath.toString
2174
+ )
2175
+ withBsp(
2176
+ inputs,
2177
+ Seq (" ." ),
2178
+ bspOptions = jsonOptions,
2179
+ bspEnvs = configEnvs,
2180
+ reuseRoot = Some (root)
2181
+ ) {
2182
+ (_, _, remoteServer) =>
2183
+ async {
2184
+ val targets = await(remoteServer.workspaceBuildTargets().asScala)
2185
+ .getTargets.asScala
2186
+ .filter(! _.getId.getUri.contains(" -test" ))
2187
+ .map(_.getId())
2188
+ val compileResult =
2189
+ await(remoteServer.buildTargetCompile(new b.CompileParams (targets.asJava)).asScala)
2190
+ if (powerIsSet) {
2191
+ expect(compileResult.getStatusCode == b.StatusCode .OK )
2192
+ val runResult =
2193
+ await(remoteServer.buildTargetRun(new b.RunParams (targets.head)).asScala)
2194
+ expect(runResult.getStatusCode == b.StatusCode .OK )
2195
+ }
2196
+ else
2197
+ expect(compileResult.getStatusCode == b.StatusCode .ERROR )
2198
+ }
2199
+ }
2200
+ }
2201
+ }
2202
+
2203
+ test(" BSP reloads --power mode after setting it via env passed to setup-ide" ) {
2204
+ val scriptName = " requires-power.sc"
2205
+ val inputs = TestInputs (os.rel / scriptName ->
2206
+ s """ //> using python
2207
+ |println("scalapy is experimental") """ .stripMargin)
2208
+ inputs.fromRoot { root =>
2209
+ os.proc(TestUtil .cli, " setup-ide" , scriptName, extraOptions).call(cwd = root)
2210
+ val ideEnvsPath = root / Constants .workspaceDirName / " ide-envs.json"
2211
+ expect(ideEnvsPath.toNIO.toFile.exists())
2212
+ val jsonOptions = List (" --envs-file" , ideEnvsPath.toString)
2213
+ withBsp(inputs, Seq (scriptName), bspOptions = jsonOptions, reuseRoot = Some (root)) {
2214
+ (_, _, remoteServer) =>
2215
+ async {
2216
+ val targets = await(remoteServer.workspaceBuildTargets().asScala)
2217
+ .getTargets.asScala
2218
+ .filter(! _.getId.getUri.contains(" -test" ))
2219
+ .map(_.getId())
2220
+
2221
+ // compilation should fail before reload, as --power mode is off
2222
+ val compileBeforeReloadResult =
2223
+ await(remoteServer.buildTargetCompile(new b.CompileParams (targets.asJava)).asScala)
2224
+ expect(compileBeforeReloadResult.getStatusCode == b.StatusCode .ERROR )
2225
+
2226
+ // enable --power mode via env for setup-ide
2227
+ os.proc(TestUtil .cli, " setup-ide" , scriptName, extraOptions)
2228
+ .call(cwd = root, env = Map (" SCALA_CLI_POWER" -> " true" ))
2229
+
2230
+ // compilation should now succeed
2231
+ val reloadResponse =
2232
+ extractWorkspaceReloadResponse(await(remoteServer.workspaceReload().asScala))
2233
+ expect(reloadResponse.isEmpty)
2234
+ val compileAfterReloadResult =
2235
+ await(remoteServer.buildTargetCompile(new b.CompileParams (targets.asJava)).asScala)
2236
+ expect(compileAfterReloadResult.getStatusCode == b.StatusCode .OK )
2237
+
2238
+ // code should also be runnable via BSP now
2239
+ val runResult =
2240
+ await(remoteServer.buildTargetRun(new b.RunParams (targets.head)).asScala)
2241
+ expect(runResult.getStatusCode == b.StatusCode .OK )
2242
+ }
2243
+ }
2244
+ }
2245
+ }
2246
+
2247
+ test(" BSP reloads --power mode after setting it via config" ) {
2248
+ val scriptName = " requires-power.sc"
2249
+ val inputs = TestInputs (os.rel / scriptName ->
2250
+ s """ //> using python
2251
+ |println("scalapy is experimental") """ .stripMargin)
2252
+ inputs.fromRoot { root =>
2253
+ val configFile = os.rel / " config" / " config.json"
2254
+ val configEnvs = Map (" SCALA_CLI_CONFIG" -> configFile.toString())
2255
+ os.proc(TestUtil .cli, " setup-ide" , scriptName, extraOptions).call(
2256
+ cwd = root,
2257
+ env = configEnvs
2258
+ )
2259
+ val ideEnvsPath = root / Constants .workspaceDirName / " ide-envs.json"
2260
+ expect(ideEnvsPath.toNIO.toFile.exists())
2261
+ val jsonOptions = List (" --envs-file" , ideEnvsPath.toString)
2262
+ withBsp(
2263
+ inputs,
2264
+ Seq (scriptName),
2265
+ bspOptions = jsonOptions,
2266
+ bspEnvs = configEnvs,
2267
+ reuseRoot = Some (root)
2268
+ ) {
2269
+ (_, _, remoteServer) =>
2270
+ async {
2271
+ val targets = await(remoteServer.workspaceBuildTargets().asScala)
2272
+ .getTargets.asScala
2273
+ .filter(! _.getId.getUri.contains(" -test" ))
2274
+ .map(_.getId())
2275
+
2276
+ // compilation should fail before reload, as --power mode is off
2277
+ val compileBeforeReloadResult =
2278
+ await(remoteServer.buildTargetCompile(new b.CompileParams (targets.asJava)).asScala)
2279
+ expect(compileBeforeReloadResult.getStatusCode == b.StatusCode .ERROR )
2280
+
2281
+ // enable --power mode via config
2282
+ os.proc(TestUtil .cli, " config" , " power" , " true" )
2283
+ .call(cwd = root, env = configEnvs)
2284
+
2285
+ // compilation should now succeed
2286
+ val reloadResponse =
2287
+ extractWorkspaceReloadResponse(await(remoteServer.workspaceReload().asScala))
2288
+ expect(reloadResponse.isEmpty)
2289
+ val compileAfterReloadResult =
2290
+ await(remoteServer.buildTargetCompile(new b.CompileParams (targets.asJava)).asScala)
2291
+ expect(compileAfterReloadResult.getStatusCode == b.StatusCode .OK )
2292
+
2293
+ // code should also be runnable via BSP now
2294
+ val runResult =
2295
+ await(remoteServer.buildTargetRun(new b.RunParams (targets.head)).asScala)
2296
+ expect(runResult.getStatusCode == b.StatusCode .OK )
2297
+ }
2298
+ }
2299
+ }
2300
+ }
2301
+
2117
2302
private def checkIfBloopProjectIsInitialised (
2118
2303
root : os.Path ,
2119
2304
buildTargetsResp : b.WorkspaceBuildTargetsResult
0 commit comments