1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/com/sap/piper/FileUtilsTest.groovy

193 lines
4.9 KiB
Groovy
Raw Normal View History

2018-02-14 12:32:46 +02:00
package com.sap.piper
2018-03-09 18:58:11 +02:00
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.TemporaryFolder
import org.junit.rules.RuleChain
import util.Rules
import com.lesfurets.jenkins.unit.BasePipelineTest
2017-07-11 15:12:03 +02:00
import com.sap.piper.FileUtils
2018-03-09 18:58:11 +02:00
import hudson.AbortException
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
class FileUtilsTest extends BasePipelineTest {
2017-07-11 15:12:03 +02:00
2018-02-12 18:31:48 +02:00
@ClassRule
public static TemporaryFolder tmp = new TemporaryFolder()
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
private ExpectedException thrown = new ExpectedException()
2017-07-11 15:12:03 +02:00
@Rule
2018-03-09 18:58:11 +02:00
public RuleChain rules = Rules.getCommonRules(this)
.around(thrown)
2017-07-11 15:12:03 +02:00
2018-02-12 18:31:48 +02:00
private static emptyDir
private static notEmptyDir
private static file
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
private static script
2018-02-12 18:31:48 +02:00
@BeforeClass
static void createTestFiles() {
2017-07-11 15:12:03 +02:00
2018-02-12 18:31:48 +02:00
emptyDir = tmp.newFolder('emptyDir').getAbsolutePath()
notEmptyDir = tmp.newFolder('notEmptyDir').getAbsolutePath()
2018-03-09 18:58:11 +02:00
file = tmp.newFile('notEmptyDir/file.txt').getAbsolutePath()
}
@Before
void setup() {
script = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment
2017-07-11 15:12:03 +02:00
}
2018-02-12 18:31:48 +02:00
2017-07-11 15:12:03 +02:00
@Test
2018-03-09 18:58:11 +02:00
void validateDirectory_nullParameterTest() {
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'dir' can not be null or empty.")
2018-03-09 18:58:11 +02:00
FileUtils.validateDirectory(script, null)
2017-07-11 15:12:03 +02:00
}
@Test
2018-04-10 18:23:44 +02:00
void validateDirectory_emptyParameterTest() {
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'dir' can not be null or empty.")
2018-03-09 18:58:11 +02:00
FileUtils.validateDirectory(script, '')
2017-07-11 15:12:03 +02:00
}
@Test
2018-03-09 18:58:11 +02:00
void validateDirectory_directoryDoestNotExistTest() {
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
def dir = new File("$emptyDir", 'test').getAbsolutePath()
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, dir) })
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
2018-03-09 18:58:11 +02:00
thrown.expectMessage("Validation failed. '$dir' does not exist.")
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
FileUtils.validateDirectory(script, dir)
2017-07-11 15:12:03 +02:00
}
@Test
2018-03-09 18:58:11 +02:00
void validateDirectory_isNotDirectoryTest() {
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, file) })
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
2018-03-09 18:58:11 +02:00
thrown.expectMessage("Validation failed. '$file' is not a directory.")
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
FileUtils.validateDirectory(script, file)
2017-07-11 15:12:03 +02:00
}
@Test
void validateDirectoryTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, notEmptyDir) })
FileUtils.validateDirectory(script, notEmptyDir)
2017-07-11 15:12:03 +02:00
}
@Test
2018-03-09 18:58:11 +02:00
void validateDirectoryIsNotEmpty_directoryIsEmptyTest() {
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, emptyDir) })
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
2018-03-09 18:58:11 +02:00
thrown.expectMessage("Validation failed. '$emptyDir' is empty.")
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
FileUtils.validateDirectoryIsNotEmpty(script, emptyDir)
2017-07-11 15:12:03 +02:00
}
@Test
void validateDirectoryIsNotEmptyTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, notEmptyDir) })
FileUtils.validateDirectoryIsNotEmpty(script, notEmptyDir)
2017-07-11 15:12:03 +02:00
}
@Test
2018-03-09 18:58:11 +02:00
void validateFile_NoFilePathTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'filePath' can not be null or empty.")
2018-03-09 18:58:11 +02:00
FileUtils.validateFile(script, null)
}
@Test
2018-03-09 18:58:11 +02:00
void validateFile_emptyParameterTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'filePath' can not be null or empty.")
2018-03-09 18:58:11 +02:00
FileUtils.validateFile(script, '')
}
@Test
2018-03-09 18:58:11 +02:00
void validateFile_fileDoesNotExistTest() {
def path = new File("$emptyDir", 'test').getAbsolutePath()
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, path) })
thrown.expect(AbortException)
2018-03-09 18:58:11 +02:00
thrown.expectMessage("Validation failed. '$path' does not exist.")
2018-03-09 18:58:11 +02:00
FileUtils.validateFile(script, path)
}
@Test
void validateFileTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, file) })
FileUtils.validateFile(script, file)
}
private script(parameters, path) {
if(parameters.script.contains('exists')) return directoryOrFileExists(path)
else if(parameters.script.contains('directory')) return isDirectory(path)
else if(parameters.script.contains('empty')) return isDirectoryEmpty(path)
else if(parameters.script.contains('file')) return isFile(path)
}
private directoryOrFileExists(dirOrFile) {
def file = new File(dirOrFile)
if (file.exists()) return 0
else return 1
}
private isDirectory(dir) {
def file = new File(dir)
if (file.isDirectory()) return 0
else return 1
}
2018-02-12 18:31:48 +02:00
2018-03-09 18:58:11 +02:00
private isDirectoryEmpty(dir) {
def file = new File(dir)
if (file.list().size() == 0) return 1
return 0
}
private isFile(filePath) {
def file = new File(filePath)
if (file.isFile()) return 0
return 1
}
}