1
+ package scala .test .toolchain
2
+
3
+ import build .bazel .tests .integration .BazelBaseTestCase
4
+ import org .specs2 .mutable .{Before , SpecificationWithJUnit }
5
+ import org .specs2 .specification .{BeforeAll , Scope }
6
+ import scala .collection .JavaConverters ._
7
+
8
+ // noinspection TypeAnnotation
9
+ class ScalaToolchainTest extends SpecificationWithJUnit with BeforeAll {
10
+
11
+ trait ctx extends Scope with Before {
12
+ val bazelDriver = new BazelBaseTestCase {
13
+ def writeFile (path : String , content : String ) = scratchFile(path, content)
14
+
15
+ def runBazel (args : String * ) = bazel(args.asJava)
16
+
17
+ def writeWorkspaceFile (workspaceName : String , repositories : List [String ], toolchains : List [String ]) = {
18
+ val toolchainList = toolchains.map(toolchain => s """ " $toolchain" """ ).mkString(" ,\n " )
19
+ // TODO: how to load current code as "local repository" inside the test?? what is the path
20
+ val localRepositories = repositories.map(repoName =>
21
+ s """
22
+ |local_repository(
23
+ | name = " $repoName",
24
+ | path = "./external/ $repoName"
25
+ |)
26
+ """ .stripMargin).mkString(" \n " )
27
+ scratchFile(" ./WORKSPACE" , s " workspace(name = ' $workspaceName') " ,
28
+ s """
29
+ |register_toolchains(
30
+ | $toolchainList
31
+ |)
32
+ |
33
+ | $localRepositories
34
+ |
35
+ |load("@io_bazel_rules_scala//scala:scala.bzl", "scala_repositories")
36
+ |scala_repositories()
37
+ """ .stripMargin)
38
+ }
39
+ }
40
+
41
+ override def before = {bazelDriver.setUp()}
42
+ }
43
+
44
+ " scala_library" should {
45
+ " test that build passes when loading default toolchain" in new ctx {
46
+ bazelDriver.writeFile(" HelloWorld.scala" ,
47
+ """ package test_expect_failure.scalacopts_from_toolchain
48
+ |
49
+ |class HelloWorld(name:String){
50
+ | def talk():String = {
51
+ | val notUsed = "No one uses me!..."
52
+ | s"hello $name"
53
+ | }
54
+ |}""" .stripMargin)
55
+ bazelDriver.writeFile(" BUILD" ,
56
+ """ load("@io_bazel_rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
57
+ |load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")
58
+ |
59
+ |scala_toolchain(
60
+ | name = "failing_scala_toolchain",
61
+ | scalacopts = ["-Ywarn-unused","-Xfatal-warnings"]
62
+ |)
63
+ |
64
+ |scala_library(
65
+ | name = "hello_world",
66
+ | srcs = ["HelloWorld.scala"],
67
+ |)
68
+ |
69
+ """ .
70
+ stripMargin)
71
+ bazelDriver.writeWorkspaceFile(
72
+ workspaceName = " rules_scala_toolchain_test" ,
73
+ repositories = List (" io_bazel_rules_scala" ),
74
+ toolchains = List (" @io_bazel_rules_scala//scala:scala_toolchain" ))
75
+ val cmd = bazelDriver.runBazel(" build" , " //:hello_world" )
76
+ val exitCode : Int = cmd.run()
77
+ val stderr = cmd.getErrorLines.asScala.mkString(" \n " )
78
+ println(stderr)
79
+ val stdout = cmd.getOutputLines.asScala.mkString(" \n " )
80
+ println(stdout)
81
+ exitCode ==== 0
82
+ }
83
+ }
84
+
85
+ override def beforeAll (): Unit = BazelBaseTestCase .setUpClass()
86
+ }
0 commit comments