-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(Yarn): Add basic support for Corepack
According to the Yarn 2+ documentation, Corepack [1] is the preferred way to install this package manager. If this method is used, the name of the executable has to be determined differently. [1]: https://yarnpkg.com/corepack Signed-off-by: Oliver Heger <[email protected]>
- 52.1.0
- 52.0.1
- 52.0.0
- 51.1.0
- 51.0.0
- 50.0.0
- 49.0.0
- 48.0.0
- 47.0.0
- 46.0.0
- 45.0.0
- 44.0.0
- 43.0.2
- 43.0.1
- 43.0.0
- 42.1.0
- 42.0.0
- 41.0.0
- 40.0.1
- 40.0.0
- 39.0.0
- 38.0.0
- 37.0.0
- 36.0.0
- 35.0.0
- 34.0.0
- 33.1.0
- 33.0.0
- 32.1.0
- 32.0.0
- 31.0.0
- 30.0.0
- 29.1.0
- 29.0.0
- 28.0.0
- 27.0.0
- 26.0.0
- 25.1.0
- 25.0.0
- 24.0.0
- 23.0.0
- 22.8.0
- 22.7.0
1 parent
e3ec11e
commit 3e9d8f4
Showing
2 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
plugins/package-managers/node/src/test/kotlin/Yarn2Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.node | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.engine.spec.tempdir | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
|
||
import java.io.File | ||
|
||
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) | ||
|
||
checkExecutableFromYarnRc(workingDir) | ||
} | ||
|
||
"return the executable defined in .yarnrc.yml if package.json is invalid" { | ||
val workingDir = tempdir() | ||
workingDir.resolve("package.json").writeText("invalid-json") | ||
|
||
checkExecutableFromYarnRc(workingDir) | ||
} | ||
|
||
"throw if no executable is defined in .yarnrc.yml" { | ||
val workingDir = tempdir() | ||
workingDir.resolve(".yarnrc.yml").writeText("someProperty: some-value") | ||
|
||
val yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration()) | ||
|
||
val exception = shouldThrow<IllegalArgumentException> { | ||
yarn.command(workingDir) | ||
} | ||
|
||
exception.localizedMessage shouldContain "No Yarn 2+ executable" | ||
} | ||
|
||
"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 yarn = Yarn2("yarn", workingDir, AnalyzerConfiguration(), RepositoryConfiguration()) | ||
|
||
val exception = shouldThrow<IllegalArgumentException> { | ||
yarn.command(workingDir) | ||
} | ||
|
||
exception.localizedMessage shouldContain executable | ||
} | ||
|
||
"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)) | ||
) | ||
|
||
val yarn = Yarn2("Yarn2", workingDir, analyzerConfiguration, RepositoryConfiguration()) | ||
val command = yarn.command(workingDir) | ||
|
||
command shouldBe "yarn" | ||
} | ||
|
||
"return the default executable name if Corepack is enabled based on the package.json" { | ||
val workingDir = tempdir() | ||
writePackageJson(workingDir, "[email protected]") | ||
|
||
val yarn = Yarn2("Yarn2", workingDir, AnalyzerConfiguration(), RepositoryConfiguration()) | ||
val command = yarn.command(workingDir) | ||
|
||
command shouldBe "yarn" | ||
} | ||
|
||
"return the executable defined in .yarnrc.yml if Corepack detection is turned off" { | ||
val workingDir = tempdir() | ||
writePackageJson(workingDir, "[email protected]") | ||
|
||
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] | ||
* and [config]. This should be the case when Corepack is not enabled. | ||
*/ | ||
private fun checkExecutableFromYarnRc(workingDir: File, config: AnalyzerConfiguration = AnalyzerConfiguration()) { | ||
val executable = "yarn-wrapper.js" | ||
workingDir.resolve(".yarnrc.yml").writeText("yarnPath: $executable") | ||
val executableFile = workingDir.resolve(executable).apply { | ||
writeText("#!/usr/bin/env node\nconsole.log('yarn')") | ||
} | ||
|
||
val yarn = Yarn2("Yarn2", workingDir, config, RepositoryConfiguration()) | ||
val command = yarn.command(workingDir) | ||
|
||
command shouldBe executableFile.absolutePath | ||
} | ||
} | ||
|
||
/** | ||
* Write a `package.json` file to [dir] with some default properties and an optional [packageManager] entry. | ||
*/ | ||
private fun writePackageJson(dir: File, packageManager: String?) { | ||
val packageManagerProperty = packageManager?.let { """"packageManager": "$it"""" }.orEmpty() | ||
dir.resolve("package.json").writeText( | ||
""" | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
$packageManagerProperty | ||
} | ||
""".trimIndent() | ||
) | ||
} |