Skip to content

Commit

Permalink
Sort directory entries alphabetically and update tests for project st…
Browse files Browse the repository at this point in the history
…ructure output
  • Loading branch information
Ahmedhossamdev committed Jan 17, 2025
1 parent 7758c55 commit 0456fa9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
9 changes: 8 additions & 1 deletion pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)

Expand All @@ -29,11 +30,16 @@ func printTree(currentDir string, prefix string) {
}
defer dir.Close()

entries, err := dir.Readdir(-1)
entries, err := dir.Readdir(-1) // Read all entries
if err != nil {
return
}

// Sort entries alphabetically
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name() < entries[j].Name()
})

for i, entry := range entries {
// Skip hidden files/directories (those starting with ".")
if strings.HasPrefix(entry.Name(), ".") {
Expand All @@ -44,6 +50,7 @@ func printTree(currentDir string, prefix string) {

if entry.IsDir() {
fmt.Printf("%s%s/\n", prefix+getTreePrefix(isLast), entry.Name())
// Recursively print the contents of the directory
printTree(filepath.Join(currentDir, entry.Name()), prefix+getIndent(isLast))
} else {
fmt.Printf("%s%s\n", prefix+getTreePrefix(isLast), entry.Name())
Expand Down
36 changes: 24 additions & 12 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,80 @@ import (
"testing"
)

// TestPrintProjectStructure tests the PrintProjectStructure function.
func TestPrintProjectStructure(t *testing.T) {
// Create a temporary directory for testing
tmpDir := t.TempDir()

// Create the folder structure dynamically
createTestProjectStructure(t, tmpDir)

// Change the working directory to the temporary directory
oldDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %v", err)
}
defer os.Chdir(oldDir)
defer os.Chdir(oldDir) // Restore the original working directory
os.Chdir(tmpDir)

// Capture the output
output := captureOutput(func() {
PrintProjectStructure()
})

// Get the base name of the temporary directory
rootName := filepath.Base(tmpDir)

// TODO: Update the expected output (more smartly)
// Define the expected output based on the created structure
expected := rootName + "/\n" +
"├── main.go\n" +
"├── pkg/\n" +
"│ └── printer/\n" +
"│ ├── printer_test.go\n" +
"│ └── printer.go\n" +
"├── cmd/\n" +
"│ └── main.go\n" +
"├── go.mod\n" +
"└── internal/\n" +
" └── utils/\n" +
" └── utils.go\n"

"├── internal/\n" +
"│ └── utils/\n" +
"│ └── utils.go\n" +
"└── pkg/\n" +
" └── printer/\n" +
" ├── printer.go\n" +
" └── printer_test.go\n"
// Normalize the output and expected strings
output = strings.TrimSpace(output)
expected = strings.TrimSpace(expected)

// Compare the output
if output != expected {
t.Errorf("Unexpected output:\nGot:\n%s\nExpected:\n%s", output, expected)
}
}

// createTestProjectStructure creates a sample project structure for testing.
// createTestProjectStructure creates a sample project structure for testing.
func createTestProjectStructure(t *testing.T, root string) {
// Define the directories to create
dirs := []string{
"cmd",
"internal/utils",
"pkg/printer",
}

// Define the files to create
files := []string{
"cmd/main.go",
"internal/utils/utils.go",
"pkg/printer/printer.go",
"pkg/printer/printer_test.go",
"go.mod",
"main.go",
}

// Create directories
for _, dir := range dirs {
err := os.MkdirAll(filepath.Join(root, dir), 0755)
if err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
}

// Create files
for _, file := range files {
f, err := os.Create(filepath.Join(root, file))
if err != nil {
Expand All @@ -80,6 +91,7 @@ func createTestProjectStructure(t *testing.T, root string) {
}
}

// captureOutput captures the output printed to stdout.
func captureOutput(f func()) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down

0 comments on commit 0456fa9

Please sign in to comment.