@@ -18,7 +18,9 @@ import (
1818 "context"
1919 "encoding/json"
2020 "errors"
21+ "fmt"
2122 "io"
23+ "os"
2224 "testing"
2325
2426 "github.com/ProspectOne/perfops-cli/perfops"
@@ -60,6 +62,8 @@ func TestRunTest(t *testing.T) {
6062 nil ,
6163 },
6264 }
65+
66+ fmt .Println ("tests" , testCases )
6367 ctx := context .Background ()
6468 for name , tc := range testCases {
6569 t .Run (name , func (t * testing.T ) {
@@ -116,6 +120,52 @@ func TestPrintOutput(t *testing.T) {
116120 }
117121}
118122
123+ func TestOutputToFile (t * testing.T ) {
124+ testCases := map [string ]struct {
125+ output func () * perfops.RunOutput
126+ exp []byte
127+ }{
128+ "all" : {
129+ func () * perfops.RunOutput {
130+ var o * perfops.RunOutput
131+ json .Unmarshal ([]byte (`{"id":"706fc55e3377104da01f05569e35a30b","items":[{"id":"bba072473bd034d432df03730e116686","result":{"output":"121","finished": true,"node":{"id":27,"as_number":12345,"latitude":22.28512548314,"longitude":114.17507171631,"country":{"id":195,"name":"Hong Kong","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"HK","iso_numeric":"344"},"city":"Hong Kong","sub_region":"Eastern Asia"},"time":1508061924.088372}}],"requested":"sendergram.com","finished":true,"elapsedTime":0.66500000000000004}` ), & o )
132+ return o
133+ },
134+ []byte ("\x1b [200DNode27, AS12345, Hong Kong, Hong Kong\n 121\n " ),
135+ },
136+ "timeout" : {
137+ func () * perfops.RunOutput {
138+ var o * perfops.RunOutput
139+ json .Unmarshal ([]byte (`{"id":"706fc55e3377104da01f05569e35a30b","items":[{"id":"bba072473bd034d432df03730e116686","result":{"output":"-2","finished": true,"node":{"id":27,"as_number":23456,"latitude":22.28512548314,"longitude":114.17507171631,"country":{"id":195,"name":"Hong Kong","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"HK","iso_numeric":"344"},"city":"Hong Kong","sub_region":"Eastern Asia"},"time":1508061924.088372}}],"requested":"sendergram.com","finished":true,"elapsedTime":0.66500000000000004}` ), & o )
140+ return o
141+ },
142+ []byte ("\x1b [200DNode27, AS23456, Hong Kong, Hong Kong\n The command timed-out. It either took too long to execute or we could not connect to your target at all.\n " ),
143+ },
144+ "array output" : {
145+ func () * perfops.RunOutput {
146+ var o * perfops.RunOutput
147+ json .Unmarshal ([]byte (`{"id":"6e0c06f7445bb8c63949f84fcdbdae55","items":[{"id":"2bff5d6b3a4df8a268afca6c60977032","result":{"dnsServer":"","node":{"as_number":197328,"id":103,"latitude":41.030549854339,"longitude":28.987083435058,"country":{"id":93,"name":"Turkey","continent":{"id":2,"name":"Asia","iso":"AS"},"iso":"TR","iso_numeric":"792","is_eu":false},"city":"Istanbul","sub_region":"Western Asia"},"finished":true,"output":["header"," 1 row", " 10 row"],"time":1539517079.937741}}],"requested":"ns2.no-ip.com","finished":true,"elapsedTime":2.21,"creditsWithdrawn":1}` ), & o )
148+ return o
149+ },
150+ []byte ("\x1b [200DNode103, AS197328, Istanbul, Turkey\n header\n 1 row\n 10 row\n " ),
151+ },
152+ }
153+ var b bytes.Buffer
154+ f := newTestFormatter (& b , false )
155+ for name , tc := range testCases {
156+ t .Run (name , func (t * testing.T ) {
157+ OutputToFile (f , tc .output (), "test.txt" )
158+ if _ , err := os .Stat ("test.txt" ); err != nil {
159+ t .Error ("File was not created" )
160+ }
161+ err := os .Remove ("test.txt" )
162+ if err != nil {
163+ return
164+ }
165+ })
166+ }
167+ }
168+
119169type testTerminalWriter struct {
120170 io.Writer
121171}
0 commit comments