Skip to content

Instantly share code, notes, and snippets.

@djodjoni
Created July 18, 2016 09:15
Show Gist options
  • Save djodjoni/90a8da34d737a1fee82abfd9d1cc6460 to your computer and use it in GitHub Desktop.
Save djodjoni/90a8da34d737a1fee82abfd9d1cc6460 to your computer and use it in GitHub Desktop.
gherkin format validation
import gherkin.AstBuilder;
import gherkin.Parser;
import gherkin.util.FixJava;
import java.io.*;
public class Verify {
private static boolean hasErrors = false;
public static void main(String[] args) throws Exception {
final File path;
if (args.length > 0) {
path = new File(args[0]);
} else {
String specsPath = System.getProperty("specs.path");
System.out.println(specsPath);
path = new File(specsPath);
}
check(path);
if (hasErrors) {
System.out.println("*** GHERKIN HAS SYNTAX ERRORS ***");
System.exit(1);
} else {
System.out.println("*** OK ***");
}
}
private static void check(File path) throws Exception {
if (path.isFile()) {
if (path.getName().endsWith(".feature")) {
parseFeature(path);
}
} else {
for (final File file : path.listFiles()) {
check(file);
}
}
}
private static void parseFeature(File f) throws FileNotFoundException, UnsupportedEncodingException {
String gherkin = FixJava.readReader(new InputStreamReader(
new FileInputStream(f), "UTF-8"));
StringBuilder json = new StringBuilder();
//JSONFormatter formatter = new JSONFormatter(json);
System.out.println("parsing: " + f.getAbsolutePath());
try {
Parser parser = new Parser(new AstBuilder());
parser.parse(gherkin);
} catch (Exception e) {
final String message = e.getMessage();
System.out.println(message);
hasErrors = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment