joelittlejohn / embedmongo-maven-plugin

Maven plugin wrapper for the flapdoodle.de embedded MongoDB API
Apache License 2.0
88 stars 51 forks source link

mongo-scripts trys to execute directories #49

Closed scattym closed 8 years ago

scattym commented 8 years ago

I have this plugin working via Jenkins however when I create a directory for scripts to run from, during the mongo-scripts goal, our version control repository automatically adds a directory called .svn.

So I have /var/builduser/workspace/fil-jobq/src/test/resources/mongodb_init_scripts/create_user.js /var/builduser/workspace/fil-jobq/src/test/resources/mongodb_init_scripts/.svn

As a result, the plugin tries to execute the directory as if it were a file and fails the build:

[ERROR] Failed to execute goal com.github.joelittlejohn.embedmongo:embedmongo-maven-plugin:0.3.0:mongo-scripts (mongo-scripts) on project fil-jobq: Unable to find file with name '.svn': /var/builduser/workspace/fil-jobq/src/test/resources/mongodb_init_scripts/.svn (Is a directory) -> [Help 1]

Is there a way to wildcard the directory, like /some/dir/*.js

Alternatively, is there a way to create a user via import mechanism? i.e. using the json file to create a user...

Thanks, Matt.

scattym commented 8 years ago

Problem seems to be that there is no check on the file type in the for loop that goes over the directory listing. Probably needs an isFile() check before trying to open it as a file.

Excerpt from MongoScriptsMojo.java

    if (scriptsDirectory.isDirectory()) {
        Scanner scanner = null;
        StringBuilder instructions = new StringBuilder();
        File[] files = scriptsDirectory.listFiles();

        if (files == null) {
            getLog().info("Can't read scripts directory: " + scriptsDirectory.getAbsolutePath());

        } else {
            getLog().info("Folder " + scriptsDirectory.getAbsolutePath() + " contains " + files.length + " file(s):");

            for (File file : files) {
                try {
                    scanner = new Scanner(file);
                    while (scanner.hasNextLine()) {
                        instructions.append(scanner.nextLine()).append("\n");
                    }
                } catch (FileNotFoundException e) {
                    throw new MojoExecutionException("Unable to find file with name '" + file.getName() + "'", e);
                } finally {
                    if (scanner != null) {
                        scanner.close();
                    }
                }
joelittlejohn commented 8 years ago

Good catch, thanks for reporting this. Feel free to submit a pull request for this if you want to. Otherwise, thanks for the diagnosis!