|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package tool |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestListToolsBuiltinDiscovery(t *testing.T) { |
| 14 | + // Test the directory scanning logic that was added to listTools |
| 15 | + // This tests that we correctly identify directories and skip non-directories |
| 16 | + |
| 17 | + // Create a temporary directory structure to simulate cmd/ directory |
| 18 | + tempDir := t.TempDir() |
| 19 | + cmdDir := filepath.Join(tempDir, "cmd") |
| 20 | + if err := os.MkdirAll(cmdDir, 0755); err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + |
| 24 | + // Create some tool directories |
| 25 | + tools := []string{"vet", "cgo", "cover", "fix", "godoc"} |
| 26 | + for _, tool := range tools { |
| 27 | + toolDir := filepath.Join(cmdDir, tool) |
| 28 | + if err := os.MkdirAll(toolDir, 0755); err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Create some non-tool directories that should be skipped |
| 34 | + nonTools := []string{"internal", "vendor"} |
| 35 | + for _, nonTool := range nonTools { |
| 36 | + nonToolDir := filepath.Join(cmdDir, nonTool) |
| 37 | + if err := os.MkdirAll(nonToolDir, 0755); err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Create a regular file (should be skipped) |
| 43 | + filePath := filepath.Join(cmdDir, "not-a-directory.txt") |
| 44 | + if err := os.WriteFile(filePath, []byte("test"), 0644); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + // Test directory reading logic (simulating the logic from listTools) |
| 49 | + entries, err := os.ReadDir(cmdDir) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + var foundTools []string |
| 55 | + for _, entry := range entries { |
| 56 | + // Skip non-directories (this is the logic we added) |
| 57 | + if !entry.IsDir() { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + toolName := entry.Name() |
| 62 | + // Skip packages that are not tools (this is the logic we added) |
| 63 | + if toolName == "internal" || toolName == "vendor" { |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + foundTools = append(foundTools, toolName) |
| 68 | + } |
| 69 | + |
| 70 | + // Sort for consistent comparison |
| 71 | + // (In the real code, this happens via the toolSet map and final output) |
| 72 | + for i := 0; i < len(foundTools)-1; i++ { |
| 73 | + for j := i + 1; j < len(foundTools); j++ { |
| 74 | + if foundTools[i] > foundTools[j] { |
| 75 | + foundTools[i], foundTools[j] = foundTools[j], foundTools[i] |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Verify we found the expected tools |
| 81 | + expectedTools := []string{"cgo", "cover", "fix", "godoc", "vet"} |
| 82 | + if len(foundTools) != len(expectedTools) { |
| 83 | + t.Errorf("Found %d tools, expected %d: %v", len(foundTools), len(expectedTools), foundTools) |
| 84 | + } |
| 85 | + |
| 86 | + for i, expected := range expectedTools { |
| 87 | + if i >= len(foundTools) || foundTools[i] != expected { |
| 88 | + t.Errorf("Expected tool %q at position %d, got %q", expected, i, foundTools[i]) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestToolSetTracking(t *testing.T) { |
| 94 | + // Test the toolSet map logic that prevents duplicates |
| 95 | + // This tests part of the new functionality in listTools |
| 96 | + |
| 97 | + // Simulate the toolSet map logic |
| 98 | + toolSet := make(map[string]bool) |
| 99 | + |
| 100 | + // Add some tools to the set (simulating tools found in tool directory) |
| 101 | + existingTools := []string{"vet", "cgo"} |
| 102 | + for _, tool := range existingTools { |
| 103 | + toolSet[tool] = true |
| 104 | + } |
| 105 | + |
| 106 | + // Test that existing tools are marked as present |
| 107 | + for _, tool := range existingTools { |
| 108 | + if !toolSet[tool] { |
| 109 | + t.Errorf("Expected tool %q to be in toolSet", tool) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Test that new tools can be added and checked |
| 114 | + newTools := []string{"cover", "fix"} |
| 115 | + for _, tool := range newTools { |
| 116 | + if toolSet[tool] { |
| 117 | + t.Errorf("Expected new tool %q to not be in toolSet initially", tool) |
| 118 | + } |
| 119 | + toolSet[tool] = true |
| 120 | + if !toolSet[tool] { |
| 121 | + t.Errorf("Expected tool %q to be in toolSet after adding", tool) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments