github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/formatter_test.go (about) 1 package controldisplay 2 3 import ( 4 "context" 5 "io" 6 "os" 7 "testing" 8 9 "github.com/turbot/steampipe/pkg/constants" 10 "github.com/turbot/steampipe/pkg/control/controlexecute" 11 "github.com/turbot/steampipe/pkg/filepaths" 12 ) 13 14 // testFormatter is an implementation of the Formatter interface 15 // values in this implementation correspond to the ones we expect in the result 16 type testFormatter struct { 17 name string 18 alias string 19 extension string 20 } 21 22 func (b *testFormatter) FileExtension() string { return b.extension } 23 func (b *testFormatter) Name() string { return b.name } 24 func (b *testFormatter) Alias() string { return b.alias } 25 func (b *testFormatter) Format(ctx context.Context, tree *controlexecute.ExecutionTree) (io.Reader, error) { 26 return nil, nil 27 } 28 29 type testCase struct { 30 input string 31 expected interface{} 32 } 33 34 var formatterTestCase = []testCase{ 35 { 36 input: "bad-format", 37 expected: "ERROR", 38 }, 39 { 40 input: "snapshot", 41 expected: testFormatter{ 42 alias: "sps", 43 extension: constants.SnapshotExtension, 44 name: constants.OutputFormatSnapshot, 45 }, 46 }, 47 { 48 input: "csv", 49 expected: testFormatter{ 50 alias: "", 51 extension: ".csv", 52 name: "csv", 53 }, 54 }, 55 { 56 input: "json", 57 expected: testFormatter{ 58 alias: "", 59 extension: ".json", 60 name: "json", 61 }, 62 }, 63 { 64 input: "asff.json", 65 expected: testFormatter{ 66 alias: "asff.json", 67 extension: ".asff.json", 68 name: "asff", 69 }, 70 }, 71 { 72 input: "nunit3.xml", 73 expected: testFormatter{ 74 alias: "nunit3.xml", 75 extension: ".nunit3.xml", 76 name: "nunit3", 77 }, 78 }, 79 } 80 81 func TestFormatResolver(t *testing.T) { 82 tmpDir, err := os.MkdirTemp(os.TempDir(), "test") 83 if err != nil { 84 t.Fatal(err) 85 } 86 defer os.RemoveAll(tmpDir) 87 filepaths.SteampipeDir = tmpDir 88 if err := EnsureTemplates(); err != nil { 89 t.Fatal(err) 90 } 91 resolver, err := NewFormatResolver(context.Background()) 92 if err != nil { 93 t.Fatal(err) 94 } 95 for _, testCase := range formatterTestCase { 96 f, err := resolver.GetFormatter(testCase.input) 97 shouldError := testCase.expected == "ERROR" 98 99 if shouldError { 100 if err == nil { 101 t.Logf("Request for '%s' should have errored - but did not", testCase.input) 102 t.Fail() 103 } 104 continue 105 } 106 107 expectedFormatter := testCase.expected.(testFormatter) 108 109 if f.Alias() != expectedFormatter.Alias() { 110 t.Logf("Alias mismatch for '%s'. Expected '%s', but got '%s'", testCase.input, expectedFormatter.Alias(), f.Alias()) 111 t.Fail() 112 continue 113 } 114 if f.FileExtension() != expectedFormatter.FileExtension() { 115 t.Logf("Extension mismatch for '%s'. Expected '%s', but got '%s'", testCase.input, expectedFormatter.FileExtension(), f.FileExtension()) 116 t.Fail() 117 continue 118 } 119 if f.Name() != expectedFormatter.Name() { 120 t.Logf("Name mismatch for '%s'. Expected '%s', but got '%s'", testCase.input, expectedFormatter.Name(), f.Name()) 121 t.Fail() 122 continue 123 } 124 } 125 }