Skip to content

Commit

Permalink
chore(node): Reduce indentation in Yarn2Test
Browse files Browse the repository at this point in the history
Use constructor init syntax for the `WordSpec` to be able to reduce
indentation for a better overview.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Dec 12, 2024
1 parent ee6016c commit 7b93abf
Showing 1 changed file with 59 additions and 61 deletions.
120 changes: 59 additions & 61 deletions plugins/package-managers/node/src/test/kotlin/yarn2/Yarn2Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,91 +31,89 @@ import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration

class Yarn2Test : WordSpec() {
init {
"command" should {
"return the executable defined in .yarnrc.yml if no package.json is present" {
checkExecutableFromYarnRc(tempdir())
}

"return the executable defined in .yarnrc.yml if no package manager is defined" {
val workingDir = tempdir()
writePackageJson(workingDir, null)
class Yarn2Test : WordSpec({
"command" should {
"return the executable defined in .yarnrc.yml if no package.json is present" {
checkExecutableFromYarnRc(tempdir())
}

checkExecutableFromYarnRc(workingDir)
}
"return the executable defined in .yarnrc.yml if no package manager is defined" {
val workingDir = tempdir()
writePackageJson(workingDir, null)

"return the executable defined in .yarnrc.yml if package.json is invalid" {
val workingDir = tempdir()
workingDir.resolve("package.json").writeText("invalid-json")
checkExecutableFromYarnRc(workingDir)
}

checkExecutableFromYarnRc(workingDir)
}
"return the executable defined in .yarnrc.yml if package.json is invalid" {
val workingDir = tempdir()
workingDir.resolve("package.json").writeText("invalid-json")

"throw if no executable is defined in .yarnrc.yml" {
val workingDir = tempdir()
workingDir.resolve(".yarnrc.yml").writeText("someProperty: some-value")
checkExecutableFromYarnRc(workingDir)
}

val yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())
"throw if no executable is defined in .yarnrc.yml" {
val workingDir = tempdir()
workingDir.resolve(".yarnrc.yml").writeText("someProperty: some-value")

val exception = shouldThrow<IllegalArgumentException> {
yarn.command(workingDir)
}
val yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())

exception.localizedMessage shouldContain "No Yarn 2+ executable"
val exception = shouldThrow<IllegalArgumentException> {
yarn.command(workingDir)
}

"throw if the executable defined in .yarnrc.yml does not exist" {
val workingDir = tempdir()
val executable = "non-existing-yarn-wrapper.js"
workingDir.resolve(".yarnrc.yml").writeText("yarnPath: $executable")
exception.localizedMessage shouldContain "No Yarn 2+ executable"
}

val yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())
"throw if the executable defined in .yarnrc.yml does not exist" {
val workingDir = tempdir()
val executable = "non-existing-yarn-wrapper.js"
workingDir.resolve(".yarnrc.yml").writeText("yarnPath: $executable")

val exception = shouldThrow<IllegalArgumentException> {
yarn.command(workingDir)
}
val yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())

exception.localizedMessage shouldContain executable
val exception = shouldThrow<IllegalArgumentException> {
yarn.command(workingDir)
}

"return the default executable name if Corepack is enabled based on the configuration option" {
val workingDir = tempdir()
val yarn2Options = mapOf("corepackOverride" to "true")
val analyzerConfiguration = AnalyzerConfiguration(
packageManagers = mapOf("Yarn2" to PackageManagerConfiguration(options = yarn2Options))
)
exception.localizedMessage shouldContain executable
}

val yarn = Yarn2("Yarn2", workingDir, analyzerConfiguration, RepositoryConfiguration())
val command = yarn.command(workingDir)
"return the default executable name if Corepack is enabled based on the configuration option" {
val workingDir = tempdir()
val yarn2Options = mapOf("corepackOverride" to "true")
val analyzerConfiguration = AnalyzerConfiguration(
packageManagers = mapOf("Yarn2" to PackageManagerConfiguration(options = yarn2Options))
)

command shouldBe "yarn"
}
val yarn = Yarn2("Yarn2", workingDir, analyzerConfiguration, RepositoryConfiguration())
val command = yarn.command(workingDir)

"return the default executable name if Corepack is enabled based on the package.json" {
val workingDir = tempdir()
writePackageJson(workingDir, "yarn@4.0.0")
command shouldBe "yarn"
}

val yarn = Yarn2("Yarn2", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())
val command = yarn.command(workingDir)
"return the default executable name if Corepack is enabled based on the package.json" {
val workingDir = tempdir()
writePackageJson(workingDir, "yarn@4.0.0")

command shouldBe "yarn"
}
val yarn = Yarn2("Yarn2", workingDir, AnalyzerConfiguration(), RepositoryConfiguration())
val command = yarn.command(workingDir)

"return the executable defined in .yarnrc.yml if Corepack detection is turned off" {
val workingDir = tempdir()
writePackageJson(workingDir, "yarn@4.0.0")
command shouldBe "yarn"
}

val yarn2Options = mapOf("corepackOverride" to "false")
val analyzerConfiguration = AnalyzerConfiguration(
packageManagers = mapOf("Yarn2" to PackageManagerConfiguration(options = yarn2Options))
)
"return the executable defined in .yarnrc.yml if Corepack detection is turned off" {
val workingDir = tempdir()
writePackageJson(workingDir, "yarn@4.0.0")

checkExecutableFromYarnRc(workingDir, analyzerConfiguration)
}
val yarn2Options = mapOf("corepackOverride" to "false")
val analyzerConfiguration = AnalyzerConfiguration(
packageManagers = mapOf("Yarn2" to PackageManagerConfiguration(options = yarn2Options))
)

checkExecutableFromYarnRc(workingDir, analyzerConfiguration)
}
}
}
})

/**
* Check whether an executable defined in a `.yarnrc.yml` file is used when invoked with the given [workingDir]
Expand Down

0 comments on commit 7b93abf

Please sign in to comment.