-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_test.go
48 lines (39 loc) · 912 Bytes
/
backup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestFileSaver(t *testing.T) {
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err.Error())
}
defer os.RemoveAll(dir)
var input = "hello world"
fs := FileSaver{Prefix: filepath.Join(dir, "backup")}
fs.Save(strings.NewReader(input))
/*
Check that there is a file in the directory
and that it matches the input
*/
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err.Error())
}
if len(entries) != 1 {
t.Fatalf("Backup directory has %d entries, should have 1.", len(entries))
}
finfo := entries[0]
if finfo.IsDir() {
t.Fatal("backup file is a directory, should not be.")
}
checkfile, err := os.ReadFile(filepath.Join(dir, finfo.Name()))
if err != nil {
t.Fatal(err.Error())
}
if string(checkfile) != input {
t.Fatalf("File does not match. Input=%q Out=%q", input, checkfile)
}
}