Skip to content

Commit

Permalink
Remove non-printable non-ASCII characters from test output
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Jan 21, 2024
1 parent 9c6fbd5 commit 9292c8b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
45 changes: 44 additions & 1 deletion ret-cli/src/test/java/cool/rdf/ret/cli/CliRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import java.util.stream.Stream;

/**
* This method uses {@link System#getSecurityManager()} and {@link System#setSecurityManager(SecurityManager)} that
* Convenient test runner to execute a command line interface either via main class, executable jar or native binary.
* Arguments and stdin can be provided, return code, stderr and stdout are captured.
* This class uses {@link System#getSecurityManager()} and {@link System#setSecurityManager(SecurityManager)} that
* are deprecated as of Java 17. However, until
* <a href="https://bugs.openjdk.java.net/browse/JDK-8199704">JDK-8199704</a> is addressed,
* we have still to rely on it.
Expand All @@ -50,10 +52,29 @@
public class CliRunner {
private static final Logger LOG = LoggerFactory.getLogger( CliRunner.class );

/**
* Represents the content of a stream (stdin/stdout/stderr)
*
* @param raw the raw byte content
*/
record StreamContent( byte[] raw ) {
/**
* Returns the stream content as UTF-8 string
*
* @return the stream content as string
*/
public String asString() {
return new String( raw, StandardCharsets.UTF_8 );
}

/**
* Returns the stream content as string with all non-printable non-ASCII characters removed
*
* @return the stream without non-printable non-ASCII characters
*/
public String cleaned() {
return asString().replaceAll( "\\P{Print}", "" );
}
}

record ExecArguments( List<String> arguments, StreamContent stdin, File workingDirectory ) {
Expand Down Expand Up @@ -114,6 +135,13 @@ public ByteArrayOutputStream call() throws Exception {
}
}

