forked from apache/skywalking-eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolang_test.go
More file actions
144 lines (125 loc) · 4.01 KB
/
Copy pathgolang_test.go
File metadata and controls
144 lines (125 loc) · 4.01 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package deps_test
import (
"io"
"os"
"path/filepath"
"runtime"
"testing"
"golang.org/x/tools/go/packages"
"github.com/apache/skywalking-eyes/pkg/deps"
"github.com/apache/skywalking-eyes/pkg/logger"
)
func TestMain(m *testing.M) {
logger.Log.SetOutput(io.Discard)
os.Exit(m.Run())
}
const (
validGoMod = `module example.com/foo
go 1.21
`
noModuleDirective = "go 1.21\n"
spdxApache20 = "Apache-2.0"
)
func TestCanResolveGoMod(t *testing.T) {
resolver := new(deps.GoModResolver)
dir := t.TempDir()
tests := []struct {
name string
filename string
want bool
}{
{"go.mod", "go.mod", true},
{"go.tool.mod", "go.tool.mod", true},
{"custom.mod", "custom.mod", true},
{"non-.mod extension", "Cargo.toml", false},
{"no extension", "go", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := writeTempFile(t, dir, tt.filename, validGoMod)
if got := resolver.CanResolve(path); got != tt.want {
t.Errorf("CanResolve(%q) = %v, want %v", tt.filename, got, tt.want)
}
})
}
}
func TestResolveGoModInvalidFile(t *testing.T) {
resolver := new(deps.GoModResolver)
config := &deps.ConfigDeps{Threshold: 75}
tests := []struct {
name string
content string
}{
{"missing module directive", noModuleDirective},
{"non-Go content", "worker_processes auto;\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
path := writeTempFile(t, dir, "go.mod", tt.content)
var report deps.Report
if err := resolver.Resolve(path, config, &report); err == nil {
t.Errorf("Resolve should return an error for: %v", tt.name)
}
})
}
}
func TestResolvePackageLicense(t *testing.T) {
resolver := new(deps.GoModResolver)
config := &deps.ConfigDeps{Threshold: 75}
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
apacheLicense, err := os.ReadFile(filepath.Join(filepath.Dir(thisFile), "..", "..", "LICENSE"))
if err != nil {
t.Fatalf("failed to read LICENSE fixture: %v", err)
}
t.Run("license found in module dir", func(t *testing.T) {
dir := t.TempDir()
writeTempFile(t, dir, "LICENSE", string(apacheLicense))
module := &packages.Module{Path: "example.com/foo", Version: "v1.0.0", Dir: dir}
var report deps.Report
if err := resolver.ResolvePackageLicense(config, module, &report); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(report.Resolved) != 1 {
t.Fatalf("expected 1 resolved, got %d", len(report.Resolved))
}
if report.Resolved[0].LicenseSpdxID != spdxApache20 {
t.Errorf("expected %v, got %v", spdxApache20, report.Resolved[0].LicenseSpdxID)
}
})
t.Run("no license found", func(t *testing.T) {
dir := t.TempDir()
module := &packages.Module{Path: "example.com/foo", Version: "v1.0.0", Dir: dir}
var report deps.Report
if err := resolver.ResolvePackageLicense(config, module, &report); err == nil {
t.Error("expected error when no license file present")
}
})
}
func writeTempFile(t *testing.T, dir, name, content string) string {
t.Helper()
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(content), 0o600); err != nil { //nolint:gosec // path is under t.TempDir()
t.Fatalf("failed to write %v: %v", name, err)
}
return path
}