Skip to content

Commit

Permalink
Add validation for output folder and enhance unit tests for edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikeysemwal committed Sep 18, 2024
1 parent 2bd3ae4 commit 0bfbd90
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/input/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package input
import (
"fmt"
"os"
"path/filepath"
"strings"

fileUtils "github.com/edoardottt/cariddi/internal/file"
Expand All @@ -47,6 +48,33 @@ func CheckOutputFile(input string) bool {
return true
}

// IsValidOutputPath checks if the directory is valid and, if created, cleans it up.
func CheckOutputPath(outputPath string) bool {
// Convert to absolute path if necessary
absPath, err := filepath.Abs(outputPath)
if err != nil {
return false
}

// Check if the directory already exists
_, err = os.Stat(absPath)
if err == nil {
// Directory exists, so it's valid, no need to delete
return true
}

// If the directory does not exist, try to create it
err = os.MkdirAll(absPath, os.ModePerm)
if err != nil {
return false
}

// Since we created the directory, clean it up
defer os.RemoveAll(absPath)

return true
}

// CheckFlags checks the flags taken as input.
func CheckFlags(flags Input) {
if flags.TXTout != "" {
Expand Down Expand Up @@ -127,4 +155,11 @@ func CheckFlags(flags Input) {
fmt.Println(" - cat urls | cariddi -headersfile headers.txt")
os.Exit(1)
}

if flags.StoredRespDir != "" {
if !CheckOutputPath(flags.StoredRespDir) {
fmt.Println("Validation failed for srd flag; there may be errors creating the output directory.")
os.Exit(1)
}
}
}
58 changes: 58 additions & 0 deletions pkg/input/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package input_test

import (
"os"
"path/filepath"
"testing"

"github.com/edoardottt/cariddi/pkg/input"
)

// TestCheckOutputPath tests the CheckOutputPath function for valid and invalid cases.
func TestCheckOutputPath(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "testdir")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tmpDir) // Cleanup after test

Check failure on line 18 in pkg/input/check_test.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before defer statement (wsl)

// Valid case: Existing directory
if !input.CheckOutputPath(tmpDir) {
t.Errorf("CheckOutputPath(%s) = false; want true", tmpDir)
}

// Verify that the existing directory is still present after check
_, err = os.Stat(tmpDir)
if err != nil {
t.Errorf("Existing directory %s should still be present after CheckOutputPath, but got error: %v", tmpDir, err)
}

// Cross-platform invalid cases
invalidPaths := []string{
// Null character is invalid on all platforms
filepath.Join(tmpDir, "invalid\000path"),
// Reserved names (common issues across various OS)
filepath.Join(tmpDir, "CON"),
// Paths with excessively long names (common path length issues)
filepath.Join(tmpDir, string(make([]byte, 260))),
}

for _, invalidPath := range invalidPaths {
if input.CheckOutputPath(invalidPath) {
t.Errorf("CheckOutputPath(%s) = true; want false", invalidPath)
}
}

// Valid case: New directory creation and cleanup
newDir := filepath.Join(tmpDir, "newdir")
if !input.CheckOutputPath(newDir) {
t.Errorf("CheckOutputPath(%s) = false; want true", newDir)
}

// After check, the directory should be removed
_, err = os.Stat(newDir)
if err == nil || !os.IsNotExist(err) {
t.Errorf("CheckOutputPath should remove the directory, but it still exists: %s", newDir)
}
}

0 comments on commit 0bfbd90

Please sign in to comment.