Skip to content

Commit

Permalink
test(output): console
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Dec 25, 2021
1 parent 427e79e commit 1b47dd2
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var scanCmd = &cobra.Command{
}

func runScan() {
fmt.Printf(color.WhiteString("Running scan for phone number %s...\n"), inputNumber)
fmt.Printf(color.WhiteString("Running scan for phone number %s...\n\n"), inputNumber)

if valid := number.IsValid(inputNumber); !valid {
logrus.WithFields(map[string]interface{}{
Expand All @@ -53,7 +53,7 @@ func runScan() {

result, errs := remoteLibrary.Scan(num)

err = output.GetOutput(output.Console).Write(result, errs)
err = output.GetOutput(output.Console, os.Stdout).Write(result, errs)
if err != nil {
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
Expand Down
26 changes: 16 additions & 10 deletions lib/output/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package output

import (
"fmt"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"io"
"reflect"
)

type ConsoleOutput struct{}
type ConsoleOutput struct {
w io.Writer
}

func NewConsoleOutput() *ConsoleOutput {
return &ConsoleOutput{}
func NewConsoleOutput(w io.Writer) *ConsoleOutput {
return &ConsoleOutput{w: w}
}

func (o *ConsoleOutput) Write(result map[string]interface{}, errs map[string]error) error {
Expand All @@ -18,7 +22,7 @@ func (o *ConsoleOutput) Write(result map[string]interface{}, errs map[string]err
logrus.WithField("name", name).Debug("Scanner returned result <nil>")
continue
}
fmt.Printf("\nResults for %s\n", name)
_, _ = fmt.Fprintf(o.w, color.WhiteString("Results for %s\n"), name)
typeOf := reflect.TypeOf(res)
for i := 0; i < typeOf.NumField(); i++ {
v := reflect.ValueOf(res).FieldByName(typeOf.Field(i).Name)
Expand All @@ -30,18 +34,20 @@ func (o *ConsoleOutput) Write(result map[string]interface{}, errs map[string]err
}).Debug("Console field was ignored")
continue
}
fmt.Printf("%s: %v\n", field, fmt.Sprintf("%v", v))
_, _ = fmt.Fprintf(o.w, "%s: %v\n", field, fmt.Sprintf("%v", v))
}
_, _ = fmt.Fprintf(o.w, "\n")
}

if len(errs) > 0 {
fmt.Println("\nThe following scanners returned errors:")
}
for name, err := range errs {
fmt.Printf("%s: %s\n", name, err)
_, _ = fmt.Fprintln(o.w, "The following scanners returned errors:")
for name, err := range errs {
_, _ = fmt.Fprintf(o.w, "%s: %s\n", name, err)
}
_, _ = fmt.Fprintf(o.w, "\n")
}

fmt.Printf("\n%d scanner(s) succeeded\n", len(result))
_, _ = fmt.Fprintf(o.w, "%d scanner(s) succeeded\n", len(result))

return nil
}
102 changes: 102 additions & 0 deletions lib/output/console_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package output

import (
"bytes"
"errors"
"github.com/stretchr/testify/assert"
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
"github.com/sundowndev/phoneinfoga/v2/test/goldenfile"
"os"
"testing"
)

func TestConsoleOutput(t *testing.T) {
testcases := []struct {
name string
dirName string
result map[string]interface{}
errs map[string]error
wantErr error
}{
{
name: "should produce empty output",
dirName: "testdata/console_empty.txt",
result: map[string]interface{}{},
errs: map[string]error{},
},
{
name: "should produce valid output",
dirName: "testdata/console_valid.txt",
result: map[string]interface{}{
"numverify": remote.NumverifyScannerResponse{
Valid: true,
Number: "test",
LocalFormat: "test",
InternationalFormat: "test",
CountryPrefix: "test",
CountryCode: "test",
CountryName: "test",
Location: "test",
Carrier: "test",
LineType: "test",
},
},
errs: map[string]error{},
},
{
name: "should produce valid output with errors",
dirName: "testdata/console_valid_with_errors.txt",
result: map[string]interface{}{
"testscanner": nil,
"numverify": remote.NumverifyScannerResponse{
Valid: true,
Number: "test",
LocalFormat: "test",
InternationalFormat: "test",
CountryPrefix: "test",
CountryCode: "test",
CountryName: "test",
Location: "test",
Carrier: "test",
LineType: "test",
},
},
errs: map[string]error{
"googlesearch": errors.New("dummy error"),
"fakescanner": errors.New("dummy error 2"),
},
},
}

for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
shouldUpdate := tt.dirName == *goldenfile.Update

expected, err := os.ReadFile(tt.dirName)
if err != nil && !shouldUpdate {
t.Fatal(err)
}

got := new(bytes.Buffer)
err = GetOutput(Console, got).Write(tt.result, tt.errs)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
} else {
assert.Nil(t, err)
}

if shouldUpdate {
err = os.WriteFile(tt.dirName, got.Bytes(), 0644)
if err != nil {
t.Fatal(err)
}
expected, err = os.ReadFile(tt.dirName)
if err != nil {
t.Fatal(err)
}
}

assert.Equal(t, string(expected), got.String())
})
}
}
8 changes: 6 additions & 2 deletions lib/output/output.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package output

import (
"io"
)

type Output interface {
Write(map[string]interface{}, map[string]error) error
}
Expand All @@ -10,10 +14,10 @@ const (
Console OutputKey = iota + 1
)

func GetOutput(o OutputKey) Output {
func GetOutput(o OutputKey, w io.Writer) Output {
switch o {
case Console:
return NewConsoleOutput()
return NewConsoleOutput(w)
}
return nil
}
1 change: 1 addition & 0 deletions lib/output/testdata/console_empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 scanner(s) succeeded
13 changes: 13 additions & 0 deletions lib/output/testdata/console_valid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Results for numverify
Valid: true
Number: test
Local format: test
International format: test
Country prefix: test
Country code: test
Country name: test
Location: test
Carrier: test
Line type: test

1 scanner(s) succeeded
17 changes: 17 additions & 0 deletions lib/output/testdata/console_valid_with_errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Results for numverify
Valid: true
Number: test
Local format: test
International format: test
Country prefix: test
Country code: test
Country name: test
Location: test
Carrier: test
Line type: test

The following scanners returned errors:
googlesearch: dummy error
fakescanner: dummy error 2

2 scanner(s) succeeded
7 changes: 7 additions & 0 deletions test/goldenfile/goldenfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package goldenfile

import (
"flag"
)

var Update = flag.String("update", "", "name of test to update")

0 comments on commit 1b47dd2

Please sign in to comment.