From 018fbcf635ab5d86644a8b148bc7640a27a9f6e3 Mon Sep 17 00:00:00 2001 From: Jim Croft Date: Fri, 13 Oct 2017 16:08:45 +0100 Subject: [PATCH] Update library loading so branch not required for localSource locations. I was trying to use JenkinsPipelineUnit to test a library I'm writing. I want to use this from within local copy (clone) of the repo using the localSource retriever. This was problematic as the code required setting defaultVersion (branch) which it then appended to the path to the library root. This PR allows the following usage: ``` def library = library() .name('jenkins-library') .allowOverride(true) .implicit(false) .targetPath(dirPath) .retriever(localSource(dirPath)) .build() helper.registerSharedLibrary(library) ``` where `dirPath` is the project root, without `@master` or other version identifier in the filesystem path. --- .../jenkins/unit/global/lib/LibraryConfiguration.groovy | 2 +- .../lesfurets/jenkins/unit/global/lib/LocalSource.groovy | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) 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 f4eeb173..44fd59ab 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/LocalSource.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LocalSource.groovy index 61babde1..2444d0bb 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,14 @@ class LocalSource implements SourceRetriever { @Override List 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()] }