-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds website directory structure validation (#319)
Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
341 additions
and
677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module validate | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
docsDirEntry = "website/docs" | ||
) | ||
|
||
func main() { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
fmt.Println("error while getting pwd") | ||
panic(err) | ||
} | ||
rootDir := filepath.Join(pwd, "..", "..") | ||
|
||
err = validateDocsDirStructure(rootDir) | ||
if err != nil { | ||
fmt.Println("error while validating docs directory structure") | ||
panic(err) | ||
} | ||
} | ||
|
||
func validateDocsDirStructure(rootDir string) error { | ||
docsDirPath := filepath.Join(rootDir, docsDirEntry) | ||
// expected directory structure | ||
expectedDirs := []string{"mutation-examples", "validation"} | ||
expectedFiles := []string{"intro.md", "pspintro.md"} | ||
|
||
// Get the list of files and directories in the docs directory | ||
files, err := os.ReadDir(docsDirPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Validate the directory structure | ||
for _, file := range files { | ||
if file.IsDir() { | ||
if !contains(expectedDirs, file.Name()) { | ||
err = fmt.Errorf("unexpected directory: %s, found at: %s", file.Name(), filepath.Join(docsDirPath, file.Name())) | ||
return err | ||
} | ||
} else { | ||
if !contains(expectedFiles, file.Name()) { | ||
err = fmt.Errorf("unexpected file: %s, found at: %s", file.Name(), filepath.Join(docsDirPath, file.Name())) | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// Check for missing directories and files | ||
for _, expectedDir := range expectedDirs { | ||
if _, err := os.Stat(filepath.Join(docsDirPath, expectedDir)); os.IsNotExist(err) { | ||
err = fmt.Errorf("missing directory: %s", expectedDir) | ||
return err | ||
} | ||
} | ||
|
||
for _, expectedFile := range expectedFiles { | ||
if _, err := os.Stat(filepath.Join(docsDirPath, expectedFile)); os.IsNotExist(err) { | ||
err = fmt.Errorf("missing file: %s", expectedFile) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func contains(items []string, item string) bool { | ||
for _, i := range items { | ||
if strings.EqualFold(i, item) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
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, 0755) | ||
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) == "" { | ||
err = os.Mkdir(path, 0755) | ||
} 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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.