Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61aacff

Browse files
authored
chore: Refactor site to improve testing (#2014)
It was difficult to develop this package due to the embed build tag being mandatory on the tests. The logic to test doesn't require any embedded files.
1 parent 89dde21 commit 61aacff

File tree

6 files changed

+40
-40
lines changed

6 files changed

+40
-40
lines changed

coderd/coderd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func New(options *Options) *API {
334334
r.Get("/state", api.workspaceBuildState)
335335
})
336336
})
337-
r.NotFound(site.DefaultHandler().ServeHTTP)
337+
r.NotFound(site.Handler(site.FS()).ServeHTTP)
338338

339339
return api
340340
}

site/embed_slim.go

-12
This file was deleted.

site/embed.go renamed to site/site.go

-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
//go:build embed
2-
// +build embed
3-
41
package site
52

63
import (
74
"bytes"
8-
"embed"
95
"fmt"
106
"io"
117
"io/fs"
@@ -21,26 +17,6 @@ import (
2117
"golang.org/x/xerrors"
2218
)
2319

24-
// The `embed` package ignores recursively including directories
25-
// that prefix with `_`. Wildcarding nested is janky, but seems to
26-
// work quite well for edge-cases.
27-
//go:embed out
28-
//go:embed out/bin/*
29-
var site embed.FS
30-
31-
func DefaultHandler() http.Handler {
32-
// the out directory is where webpack builds are created. It is in the same
33-
// directory as this file (package site).
34-
siteFS, err := fs.Sub(site, "out")
35-
36-
if err != nil {
37-
// This can't happen... Go would throw a compilation error.
38-
panic(err)
39-
}
40-
41-
return Handler(siteFS)
42-
}
43-
4420
// Handler returns an HTTP handler for serving the static site.
4521
func Handler(fileSystem fs.FS) http.Handler {
4622
// html files are handled by a text/template. Non-html files

site/site_embed.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build embed
2+
// +build embed
3+
4+
package site
5+
6+
import (
7+
"embed"
8+
"io/fs"
9+
)
10+
11+
//go:embed out
12+
//go:embed out/bin/*
13+
var site embed.FS
14+
15+
func FS() fs.FS {
16+
// the out directory is where webpack builds are created. It is in the same
17+
// directory as this file (package site).
18+
out, err := fs.Sub(site, "out")
19+
if err != nil {
20+
// This can't happen... Go would throw a compilation error.
21+
panic(err)
22+
}
23+
return out
24+
}

site/site_slim.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !embed
2+
// +build !embed
3+
4+
package site
5+
6+
import (
7+
"embed"
8+
"io/fs"
9+
)
10+
11+
var slim embed.FS
12+
13+
func FS() fs.FS {
14+
return slim
15+
}

site/embed_test.go renamed to site/site_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build embed
2-
// +build embed
3-
41
package site_test
52

63
import (

0 commit comments

Comments
 (0)