Skip to content

Commit

Permalink
Merge pull request #1877 from sixlettervariables/run-board-validator-…
Browse files Browse the repository at this point in the history
…on-push

Update BoardsValidator and run it on Push/PR with boards
  • Loading branch information
sixlettervariables committed May 21, 2020
2 parents ca0892c + 52c7a96 commit 715c62d
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 17 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/validate-boards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Validates Boards
#
# Jobs:
#
# - board_validator: Validate boards in MM
#

name: Validate Boards

#
# This Action Definition should be triggered only on
# master being updated or Pull Requests being added
# or updated against master.
#
on:
push:
branches: [ master ]
paths:
# This set of paths is the reasonable set which
# should trigger a validation of the boards in MM.
# If any changes are made to Board that use
# a new class, update these blocks.
- '**.board'
- 'megamek/src/megamek/common/Board.java'
- 'megamek/src/megamek/common/Building.java'
- 'megamek/src/megamek/common/Terrains.java'
- 'megamek/src/megamek/utils/BoardsValidator.java'
pull_request:
branches: [ master ]
paths:
- '**.board'
- 'megamek/src/megamek/common/Board.java'
- 'megamek/src/megamek/common/Building.java'
- 'megamek/src/megamek/common/Terrains.java'
- 'megamek/src/megamek/utils/BoardsValidator.java'

jobs:

board_validator:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Grant execute permission for gradlew (*nix or MacOS)
run: chmod +x gradlew

#
# Build the MegaMek project
#
- name: Create MM Jar
run: ./gradlew jar --stacktrace

#
# Run the Boards Validator
#
- name: Validate Boards
working-directory: megamek
# Runs the BoardsValidator in default mode, which rips through
# the ./data directory of the current working directory.
#
# The -q flag tells the validator to only print the filenames
# that are invalid.
run: java -cp build/libs/MegaMek.jar megamek.utils.BoardsValidator -q
114 changes: 97 additions & 17 deletions megamek/src/megamek/utils/BoardsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import megamek.common.Board;
import megamek.common.Configuration;
Expand All @@ -31,10 +35,18 @@
*/
public class BoardsValidator {

int numBoardErrors = 0;
private int numBoardErrors = 0;
private boolean isVerbose;

public BoardsValidator() {

/**
* Sets a value indicating whether a full listing of errors
* should be printed when validating a board.
* @param b {@code true} if the specific errors for each board
* should be printed, otherwise {@code false} for just
* the file name.
*/
public void setIsVerbose(boolean b) {
isVerbose = b;
}

/**
Expand Down Expand Up @@ -67,20 +79,32 @@ private void scanForBoards(File file) throws IOException {
* @param boardFile
* @throws FileNotFoundException
*/
private void validateBoard(File boardFile) throws FileNotFoundException {
private void validateBoard(File boardFile) throws FileNotFoundException, IOException {
// If this isn't a board, ignore it
if (!boardFile.toString().endsWith(".board")) {
return;
}

java.io.InputStream is = new FileInputStream(boardFile);
StringBuffer errBuff = new StringBuffer();
Board b = new Board();
b.load(is, errBuff, false);
if (errBuff.length() > 0) {
System.out.println("Found Invalid Board! Board: " + boardFile);
System.out.println(errBuff);
numBoardErrors++;
try (java.io.InputStream is = new FileInputStream(boardFile)) {
StringBuffer errBuff = new StringBuffer();
Board b = new Board();

try {
b.load(is, errBuff, false);
} catch (Exception e) {
errBuff.append(e.toString());
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
errBuff.append(writer.toString());
}

if (errBuff.length() > 0) {
numBoardErrors++;
System.out.println("Found Invalid Board! Board: " + boardFile);
if (isVerbose) {
System.out.println(errBuff);
}
}
}
}

Expand All @@ -89,15 +113,71 @@ private void validateBoard(File boardFile) throws FileNotFoundException {
* @param args
*/
public static void main(String[] args) {
Args a = Args.parse(args);
if (a.showHelp) {
System.out.println("Usage: java -cp MegaMek.jar megamek.utils.BoardsValidator [OPTIONS] [paths]");
System.out.println();
System.out.println(" -q, --quiet Only print invalid file names.");
System.out.println(" -?, -h, --help Show this message and quit.");
System.out.println();
System.out.println("Examples:");
System.out.println();
System.out.println("Validate every board in the ./data subdirectory of the");
System.out.println("current working directory:");
System.out.println();
System.out.println(" > java -cp MegaMek.jar megamek.utils.BoardsValidator");
System.out.println();
System.out.println("Validate a given board:");
System.out.println();
System.out.println(" > java -cp MegaMek.jar megamek.utils.BoardsValidator SomeFile.board");
System.out.println();
System.out.println("Validate a directory of boards:");
System.out.println();
System.out.println(" > java -cp MegaMek.jar megamek.utils.BoardsValidator SomeFiles");
System.out.println();
System.exit(2);
return;
}

BoardsValidator validator = new BoardsValidator();
validator.setIsVerbose(!a.isQuiet);

try {
File boardDir = Configuration.boardsDir();
BoardsValidator validator = new BoardsValidator();

validator.scanForBoards(boardDir);
if (a.paths.size() == 0) {
File boardDir = Configuration.boardsDir();
validator.scanForBoards(boardDir);
} else {
for (String path : a.paths) {
validator.scanForBoards(new File(path));
}
}

System.out.println("Found " + validator.numBoardErrors + " boards with errors!");
}catch (IOException e){
System.exit(validator.numBoardErrors > 0 ? 1 : 0);
} catch (IOException e) {
System.out.println("IOException!");
e.printStackTrace();
System.exit(64);
}
}

private static class Args {
public boolean showHelp;
public boolean isQuiet;
public List<String> paths = new ArrayList<>();

private static Args parse(String[] args) {
Args a = new Args();
for (String arg : args) {
if ("-?".equals(arg) || "-h".equals(arg) || "--help".equals(arg)) {
a.showHelp = true;
} else if ("-q".equals(arg) || "--quiet".equals(arg)) {
a.isQuiet = true;
} else {
a.paths.add(arg);
}
}
return a;
}
}
}

0 comments on commit 715c62d

Please sign in to comment.