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

180 lines
4.8 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.BasePiperTest
2018-03-09 18:58:11 +02:00
import util.Rules
import hudson.AbortException
2017-07-11 15:12:03 +02:00
class FileUtilsTest extends BasePiperTest {
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-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()
}
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.")
FileUtils.validateDirectory(nullScript, 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.")
FileUtils.validateDirectory(nullScript, '')
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
FileUtils.validateDirectory(nullScript, 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
FileUtils.validateDirectory(nullScript, 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(nullScript, 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
FileUtils.validateDirectoryIsNotEmpty(nullScript, 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(nullScript, 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.")
FileUtils.validateFile(nullScript, 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.")
FileUtils.validateFile(nullScript, '')
}
@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.")
FileUtils.validateFile(nullScript, path)
}
@Test
void validateFileTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> script(m, file) })
FileUtils.validateFile(nullScript, file)
2018-03-09 18:58:11 +02:00
}
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
}
}