-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
no_os_test.go
47 lines (40 loc) · 1001 Bytes
/
no_os_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
//go:build !wasm
// +build !wasm
package hackpadfs
import (
"bytes"
"encoding/json"
"io"
"os/exec"
"testing"
"github.com/hack-pad/hackpadfs/internal/assert"
)
func TestNoImportOS(t *testing.T) {
t.Parallel()
// Ensure we don't import the standard library "os" package unnecessarily in the main interface package.
// This helps reduce footprint, since os is not required unless using the hackpadfs/os implementation.
cmd := exec.Command("go", "list", "-json", ".")
output, err := cmd.CombinedOutput()
assert.NoError(t, err)
type module struct {
ImportPath string
Deps []string
}
decoder := json.NewDecoder(bytes.NewReader(output))
var listOut []module
for {
var m module
err := decoder.Decode(&m)
if err == io.EOF {
break
}
assert.NoError(t, err)
listOut = append(listOut, m)
}
assert.NotZero(t, len(listOut))
for _, pkg := range listOut {
if !assert.NotContains(t, pkg.Deps, "os") {
t.Log("Failed module import path:", pkg.ImportPath)
}
}
}