Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3deb856
info and error messages are configurable
shamlymhd Nov 22, 2022
d3470c9
change added
shamlymhd Nov 22, 2022
94d6d18
test removed for testing
shamlymhd Nov 22, 2022
09579b5
test added
shamlymhd Nov 22, 2022
4e4f986
newly created tests are removed
shamlymhd Nov 22, 2022
122fad2
fixed
shamlymhd Nov 22, 2022
6a116d8
resolved requested changes except tests
shamlymhd Nov 23, 2022
f052000
whitespaces added
shamlymhd Nov 23, 2022
edb505c
changes added
shamlymhd Nov 23, 2022
f5e4c73
resolved duplicate string
shamlymhd Nov 23, 2022
1415e30
tests added
shamlymhd Nov 23, 2022
564aa7f
conflicts removed
shamlymhd Nov 23, 2022
cac0e5a
changes and minor fix added
shamlymhd Nov 24, 2022
e4a5739
Delete .gitignore
shamlymhd Nov 24, 2022
f06c6f4
Delete compiler.xml
shamlymhd Nov 24, 2022
d2a8f25
Delete encodings.xml
shamlymhd Nov 24, 2022
283fd59
Delete jarRepositories.xml
shamlymhd Nov 24, 2022
9e1d327
Delete misc.xml
shamlymhd Nov 24, 2022
b77c3d4
Delete vcs.xml
shamlymhd Nov 24, 2022
d1c726a
gitignore modified
shamlymhd Nov 29, 2022
990f5f1
sending the skipped files
shamlymhd Nov 29, 2022
ae1df18
Delete vcs.xml
shamlymhd Nov 29, 2022
668d8a0
Delete .gitignore
shamlymhd Nov 29, 2022
cd17d4e
Delete compiler.xml
shamlymhd Nov 29, 2022
c295e3c
Delete encodings.xml
shamlymhd Nov 29, 2022
98ce4d1
Delete jarRepositories.xml
shamlymhd Nov 29, 2022
4ec096f
Delete misc.xml
shamlymhd Nov 29, 2022
f51a488
changes
shamlymhd Nov 29, 2022
44982e0
Merge branch 'shamly-fix-i' of https://github.com/shamlymhd/plugin-ut…
shamlymhd Nov 29, 2022
fee8298
changes added
shamlymhd Nov 29, 2022
f1d2824
Merge remote-tracking branch 'origin/master' into shamly-fix-i
uhafner Nov 29, 2022
7fb1d38
Improve naming.
uhafner Nov 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/main/java/io/jenkins/plugins/util/AgentFileVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public abstract class AgentFileVisitor<T extends Serializable>
private final String filePattern;
private final String encoding;
private final boolean followSymbolicLinks;
private final boolean errorOnEmptyFiles;
private final FileSystemFacade fileSystemFacade;
private static final String EMPTY_FILE = "Skipping file '%s' because it's empty";

/**
* Creates a new instance of {@link AgentFileVisitor}.
Expand All @@ -52,20 +54,22 @@ public abstract class AgentFileVisitor<T extends Serializable>
* @param encoding
* encoding of the files to parse
* @param followSymbolicLinks
* if the scanner should traverse symbolic links
* determines whether the visitor should traverse symbolic links
* @param errorOnEmptyFiles
* determines whether the visitor should log errors if a file is empty
*/
protected AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks) {
this(filePattern, encoding, followSymbolicLinks, new FileSystemFacade());
protected AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles) {
this(filePattern, encoding, followSymbolicLinks, errorOnEmptyFiles, new FileSystemFacade());
}

@VisibleForTesting
AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks,
final FileSystemFacade fileSystemFacade) {
AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles, final FileSystemFacade fileSystemFacade) {
super();

this.filePattern = filePattern;
this.encoding = encoding;
this.followSymbolicLinks = followSymbolicLinks;
this.errorOnEmptyFiles = errorOnEmptyFiles;
this.fileSystemFacade = fileSystemFacade;
}

