From: Peter Kajsa Date: Wed, 1 Feb 2017 11:07:46 +0000 (+0100) Subject: Bug 7396 - yang-system-test could support recursive search X-Git-Tag: release/carbon~75 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=344d158b2f97fd52ef6208deeaf111b5deb0aa4d;p=yangtools.git Bug 7396 - yang-system-test could support recursive search Yang-system-test: added new option '-r' for recursively search sub-directories for yang files. Change-Id: I265c0dfa1006aee5b7bb1a85342d162866232a3b Signed-off-by: Peter Kajsa --- diff --git a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java index 94840bb0f7..748ec5f7f3 100644 --- a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java +++ b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/Main.java @@ -38,6 +38,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; * -h,--help print help message and exit. * -p,--path <arg> path is a colon (:) separated list of directories * to search for yang modules. + * -r, --recursive recursive search of directories specified by -p option * -v,--verbose shows details about the results of test running. * */ @@ -57,6 +58,11 @@ public class Main { path.setRequired(false); options.addOption(path); + final Option recursiveSearch = new Option("r", "recursive", false, + "recursive search of directories specified by -p option."); + recursiveSearch.setRequired(false); + options.addOption(recursiveSearch); + final Option verbose = new Option("v", "verbose", false, "shows details about the results of test running."); verbose.setRequired(false); options.addOption(verbose); @@ -96,11 +102,11 @@ public class Main { final List yangFiles = Arrays.asList(arguments.getArgs()); final HashSet supportedFeatures = initSupportedFeatures(arguments); - runSystemTest(yangLibDirs, yangFiles, supportedFeatures); + runSystemTest(yangLibDirs, yangFiles, supportedFeatures, arguments.hasOption("recursive")); } private static void runSystemTest(final List yangLibDirs, final List yangFiles, - final HashSet supportedFeatures) { + final HashSet supportedFeatures, final boolean recursiveSearch) { LOG.log(Level.INFO, "Yang model dirs: {0} ", yangLibDirs); LOG.log(Level.INFO, "Yang model files: {0} ", yangFiles); LOG.log(Level.INFO, "Supported features: {0} ", supportedFeatures); @@ -111,7 +117,7 @@ public class Main { final Stopwatch stopWatch = Stopwatch.createStarted(); try { - context = SystemTestUtils.parseYangSources(yangLibDirs, yangFiles, supportedFeatures); + context = SystemTestUtils.parseYangSources(yangLibDirs, yangFiles, supportedFeatures, recursiveSearch); } catch (final Exception e) { LOG.log(Level.SEVERE, "Failed to create SchemaContext.", e); System.exit(1); diff --git a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java index d8be9fd8be..e35a6e899b 100644 --- a/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java +++ b/yang/yang-system-test/src/main/java/org/opendaylight/yangtools/yang/parser/system/test/SystemTestUtils.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.parser.system.test; +import com.google.common.base.Preconditions; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; @@ -36,7 +37,7 @@ class SystemTestUtils { }; static SchemaContext parseYangSources(final List yangLibDirs, final List yangTestFiles, - final Set supportedFeatures) throws FileNotFoundException, ReactorException { + final Set supportedFeatures, final boolean recursiveSearch) throws FileNotFoundException, ReactorException { /* * Current dir "." should be always present implicitly in the list of * directories where dependencies are searched for @@ -47,7 +48,7 @@ class SystemTestUtils { final List libFiles = new ArrayList<>(); for (final String yangLibDir : yangLibDirs) { - libFiles.addAll(getYangFiles(yangLibDir)); + libFiles.addAll(getYangFiles(yangLibDir, recursiveSearch)); } final List testFiles = new ArrayList<>(); @@ -86,11 +87,29 @@ class SystemTestUtils { return yangSources; } - private static Collection getYangFiles(final String yangSourcesDirectoryPath) throws FileNotFoundException { + private static Collection getYangFiles(final String yangSourcesDirectoryPath, final boolean recursiveSearch) + throws FileNotFoundException { final File testSourcesDir = new File(yangSourcesDirectoryPath); if (testSourcesDir == null || !testSourcesDir.isDirectory()) { throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath)); } - return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER)); + + return recursiveSearch ? searchYangFiles(testSourcesDir) : Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER)); + } + + private static List searchYangFiles(final File dir) { + Preconditions.checkNotNull(dir); + Preconditions.checkArgument(dir.isDirectory(), "File %s is not a directory", dir.getPath()); + + final List yangFiles = new ArrayList<>(); + for (final File file : dir.listFiles()) { + if (file.isDirectory()) { + yangFiles.addAll(searchYangFiles(file)); + } else if (file.isFile() && file.getName().toLowerCase().endsWith(".yang")) { + yangFiles.add(file); + } + } + + return yangFiles; } }