mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 11:13:33 +01:00
test: remove redundant tests
Remove integration tests in config_extended_test.go that duplicate unit tests in config_test.go for findLocalCheatpath. Remove redundant fuzz functions (FuzzFindLocalCheatpathNearestWins, FuzzTagsStress), a duplicate parse test (TestParseInvalidYAML), and a permanently-skipped interactive test (TestPromptIntegration). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -30,242 +30,6 @@ func TestConfigYAMLErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigLocalCheatpath tests local .cheat directory detection
|
||||
func TestConfigLocalCheatpath(t *testing.T) {
|
||||
// Create a temporary directory to act as working directory
|
||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
||||
}
|
||||
|
||||
// Save current working directory
|
||||
oldCwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get cwd: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldCwd)
|
||||
|
||||
// Change to temp directory
|
||||
err = os.Chdir(tempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to change dir: %v", err)
|
||||
}
|
||||
|
||||
// Create .cheat directory
|
||||
localCheat := filepath.Join(tempDir, ".cheat")
|
||||
err = os.Mkdir(localCheat, 0755)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create .cheat dir: %v", err)
|
||||
}
|
||||
|
||||
// Load config
|
||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// Check that local cheatpath was added
|
||||
found := false
|
||||
for _, cp := range conf.Cheatpaths {
|
||||
if cp.Name == "cwd" && cp.Path == localCheat {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("local .cheat directory was not added to cheatpaths")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigLocalCheatpathInParent tests that .cheat in a parent directory is found
|
||||
func TestConfigLocalCheatpathInParent(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
||||
}
|
||||
|
||||
oldCwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get cwd: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldCwd)
|
||||
|
||||
// Create .cheat in the root of the temp dir
|
||||
localCheat := filepath.Join(tempDir, ".cheat")
|
||||
if err := os.Mkdir(localCheat, 0755); err != nil {
|
||||
t.Fatalf("failed to create .cheat dir: %v", err)
|
||||
}
|
||||
|
||||
// Create a subdirectory and cd into it
|
||||
subDir := filepath.Join(tempDir, "sub")
|
||||
if err := os.Mkdir(subDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create sub dir: %v", err)
|
||||
}
|
||||
if err := os.Chdir(subDir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
|
||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, cp := range conf.Cheatpaths {
|
||||
if cp.Name == "cwd" && cp.Path == localCheat {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("parent .cheat directory was not added to cheatpaths")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigLocalCheatpathNearestWins tests that the nearest .cheat wins
|
||||
func TestConfigLocalCheatpathNearestWins(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
||||
}
|
||||
|
||||
oldCwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get cwd: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldCwd)
|
||||
|
||||
// Create .cheat at root
|
||||
if err := os.Mkdir(filepath.Join(tempDir, ".cheat"), 0755); err != nil {
|
||||
t.Fatalf("failed to create root .cheat dir: %v", err)
|
||||
}
|
||||
|
||||
// Create sub/.cheat (the nearer one)
|
||||
subDir := filepath.Join(tempDir, "sub")
|
||||
if err := os.Mkdir(subDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create sub dir: %v", err)
|
||||
}
|
||||
nearCheat := filepath.Join(subDir, ".cheat")
|
||||
if err := os.Mkdir(nearCheat, 0755); err != nil {
|
||||
t.Fatalf("failed to create near .cheat dir: %v", err)
|
||||
}
|
||||
|
||||
// cd into sub/deep/
|
||||
deepDir := filepath.Join(subDir, "deep")
|
||||
if err := os.Mkdir(deepDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create deep dir: %v", err)
|
||||
}
|
||||
if err := os.Chdir(deepDir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
|
||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, cp := range conf.Cheatpaths {
|
||||
if cp.Name == "cwd" {
|
||||
if cp.Path != nearCheat {
|
||||
t.Errorf("expected nearest .cheat %s, got %s", nearCheat, cp.Path)
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("no cwd cheatpath found")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigNoLocalCheatpath tests that no cwd cheatpath is added when no .cheat exists
|
||||
func TestConfigNoLocalCheatpath(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
oldCwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get cwd: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldCwd)
|
||||
|
||||
if err := os.Chdir(tempDir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
|
||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
for _, cp := range conf.Cheatpaths {
|
||||
if cp.Name == "cwd" {
|
||||
t.Error("cwd cheatpath should not be added when no .cheat exists")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigLocalCheatpathFileSkipped tests that a .cheat file (not dir) is skipped
|
||||
func TestConfigLocalCheatpathFileSkipped(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
oldCwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get cwd: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldCwd)
|
||||
|
||||
// Create .cheat as a file, not a directory
|
||||
if err := os.WriteFile(filepath.Join(tempDir, ".cheat"), []byte("not a dir"), 0644); err != nil {
|
||||
t.Fatalf("failed to create .cheat file: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Chdir(tempDir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
|
||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
for _, cp := range conf.Cheatpaths {
|
||||
if cp.Name == "cwd" {
|
||||
t.Error("cwd cheatpath should not be added for a .cheat file")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigDefaults tests default values
|
||||
func TestConfigDefaults(t *testing.T) {
|
||||
// Load empty config
|
||||
|
||||
@@ -65,58 +65,3 @@ func FuzzFindLocalCheatpath(f *testing.F) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzFindLocalCheatpathNearestWins verifies that when two .cheat directories
|
||||
// exist at different levels of the ancestor chain, the nearest one is returned.
|
||||
func FuzzFindLocalCheatpathNearestWins(f *testing.F) {
|
||||
f.Add(uint8(5), uint8(1), uint8(3))
|
||||
f.Add(uint8(8), uint8(0), uint8(7))
|
||||
f.Add(uint8(3), uint8(0), uint8(2))
|
||||
f.Add(uint8(10), uint8(2), uint8(8))
|
||||
|
||||
f.Fuzz(func(t *testing.T, totalDepth, shallowRaw, deepRaw uint8) {
|
||||
depth := int(totalDepth%12) + 2 // 2..13 (need room for two placements)
|
||||
s := int(shallowRaw) % depth
|
||||
d := int(deepRaw) % depth
|
||||
|
||||
// Need two distinct levels
|
||||
if s == d {
|
||||
d = (d + 1) % depth
|
||||
}
|
||||
// Ensure s < d (shallow is higher in tree, deep is closer to search dir)
|
||||
if s > d {
|
||||
s, d = d, s
|
||||
}
|
||||
|
||||
tempDir := t.TempDir()
|
||||
|
||||
// Build chain
|
||||
dirs := make([]string, 0, depth+1)
|
||||
dirs = append(dirs, tempDir)
|
||||
current := tempDir
|
||||
for i := 0; i < depth; i++ {
|
||||
current = filepath.Join(current, fmt.Sprintf("d%d", i))
|
||||
if err := os.Mkdir(current, 0755); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
dirs = append(dirs, current)
|
||||
}
|
||||
|
||||
// Place .cheat at both levels
|
||||
shallowCheat := filepath.Join(dirs[s], ".cheat")
|
||||
deepCheat := filepath.Join(dirs[d], ".cheat")
|
||||
if err := os.Mkdir(shallowCheat, 0755); err != nil {
|
||||
t.Fatalf("mkdir shallow .cheat: %v", err)
|
||||
}
|
||||
if err := os.Mkdir(deepCheat, 0755); err != nil {
|
||||
t.Fatalf("mkdir deep .cheat: %v", err)
|
||||
}
|
||||
|
||||
// Search from the deepest directory — should find the deeper (nearer) .cheat
|
||||
result := findLocalCheatpath(current)
|
||||
if result != deepCheat {
|
||||
t.Errorf("depth=%d shallow=%d deep=%d: expected nearest %s, got %s",
|
||||
depth, s, d, deepCheat, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user