/**
* Executes a command line tool via its main class. This also captures any System.exit() calls that might be done.
*
* @param clazz the class
* @param execArguments the arguments
* @return the execution result
*/
public static Result runMainClass( final Class<?> clazz, final ExecArguments execArguments ) {
final SecurityManager originalSecurityManager = System.getSecurityManager();
final CaptureSystemExitSecurityManager securityManager = new CaptureSystemExitSecurityManager( originalSecurityManager );
Expand Down Expand Up @@ -164,6 +192,14 @@ public static Result runMainClass( final Class<?> clazz, final ExecArguments exe
}


/**
* Executes a command line interface via its executable jar
*
* @param jarFile the jar file
* @param execArguments the arguments
* @param jvmArguments additional arguments that should be passed to the Java call
* @return the execution result
*/
public static Result runJar( final File jarFile, final ExecArguments execArguments, final List<String> jvmArguments ) {
final File javaBinary = new File( ProcessHandle.current().info().command().orElse( "java" ) );

Expand All @@ -176,6 +212,13 @@ public static Result runJar( final File jarFile, final ExecArguments execArgumen
return runBinary( javaBinary, new ExecArguments( javaExecArguments, execArguments.stdin(), execArguments.workingDirectory() ) );
}

/**
* Executes a command line interface via its native executable
*
* @param binary the executable file
* @param executionArgument the arguments
* @return the execution result
*/
public static Result runBinary( final File binary, final ExecArguments executionArgument ) {
LOG.debug( "Executing command (in {}): \"{}\" {}",
executionArgument.workingDirectory(), binary, executionArgument.arguments().stream()
Expand Down
36 changes: 18 additions & 18 deletions ret-cli/src/test/java/cool/rdf/ret/cli/RetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ static void afterAll() {
void testNoArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( List.of() ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdOut().asString() ).as( "stdout" ).contains( "Usage:" );
assertThat( result.stdOut().cleaned() ).as( "stdout" ).contains( "Usage:" );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
}

@Test
void testHelp() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( "--help" ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdOut().asString() ).as( "stdout" ).contains( "Usage:" );
assertThat( result.stdOut().cleaned() ).as( "stdout" ).contains( "Usage:" );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
}

Expand All @@ -97,16 +97,16 @@ void testInvalidArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( "invalid_argument" ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( ERROR );
assertThat( result.stdOut().raw() ).as( "stdout" ).isEmpty();
assertThat( result.stdErr().asString() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().cleaned() ).as( "stderr" ).contains( "Error:" );
}

@Test
void testHelpDiagram() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( "help", RetDiagram.COMMAND_NAME ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdOut().asString() ).as( "stdout" )
assertThat( result.stdOut().cleaned() ).as( "stdout" )
.contains( "Usage: " + Ret.COMMAND_NAME + " " + RetDiagram.COMMAND_NAME );
assertThat( result.stdOut().asString() ).as( "stdout" ).contains( "--direction=" );
assertThat( result.stdOut().cleaned() ).as( "stdout" ).contains( "--direction=" );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
}

Expand All @@ -115,8 +115,8 @@ void testDiagramWithoutArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( RetDiagram.COMMAND_NAME ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( ERROR );
assertThat( result.stdOut().raw() ).as( "stdout" ).isEmpty();
assertThat( result.stdErr().asString() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().asString() ).as( "stderr" )
assertThat( result.stdErr().cleaned() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().cleaned() ).as( "stderr" )
.contains( "Usage: " + Ret.COMMAND_NAME + " " + RetDiagram.COMMAND_NAME );
}

Expand All @@ -125,7 +125,7 @@ void testDiagramWithInvalidArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( RetDiagram.COMMAND_NAME, "invalid_argument" ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( ERROR );
assertThat( result.stdOut().raw() ).as( "stdout" ).isEmpty();
assertThat( result.stdErr().asString() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().cleaned() ).as( "stderr" ).contains( "Error:" );
}

@ParameterizedTest
Expand Down Expand Up @@ -153,9 +153,9 @@ void testDiagramGeneration( final String testFileName ) throws IOException {
void testHelpWrite() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( "help", RetWrite.COMMAND_NAME ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdOut().asString() ).as( "stdout" )
assertThat( result.stdOut().cleaned() ).as( "stdout" )
.contains( "Usage: " + Ret.COMMAND_NAME + " " + RetWrite.COMMAND_NAME );
assertThat( result.stdOut().asString() ).as( "stdout" ).contains( "--alignObjects" );
assertThat( result.stdOut().cleaned() ).as( "stdout" ).contains( "--alignObjects" );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
}

Expand All @@ -164,8 +164,8 @@ void testWriteWithoutArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( RetWrite.COMMAND_NAME ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( ERROR );
assertThat( result.stdOut().raw() ).as( "stdout" ).isEmpty();
assertThat( result.stdErr().asString() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().asString() ).as( "stderr" )
assertThat( result.stdErr().cleaned() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().cleaned() ).as( "stderr" )
.contains( "Usage: " + Ret.COMMAND_NAME + " " + RetWrite.COMMAND_NAME );
}

Expand All @@ -174,7 +174,7 @@ void testWriteWithInvalidArguments() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( RetWrite.COMMAND_NAME, "invalid_argument" ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( ERROR );
assertThat( result.stdOut().raw() ).as( "stdout" ).isEmpty();
assertThat( result.stdErr().asString() ).as( "stderr" ).contains( "Error:" );
assertThat( result.stdErr().cleaned() ).as( "stderr" ).contains( "Error:" );
}

@Test
Expand All @@ -198,7 +198,7 @@ void testWriteTurtle() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( arguments, stdin ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
assertThatCode( () -> loadTurtle( result.stdOut().asString() ) ).doesNotThrowAnyException();
assertThatCode( () -> loadTurtle( result.stdOut().cleaned() ) ).doesNotThrowAnyException();
}

@Test
Expand Down Expand Up @@ -235,7 +235,7 @@ void testWriteRdfXml() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( arguments, stdin ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
assertThatCode( () -> load( result.stdOut().asString(), Lang.RDFXML ) ).doesNotThrowAnyException();
assertThatCode( () -> load( result.stdOut().cleaned(), Lang.RDFXML ) ).doesNotThrowAnyException();
}

@Test
Expand All @@ -255,7 +255,7 @@ void testWriteNTriple() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( arguments, stdin ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
assertThatCode( () -> load( result.stdOut().asString(), Lang.NTRIPLES ) ).doesNotThrowAnyException();
assertThatCode( () -> load( result.stdOut().cleaned(), Lang.NTRIPLES ) ).doesNotThrowAnyException();
}

@Test
Expand All @@ -272,7 +272,7 @@ void testWriteTurtleWithEmptyBase() {
final CliRunner.Result result = runCli( new CliRunner.ExecArguments( arguments, stdin ) );
assertThat( result.exitStatus() ).as( "command return code" ).isEqualTo( OK );
assertThat( result.stdErr().raw() ).as( "stderr" ).isEmpty();
assertThatCode( () -> loadTurtle( result.stdOut().asString() ) ).doesNotThrowAnyException();
assertThat( result.stdOut().asString() ).isEqualToIgnoringWhitespace( turtleDocument );
assertThatCode( () -> loadTurtle( result.stdOut().cleaned() ) ).doesNotThrowAnyException();
assertThat( result.stdOut().cleaned() ).isEqualToIgnoringWhitespace( turtleDocument );
}
}

0 comments on commit 9292c8b

Please sign in to comment.