Skip to content

Commit

Permalink
chore: Improved/added tests, temporary fix for picocli option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jul 16, 2023
1 parent e0c4fe5 commit 02faac2
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
import com.fortify.cli.common.rest.unirest.GenericUnirestFactory;
import com.fortify.cli.common.variable.FcliVariableHelper;

import lombok.AccessLevel;
import lombok.Getter;
import picocli.CommandLine;

public final class DefaultFortifyCLIRunner implements IFortifyCLIRunner {
@Getter(value = AccessLevel.PRIVATE, lazy = true)
private final CommandLine commandLine = createCommandLine();
// TODO See https://github.com/remkop/picocli/issues/2066
//@Getter(value = AccessLevel.PRIVATE, lazy = true)
//private final CommandLine commandLine = createCommandLine();

private CommandLine createCommandLine() {
FortifyCLIStaticInitializer.getInstance().initialize();
Expand All @@ -42,7 +41,8 @@ private CommandLine createCommandLine() {
public int run(String... args) {
String[] resolvedArgs = FcliVariableHelper.resolveVariables(args);
FortifyCLIDynamicInitializer.getInstance().initialize(resolvedArgs);
CommandLine cl = getCommandLine();
//CommandLine cl = getCommandLine(); // TODO See https://github.com/remkop/picocli/issues/2066
CommandLine cl = createCommandLine();
cl.clearExecutionResults();
return cl.execute(resolvedArgs);
}
Expand Down
2 changes: 1 addition & 1 deletion fcli-other/fcli-functional-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ testing {
}
options {
testLogging {
showStandardStreams = true
showStandardStreams = false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.fortify.cli.ftest.core;

import com.fortify.cli.ftest._common.Fcli
import com.fortify.cli.ftest._common.Fcli.FcliResult
import com.fortify.cli.ftest._common.spec.FcliBaseSpec
import com.fortify.cli.ftest._common.spec.Prefix

// TODO Add/improve tests, and reduce duplication between OutputOptionsGetSpec/OutputOptionsListSpec.
// Some ideas, in particular for table/csv-based tests:
// - Check for array of expected words on each line, i.e. splitting the input on spaces/comma's
// - Define expected array of words for headers and particular records in fields, i.e.
// expectedWordsRecordId0, expectedWordsLastRecord, expectedWordsTableHeader, expectedWordsCsvHeader
// - Define methods for performing certain checks, i.e. hasAllWords(line, expectedWords), ...
// - Share all of the above between List/GetSpecs, for example through helper class, abstract base class,
// or combining both get and list tests in single spec?
@Prefix("core.output.get")
class OutputOptionsGetSpec extends FcliBaseSpec {
private static final FcliResult generate(String outputFormat) {
def args = ["util", "sample-data", "get", "0"]
if ( outputFormat!=null ) { args+=["-o", outputFormat] }
return Fcli.runOrFail(args)
}

def "table.no-opts"() {
when:
def result = generate("table")
then:
verifyAll(result.stdout) {
size()==2
it[0].contains('Id String value Long value Double value Boolean value Date value Date time value Nested object string value Nested object boolean value Nested string aray')
it[1].contains('0 value1 1000 0.7 true 2000-01-01 2000-01-01T00:00:00+00:00 nestedObjectValue1 true nestedArrayValue3, nestedArrayValue4')
}
}

def "table-plain.no-opts"() {
def outputArg = "table-plain"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
size()==1
it[0].contains('0 value1 1000 0.7 true 2000-01-01 2000-01-01T00:00:00+00:00 nestedObjectValue1 true nestedArrayValue3, nestedArrayValue4')
}
}

def "csv.no-opts"() {
def outputArg = "csv"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
size()==2
it[0] == 'id,stringValue,longValue,doubleValue,booleanValue,dateValue,dateTimeValue,nestedObjectStringValue,nestedObjectBooleanValue,nestedStringAray'
it[1] == '0,value1,1000,0.7,true,2000-01-01,"2000-01-01T00:00:00+00:00",nestedObjectValue1,true,"nestedArrayValue3, nestedArrayValue4"'
}
}

def "csv-plain.no-opts"() {
def outputArg = "csv-plain"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
size()==1
it[0] == '0,value1,1000,0.7,true,2000-01-01,"2000-01-01T00:00:00+00:00",nestedObjectValue1,true,"nestedArrayValue3, nestedArrayValue4"'
}
}

def "json.no-opts"() {
def outputArg = "json"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "json-flat.no-opts"() {
def outputArg = "json-flat"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "tree.no-opts"() {
def outputArg = "tree"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "tree-flat.no-opts"() {
def outputArg = "tree-flat"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "xml.no-opts"() {
def outputArg = "xml"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "xml-flat.no-opts"() {
def outputArg = "xml-flat"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "yaml.no-opts"(String outputArg) {
// For now, Yaml is the default output for get operations; if this is ever
// changed (see comment at StandardOutputConfig#details), the where-block
// below will need to be moved to the appropriate method in this spec.
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
where:
outputArg | _
"yaml" | _
null | _
}

def "yaml-flat.no-opts"() {
def outputArg = "table-plain"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
// TODO Add expectations
}
}

def "expr"() {
def outputArg = "expr={id}: {stringValue}"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
size()==1
it[0] == '0: value1'
}
}

def "json-properties"() {
def outputArg = "json-properties"
when:
def result = generate(outputArg)
then:
verifyAll(result.stdout) {
size()==20
// TODO Add expectations
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import com.fortify.cli.ftest._common.Fcli.FcliResult
import com.fortify.cli.ftest._common.spec.FcliBaseSpec
import com.fortify.cli.ftest._common.spec.Prefix

// TODO See comments in OutputOptionsGetSpec
@Prefix("core.output.list")
class ListOutputOptionsSpec extends FcliBaseSpec {
private FcliResult generate(String outputArg) {
def args = ["util", "sample-data", "list"] as String[]
if ( outputArg!=null ) { args+=["-o", outputArg] }
class OutputOptionsListSpec extends FcliBaseSpec {
private static final FcliResult generate(String outputFormat) {
def args = ["util", "sample-data", "list"]
if ( outputFormat!=null ) { args+=["-o", outputFormat] }
return Fcli.runOrFail(args)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.fortify.cli.ftest.core;

import com.fortify.cli.ftest._common.Fcli
import com.fortify.cli.ftest._common.Fcli.FcliResult
import com.fortify.cli.ftest._common.spec.FcliBaseSpec
import com.fortify.cli.ftest._common.spec.Prefix

@Prefix("core.query")
class QuerySpec extends FcliBaseSpec {
private static final FcliResult generate(String query) {
def args = ["util", "sample-data", "list"]
if ( query!=null ) { args+=["-q", query] }
return Fcli.runOrFail(args)
}

def "contains"() {
expect:
// TODO Implement this (and many other) spec, using sample data from
// "fcli util sample-data list", making sure that we cover for
// example https://github.com/fortify/fcli/issues/344
true
when:
def result = generate("{'value1',null}.contains(stringValue)")
then:
verifyAll(result.stdout) {
size()==15553
it[0].contains('Id String value Long value Double value Boolean value Date value Date time value Nested object string value Nested object boolean value Nested string aray')
it[1].contains('0 value1 1000 0.7 true 2000-01-01 2000-01-01T00:00:00+00:00 nestedObjectValue1 true nestedArrayValue3, nestedArrayValue4')
it[15552].contains('23327 N/A N/A N/A N/A N/A N/A N/A N/A')
}
}

}

0 comments on commit 02faac2

Please sign in to comment.