-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from jenkinsci/check-file-params
validate file based arguments point to existing files / directories
- Loading branch information
Showing
5 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Xmx128m -XX:+HeapDumpOnOutOfMemoryError -XX:+TieredCompilation -XX:TieredStopAtLevel=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/jenkins/tools/test/picocli/ExistingFileTypeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.jenkins.tools.test.picocli; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.io.File; | ||
import picocli.CommandLine.ITypeConverter; | ||
import picocli.CommandLine.TypeConversionException; | ||
|
||
/** Converter that converts to a File that must exist (either as a directory or a file) */ | ||
public class ExistingFileTypeConverter implements ITypeConverter<File> { | ||
|
||
@SuppressFBWarnings( | ||
value = "PATH_TRAVERSAL_IN", | ||
justification = "by deign, we are converting an argument from the CLI") | ||
@Override | ||
public File convert(String value) throws Exception { | ||
File f = new File(value); | ||
if (!f.exists()) { | ||
throw new TypeConversionException("Specified file " + value + " does not exist"); | ||
} | ||
return f; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/org/jenkins/tools/test/picocli/ExistingFileTypeConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.jenkins.tools.test.picocli; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.File; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import picocli.CommandLine.TypeConversionException; | ||
|
||
class ExistingFileTypeConverterTest { | ||
|
||
@Test | ||
void testValidFile(@TempDir File f) throws Exception { | ||
ExistingFileTypeConverter converter = new ExistingFileTypeConverter(); | ||
File converted = converter.convert(f.getPath()); | ||
assertThat(converted, is(f)); | ||
} | ||
|
||
@Test | ||
void testMissingFile(@TempDir File f) throws Exception { | ||
ExistingFileTypeConverter converter = new ExistingFileTypeConverter(); | ||
TypeConversionException tce = | ||
assertThrows( | ||
TypeConversionException.class, | ||
() -> converter.convert(new File(f, "whatever").getPath())); | ||
assertThat(tce.getMessage(), containsString("whatever")); | ||
} | ||
} |