summaryrefslogtreecommitdiff
path: root/scalalib/test/src/mill/scalalib/publish/PomTests.scala
blob: 307ae379342c34cef3aa07d3b4820f8d3983e7ec (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
package mill.scalalib.publish

import utest._
import mill._

import scala.xml.{NodeSeq, XML}

object PomTests extends TestSuite {

  def tests: Tests = Tests {
    val artifactId = "mill-scalalib_2.12"
    val artifact =
      Artifact("com.lihaoyi", "mill-scalalib_2.12", "0.0.1")
    val deps = Agg(
      Dependency(Artifact("com.lihaoyi", "mill-main_2.12", "0.1.4"),
                 Scope.Compile),
      Dependency(Artifact("org.scala-sbt", "test-interface", "1.0"),
                 Scope.Compile),
      Dependency(Artifact("com.lihaoyi", "pprint_2.12", "0.5.3"),
                 Scope.Compile, exclusions = List("com.lihaoyi" -> "fansi_2.12", "*" -> "sourcecode_2.12"))
    )
    val settings = PomSettings(
      description = "mill-scalalib",
      organization = "com.lihaoyi",
      url = "https://github.com/lihaoyi/mill",
      licenses = Seq(License.`MIT`),
      versionControl = VersionControl.github("lihaoyi", "mill"),
      developers = List(
        Developer("lihaoyi",
                  "Li Haoyi",
                  "https://github.com/lihaoyi",
                  None,
                  None),
        Developer("rockjam",
                  "Nikolai Tatarinov",
                  "https://github.com/rockjam",
                  Some("80pct done Inc."),
                  Some("https://80pctdone.com/"))
      )
    )

    'fullPom - {
      val fullPom = pomXml(artifact, deps, artifactId, settings)

      'topLevel - {
        assert(
          singleText(fullPom \ "modelVersion") == "4.0.0",
          singleText(fullPom \ "name") == artifactId,
          singleText(fullPom \ "groupId") == artifact.group,
          singleText(fullPom \ "artifactId") == artifact.id,
          singleText(fullPom \ "packaging") == "jar",
          singleText(fullPom \ "description") == settings.description,
          singleText(fullPom \ "version") == artifact.version,
          singleText(fullPom \ "url") == settings.url
        )
      }

      'licenses - {
        val licenses = fullPom \ "licenses" \ "license"

        assert(licenses.size == 1)

        val license = licenses.head
        val pomLicense = settings.licenses.head
        assert(
          singleText(license \ "name") == pomLicense.name,
          singleText(license \ "url") == pomLicense.url,
          singleText(license \ "distribution") == pomLicense.distribution
        )
      }

      'scm - {
        val scm = (fullPom \ "scm").head
        val pomScm = settings.versionControl

        assert(
          optText(scm \ "connection") == pomScm.connection,
          optText(scm \ "developerConnection") == pomScm.developerConnection,
          optText(scm \ "tag").isEmpty,
          optText(scm \ "url") == pomScm.browsableRepository
        )
      }

      'developers - {
        val developers = fullPom \ "developers" \ "developer"

        assert(developers.size == 2)

        val pomDevelopers = settings.developers

        assert(
          singleText(developers.head \ "id") == pomDevelopers.head.id,
          singleText(developers.head \ "name") == pomDevelopers.head.name,
          optText(developers.head \ "organization").isEmpty,
          optText(developers.head \ "organizationUrl").isEmpty
        )

        assert(
          singleText(developers.last \ "id") == pomDevelopers.last.id,
          singleText(developers.last \ "name") == pomDevelopers.last.name,
          optText(developers.last \ "organization") == pomDevelopers.last.organization,
          optText(developers.last \ "organizationUrl") == pomDevelopers.last.organizationUrl
        )
      }

      'dependencies - {
        val dependencies = fullPom \ "dependencies" \ "dependency"

        assert(dependencies.size == 3)

        val pomDeps = deps.indexed

        dependencies.zipWithIndex.foreach {
          case (dep, index) =>
            assert(
              singleText(dep \ "groupId") == pomDeps(index).artifact.group,
              singleText(dep \ "artifactId") == pomDeps(index).artifact.id,
              singleText(dep \ "version") == pomDeps(index).artifact.version,
              optText(dep \ "scope").isEmpty,
              (dep \ "exclusions").zipWithIndex.forall { case (node, j) =>
                singleText(node \ "exclusion" \ "groupId") == pomDeps(index).exclusions(j)._1 &&
                  singleText(node \ "exclusion" \ "artifactId") == pomDeps(index).exclusions(j)._2
              }
            )
        }
      }
    }

    'pomEmptyScm - {
      val updatedSettings = settings.copy(
        versionControl = VersionControl(
          browsableRepository = Some("git://github.com/lihaoyi/mill.git"),
          connection = None,
          developerConnection = None,
          tag = None
        ))
      val pomEmptyScm = pomXml(artifact, deps, artifactId, updatedSettings)

      'scm - {
        val scm = (pomEmptyScm \ "scm").head
        val pomScm = updatedSettings.versionControl

        assert(
          optText(scm \ "connection").isEmpty,
          optText(scm \ "developerConnection").isEmpty,
          optText(scm \ "tag").isEmpty,
          optText(scm \ "url") == pomScm.browsableRepository
        )
      }
    }

    'pomNoLicenses - {
      val updatedSettings = settings.copy(licenses = Seq.empty)
      val pomNoLicenses = pomXml(artifact, deps, artifactId, updatedSettings)

      'licenses - {
        assert(
          (pomNoLicenses \ "licenses").nonEmpty,
          (pomNoLicenses \ "licenses" \ "licenses").isEmpty
        )
      }
    }

    'pomNoDeps - {
      val pomNoDeps = pomXml(artifact,
                             dependencies = Agg.empty,
                             artifactId = artifactId,
                             pomSettings = settings)

      'dependencies - {
        assert(
          (pomNoDeps \ "dependencies").nonEmpty,
          (pomNoDeps \ "dependencies" \ "dependency").isEmpty
        )
      }
    }

    'pomNoDevelopers - {
      val updatedSettings = settings.copy(developers = Seq.empty)
      val pomNoDevelopers = pomXml(artifact, deps, artifactId, updatedSettings)

      'developers - {
        assert(
          (pomNoDevelopers \ "developers").nonEmpty,
          (pomNoDevelopers \ "developers" \ "developer").isEmpty
        )
      }
    }
  }

  def pomXml(artifact: Artifact,
             dependencies: Agg[Dependency],
             artifactId: String,
             pomSettings: PomSettings) =
    XML.loadString(Pom(artifact, dependencies, artifactId, pomSettings))

  def singleText(seq: NodeSeq) =
    seq
      .map(_.text)
      .headOption
      .getOrElse(throw new RuntimeException("seq was empty"))

  def optText(seq: NodeSeq) = seq.map(_.text).headOption

}