|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package deps_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "runtime" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "golang.org/x/tools/go/packages" |
| 28 | + |
| 29 | + "github.com/apache/skywalking-eyes/pkg/deps" |
| 30 | + "github.com/apache/skywalking-eyes/pkg/logger" |
| 31 | +) |
| 32 | + |
| 33 | +func TestMain(m *testing.M) { |
| 34 | + logger.Log.SetOutput(io.Discard) |
| 35 | + os.Exit(m.Run()) |
| 36 | +} |
| 37 | + |
| 38 | +const ( |
| 39 | + validGoMod = `module example.com/foo |
| 40 | +
|
| 41 | +go 1.21 |
| 42 | +` |
| 43 | + noModuleDirective = "go 1.21\n" |
| 44 | + spdxApache20 = "Apache-2.0" |
| 45 | +) |
| 46 | + |
| 47 | +func TestCanResolveGoMod(t *testing.T) { |
| 48 | + resolver := new(deps.GoModResolver) |
| 49 | + dir := t.TempDir() |
| 50 | + |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + filename string |
| 54 | + want bool |
| 55 | + }{ |
| 56 | + {"go.mod", "go.mod", true}, |
| 57 | + {"go.tool.mod", "go.tool.mod", true}, |
| 58 | + {"custom.mod", "custom.mod", true}, |
| 59 | + {"non-.mod extension", "Cargo.toml", false}, |
| 60 | + {"no extension", "go", false}, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + path := writeTempFile(t, dir, tt.filename, validGoMod) |
| 66 | + if got := resolver.CanResolve(path); got != tt.want { |
| 67 | + t.Errorf("CanResolve(%q) = %v, want %v", tt.filename, got, tt.want) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestResolveGoModInvalidFile(t *testing.T) { |
| 74 | + resolver := new(deps.GoModResolver) |
| 75 | + config := &deps.ConfigDeps{Threshold: 75} |
| 76 | + |
| 77 | + tests := []struct { |
| 78 | + name string |
| 79 | + content string |
| 80 | + }{ |
| 81 | + {"missing module directive", noModuleDirective}, |
| 82 | + {"non-Go content", "worker_processes auto;\n"}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + dir := t.TempDir() |
| 88 | + path := writeTempFile(t, dir, "go.mod", tt.content) |
| 89 | + var report deps.Report |
| 90 | + if err := resolver.Resolve(path, config, &report); err == nil { |
| 91 | + t.Errorf("Resolve should return an error for: %v", tt.name) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestResolvePackageLicense(t *testing.T) { |
| 98 | + resolver := new(deps.GoModResolver) |
| 99 | + config := &deps.ConfigDeps{Threshold: 75} |
| 100 | + |
| 101 | + _, thisFile, _, ok := runtime.Caller(0) |
| 102 | + if !ok { |
| 103 | + t.Fatal("runtime.Caller failed") |
| 104 | + } |
| 105 | + apacheLicense, err := os.ReadFile(filepath.Join(filepath.Dir(thisFile), "..", "..", "LICENSE")) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("failed to read LICENSE fixture: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + t.Run("license found in module dir", func(t *testing.T) { |
| 111 | + dir := t.TempDir() |
| 112 | + writeTempFile(t, dir, "LICENSE", string(apacheLicense)) |
| 113 | + |
| 114 | + module := &packages.Module{Path: "example.com/foo", Version: "v1.0.0", Dir: dir} |
| 115 | + var report deps.Report |
| 116 | + if err := resolver.ResolvePackageLicense(config, module, &report); err != nil { |
| 117 | + t.Fatalf("unexpected error: %v", err) |
| 118 | + } |
| 119 | + if len(report.Resolved) != 1 { |
| 120 | + t.Fatalf("expected 1 resolved, got %d", len(report.Resolved)) |
| 121 | + } |
| 122 | + if report.Resolved[0].LicenseSpdxID != spdxApache20 { |
| 123 | + t.Errorf("expected %v, got %v", spdxApache20, report.Resolved[0].LicenseSpdxID) |
| 124 | + } |
| 125 | + }) |
| 126 | + |
| 127 | + t.Run("no license found", func(t *testing.T) { |
| 128 | + dir := t.TempDir() |
| 129 | + module := &packages.Module{Path: "example.com/foo", Version: "v1.0.0", Dir: dir} |
| 130 | + var report deps.Report |
| 131 | + if err := resolver.ResolvePackageLicense(config, module, &report); err == nil { |
| 132 | + t.Error("expected error when no license file present") |
| 133 | + } |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +func writeTempFile(t *testing.T, dir, name, content string) string { |
| 138 | + t.Helper() |
| 139 | + path := filepath.Join(dir, name) |
| 140 | + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { |
| 141 | + t.Fatalf("failed to write %v: %v", name, err) |
| 142 | + } |
| 143 | + return path |
| 144 | +} |
0 commit comments