summaryrefslogtreecommitdiff
path: root/contrib/buildinfo/test/src/mill/contrib/BuildInfoTests.scala
blob: 9e1549de51d31929831acc8ec01ba671f08ed24f (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
package mill.contrib

import ammonite.ops._
import java.util.jar.JarFile
import mill._
import mill.define.Target
import mill.eval.Result._
import mill.eval.{Evaluator, Result}
import mill.modules.Assembly
import mill.scalalib.publish.VersionControl
import mill.scalalib.publish._
import mill.util.{TestEvaluator, TestUtil}
import scala.collection.JavaConverters._
import utest._
import utest.framework.TestPath


object BuildInfoTests extends TestSuite {

  val scalaVersionString = "2.12.4"
  trait BuildInfoModule extends TestUtil.BaseModule with scalalib.ScalaModule with BuildInfo {
    def millSourcePath =  TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
    def scalaVersion = scalaVersionString
  }

  object EmptyBuildInfo extends BuildInfoModule

  object BuildInfo extends BuildInfoModule {
    def buildInfoMembers=T{
      Map(
        "scalaVersion" -> scalaVersion(),
      )
    }
  }

  object BuildInfoSettings extends BuildInfoModule {
    def buildInfoPackageName = Some("foo")
    def buildInfoObjectName = "bar"
    def buildInfoMembers=T{
      Map(
        "scalaVersion" -> scalaVersion()
      )
    }
  }

  val resourcePath = pwd / 'contrib / 'buildinfo / 'test / 'resources / "buildinfo"

  def workspaceTest[T, M <: TestUtil.BaseModule](m: M, resourcePath: Path = resourcePath)
                                                (t: TestEvaluator[M] => T)
                                                (implicit tp: TestPath): T = {
    val eval = new TestEvaluator(m)
    rm(m.millSourcePath)
    rm(eval.outPath)
    mkdir(m.millSourcePath / up)
    cp(resourcePath, m.millSourcePath)
    t(eval)
  }

  def tests: Tests = Tests {

    'buildinfo - {
      'createSourcefile - workspaceTest(BuildInfo){ eval =>
        val expected = 
          s"""|
              |object BuildInfo {
              |  def scalaVersion = "2.12.4"
              |}""".stripMargin
        val Right((result, evalCount)) = eval.apply(BuildInfo.buildInfo)
        assert(
          result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" &&
            exists(result.head.path) &&
            read! result.head.path == expected
        )
      }

      'notCreateEmptySourcefile - workspaceTest(EmptyBuildInfo){ eval =>
        val Right((result, evalCount)) = eval.apply(EmptyBuildInfo.buildInfo)
        assert(
          result.isEmpty &&
            !exists(eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala")
        )
      }

      'supportCustomSettings - workspaceTest(BuildInfoSettings){ eval =>
        val expected = 
          s"""|package foo
              |object bar {
              |  def scalaVersion = "2.12.4"
              |}""".stripMargin
        val Right((result, evalCount)) = eval.apply(BuildInfoSettings.buildInfo)
        assert(
          result.head.path == eval.outPath / 'buildInfo / 'dest / "BuildInfo.scala" &&
            exists(result.head.path) &&
            read! result.head.path == expected
        )
      }

      'compile - workspaceTest(BuildInfo){ eval =>
        val Right((result, evalCount)) = eval.apply(BuildInfo.compile)
        assert(true)
      }

      'run - workspaceTest(BuildInfo){ eval =>
        val runResult = eval.outPath / "hello-mill"
        val Right((result, evalCount)) = eval.apply(BuildInfo.run(runResult.toString))
        assert(
          exists(runResult),
          read(runResult) == scalaVersionString)
      }
    }
  }
}