mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
[refactor] CPE read/write from/to disk: remote repetitive coding (#1110)
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
parent
cbe368fe36
commit
7b95c04752
@ -3,6 +3,7 @@ import org.junit.Test
|
||||
import org.junit.rules.RuleChain
|
||||
import util.BasePiperTest
|
||||
import util.JenkinsFileExistsRule
|
||||
import util.JenkinsReadFileRule
|
||||
import util.JenkinsWriteFileRule
|
||||
import util.JenkinsReadYamlRule
|
||||
import util.Rules
|
||||
@ -12,10 +13,13 @@ import static org.hamcrest.Matchers.contains
|
||||
import static org.hamcrest.Matchers.hasItem
|
||||
import static org.junit.Assert.assertThat
|
||||
|
||||
import org.junit.After
|
||||
|
||||
class CommonPipelineEnvironmentTest extends BasePiperTest {
|
||||
|
||||
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
|
||||
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
|
||||
private JenkinsReadFileRule readFileRule = new JenkinsReadFileRule(this, null)
|
||||
|
||||
@Rule
|
||||
public RuleChain rules = Rules
|
||||
@ -23,6 +27,12 @@ class CommonPipelineEnvironmentTest extends BasePiperTest {
|
||||
.around(new JenkinsReadYamlRule(this))
|
||||
.around(writeFileRule)
|
||||
.around(fileExistsRule)
|
||||
.around(readFileRule)
|
||||
|
||||
@After
|
||||
void tearDown() {
|
||||
nullScript.metaClass.findFiles = null
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomValueList() {
|
||||
@ -51,4 +61,33 @@ class CommonPipelineEnvironmentTest extends BasePiperTest {
|
||||
assertThat(writeFileRule.files['.pipeline/commonPipelineEnvironment/artifactVersion'], is('1.0.0'))
|
||||
assertThat(writeFileRule.files['.pipeline/commonPipelineEnvironment/custom/custom1'], is('customVal1'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void readFromDisk() {
|
||||
|
||||
fileExistsRule.existingFiles.addAll([
|
||||
'.pipeline/commonPipelineEnvironment/artifactVersion',
|
||||
'.pipeline/commonPipelineEnvironment/custom/custom1',
|
||||
])
|
||||
|
||||
nullScript.metaClass.findFiles {
|
||||
[
|
||||
[
|
||||
'getName': {'custom1'},
|
||||
'getPath': {'.pipeline/commonPipelineEnvironment/custom'},
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
readFileRule.files.putAll([
|
||||
'.pipeline/commonPipelineEnvironment/artifactVersion': '1.0.0',
|
||||
'.pipeline/commonPipelineEnvironment/custom': 'customVal1',
|
||||
])
|
||||
|
||||
nullScript.commonPipelineEnvironment.readFromDisk(nullScript)
|
||||
|
||||
assertThat(nullScript.commonPipelineEnvironment.artifactVersion, is('1.0.0'))
|
||||
assertThat(nullScript.commonPipelineEnvironment.valueMap['custom1'], is('customVal1'))
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ class JenkinsReadFileRule implements TestRule {
|
||||
|
||||
final BasePipelineTest testInstance
|
||||
final String testRoot
|
||||
final Map files = [:]
|
||||
|
||||
JenkinsReadFileRule(BasePipelineTest testInstance, String testRoot) {
|
||||
this.testInstance = testInstance
|
||||
@ -25,17 +26,28 @@ class JenkinsReadFileRule implements TestRule {
|
||||
@Override
|
||||
void evaluate() throws Throwable {
|
||||
|
||||
testInstance.helper.registerAllowedMethod( 'readFile', [String.class], {s -> return (loadFile("${testRoot}/${s}")).getText('UTF-8')} )
|
||||
testInstance.helper.registerAllowedMethod( 'readFile', [String.class], {s -> return load(s, 'UTF-8')} )
|
||||
|
||||
testInstance.helper.registerAllowedMethod( 'readFile', [Map.class], {m -> return (loadFile("${testRoot}/${m.file}")).getText(m.encoding?m.encoding:'UTF-8')} )
|
||||
testInstance.helper.registerAllowedMethod( 'readFile', [Map.class], {m -> return load(m.file, m.encoding?m.encoding:'UTF-8')} )
|
||||
|
||||
base.evaluate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String load(String path, String encoding) {
|
||||
|
||||
if(files[path]) {
|
||||
return files[path]
|
||||
}
|
||||
|
||||
if(testRoot == null) {
|
||||
throw new IllegalStateException("Test root not set. Resolving: \"${path}\".")
|
||||
}
|
||||
loadFile(testRoot + '/' + path).getText(encoding)
|
||||
}
|
||||
|
||||
File loadFile(String path){
|
||||
return new File(path)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -145,20 +145,20 @@ class commonPipelineEnvironment implements Serializable {
|
||||
return config
|
||||
}
|
||||
|
||||
def files = [
|
||||
[filename: '.pipeline/commonPipelineEnvironment/artifactVersion', property: 'artifactVersion'],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/github/owner', property: 'githubOrg'],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/github/repository', property: 'githubRepo'],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/branch', property: 'gitBranch'],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/commitId', property: 'gitCommitId'],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/commitMessage', property: 'gitCommitMessage'],
|
||||
]
|
||||
|
||||
void writeToDisk(script) {
|
||||
|
||||
def files = [
|
||||
[filename: '.pipeline/commonPipelineEnvironment/artifactVersion', content: artifactVersion],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/github/owner', content: githubOrg],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/github/repository', content: githubRepo],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/branch', content: gitBranch],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/commitId', content: gitCommitId],
|
||||
[filename: '.pipeline/commonPipelineEnvironment/git/commitMessage', content: gitCommitMessage],
|
||||
]
|
||||
|
||||
files.each({f ->
|
||||
if (f.content && !script.fileExists(f.filename)) {
|
||||
script.writeFile file: f.filename, text: f.content
|
||||
if (this[f.property] && !script.fileExists(f.filename)) {
|
||||
script.writeFile file: f.filename, text: this[f.property]
|
||||
}
|
||||
})
|
||||
|
||||
@ -171,43 +171,20 @@ class commonPipelineEnvironment implements Serializable {
|
||||
})
|
||||
}
|
||||
|
||||
void readFromDisk() {
|
||||
def file = '.pipeline/commonPipelineEnvironment/artifactVersion'
|
||||
if (fileExists(file)) {
|
||||
artifactVersion = readFile(file)
|
||||
}
|
||||
void readFromDisk(script) {
|
||||
|
||||
file = '.pipeline/commonPipelineEnvironment/github/owner'
|
||||
if (fileExists(file)) {
|
||||
githubOrg = readFile(file)
|
||||
}
|
||||
files.each({f ->
|
||||
if (script.fileExists(f.filename)) {
|
||||
this[f.property] = script.readFile(f.filename)
|
||||
}
|
||||
})
|
||||
|
||||
file = '.pipeline/commonPipelineEnvironment/github/repository'
|
||||
if (fileExists(file)) {
|
||||
githubRepo = readFile(file)
|
||||
}
|
||||
|
||||
file = '.pipeline/commonPipelineEnvironment/git/branch'
|
||||
if (fileExists(file)) {
|
||||
gitBranch = readFile(file)
|
||||
}
|
||||
|
||||
file = '.pipeline/commonPipelineEnvironment/git/commitId'
|
||||
if (fileExists(file)) {
|
||||
gitCommitId = readFile(file)
|
||||
}
|
||||
|
||||
file = '.pipeline/commonPipelineEnvironment/git/commitMessage'
|
||||
if (fileExists(file)) {
|
||||
gitCommitMessage = readFile(file)
|
||||
}
|
||||
|
||||
def customValues = findFiles(glob: '.pipeline/commonPipelineEnvironment/custom/*')
|
||||
def customValues = script.findFiles(glob: '.pipeline/commonPipelineEnvironment/custom/*')
|
||||
|
||||
customValues.each({f ->
|
||||
def fileName = f.getName()
|
||||
def param = fileName.split('/')[fileName.split('\\/').size()-1]
|
||||
valueMap[param] = readFile(f.getPath())
|
||||
valueMap[param] = script.readFile(f.getPath())
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user