Skip to content

Commit

Permalink
Do not implicitly call Identifier.toString() in tests
Browse files Browse the repository at this point in the history
The assertion of a data class results to be non-null implicitly calls
toString() on all nested data classes which often includes the Identifier
class. While this does not do any harm, it is often unnecessary to create
the full string, and it makes it diffcult to ensure Identifier.toString()
is not called for serialization. Thus, rewrite code to not use the
"shouldNotBe null" assertion.

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@here.com>
  • Loading branch information
sschuberth committed Feb 7, 2019
1 parent 28310f0 commit cf28e50
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 157 deletions.
11 changes: 8 additions & 3 deletions analyzer/src/funTest/kotlin/BabelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.here.ort.utils.test.patchExpectedResult

import io.kotlintest.TestCase
import io.kotlintest.TestResult
import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.shouldBe
import io.kotlintest.specs.WordSpec

Expand Down Expand Up @@ -60,16 +61,20 @@ class BabelTest : WordSpec() {
init {
"Babel dependencies" should {
"be correctly analyzed" {
val packageFile = File(projectDir, "package.json")
val definitionFile = File(projectDir, "package.json")

val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "${projectDir.name}-expected-output.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)
val actualResult = createNPM().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]

patchActualResult(yamlMapper.writeValueAsString(actualResult)) shouldBe expectedResult
val result = createNPM().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
patchActualResult(yamlMapper.writeValueAsString(resultForDefinitionFile)) shouldBe expectedResult
}
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions analyzer/src/funTest/kotlin/BowerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import com.here.ort.utils.test.USER_DIR
import com.here.ort.utils.test.patchExpectedResult

import io.kotlintest.matchers.beEmpty
import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.should
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.specs.StringSpec

import java.io.File
Expand All @@ -44,19 +44,21 @@ class BowerTest : StringSpec() {

init {
"Project dependencies are detected correctly" {
val packageFile = File(projectDir, "bower.json")
val definitionFile = File(projectDir, "bower.json")
val vcsPath = vcsDir.getPathToRoot(projectDir)
val expectedResult = patchExpectedResult(File(projectDir.parentFile, "bower-expected-output.yml"),
definitionFilePath = "$vcsPath/bower.json",
path = vcsPath,
revision = vcsRevision,
url = normalizeVcsUrl(vcsUrl))

val result = createBower().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createBower().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors should beEmpty()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors should beEmpty()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}
}

Expand Down
23 changes: 13 additions & 10 deletions analyzer/src/funTest/kotlin/BundlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import com.here.ort.utils.test.DEFAULT_REPOSITORY_CONFIGURATION
import com.here.ort.utils.test.USER_DIR
import com.here.ort.utils.test.patchExpectedResult

import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.matchers.startWith
import io.kotlintest.specs.WordSpec

Expand Down Expand Up @@ -66,16 +66,19 @@ class BundlerTest : WordSpec() {

"show error if no lockfile is present" {
val definitionFile = File(projectsDir, "no-lockfile/Gemfile")
val actualResult = createBundler().resolveDependencies(USER_DIR, listOf(definitionFile))[definitionFile]
val result = createBundler().resolveDependencies(USER_DIR, listOf(definitionFile))

actualResult shouldNotBe null
actualResult!!.project.id shouldBe
Identifier("Bundler::src/funTest/assets/projects/synthetic/bundler/no-lockfile/Gemfile:")
actualResult.project.definitionFilePath shouldBe
"analyzer/src/funTest/assets/projects/synthetic/bundler/no-lockfile/Gemfile"
actualResult.packages.size shouldBe 0
actualResult.errors.size shouldBe 1
actualResult.errors.first().message should startWith("IllegalArgumentException: No lockfile found in")
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.project.id shouldBe
Identifier("Bundler::src/funTest/assets/projects/synthetic/bundler/no-lockfile/Gemfile:")
resultForDefinitionFile.project.definitionFilePath shouldBe
"analyzer/src/funTest/assets/projects/synthetic/bundler/no-lockfile/Gemfile"
resultForDefinitionFile.packages.size shouldBe 0
resultForDefinitionFile.errors.size shouldBe 1
resultForDefinitionFile.errors.first().message should
startWith("IllegalArgumentException: No lockfile found in")
}
}

"resolve dependencies correctly when the project is a Gem" {
Expand Down
65 changes: 38 additions & 27 deletions analyzer/src/funTest/kotlin/GoDepTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,82 @@ package com.here.ort.analyzer

import com.here.ort.analyzer.managers.GoDep
import com.here.ort.downloader.VersionControlSystem
import com.here.ort.model.Identifier
import com.here.ort.model.Project
import com.here.ort.model.VcsInfo
import com.here.ort.model.config.AnalyzerConfiguration
import com.here.ort.model.yamlMapper
import com.here.ort.utils.test.DEFAULT_ANALYZER_CONFIGURATION
import com.here.ort.utils.test.DEFAULT_REPOSITORY_CONFIGURATION
import com.here.ort.utils.test.USER_DIR

import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.matchers.startWith
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.specs.WordSpec

import java.io.File

class GoDepTest : WordSpec() {
private val projectsDir = File("src/funTest/assets/projects")
private val vcsDir = VersionControlSystem.forDirectory(projectsDir)!!
private val vcsRevision = vcsDir.getRevision()

init {
"GoDep" should {
"resolve dependencies from a lockfile correctly" {
val manifestFile = File(projectsDir, "external/qmstr/Gopkg.toml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(manifestFile))[manifestFile]
val definitionFile = File(projectsDir, "external/qmstr/Gopkg.toml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(definitionFile))[definitionFile]
val expectedResult = File(projectsDir, "external/qmstr-expected-output.yml").readText()

yamlMapper.writeValueAsString(result) shouldBe expectedResult
}

"show error if no lockfile is present" {
val manifestFile = File(projectsDir, "synthetic/godep/no-lockfile/Gopkg.toml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(manifestFile))[manifestFile]

result shouldNotBe null
result!!.project.id shouldBe
Identifier("GoDep::src/funTest/assets/projects/synthetic/godep/no-lockfile/Gopkg.toml:")
result.project.definitionFilePath shouldBe
"analyzer/src/funTest/assets/projects/synthetic/godep/no-lockfile/Gopkg.toml"
result.packages.size shouldBe 0
result.errors.size shouldBe 1
result.errors.first().message should startWith("IllegalArgumentException: No lockfile found in")
val definitionFile = File(projectsDir, "synthetic/godep/no-lockfile/Gopkg.toml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
// Note: These fall-back coordinates actually are generated by the PackageManager base class if an
// exception is thrown.
resultForDefinitionFile.project.id.toCoordinates() shouldBe
"GoDep::src/funTest/assets/projects/synthetic/godep/no-lockfile/Gopkg.toml:"

resultForDefinitionFile.project.definitionFilePath shouldBe
"analyzer/src/funTest/assets/projects/synthetic/godep/no-lockfile/Gopkg.toml"
resultForDefinitionFile.packages.size shouldBe 0
resultForDefinitionFile.errors.size shouldBe 1
resultForDefinitionFile.errors.first().message should
startWith("IllegalArgumentException: No lockfile found in")
}
}

"invoke the dependency solver if no lockfile is present and allowDynamicVersions is set" {
val manifestFile = File(projectsDir, "synthetic/godep/no-lockfile/Gopkg.toml")
val definitionFile = File(projectsDir, "synthetic/godep/no-lockfile/Gopkg.toml")
val config = AnalyzerConfiguration(false, true)
val result = createGoDep(config).resolveDependencies(USER_DIR, listOf(manifestFile))[manifestFile]

result shouldNotBe null
result!!.project shouldNotBe Project.EMPTY
result.packages.size shouldBe 4
result.errors.size shouldBe 0
val result = createGoDep(config).resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.project.id.toCoordinates() shouldBe
"GoDep::no-lockfile:$vcsRevision"
resultForDefinitionFile.project.definitionFilePath shouldBe
"analyzer/src/funTest/assets/projects/synthetic/godep/no-lockfile/Gopkg.toml"
resultForDefinitionFile.packages.size shouldBe 4
resultForDefinitionFile.errors.size shouldBe 0
}
}

"import dependencies from Glide" {
val manifestFile = File(projectsDir, "external/sprig/glide.yaml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(manifestFile))[manifestFile]
val definitionFile = File(projectsDir, "external/sprig/glide.yaml")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(definitionFile))[definitionFile]
val expectedResult = File(projectsDir, "external/sprig-expected-output.yml").readText()

yamlMapper.writeValueAsString(result) shouldBe expectedResult
}

"import dependencies from godeps" {
val manifestFile = File(projectsDir, "external/godep/Godeps/Godeps.json")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(manifestFile))[manifestFile]
val definitionFile = File(projectsDir, "external/godep/Godeps/Godeps.json")
val result = createGoDep().resolveDependencies(USER_DIR, listOf(definitionFile))[definitionFile]
val expectedResult = File(projectsDir, "external/godep-expected-output.yml").readText()

yamlMapper.writeValueAsString(result) shouldBe expectedResult
Expand Down
38 changes: 22 additions & 16 deletions analyzer/src/funTest/kotlin/GradleAndroidTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import com.here.ort.utils.test.DEFAULT_REPOSITORY_CONFIGURATION
import com.here.ort.utils.test.patchExpectedResult
import com.here.ort.utils.test.USER_DIR

import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.specs.StringSpec

import java.io.File
Expand All @@ -43,48 +43,54 @@ class GradleAndroidTest : StringSpec() {

init {
"Root project dependencies are detected correctly".config(tags = setOf(AndroidTag)) {
val packageFile = File(projectDir, "build.gradle")
val definitionFile = File(projectDir, "build.gradle")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "gradle-android-expected-output-root.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}

"Project dependencies are detected correctly".config(tags = setOf(AndroidTag)) {
val packageFile = File(projectDir, "app/build.gradle")
val definitionFile = File(projectDir, "app/build.gradle")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "gradle-android-expected-output-app.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}

"External dependencies are detected correctly".config(tags = setOf(AndroidTag)) {
val packageFile = File(projectDir, "lib/build.gradle")
val definitionFile = File(projectDir, "lib/build.gradle")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "gradle-android-expected-output-lib.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}
}

Expand Down
38 changes: 22 additions & 16 deletions analyzer/src/funTest/kotlin/GradleKotlinScriptTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import com.here.ort.utils.test.DEFAULT_REPOSITORY_CONFIGURATION
import com.here.ort.utils.test.USER_DIR
import com.here.ort.utils.test.patchExpectedResult

import io.kotlintest.matchers.maps.shouldContainKey
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.specs.StringSpec

import java.io.File
Expand All @@ -42,48 +42,54 @@ class GradleKotlinScriptTest : StringSpec() {

init {
"root project dependencies are detected correctly" {
val packageFile = File(projectDir, "build.gradle.kts")
val definitionFile = File(projectDir, "build.gradle.kts")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "multi-kotlin-project-expected-output-root.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}

"core project dependencies are detected correctly" {
val packageFile = File(projectDir, "core/build.gradle.kts")
val definitionFile = File(projectDir, "core/build.gradle.kts")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "multi-kotlin-project-expected-output-core.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}

"cli project dependencies are detected correctly" {
val packageFile = File(projectDir, "cli/build.gradle.kts")
val definitionFile = File(projectDir, "cli/build.gradle.kts")
val expectedResult = patchExpectedResult(
File(projectDir.parentFile, "multi-kotlin-project-expected-output-cli.yml"),
url = normalizeVcsUrl(vcsUrl),
revision = vcsRevision
)

val result = createGradle().resolveDependencies(USER_DIR, listOf(packageFile))[packageFile]
val result = createGradle().resolveDependencies(USER_DIR, listOf(definitionFile))

result shouldNotBe null
result!!.errors shouldBe emptyList()
yamlMapper.writeValueAsString(result) shouldBe expectedResult
result shouldContainKey definitionFile
result[definitionFile]!!.let { resultForDefinitionFile ->
resultForDefinitionFile.errors shouldBe emptyList()
yamlMapper.writeValueAsString(resultForDefinitionFile) shouldBe expectedResult
}
}
}

Expand Down
Loading

0 comments on commit cf28e50

Please sign in to comment.