aboutsummaryrefslogtreecommitdiff
path: root/test/test.scala
blob: dfc35d019c14023264a8cc29898dc6669c6f7bcb (plain) (blame)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import cbt._
import java.util.concurrent.ConcurrentHashMap
import java.io.File
import java.nio.file._
import java.net.URL

// micro framework
object Main{
  def main(_args: Array[String]): Unit = {
    val start = System.currentTimeMillis
    val args = new Stage1ArgsParser(_args.toVector)
    implicit val logger: Logger = new Logger(args.enabledLoggers, System.currentTimeMillis)
    val lib = new Lib(logger)
    val cbtHome = new File(System.getenv("CBT_HOME"))
    
    var successes = 0
    var failures = 0
    def assertException[T:scala.reflect.ClassTag](msg: String = "")(code: => Unit)(implicit logger: Logger) = {
      try{ 
        code
        assert(false, msg)
      }catch{ case _:AssertionError => }
    }
    def assert(condition: Boolean, msg: String = "")(implicit logger: Logger) = {
      scala.util.Try{
        Predef.assert(condition, "["++msg++"]")
      }.map{ _ =>
        print(".")
        successes += 1
      }.recover{
        case e: AssertionError =>
          println("FAILED")
          e.printStackTrace
          failures += 1
      }.get
    }

    def runCbt(path: String, _args: Seq[String])(implicit logger: Logger): Result = {
      import java.io._
      val allArgs: Seq[String] = ((cbtHome.string ++ "/cbt") +: "direct" +: (_args ++ args.propsRaw))
      logger.test(allArgs.toString)
      val pb = new ProcessBuilder( allArgs :_* )
      pb.directory(cbtHome ++ ("/test/" ++ path))
      val p = pb.start
      val berr = new BufferedReader(new InputStreamReader(p.getErrorStream));
      val bout = new BufferedReader(new InputStreamReader(p.getInputStream));
      import collection.JavaConversions._
      val err = Stream.continually(berr.readLine()).takeWhile(_ != null).mkString("\n")
      val out = Stream.continually(bout.readLine()).takeWhile(_ != null).mkString("\n")
      p.waitFor
      Result(p.exitValue == 0, out, err)
    }
    case class Result(exit0: Boolean, out: String, err: String)
    def assertSuccess(res: Result, msg: => String)(implicit logger: Logger) = {
      assert(res.exit0, msg ++ res.toString)
    }

    // tests
    def usage(path: String)(implicit logger: Logger) = {
      val usageString = "Methods provided by CBT"
      val res = runCbt(path, Seq())
      logger.test(res.toString)
      val debugToken = "usage " ++ path ++ " "
      assertSuccess(res,debugToken)
      assert(res.out == "", debugToken ++ res.toString)
      assert(res.err contains usageString, debugToken ++ res.toString)
    }
    def compile(path: String)(implicit logger: Logger) = task("compile", path)
    def task(name: String, path: String)(implicit logger: Logger) = {
      val res = runCbt(path, Seq(name))
      val debugToken = name ++ " " ++ path ++ " "
      assertSuccess(res,debugToken)
      // assert(res.err == "", res.err) // FIXME: enable this
    }

    def clean(path: String)(implicit logger: Logger) = {
      val res = runCbt(path, Seq("clean", "dry-run", "force"))
      val debugToken = "\n"++lib.red("Deleting") ++ " " ++ (cbtHome ++("/test/"++path++"/target")).toPath.toAbsolutePath.toString++"\n"
      val debugToken2 = "\n"++lib.red("Deleting") ++ " " ++ (cbtHome ++("/test/"++path)).toPath.toAbsolutePath.toString++"\n"
      assertSuccess(res,debugToken)
      assert(res.out == "", debugToken ++ " " ++ res.toString)
      assert(res.err.contains(debugToken), debugToken ++ " " ++ res.toString)
      assert(
        !res.err.contains(debugToken2),
        "Tried to delete too much: " ++ debugToken2 ++ " " ++ res.toString
      )
      res.err.split("\n").filter(_.startsWith(lib.red("Deleting"))).foreach{ line =>
        assert(
          line.size >= debugToken2.trim.size,
          "Tried to delete too much: " ++ line ++"   debugToken2: " ++ debugToken2
        )
      }
    }

    logger.test( "Running tests " ++ _args.toList.toString )

    val cache = cbtHome ++ "/cache"
    val mavenCache = cache ++ "/maven"
    val cbtHasChanged = true
    def Resolver(urls: URL*) = MavenResolver(cbtHasChanged, mavenCache, urls: _*)

    {
      val noContext = ContextImplementation(
        cbtHome ++ "/test/nothing",
        cbtHome,
        Array(),
        Array(),
        start,
        cbtHasChanged,
        null,
        null,
        new ConcurrentHashMap[String,AnyRef],
        new ConcurrentHashMap[AnyRef,ClassLoader],
        cache,
        cbtHome,
        cbtHome,
        cbtHome ++ "/compatibilityTarget",
        null
      )

      val b = new BasicBuild(noContext){
        override def dependencies =
          Resolver(mavenCentral).bind(
            MavenDependency("net.incongru.watchservice","barbary-watchservice","1.0"),
            MavenDependency("net.incongru.watchservice","barbary-watchservice","1.0")
          )
      }
      val cp = b.classpath
      assert(cp.strings.distinct == cp.strings, "duplicates in classpath: " ++ cp.string)
    }

    // test that messed up artifacts crash with an assertion (which should tell the user what's up)
    assertException[AssertionError](){
      Resolver(mavenCentral).bindOne( MavenDependency("com.jcraft", "jsch", " 0.1.53") ).classpath
    }
    assertException[AssertionError](){
      Resolver(mavenCentral).bindOne( MavenDependency("com.jcraft", null, "0.1.53") ).classpath
    }
    assertException[AssertionError](){
      Resolver(mavenCentral).bindOne( MavenDependency("com.jcraft", "", " 0.1.53") ).classpath
    }
    assertException[AssertionError](){
      Resolver(mavenCentral).bindOne( MavenDependency("com.jcraft%", "jsch", " 0.1.53") ).classpath
    }
    assertException[AssertionError](){
      Resolver(mavenCentral).bindOne( MavenDependency("", "jsch", " 0.1.53") ).classpath
    }

    (
      (
        if(System.getenv("CIRCLECI") == null){
          // tenporarily disable on circleci as it seems to have trouble reliably
          // downloading from bintray
          Dependencies(
            Resolver( bintray("tpolecat") ).bind(
              lib.ScalaDependency("org.tpolecat","tut-core","0.4.2", scalaMajorVersion="2.11")
            )
          ).classpath.strings
        } else Nil
      ) ++
     Dependencies(
      Resolver( sonatypeReleases ).bind(
        MavenDependency("org.cvogt","scala-extensions_2.11","0.5.1")
      )
    ).classpath.strings
      ++
      Dependencies(
        Resolver( mavenCentral ).bind(
          MavenDependency("ai.x","lens_2.11","1.0.0")
        )
      ).classpath.strings
    ).foreach{
      path => assert(new File(path).exists, path)
    }

    usage("nothing")
    compile("nothing")
    //clean("nothing")
    usage("multi-build")
    compile("multi-build")
    clean("multi-build")
    usage("simple")
    compile("simple")
    clean("simple")
    usage("simple-fixed")
    compile("simple-fixed")
    
    compile("../plugins/sbt_layout")
    compile("../plugins/scalafmt")
    compile("../plugins/scalajs")
    compile("../plugins/scalariform")
    compile("../plugins/scalatest")
    compile("../plugins/wartremover")
    compile("../plugins/uber-jar")
    compile("../examples/scalafmt-example")
    compile("../examples/scalariform-example")
    compile("../examples/scalatest-example")
    compile("../examples/scalajs-react-example/js")
    compile("../examples/scalajs-react-example/jvm")
    compile("../examples/multi-project-example")
    if(sys.props("java.version").startsWith("1.7")){
      System.err.println("\nskipping dotty tests on Java 7")
    } else {
      compile("../examples/dotty-example")
      task("run","../examples/dotty-example")
      task("doc","../examples/dotty-example")
    }
    task("fastOptJS","../examples/scalajs-react-example/js")
    task("fullOptJS","../examples/scalajs-react-example/js")
    compile("../examples/uber-jar-example")
    
    {
      val res = runCbt("simple", Seq("printArgs","1","2","3"))
      assert(res.exit0)
      assert(res.out == "1 2 3", res.out)
    }

    {
      val res = runCbt("forgot-extend", Seq("run"))
      assert(!res.exit0)
      assert(res.err contains "Build cannot be cast to cbt.BuildInterface", res.err)
    }

    {
      val res = runCbt("no-build-file", Seq("run"))
      assert(!res.exit0)
      assert(res.err contains "No file build.scala (lower case) found in", res.err)
    }

    {
      val res = runCbt("empty-build-file", Seq("run"))
      assert(!res.exit0)
      assert(res.err contains "You need to define a class Build in build.scala in", res.err)
    }

    {
      val res = runCbt("../examples/wartremover-example", Seq("compile"))
      assert(!res.exit0)
      assert(res.err.contains("var is disabled"), res.out)
      assert(res.err.contains("null is disabled"), res.out)
    }

    System.err.println(" DONE!")
    System.err.println( successes.toString ++ " succeeded, "++ failures.toString ++ " failed" )
    if(failures > 0) System.exit(1) else System.exit(0)
  }
}