diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/ProjectSource.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/ProjectSource.groovy index bb4c8e06..6c3c4566 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/ProjectSource.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/ProjectSource.groovy @@ -7,7 +7,12 @@ import groovy.transform.Immutable /** * Retrieves the shared lib sources of the current project which are expected to be - * at the default location "./vars". + * at the default location ("./vars"), "./src", "./resources"). + * When working with this retriever the LibraryConfiguration which is provided + * with this SourceRetrievers should be configured with allowOverride=false + * since the ProjectSource retriever does not support loading of alternate versions. As + * already outlined above this source retriever always loads the version as it is + * contained at the default location. */ @Immutable diff --git a/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetriever.groovy b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetriever.groovy new file mode 100644 index 00000000..779420a3 --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetriever.groovy @@ -0,0 +1,85 @@ +package com.lesfurets.jenkins + +import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library +import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource +import static org.assertj.core.api.Assertions.assertThat + +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.junit.runners.Parameterized.Parameter +import org.junit.runners.Parameterized.Parameters + +import com.lesfurets.jenkins.unit.BasePipelineTest + +@RunWith(Parameterized.class) +class TestSharedLibraryWithProjectSourceRetriever extends BasePipelineTest { + + // For simplicity we use the common@master here. In this case 'commons@master' + // does not denote the master branch of the commons lib. In this case it is + // just a folder containing the lib. ProjectSourceRetriever is by design + // agnostic to branches and agnostic to library names. + // By default ProjectSourceRetriever works upon '.', which is the project + // root directory. Here for testing the retriever is used in a way so that + // it refers to the folder mentioned below. + String sharedLibs = this.class.getResource('/libs/commons@master').getFile() + + @Parameter(0) + public String script + @Parameter(1) + public boolean allowOverride + @Parameter(2) + public boolean implicit + @Parameter(3) + public boolean expected + + @Override + @Before + void setUp() throws Exception { + scriptRoots += 'src/test/jenkins' + super.setUp() + } + + @Parameters(name = "Test {0} allowOverride:{1} implicit:{2} expected:{3}") + static Collection data() { + return [['libraryJob', false, false, false], + ['libraryJob', false, false, false], + ['libraryJob_implicit', false, false, true], + ['libraryJob_implicit', false, true, false], + + // Utils2, denoted in the job below, cannot be resolved + // since we do not have the lib from the feature branch + // despite this is requested in the pipeline. + // ProjectSourceRetriever does by design + // not take branches into account. + ['libraryJob_feature', true, true, true], + + ].collect { it as Object[] } + } + + @Test + void library_annotation() throws Exception { + boolean exception = false + def library = library().name('commons') + .defaultVersion('') + .allowOverride(allowOverride) + .implicit(implicit) + .targetPath('') + .retriever(projectSource(sharedLibs)) + .build() + helper.registerSharedLibrary(library) + try { + def script = runScript("job/library/${script}.jenkins") + script.execute() + printCallStack() + } catch (e) { + e.printStackTrace() + exception = true + } + assertThat(expected).isEqualTo(exception) + + } +} diff --git a/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetrieverCPS.groovy b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetrieverCPS.groovy new file mode 100644 index 00000000..f6ad131c --- /dev/null +++ b/src/test/groovy/com/lesfurets/jenkins/TestSharedLibraryWithProjectSourceRetrieverCPS.groovy @@ -0,0 +1,85 @@ +package com.lesfurets.jenkins + +import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library +import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource +import static org.assertj.core.api.Assertions.assertThat + +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.junit.runners.Parameterized.Parameter +import org.junit.runners.Parameterized.Parameters + +import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS + +@RunWith(Parameterized.class) +class TestSharedLibraryWithProjectSourceRetrieverCPS extends BasePipelineTestCPS { + + // For simplicity we use the common@master here. In this case 'commons@master' + // does not denote the master branch of the commons lib. In this case it is + // just a folder containing the lib. ProjectSourceRetriever is by design + // agnostic to branches and agnostic to library names. + // By default ProjectSourceRetriever works upon '.', which is the project + // root directory. Here for testing the retriever is used in a way so that + // it refers to the folder mentioned below. + String sharedLibs = this.class.getResource('/libs/commons@master').getFile() + + @Parameter(0) + public String script + @Parameter(1) + public boolean allowOverride + @Parameter(2) + public boolean implicit + @Parameter(3) + public boolean expected + + @Override + @Before + void setUp() throws Exception { + scriptRoots += 'src/test/jenkins' + super.setUp() + } + + @Parameters(name = "Test {0} allowOverride:{1} implicit:{2} expected:{3}") + static Collection data() { + return [['libraryJob', false, false, false], + ['libraryJob', false, false, false], + ['libraryJob_implicit', false, false, true], + ['libraryJob_implicit', false, true, false], + + // Utils2, denoted in the job below, cannot be resolved + // since we do not have the lib from the feature branch + // despite this is requested in the pipeline. + // ProjectSourceRetriever does by design + // not take branches into account. + ['libraryJob_feature', true, true, true], + + ].collect { it as Object[] } + } + + @Test + void library_annotation() throws Exception { + boolean exception = false + def library = library().name('commons') + .defaultVersion('') + .allowOverride(allowOverride) + .implicit(implicit) + .targetPath('') + .retriever(projectSource(sharedLibs)) + .build() + helper.registerSharedLibrary(library) + try { + def script = runScript("job/library/${script}.jenkins") + script.execute() + printCallStack() + } catch (e) { + e.printStackTrace() + exception = true + } + assertThat(expected).isEqualTo(exception) + + } +}