summaryrefslogtreecommitdiff
path: root/contrib/scalapblib/test/src/TutorialTests.scala
blob: 35ad69e01f736b434c69d9d96af4c95ac28c5a2a (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
package mill.contrib.scalapblib

import mill.api.Result
import mill.util.{TestEvaluator, TestUtil}
import utest.framework.TestPath
import utest.{TestSuite, Tests, assert, _}

object TutorialTests extends TestSuite {

  trait TutorialBase extends TestUtil.BaseModule {
    override def millSourcePath: os.Path = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
  }

  trait TutorialModule extends ScalaPBModule {
    def scalaVersion = "2.12.4"
    def scalaPBVersion = "0.7.4"
    def scalaPBFlatPackage = true
  }

  object Tutorial extends TutorialBase {

    object core extends TutorialModule {
      override def scalaPBVersion = "0.7.4"
    }
  }

  object TutorialWithProtoc extends TutorialBase {
    object core extends TutorialModule {
      override def scalaPBProtocPath = Some("/dev/null")
    }
  }

  val resourcePath: os.Path = os.pwd / 'contrib / 'scalapblib / 'test / 'protobuf / 'tutorial

  def protobufOutPath(eval: TestEvaluator): os.Path =
    eval.outPath / 'core / 'compileScalaPB / 'dest / 'com / 'example / 'tutorial

  def workspaceTest[T](m: TestUtil.BaseModule)(t: TestEvaluator => T)
                      (implicit tp: TestPath): T = {
    val eval = new TestEvaluator(m)
    os.remove.all(m.millSourcePath)
    println(m.millSourcePath)
    os.remove.all(eval.outPath)
    println(eval.outPath)
    os.makeDir.all(m.millSourcePath / 'core / 'protobuf)
    os.copy(resourcePath, m.millSourcePath / 'core / 'protobuf / 'tutorial)
    t(eval)
  }

  def compiledSourcefiles: Seq[os.RelPath] = Seq[os.RelPath](
    "AddressBook.scala",
    "Person.scala",
    "TutorialProto.scala"
  )

  def tests: Tests = Tests {
    'scalapbVersion - {

      'fromBuild - workspaceTest(Tutorial) { eval =>
        val Right((result, evalCount)) = eval.apply(Tutorial.core.scalaPBVersion)

        assert(
          result == "0.7.4",
          evalCount > 0
        )
      }
    }

    'compileScalaPB - {
      'calledDirectly - workspaceTest(Tutorial) { eval =>
        val Right((result, evalCount)) = eval.apply(Tutorial.core.compileScalaPB)

        val outPath = protobufOutPath(eval)

        val outputFiles = os.walk(result.path).filter(os.isFile)

        val expectedSourcefiles = compiledSourcefiles.map(outPath / _)

        assert(
          result.path == eval.outPath / 'core / 'compileScalaPB / 'dest,
          outputFiles.nonEmpty,
          outputFiles.forall(expectedSourcefiles.contains),
          outputFiles.size == 3,
          evalCount > 0
        )

        // don't recompile if nothing changed
        val Right((_, unchangedEvalCount)) = eval.apply(Tutorial.core.compileScalaPB)

        assert(unchangedEvalCount == 0)
      }

      // This throws a NullPointerException in coursier somewhere
      //
      // 'triggeredByScalaCompile - workspaceTest(Tutorial) { eval =>
      //   val Right((_, evalCount)) = eval.apply(Tutorial.core.compile)

      //   val outPath = protobufOutPath(eval)

      //   val outputFiles = os.walk(outPath).filter(_.isFile)

      //   val expectedSourcefiles = compiledSourcefiles.map(outPath / _)

      //   assert(
      //     outputFiles.nonEmpty,
      //     outputFiles.forall(expectedSourcefiles.contains),
      //     outputFiles.size == 3,
      //     evalCount > 0
      //   )

      //   // don't recompile if nothing changed
      //   val Right((_, unchangedEvalCount)) = eval.apply(Tutorial.core.compile)

      //   assert(unchangedEvalCount == 0)
      // }
    }

    'useExternalProtocCompiler - {
      /* This ensure that the `scalaPBProtocPath` is properly used.
       * As the given path is incorrect, the compilation should fail.
       */
      'calledWithWrongProtocFile - workspaceTest(TutorialWithProtoc) { eval =>
        val result = eval.apply(TutorialWithProtoc.core.compileScalaPB)
        assert(result.isLeft)
      }
    }
  }
}