pkg/dictionary/dictionary_integration_test.go   A
last analyzed

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 100
dl 0
loc 158
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A dictionary_test.TestDictionaryFromFile 0 10 1
A dictionary_test.TestDictionaryWithUnableToReadFolderShouldFail 0 11 1
A dictionary_test.TestShouldFailToCreateDictionaryFromInvalidPath 0 3 1
A dictionary_test.TestDictionaryFromAbsolutePath 0 13 1
A dictionary_test.TestDictionaryFromFileWithInvalidPath 0 8 1
A dictionary_test.TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status 0 14 2
A dictionary_test.TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout 0 22 2
A dictionary_test.TestNewDictionaryFromRemoteFile 0 26 2
A dictionary_test.removeTestDirectory 0 19 5
1
package dictionary_test
2
3
import (
4
	"net/http"
5
	"net/http/httptest"
6
	"os"
7
	"path/filepath"
8
	"strings"
9
	"testing"
10
	"time"
11
12
	"github.com/stefanoj3/dirstalk/pkg/common/test"
13
	"github.com/stefanoj3/dirstalk/pkg/dictionary"
14
	"github.com/stretchr/testify/assert"
15
)
16
17
func TestDictionaryFromFile(t *testing.T) {
18
	entries, err := dictionary.NewDictionaryFrom("testdata/dict.txt", &http.Client{})
19
	assert.NoError(t, err)
20
21
	expectedValue := []string{
22
		"home",
23
		"home/index.php",
24
		"blabla",
25
	}
26
	assert.Equal(t, expectedValue, entries)
27
}
28
29
func TestShouldFailToCreateDictionaryFromInvalidPath(t *testing.T) {
30
	_, err := dictionary.NewDictionaryFrom("http:///home/\n", &http.Client{})
31
	assert.Error(t, err)
32
}
33
34
func TestDictionaryFromAbsolutePath(t *testing.T) {
35
	path, err := filepath.Abs("testdata/dict.txt")
36
	assert.NoError(t, err)
37
38
	entries, err := dictionary.NewDictionaryFrom(path, &http.Client{})
39
	assert.NoError(t, err)
40
41
	expectedValue := []string{
42
		"home",
43
		"home/index.php",
44
		"blabla",
45
	}
46
	assert.Equal(t, expectedValue, entries)
47
}
48
49
func TestDictionaryWithUnableToReadFolderShouldFail(t *testing.T) {
50
	newFolderPath := "testdata/" + test.RandStringRunes(10)
51
52
	err := os.Mkdir(newFolderPath, 0200)
53
	assert.NoError(t, err)
54
55
	defer removeTestDirectory(t, newFolderPath)
56
57
	_, err = dictionary.NewDictionaryFrom(newFolderPath, &http.Client{})
58
	assert.Error(t, err)
59
	assert.Contains(t, err.Error(), "permission denied")
60
}
61
62
func TestDictionaryFromFileWithInvalidPath(t *testing.T) {
63
	t.Parallel()
64
65
	d, err := dictionary.NewDictionaryFrom("testdata/gibberish_nonexisting_file", &http.Client{})
66
	assert.Error(t, err)
67
	assert.Nil(t, d)
68
69
	assert.Contains(t, err.Error(), "unable to open")
70
}
71
72
func TestNewDictionaryFromRemoteFile(t *testing.T) {
73
	srv := httptest.NewServer(
74
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75
			dict := `/home
76
/about
77
/contacts
78
something
79
potato
80
`
81
			w.WriteHeader(http.StatusOK)
82
			_, _ = w.Write([]byte(dict)) //nolint:errcheck
83
		}),
84
	)
85
	defer srv.Close()
86
87
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
88
	assert.NoError(t, err)
89
90
	expectedValue := []string{
91
		"/home",
92
		"/about",
93
		"/contacts",
94
		"something",
95
		"potato",
96
	}
97
	assert.Equal(t, expectedValue, entries)
98
}
99
100
func TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout(t *testing.T) {
101
	srv := httptest.NewServer(
102
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
103
			time.Sleep(time.Millisecond) // out of paranoia - we dont want unstable tests
104
105
			w.WriteHeader(http.StatusOK)
106
			_, _ = w.Write([]byte("/home")) //nolint:errcheck
107
		}),
108
	)
109
	defer srv.Close()
110
111
	entries, err := dictionary.NewDictionaryFrom(
112
		srv.URL,
113
		&http.Client{
114
			Timeout: time.Microsecond,
115
		},
116
	)
117
	assert.Error(t, err)
118
	assert.Contains(t, err.Error(), "failed to get")
119
	assert.Contains(t, err.Error(), "Timeout")
120
121
	assert.Nil(t, entries)
122
}
123
124
func TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status(t *testing.T) {
125
	srv := httptest.NewServer(
126
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
127
			w.WriteHeader(http.StatusForbidden)
128
		}),
129
	)
130
	defer srv.Close()
131
132
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
133
	assert.Error(t, err)
134
	assert.Contains(t, err.Error(), srv.URL)
135
	assert.Contains(t, err.Error(), "status code 403")
136
137
	assert.Nil(t, entries)
138
}
139
140
func removeTestDirectory(t *testing.T, path string) {
141
	if !strings.Contains(path, "testdata") {
142
		t.Fatalf("cannot delete `%s`, it is not in a `testdata` folder", path)
143
144
		return
145
	}
146
147
	stats, err := os.Stat(path)
148
	if err != nil {
149
		t.Fatalf("failed to read `%s` properties", path)
150
	}
151
152
	if !stats.IsDir() {
153
		t.Fatalf("cannot delete `%s`, it is not a directory", path)
154
	}
155
156
	err = os.Remove(path)
157
	if err != nil {
158
		t.Fatalf("failed to remove `%s`: %s", path, err.Error())
159
	}
160
}
161