Expand Down Expand Up @@ -98,7 +102,12 @@ private List<T> scanFiles(final File workspace, final String[] fileNames, final
log.logError("Skipping file '%s' because Jenkins has no permission to read the file", fileName);
}
else if (fileSystemFacade.isEmpty(file)) {
log.logError("Skipping file '%s' because it's empty", fileName);
if (errorOnEmptyFiles) {
log.logError(EMPTY_FILE, fileName);
}
else {
log.logInfo(EMPTY_FILE, fileName);
}
}
else {
results.add(processFile(file, new CharsetValidation().getCharset(encoding), log));
Expand Down
51 changes: 38 additions & 13 deletions src/test/java/io/jenkins/plugins/util/AgentFileVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AgentFileVisitorTest extends SerializableTest<StringScanner> {
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReportErrorOnEmptyResults(final boolean followLinks, final String message) {
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks,
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks, true,
createFileSystemFacade(followLinks));

FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
Expand All @@ -57,7 +57,7 @@ void shouldReportErrorOnEmptyResults(final boolean followLinks, final String mes
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReturnSingleResult(final boolean followLinks, final String message) {
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks,
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks, true,
createFileSystemFacade(followLinks, "/one.txt"));

FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
Expand All @@ -75,7 +75,7 @@ void shouldReturnSingleResult(final boolean followLinks, final String message) {
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReturnMultipleResults(final boolean followLinks, final String message) {
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks,
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", followLinks, true,
createFileSystemFacade(followLinks, "/one.txt", "/two.txt"));

FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
Expand All @@ -91,20 +91,20 @@ void shouldReturnMultipleResults(final boolean followLinks, final String message
}

@Test
@DisplayName("Should handle empty or forbidden files")
void shouldReturnMultipleResults() {
@DisplayName("Should log error for empty or forbidden files")
void shouldLogErrorForEmptyAndForbiddenFiles() {
FileSystemFacade fileSystemFacade = createFileSystemFacade(true,
"/one.txt", "/two.txt", "empty.txt", "not-readable.txt");

Path empty = workspace.toPath().resolve("empty.txt");
when(fileSystemFacade.resolve(workspace, "empty.txt")).thenReturn(empty);
when(fileSystemFacade.isNotReadable(empty)).thenReturn(true);
when(fileSystemFacade.isEmpty(empty)).thenReturn(true);

Path notReadable = workspace.toPath().resolve("not-readable.txt");
when(fileSystemFacade.resolve(workspace, "not-readable.txt")).thenReturn(notReadable);
when(fileSystemFacade.isEmpty(notReadable)).thenReturn(true);
when(fileSystemFacade.isNotReadable(notReadable)).thenReturn(true);

StringScanner scanner = new StringScanner(PATTERN, "UTF-8", true,
StringScanner scanner = new StringScanner(PATTERN, "UTF-8", true, true,
fileSystemFacade);

FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
Expand All @@ -116,8 +116,33 @@ void shouldReturnMultipleResults() {
"Successfully processed file '/two.txt'");
assertThat(actualResult.hasErrors()).isTrue();
assertThat(actualResult.getLog().getErrorMessages()).containsExactly("Errors during parsing",
"Skipping file 'empty.txt' because Jenkins has no permission to read the file",
"Skipping file 'not-readable.txt' because it's empty");
"Skipping file 'empty.txt' because it's empty",
"Skipping file 'not-readable.txt' because Jenkins has no permission to read the file");
}

@Test
@DisplayName("Should skip logging of errors when parsing empty files")
void shouldSkipLoggingOfErrorsForEmptyFiles() {
FileSystemFacade fileSystemFacade = createFileSystemFacade(true,
"/one.txt", "/two.txt", "empty.txt");

Path empty = workspace.toPath().resolve("empty.txt");
when(fileSystemFacade.resolve(workspace, "empty.txt")).thenReturn(empty);
when(fileSystemFacade.isEmpty(empty)).thenReturn(true);

StringScanner scanner = new StringScanner(PATTERN, "UTF-8", true, false,
fileSystemFacade);

FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).containsExactly(CONTENT + 1, CONTENT + 2);
assertThat(actualResult.getLog().getInfoMessages()).contains(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"-> found 3 files",
"Successfully processed file '/one.txt'",
"Successfully processed file '/two.txt'",
"Skipping file 'empty.txt' because it's empty");
assertThat(actualResult.hasErrors()).isFalse();
assertThat(actualResult.getLog().getErrorMessages()).isEmpty();
}

private FileSystemFacade createFileSystemFacade(final boolean followLinks, final String... files) {
Expand All @@ -131,16 +156,16 @@ private FileSystemFacade createFileSystemFacade(final boolean followLinks, final

@Override
protected StringScanner createSerializable() {
return new StringScanner(PATTERN, "UTF-8", true, createFileSystemFacade(true));
return new StringScanner(PATTERN, "UTF-8", true, true, createFileSystemFacade(true));
}

static class StringScanner extends AgentFileVisitor<String> {
private static final long serialVersionUID = -6902473746775046311L;
private int counter = 1;

@VisibleForTesting
protected StringScanner(final String filePattern, final String encoding, final boolean followSymbolicLinks, final FileSystemFacade fileSystemFacade) {
super(filePattern, encoding, followSymbolicLinks, fileSystemFacade);
protected StringScanner(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles, final FileSystemFacade fileSystemFacade) {
super(filePattern, encoding, followSymbolicLinks, errorOnEmptyFiles, fileSystemFacade);
}

@Override
Expand Down