Skip to content

Commit 73d032d

Browse files
committed
some cleanup of things and moar unit tests...
Signed-off-by: ed kim <ekim8015@gmail.com>
1 parent 49f20ad commit 73d032d

File tree

6 files changed

+91
-11
lines changed

6 files changed

+91
-11
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
test:
1+
tester:
22
go test ./... -v
33
test-coverage:
4-
go test ./... -coverprofile cp.out
4+
go test ./... -coverprofile cp.out
5+
6+
test-nocache:
7+
go test ./... -v -count=1

command/init.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ func (c *Config) RunInit() error {
7676

7777
}
7878

79-
// TODO: Early exit of RunInit if we find config in current working directory
80-
8179
// Check location supplied by init parameter first
8280
if len(c.location) > 0 {
8381
c.location = path.Clean(c.location)
@@ -116,7 +114,7 @@ func (c *Config) RunInit() error {
116114
array, err := walker.Walker(&handler)
117115

118116
for _, fileObject := range array {
119-
zap.S().Debugf("Something something %v", fileObject)
117+
zap.S().Debugf("single record dump %v", fileObject)
120118
}
121119

122120
return nil

filewalk/walker.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewWalker(location string, exclude, include []string) *WalkerConfig {
3131
// FileObject might be something we use.
3232
type FileObject struct {
3333
name string
34+
path string
3435
md5 string
3536
sha1sum string
3637
etag string
@@ -48,22 +49,22 @@ func (w *WalkerConfig) Walker(fh Handler) (ReturnObject, error) {
4849
var fileObject FileObject
4950
helper := &godirwalk.Options{
5051
Callback: func(osPathname string, de *godirwalk.Dirent) error {
51-
zap.S().Debugf("lets try match %s %s\n", w.Exclude, osPathname)
5252
if de.IsDir() {
5353
return nil
5454
}
55-
55+
zap.S().Debugf("lets try match %s %s", w.Include, osPathname)
5656
matched := w.returnMatch(osPathname)
5757
if matched {
58-
zap.S().Debugf("matched: %s\n", osPathname)
58+
zap.S().Debugf("matched: %s", osPathname)
5959
file := fh.loadFile(osPathname)
6060
sha1sum := fh.sha1sum(file)
6161
zap.S().Debugf("sha1sum of the file is %s", sha1sum) // TODO: make the logging better
6262
md5 := fh.md5(file)
6363
zap.S().Debugf("md5sum of the file is %s", md5)
6464
etag := fh.etag(file)
6565
zap.S().Debugf("etag of the file is %s", etag)
66-
fileObject.name = osPathname
66+
fileObject.name = de.Name()
67+
fileObject.path = osPathname // TODO: Clean this up?
6768
fileObject.md5 = md5
6869
fileObject.etag = etag
6970
fileObject.sha1sum = sha1sum
@@ -78,7 +79,7 @@ func (w *WalkerConfig) Walker(fh Handler) (ReturnObject, error) {
7879
Unsorted: true, // (optional) set true for faster yet non-deterministic enumeration (see godoc)
7980
}
8081
_ = godirwalk.Walk(w.Location, helper)
81-
zap.S().Debugf("all the things: %v", buff)
82+
zap.S().Debugf("all the files found are: %v", buff)
8283
return returnObject, nil
8384
}
8485

filewalk/walker_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package filewalk
22

33
import (
4+
"crypto/md5"
5+
"crypto/sha1"
6+
"fmt"
7+
"reflect"
48
"testing"
59

610
"github.com/edify42/camera-backup/config"
@@ -90,3 +94,76 @@ func TestWalkerConfig_returnMatch(t *testing.T) {
9094
})
9195
}
9296
}
97+
98+
// Mocked out interface functions.
99+
100+
type testHandle struct{}
101+
102+
func (h *testHandle) sha1sum(data []byte) string {
103+
return fmt.Sprintf("%x", sha1.Sum(data))
104+
}
105+
106+
func (h *testHandle) md5(data []byte) string {
107+
return fmt.Sprintf("%x", md5.Sum(data))
108+
}
109+
110+
func (h *testHandle) etag(data []byte) string {
111+
return fmt.Sprintf("%x", md5.Sum(data))
112+
}
113+
114+
func (h *testHandle) loadFile(path string) []byte {
115+
return []byte(path)
116+
}
117+
118+
func TestWalkerConfig_Walker(t *testing.T) {
119+
type fields struct {
120+
Location string
121+
Exclude []string
122+
Include []string
123+
}
124+
type args struct {
125+
fh Handler
126+
}
127+
128+
myHandle := &testHandle{}
129+
var returnObject ReturnObject
130+
131+
tests := []struct {
132+
name string
133+
fields fields
134+
args args
135+
want ReturnObject
136+
wantErr bool
137+
}{
138+
{
139+
name: "First test case",
140+
fields: fields{
141+
Location: "here",
142+
Exclude: []string{""},
143+
Include: []string{""},
144+
},
145+
args: args{
146+
fh: myHandle,
147+
},
148+
want: returnObject,
149+
wantErr: false,
150+
},
151+
}
152+
for _, tt := range tests {
153+
t.Run(tt.name, func(t *testing.T) {
154+
w := &WalkerConfig{
155+
Location: tt.fields.Location,
156+
Exclude: tt.fields.Exclude,
157+
Include: tt.fields.Include,
158+
}
159+
got, err := w.Walker(tt.args.fh)
160+
if (err != nil) != tt.wantErr {
161+
t.Errorf("WalkerConfig.Walker() error = %v, wantErr %v", err, tt.wantErr)
162+
return
163+
}
164+
if !reflect.DeepEqual(got, tt.want) {
165+
t.Errorf("WalkerConfig.Walker() = %v, want %v", got, tt.want)
166+
}
167+
})
168+
}
169+
}

localstore/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func InitDB(i Sqlstore) error {
5353
return err
5454
}
5555

56-
db, err := sql.Open("sqlite3", database)
56+
db, err := sql.Open("sqlite3", database) // Might mock out this bit later into a separate function....Unsure why it works...
5757
if err != nil {
5858
zap.S().Errorf("Could not open the database %v", err)
5959
return err

localstore/init_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func TestInitDB(t *testing.T) {
9898
args: args{
9999
i: c,
100100
},
101+
wantErr: false,
101102
},
102103
}
103104
for _, tt := range tests {

0 commit comments

Comments
 (0)