provide better error messages for malformed yaml

This commit is contained in:
Jonathan Shook 2021-04-13 19:42:13 -05:00
parent 33184e1182
commit df038f0cd9
6 changed files with 76 additions and 11 deletions

View File

@ -220,7 +220,7 @@ public class NBIO implements NBPathsAPI.Facets {
List<Content<?>> list = list();
if (list.size() > 1) {
throw new BasicError("Found more than one source for " + this.toString() + ", but expected to find one at" +
throw new BasicError("Found more than one source for " + this + ", but expected to find one at" +
" most.");
}
throw new RuntimeException("Invalid code, go fix it, this should never happen.");
@ -232,12 +232,12 @@ public class NBIO implements NBPathsAPI.Facets {
List<Content<?>> list = list();
if (list.size() == 0) {
throw new BasicError("Unable to find even a single source for '" + this.toString() + "'");
throw new BasicError("Unable to find even a single source for '" + this + "'");
}
if (list.size() > 1) {
String found = list.stream().map(c -> c.getURI().toString()).collect(Collectors.joining(","));
throw new BasicError(("Found too many sources for '" + this.toString() + "', ambiguous name. Pick from " + found));
throw new BasicError(("Found too many sources for '" + this + "', ambiguous name. Pick from " + found));
}
return list.get(0);
@ -309,15 +309,16 @@ public class NBIO implements NBPathsAPI.Facets {
searches.add(".*");
}
for (String prefix : prefixes) {
List<Path> founds = resolver.resolveDirectory(prefix);
List<Path> directories = resolver.resolveDirectory(prefix);
NBIOWalker.CollectVisitor capture = new NBIOWalker.CollectVisitor(true,false);
for (Path path : founds) {
for (Path dirPath : directories) {
for (String searchPattern : searches) {
NBIOWalker.RegexFilter filter = new NBIOWalker.RegexFilter(searchPattern,true);
NBIOWalker.PathSuffixFilter filter = new NBIOWalker.PathSuffixFilter(searchPattern);
//NBIOWalker.RegexFilter filter = new NBIOWalker.RegexFilter(searchPattern,false);
// RegexPathFilter filter = new RegexPathFilter(searchPattern, true);
NBIOWalker.walkFullPath(path, capture, filter);
NBIOWalker.walkFullPath(dirPath, capture, filter);
}
}
capture.get().stream().map(PathContent::new).forEach(foundFiles::add);

View File

@ -34,8 +34,8 @@ public class NBIOWalker {
* This form uses only the filename component in Paths to be matched by the filter, and the short name is also
* what is returned by the filter.
*
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param filter The Path filter to determine whether a path is included
*/
public static void walkShortPath(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
@ -48,8 +48,8 @@ public class NBIOWalker {
* This form uses only the full path from the initial search path root in all Paths to be matched by
* the filter, and this form of a Path component is also returned in all Paths seen by the visitor.
*
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param filter The Path filter to determine whether a path is included
*/
public static void walkFullPath(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
@ -159,6 +159,32 @@ public class NBIOWalker {
}
}
public static class PathSuffixFilter implements DirectoryStream.Filter<Path> {
private final Pattern[] pathRegexes;
public PathSuffixFilter(String filename) {
Path parts = Path.of(filename);
pathRegexes = new Pattern[parts.getNameCount()];
for (int i = 0; i < parts.getNameCount(); i++) {
pathRegexes[i]=Pattern.compile(parts.getName(i).toString());
}
}
@Override
public boolean accept(Path entry) throws IOException {
int offset = entry.getNameCount()-pathRegexes.length;
if (offset<0) {
return false;
}
for (int i = 0; i < pathRegexes.length; i++) {
if (!pathRegexes[i].matcher(entry.getName(i+offset).toString()).matches()) {
return false;
}
}
return true;
}
}
public static class RegexFilter implements DirectoryStream.Filter<Path> {
private final Pattern regex;

View File

@ -89,6 +89,9 @@ public class NBIOTest {
Optional<Content<?>> testcsv1 = forCsvExtension.first();
assertThat(testcsv1).isNotPresent();
List<Content<?>> list = forCsvExtension.list();
assertThat(list).hasSize(0);
}
@Test
@ -190,4 +193,33 @@ public class NBIOTest {
System.out.println("found " + list.size() + " entries for path '.'");
assertThat(list).hasSize(0);
}
@Test
public void test() {
List<Content<?>> list = NBIO.fs()
.prefix(Paths.get("target/test-classes/").toString())
.name("gamma.yaml").list();
assertThat(list).hasSize(1);
}
@Test
public void testWildcardFilenameMatch() {
NBPathsAPI.DoSearch gammasSearch = NBIO.all()
.prefix(Paths.get("target/test-classes/").toString())
.name(".*gamma")
.extension("yaml");
List<Content<?>> gammas = gammasSearch.list();
assertThat(gammas).hasSize(3);
}
@Test
public void testSpecificFilenameMatch() {
NBPathsAPI.DoSearch gammasSearch = NBIO.all()
.prefix(Paths.get("target/test-classes/").toString())
.name("gamma")
.extension("yaml");
List<Content<?>> gammas = gammasSearch.list();
assertThat(gammas).hasSize(1);
}
}

View File

@ -0,0 +1,2 @@
# This is just a testing file.
description: This is just an alpha-gamma testing file in nesteddir1

View File

@ -0,0 +1,2 @@
# This is just a testing file.
description: This is just an alpha-gamma testing file in nesteddir1/nesteddir2

View File

@ -0,0 +1,2 @@
# This is just a testing file.
description: This is just a gamma testing file