GraphWalker / graphwalker-project

This is the repo for the Model-based testing tool GraphWalker.

Home Page:http://graphwalker.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Add filter to exclude processing non-supported model files

neofreko opened this issue · comments

Within my project, I have some test data which I put into test/resources. It turned out graphwalker:generate-test-sources takes all files in the test/resources and tries to generate model code for it. For couple of time, it didn't give us any issue as it were able to generate non-faulty file from not-intended-for-graphwalker files. However, we start getting issue recently due to the inclusion of html files.

I tried peeking into the source code but found no parameter to provide to exclude those files for graphwalker. Therefore, I file this feature request. To be more constructive, what is the recommended technical detail of adding such feature?

Note:
Thanks for the awesome project, by the way. It change the way how my project implement test automation.

Not sure there are recommendations regarding details in a feature request, or I forgot =)

But if you could create a small sample project that shows this unwanted behaviour, to start with, it would be great. I guess you could do A or B below, and post the link here.

A. Create a small project, the neofreko style, add some files so the issue occurs
B. Fork the grapwalker-example repo, make a branch, add files so that issue occurs

I think the problem described is reproducible like this:

mvn archetype:generate -B -DarchetypeGroupId=org.graphwalker -DarchetypeArtifactId=graphwalker-maven-archetype -DarchetypeVersion=4.2.0 -DgroupId=com.company -DartifactId=myProject
cd myProject/
mkdir src/main/resources/img
touch src/main/resources/img/button.png
mvn package

The last maven command generates 1 error:

[ERROR] No suitable context factory found for file: <SOME PATH>/myProject/src/main/resources/img/button.png

If I understand @neofreko correctly, the feature request asks that it should be possible to filter unwanted file types, or folders, so the error above is not generated.

One way of telling graphwalker what to filter, would perhaps to add that to the pom.xml,
using the created pom in the example above as an example:

  <build>
    <plugins>
     <plugin>
        <groupId>org.graphwalker</groupId>
        <artifactId>graphwalker-maven-plugin</artifactId>
        <version>${graphwalker.version}</version>
        <!-- Bind goals to the default lifecycle -->
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate-sources</goal>
            </goals>
            <configuration>
              <excludes>
                <exclude>img/**</exclude>
                <exclude>**/*.png</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

But of course we need to implement the filtering asked for from the pom file.
I see that we have a method in our maven plugin code that filters for files, but the method findFiles is unused anywhere in our code. Maybe the intention once was to add possibility to filter files?

protected Set<File> findFiles(String includes, String excludes, File... directories) {
Set<File> files = new HashSet<>();
for (File directory : directories) {
if (directory.exists()) {
try {
for (Object filename : FileUtils.getFileNames(directory, includes, excludes, true, true)) {
files.add(new File((String) filename));
}
} catch (Throwable t) {
getLog().debug(t);
}
}
}
return files;
}

@nilols Thank you for the suggestion. @KristianKarl has pretty much nailed the example.

I get the picture now. I'm going to take a shot at adding the feature sometime soon 🤞

There might be a bug here too, because each context factory should only handle the files it accept, and there is no context factory for either html or png, so it shouldn't be a problem.

But of course we should also accept configuration to reduce the number of files processed.

I also noticed that the validation mojo is not implemented =/

I did some work in this issue. I created PR #258
It does not add any filtering, but rather changes the ERROR to an INFO log statement. And, it adds INFO regarding valid models found and prints out which sources was created. I think that it will be somewhat more helpful.

Below is an example generating sources from the PetClinic example project:

$ ./mvnw clean graphwalker:generate-sources
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< org.graphwalker.example:java-petclinic >---------------
[INFO] Building GraphWalker Pet Clinic Example 4.4.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ java-petclinic ---
[INFO] Deleting /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target
[INFO] 
[INFO] --- graphwalker-maven-plugin:4.4.0-SNAPSHOT:generate-sources (default-cli) @ java-petclinic ---
[INFO] No suitable context factory found for file: /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/src/main/resources/someFile.txt
[INFO] Source generated from: /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/src/main/resources/com/company/PetClinic.json -> 
[INFO]                        /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target/generated-sources/graphwalker/com/company/FindOwners.java
[INFO]                        /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target/generated-sources/graphwalker/com/company/NewOwner.java
[INFO]                        /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target/generated-sources/graphwalker/com/company/OwnerInformation.java
[INFO]                        /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target/generated-sources/graphwalker/com/company/PetClinic.java
[INFO]                        /home/krikar/dev/mbt/graphwalker/graphwalker-example/java-petclinic/target/generated-sources/graphwalker/com/company/Veterinarians.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.712 s
[INFO] Finished at: 2021-02-26T08:31:44+01:00
[INFO] ------------------------------------------------------------------------

The challenge with CodeGenerator.generate method is that a directory is passed. So filtering out files inside that directory is a bit tricky.

In short, I hope this solution will remove the unfortunate ERROR statement, and provide the user with a better understanding of whats happening.

@neofreko would the above fix solve your problem?

@KristianKarl Thanks alot! This will be sufficient.