Skip to content

Commit 73f8297

Browse files
committed
test: add ioutil abstraction coverage
1 parent 5857550 commit 73f8297

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

cmd/get.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var GetCmd = &cobra.Command{
1616
Short: "Scrape and download assets from a URL, a file or a both",
1717
Args: cobra.MinimumNArgs(1),
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
log.Logger = instance.DefaultLogger(cmd.OutOrStderr())
19+
log.Logger = log.Output(instance.DefaultLogger(cmd.OutOrStderr()))
2020

2121
g := instance.New(cmd)
2222
g.ParseFlags()

internal/instance/log.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88

99
"github.com/fatih/color"
1010
"github.com/rs/zerolog"
11-
"github.com/rs/zerolog/log"
1211
)
1312

14-
func DefaultLogger(w io.Writer) zerolog.Logger {
15-
return log.Output(zerolog.ConsoleWriter{
13+
func DefaultLogger(w io.Writer) zerolog.ConsoleWriter {
14+
return zerolog.ConsoleWriter{
1615
Out: w,
1716
TimeFormat: time.RFC3339Nano,
1817
FormatTimestamp: func(i interface{}) string { return "" },
@@ -62,13 +61,5 @@ func DefaultLogger(w io.Writer) zerolog.Logger {
6261
FormatMessage: func(i interface{}) string {
6362
return fmt.Sprintf("%s", i)
6463
},
65-
})
64+
}
6665
}
67-
68-
/*
69-
╷ Error: Something bad happened
70-
│ Description of the error, here we can be more detailed
71-
╵ something.hcl:2-3
72-
╷ Error: Something bad happened
73-
╵ Description of the error, here we can be more detailed
74-
*/

internal/instance/log_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package instance
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/rs/zerolog/log"
9+
)
10+
11+
func TestDefaultLogger(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
f func()
15+
want string
16+
}{
17+
{
18+
"trace",
19+
func() { log.Trace().Bool("test", true).Msg("trace") },
20+
" TRC trace test=true\n",
21+
},
22+
{
23+
"debug",
24+
func() { log.Debug().Bool("test", true).Msg("debug") },
25+
" DBG debug test=true\n",
26+
},
27+
{
28+
"info",
29+
func() { log.Info().Bool("test", true).Msg("info") },
30+
" INF info test=true\n",
31+
},
32+
{
33+
"warning",
34+
func() { log.Warn().Bool("test", true).Msg("warn") },
35+
" WRN warn test=true\n",
36+
},
37+
{
38+
"error",
39+
func() { log.Err(fmt.Errorf("text")).Bool("test", true).Msg("err") },
40+
" ERR err error=text test=true\n",
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
buf := bytes.Buffer{}
47+
48+
log.Logger = log.Output(DefaultLogger(&buf))
49+
50+
tt.f()
51+
52+
if got := buf.String(); got != tt.want {
53+
t.Errorf("got: %v, want: %v", got, tt.want)
54+
}
55+
})
56+
}
57+
}

internal/utils/filesystem_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,37 @@ func TestAbs(t *testing.T) {
116116

117117
resetWd()
118118
}
119+
120+
func TestIoUtil(t *testing.T) {
121+
initialWd, _ := os.Getwd()
122+
defer func() {
123+
_ = os.Chdir(initialWd)
124+
}()
125+
126+
root := tu.GetOSRoot()
127+
Fs = new(tu.MockFs)
128+
Wd = root
129+
130+
// test writefile
131+
if err := Io.WriteFile(Fs, "test.txt", []byte("test"), 0644); err != nil {
132+
t.Fail()
133+
}
134+
135+
// test readfile
136+
if b, err := Io.ReadFile(Fs, "test.txt"); err != nil {
137+
t.Fail()
138+
} else {
139+
if string(b) != "test" {
140+
t.Fail()
141+
}
142+
}
143+
144+
// test exists
145+
if exists, err := Io.Exists(Fs, "test.txt"); err != nil {
146+
t.Fail()
147+
} else {
148+
if !exists {
149+
t.Fail()
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)