4

我正在尝试使用 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") _
4

1 回答 1

0

这并不像听起来那么容易。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())
于 2018-12-21T17:07:58.427 回答