This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
link_test.go
209 lines (158 loc) · 4.63 KB
/
link_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
// createLinkAndCategorise will create a link and categorise it. If
// createLinkManually is set, the link will be created "manually" (without the
// constructor) and categorise() called. If not set, the constructor will be
// used.
func createLinkAndCategorise(assert *assert.Assertions, createLinkManually bool) {
dir, err := os.MkdirTemp("", "")
assert.NoError(err)
cwd, err := os.Getwd()
assert.NoError(err)
defer os.Chdir(cwd)
err = os.Chdir(dir)
assert.NoError(err)
defer os.RemoveAll(dir)
readmeDirName := "dir-with-readme"
emptyDirName := "empty"
makeDirs(assert, dir, readmeDirName, emptyDirName)
readmeDirPath := filepath.Join(readmeDirName, readmeName)
topLevelReadmeName := "top-level.md"
topLevelReadmeLink := filepath.Join("/", topLevelReadmeName)
topLevelReadmePath := filepath.Join(dir, topLevelReadmeName)
type testData struct {
linkAddress string
expectedPath string
expectedType LinkType
expectError bool
// Set if expectedPath should be checked
checkPath bool
}
docRoot = dir
data := []testData{
{"", "", -1, true, false},
{"a", "", -1, true, false},
{"a.b", "", -1, true, false},
{"a#b", "", -1, true, false},
{"htt://foo", "", -1, true, false},
{"HTTP://foo", "", -1, true, false},
{"moohttp://foo", "", -1, true, false},
{"mailto", "", -1, true, false},
{"http", "", -1, true, false},
{"https", "", -1, true, false},
{"http://foo", "", urlLink, false, false},
{"https://foo/", "", urlLink, false, false},
{"https://foo/bar", "", urlLink, false, false},
{"mailto:me", "", mailLink, false, false},
{".", "", externalFile, false, false},
{"/", "", externalFile, false, false},
{emptyDirName, "", externalFile, false, false},
{readmeDirName, readmeDirPath, externalLink, false, true},
{"foo.md", "foo.md", externalLink, false, true},
{"foo.md#bar", "foo.md", externalLink, false, true},
{topLevelReadmeLink, topLevelReadmePath, externalLink, false, true},
}
logger := logrus.WithField("test", "true")
description := ""
for i, d := range data {
var link Link
var err error
doc := newDoc("foo", logger)
if createLinkManually {
link = Link{
Doc: doc,
Address: d.linkAddress,
Description: description,
}
err = link.categorise()
} else {
link, err = newLink(doc, d.linkAddress, description)
}
msg := fmt.Sprintf("test[%d] manual-link: %v: %+v, link: %+v\n", i, createLinkManually, d, link)
if d.expectError {
assert.Error(err, msg)
continue
}
assert.NoError(err, msg)
assert.Equal(link.Doc, doc)
assert.Equal(link.Address, d.linkAddress)
assert.Equal(link.Description, description)
assert.Equal(link.Type, d.expectedType)
if d.checkPath {
assert.Equal(d.expectedPath, link.ResolvedPath)
}
}
}
func TestNewLink(t *testing.T) {
assert := assert.New(t)
createLinkAndCategorise(assert, false)
}
func TestLinkCategorise(t *testing.T) {
assert := assert.New(t)
createLinkAndCategorise(assert, true)
}
func TestLinkHandleImplicitREADME(t *testing.T) {
assert := assert.New(t)
dir, err := os.MkdirTemp("", "")
assert.NoError(err)
defer os.RemoveAll(dir)
cwd, err := os.Getwd()
assert.NoError(err)
defer os.Chdir(cwd)
err = os.Chdir(dir)
assert.NoError(err)
defer os.RemoveAll(dir)
readmeDirName := "dir-with-readme"
emptyDirName := "empty"
makeDirs(assert, dir, readmeDirName, emptyDirName)
readmePath := filepath.Join(readmeDirName, readmeName)
emptyFileName := "empty-file"
err = createFile(emptyFileName, "")
assert.NoError(err)
type testData struct {
linkAddr string
expectedPath string
expectedType LinkType
isREADME bool
expectError bool
}
data := []testData{
{"", "", unknownLink, false, true},
{"foo", "", unknownLink, false, true},
{emptyFileName, "", unknownLink, false, false},
{emptyDirName, "", unknownLink, false, false},
{readmeDirName, readmePath, externalLink, true, false},
}
logger := logrus.WithField("test", "true")
for i, d := range data {
doc := newDoc("foo", logger)
link := Link{
Doc: doc,
Address: d.linkAddr,
}
msg := fmt.Sprintf("test[%d]: %+v\n", i, d)
isREADME, err := link.handleImplicitREADME()
if d.expectError {
assert.Error(err, msg)
continue
}
assert.NoError(err, msg)
assert.Equal(isREADME, d.isREADME)
assert.Equal(isREADME, d.isREADME)
assert.Equal(link.Address, d.linkAddr)
assert.Equal(link.Type, d.expectedType)
assert.Equal(link.ResolvedPath, d.expectedPath)
}
}