Skip to content

Commit 5988bbd

Browse files
authored
ci: run tests on windows (#33)
1 parent e08b5b0 commit 5988bbd

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

.github/workflows/test.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ on:
1010

1111
env:
1212
# Upload coverage only for this go version.
13-
LATEST_GO_VERSION: "1.19"
13+
LATEST_GO_VERSION: "1.20"
1414

1515
jobs:
1616
test:
1717
strategy:
1818
matrix:
19-
os: ["ubuntu-latest", "macos-latest"]
20-
go: ["1.18", "1.19"]
19+
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
20+
go: ["1.18", "1.19", "1.20"]
2121

2222
name: ${{ matrix.os }} & Go ${{ matrix.go }}
2323

@@ -33,6 +33,7 @@ jobs:
3333
go-version: ${{ matrix.go }}
3434

3535
- name: Test
36+
shell: bash
3637
run: |
3738
go vet ./...
3839
go test -v -race -coverprofile=coverage.out -covermode=atomic $(go list ./... | grep -v /examples)

context_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package kid
33
import (
44
"encoding/json"
55
"encoding/xml"
6+
"fmt"
67
"net/http"
78
"net/http/httptest"
89
"net/url"
10+
"path/filepath"
911
"strings"
1012
"testing"
1113

@@ -19,6 +21,13 @@ type person struct {
1921
Age int `json:"age" xml:"age"`
2022
}
2123

24+
func getNewLineStr() string {
25+
if filepath.Separator != rune('/') {
26+
return "\r\n"
27+
}
28+
return "\n"
29+
}
30+
2231
func TestNewContext(t *testing.T) {
2332
k := New()
2433

@@ -418,9 +427,15 @@ func TestContextHTML(t *testing.T) {
418427

419428
err := ctx.HTML(http.StatusAccepted, "index.html", nil)
420429

430+
newLine := getNewLineStr()
431+
expectedRes := fmt.Sprintf(
432+
"%s<html><body>%s<p>content</p>%s</body></html>%s",
433+
newLine, newLine, newLine, newLine,
434+
)
435+
421436
assert.NoError(t, err)
422437
assert.Equal(t, http.StatusAccepted, res.Code)
423-
assert.Equal(t, "\n<html><body>\n<p>content</p>\n</body></html>\n", res.Body.String())
438+
assert.Equal(t, expectedRes, res.Body.String())
424439
assert.Equal(t, "text/html", res.Header().Get("Content-Type"))
425440
}
426441

html_renderer/html.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (r *defaultHTMLRenderer) shouldntLoadTemplates() bool {
147147

148148
// isLayout determines if the file is a layout file or not.
149149
func (r *defaultHTMLRenderer) isLayout(file string) bool {
150-
return strings.HasPrefix(file, r.rootDir+r.layoutDir)
150+
return strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(r.rootDir+r.layoutDir))
151151
}
152152

153153
// isValidExt determines if the file has valid extension.
@@ -157,7 +157,7 @@ func (r *defaultHTMLRenderer) isValidExt(file string) bool {
157157

158158
// getTemplateName extracts template name from file path.
159159
func (r *defaultHTMLRenderer) getTemplateName(filePath string) string {
160-
return filePath[len(r.rootDir):]
160+
return filepath.ToSlash(filePath[len(r.rootDir):])
161161
}
162162

163163
// getFilesToParse merges template path and layouts into a string slice.

html_renderer/html_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package htmlrenderer
22

33
import (
4+
"fmt"
45
"html/template"
56
"net/http"
67
"net/http/httptest"
8+
"path/filepath"
79
"testing"
810

911
"github.com/mojixcoder/kid/errors"
@@ -15,6 +17,13 @@ func newTestHTMLRenderer() *defaultHTMLRenderer {
1517
return htmlRenderer
1618
}
1719

20+
func getNewLineStr() string {
21+
if filepath.Separator != rune('/') {
22+
return "\r\n"
23+
}
24+
return "\n"
25+
}
26+
1827
func TestNew(t *testing.T) {
1928
htmlRenderer := New("templates/", "layouts/", ".html", true)
2029

@@ -56,12 +65,16 @@ func TestDefaultHTMLRendererGetTemplateAndLayoutFiles(t *testing.T) {
5665
assert.NoError(t, err)
5766
assert.Equal(
5867
t,
59-
[]string{"../testdata/templates/layouts/base.html"},
68+
[]string{filepath.Join("..", "testdata", "templates", "layouts", "base.html")},
6069
layoutFiles,
6170
)
6271
assert.Equal(
6372
t,
64-
[]string{"../testdata/templates/index.html", "../testdata/templates/pages/page.html", "../testdata/templates/pages/page2.html"},
73+
[]string{
74+
filepath.Join("..", "testdata", "templates", "index.html"),
75+
filepath.Join("..", "testdata", "templates", "pages", "page.html"),
76+
filepath.Join("..", "testdata", "templates", "pages", "page2.html"),
77+
},
6578
templateFiles,
6679
)
6780

@@ -164,20 +177,32 @@ func TestDefaultHTMLRendererRenderHTML(t *testing.T) {
164177
assert.Equal(t, "template doesn't_exists.html not found", httpErr.Message)
165178
assert.Equal(t, http.StatusInternalServerError, httpErr.Code)
166179

180+
newline := getNewLineStr()
181+
167182
res = httptest.NewRecorder()
168183
err = htmlRenderer.RenderHTML(res, "index.html", nil)
169184
assert.NoError(t, err)
170-
assert.Equal(t, "\n<html><body>\n<p>content</p>\n</body></html>\n", res.Body.String())
185+
assert.Equal(
186+
t,
187+
fmt.Sprintf("%s<html><body>%s<p>content</p>%s</body></html>%s", newline, newline, newline, newline),
188+
res.Body.String(),
189+
)
171190

172191
res = httptest.NewRecorder()
173192
err = htmlRenderer.RenderHTML(res, "pages/page.html", map[string]string{"key": "page contents"})
174193
assert.NoError(t, err)
175-
assert.Equal(t, "\n<html><body>\n<p>page contents</p>\n</body></html>\n", res.Body.String())
194+
assert.Equal(t,
195+
fmt.Sprintf("%s<html><body>%s<p>page contents</p>%s</body></html>%s", newline, newline, newline, newline),
196+
res.Body.String(),
197+
)
176198

177199
res = httptest.NewRecorder()
178200
err = htmlRenderer.RenderHTML(res, "pages/page2.html", nil)
179201
assert.NoError(t, err)
180-
assert.Equal(t, "\n<html><body>\n<p>Hello Tom</p>\n</body></html>\n", res.Body.String())
202+
assert.Equal(t,
203+
fmt.Sprintf("%s<html><body>%s<p>Hello Tom</p>%s</body></html>%s", newline, newline, newline, newline),
204+
res.Body.String(),
205+
)
181206
}
182207

183208
func TestNewInternalServerHTTPError(t *testing.T) {

0 commit comments

Comments
 (0)