Datasets:
File size: 2,859 Bytes
df0899b | 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 | package main
import (
"os"
"path/filepath"
"testing"
)
func TestValidateDocsDirStructure(t *testing.T) {
testCases := []struct {
name string
dirStructure []string
expectedErr bool
}{
{
name: "Valid directory structure",
dirStructure: []string{"mutation-examples", "validation", "intro.md", "pspintro.md"},
expectedErr: false,
},
{
name: "Unexpected directory",
dirStructure: []string{"mutation-examples", "unexpected-dir", "validation", "intro.md", "pspintro.md"},
expectedErr: true,
},
{
name: "Unexpected file",
dirStructure: []string{"mutation-examples", "validation", "unexpected-file.md", "intro.md", "pspintro.md"},
expectedErr: true,
},
{
name: "Missing file",
dirStructure: []string{"mutation-examples", "validation", "intro.md"},
expectedErr: true,
},
{
name: "Missing directory",
dirStructure: []string{"mutation-examples", "intro.md", "pspintro.md"},
expectedErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("Error creating temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Create the directory structure
docsDirPath := filepath.Join(tmpDir, docsDirEntry)
err = os.MkdirAll(docsDirPath, 0o755)
if err != nil {
t.Fatalf("Error creating docs dir: %v", err)
}
for _, item := range tc.dirStructure {
path := filepath.Join(docsDirPath, item)
if filepath.Ext(path) == "" {
if os.Mkdir(path, 0o755) != nil {
t.Fatalf("Error creating directory: %v", path)
}
} else {
f, err := os.Create(path)
if err != nil {
t.Fatalf("Error creating the file %s: %v", item, err)
}
defer f.Close()
_, err = f.Write([]byte{})
if err != nil {
t.Fatalf("Error writing to the file %s: %v", item, err)
}
}
}
err = validateDocsDirStructure(tmpDir)
if tc.expectedErr && err == nil {
t.Errorf("Expected error, but got nil")
}
})
}
}
func TestContains(t *testing.T) {
testCases := []struct {
name string
items []string
item string
expected bool
}{
{
name: "Item in list",
items: []string{"item1", "item2", "item3"},
item: "item2",
expected: true,
},
{
name: "Item not in list",
items: []string{"item1", "item2", "item3"},
item: "item4",
expected: false,
},
{
name: "list is empty",
items: []string{},
item: "foo",
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := contains(tc.items, tc.item)
if result != tc.expected {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
})
}
}
|