-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile_test.go
62 lines (48 loc) · 1.35 KB
/
file_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func Test_FileGetString(t *testing.T) {
const hello = "Hello World!"
t.Run("wrong file", func(t *testing.T) {
_, err := FileGetString("invalid_file")
require.Error(t, err)
})
t.Run("new file", func(t *testing.T) {
tempFile := filepath.Join(t.TempDir(), "testfile.txt")
err := ioutil.WriteFile(tempFile, []byte(hello), 0600)
require.NoError(t, err)
str, err := FileGetString(tempFile)
require.NoError(t, err)
require.Equal(t, hello, str)
})
t.Run("http", func(t *testing.T) {
addr := "https://raw.githubusercontent.com/xelaj/go-dry/master/file_test.go"
req, err := http.NewRequest("GET", addr, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
str, err := FileGetString("https://raw.githubusercontent.com/xelaj/go-dry/master/file_test.go")
require.NoError(t, err)
require.Equal(t, string(raw), str)
})
}
func Test_FileIsDir(t *testing.T) {
if FileIsDir("testfile.txt") {
t.Fail()
}
if !FileIsDir(".") {
t.Fail()
}
}