gpt4 book ai didi

unit-testing - 如何从当前分支使用 JenkinsPipelineUnit 的共享库

转载 作者:行者123 更新时间:2023-12-01 11:24:43 24 4
gpt4 key购买 nike

我正在尝试使用 jenkinsPipelineUnit 来测试与我的共享库存在于同一个 git 存储库中的 JenkinsFile。此 Jenkinsfile 引用位于 src 中的共享库。看来我必须提交我的共享库更改才能测试它们,即使我在检索器中使用 localSource。

如何在不先提交代码的情况下加载我的共享库并对它们进行单元测试?

这是我当前不起作用的代码:

    def library = library().name('pipeline-utils')
.defaultVersion("master")
.allowOverride(true)
.implicit(false)
.targetPath(sharedLibs)
.retriever(localSource(sharedLibs))
.build()
helper.registerSharedLibrary(library)

try {
def script = runScript("pipelines/test.groovy")
}

我收到此错误:
    file:/Users/<myuserid>/git/pipelines/test.groovy: 2: 
Error on loading library pipeline-utils@myteam/pipelineUnitTest :
Directory /Users/<myuserid>/git/out/test/classes/com/company/test/pipeline-utils@myteam/pipelineUnitTest does not exists @ line 2, column 1.
@Library("pipeline-utils@myteam/pipelineUnitTest") _

最佳答案

这并不像听起来那么容易。 JenkinsPipelineUnit 一年后不再移动,而一些有趣的工作正在等待拉取请求。以下是我必须执行的步骤才能在本地运行,但在 jenkins 上我的存储库目录的名称每次都可能不同。

1. 创建自定义版本的 JenkinsPipelineUnit

我从 https://github.com/jenkinsci/JenkinsPipelineUnit/pull/75 开始但不得不添加一些其他更改。这些是所有的变化:

diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
index f4eeb17..dc13b9c 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryConfiguration.groovy
@@ -18,7 +18,7 @@ class LibraryConfiguration {
String targetPath

LibraryConfiguration validate() {
- if (name && defaultVersion && retriever && targetPath)
+ if (name && retriever && targetPath && ((retriever instanceof LocalSource || defaultVersion)))
return this
throw new IllegalStateException("LibraryConfiguration is not properly initialized ${this.toString()}")
}
diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
index 120a316..a253f2d 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy
@@ -117,11 +117,14 @@ class LibraryLoader {
}

private static boolean matches(String libName, String version, LibraryConfiguration libraryDescription) {
+ if (libraryDescription.allowOverride) {
+ return true
+ }
if (libraryDescription.name == libName) {
if (version == null) {
return true
}
- if (libraryDescription.allowOverride || libraryDescription.defaultVersion == version) {
+ if (libraryDescription.defaultVersion == version) {
return true
}
}
diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
index 61babde..4edca23 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy
@@ -11,7 +11,13 @@ class LocalSource implements SourceRetriever {

@Override
List<URL> retrieve(String repository, String branch, String targetPath) {
- def sourceDir = new File(sourceURL).toPath().resolve("$repository@$branch").toFile()
+ def sourceURLPath = new File(sourceURL).toPath()
+ def sourceDir
+ if (branch) {
+ sourceDir = sourceURLPath.resolve("$repository@$branch").toFile()
+ } else {
+ sourceDir = sourceURLPath.resolve(repository).toFile()
+ }
if (sourceDir.exists()) {
return [sourceDir.toURI().toURL()]
}

2. 将您当前的存储库目录注册为您的共享库

灵感来自: https://github.com/jimcroft/jenkinslib-example/blob/master/test/com/example/TestCase1.groovy

在您的 TestClass.groovy 中:
void setup() {
String repositoryDirectoryName = FilenameUtils.getName(System.getProperty("user.dir"))
String dirPath = new File( System.getProperty("user.dir") )
.getAbsoluteFile()
.getParentFile()
.getAbsolutePath()

// This next call bypasses registerSharedLibrary; to allow registering a library with a different directory name
helper.libraries.put('my-jenkins-library', library(repositoryDirectoryName)
.allowOverride(true)
.implicit(false)
.targetPath(dirPath)
.retriever(localSource(dirPath))
.build())

关于unit-testing - 如何从当前分支使用 JenkinsPipelineUnit 的共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48564563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com