-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproject_test.go
More file actions
130 lines (112 loc) · 3.53 KB
/
project_test.go
File metadata and controls
130 lines (112 loc) · 3.53 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestDetectProjectsEmpty(t *testing.T) {
dir := t.TempDir()
projects := detectProjects(dir)
if len(projects) != 0 {
t.Errorf("detectProjects(empty) = %d projects, want 0", len(projects))
}
}
func TestDetectProjectsGo(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module test"), 0644)
projects := detectProjects(dir)
if len(projects) != 1 {
t.Fatalf("detectProjects(go.mod) = %d projects, want 1", len(projects))
}
if projects[0].Name != "Go" {
t.Errorf("project name = %q, want %q", projects[0].Name, "Go")
}
}
func TestDetectProjectsNode(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}"), 0644)
projects := detectProjects(dir)
if len(projects) != 1 {
t.Fatalf("detectProjects(package.json) = %d projects, want 1", len(projects))
}
if projects[0].Name != "Node.js" {
t.Errorf("project name = %q, want %q", projects[0].Name, "Node.js")
}
}
func TestDetectProjectsMultiple(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module test"), 0644)
os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte("FROM alpine"), 0644)
os.WriteFile(filepath.Join(dir, "Makefile"), []byte("all:"), 0644)
projects := detectProjects(dir)
if len(projects) != 3 {
t.Errorf("detectProjects(multiple) = %d projects, want 3", len(projects))
}
names := make(map[string]bool)
for _, p := range projects {
names[p.Name] = true
}
for _, expected := range []string{"Go", "Docker", "Make-based"} {
if !names[expected] {
t.Errorf("expected %q to be detected", expected)
}
}
}
func TestDetectProjectsPythonDedup(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "requirements.txt"), []byte("flask"), 0644)
os.WriteFile(filepath.Join(dir, "pyproject.toml"), []byte("[project]"), 0644)
os.WriteFile(filepath.Join(dir, "setup.py"), []byte(""), 0644)
projects := detectProjects(dir)
pythonCount := 0
for _, p := range projects {
if p.Name == "Python" {
pythonCount++
}
}
if pythonCount != 1 {
t.Errorf("Python detected %d times, want 1", pythonCount)
}
}
func TestDetectProjectsDotNetGlob(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "MyApp.csproj"), []byte("<Project>"), 0644)
projects := detectProjects(dir)
if len(projects) != 1 || projects[0].Name != ".NET" {
t.Errorf("detectProjects(*.csproj) failed, got %v", projects)
}
}
func TestFormatProjectInfoEmpty(t *testing.T) {
dir := t.TempDir()
info := formatProjectInfo(dir)
if info != "" {
t.Errorf("formatProjectInfo(empty) = %q, want empty", info)
}
}
func TestFormatProjectInfoSingle(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "package.json"), []byte("{}"), 0644)
info := formatProjectInfo(dir)
if info == "" {
t.Error("formatProjectInfo(package.json) returned empty")
}
if !strings.Contains(info, "Node.js") {
t.Errorf("formatProjectInfo = %q, missing Node.js", info)
}
if !strings.Contains(info, "npm") {
t.Errorf("formatProjectInfo = %q, missing npm tool", info)
}
}
func TestFormatProjectInfoMultiple(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "Cargo.toml"), []byte("[package]"), 0644)
os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte("FROM rust"), 0644)
info := formatProjectInfo(dir)
if !strings.Contains(info, "Rust") {
t.Errorf("formatProjectInfo = %q, missing Rust", info)
}
if !strings.Contains(info, "Docker") {
t.Errorf("formatProjectInfo = %q, missing Docker", info)
}
}