Skip to content

Commit b04124f

Browse files
authored
fix: Increase tolerance for invalid input directories (#410)
1 parent 66d2080 commit b04124f

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

pkg/app/app.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ func defaultFuncApiUrl(_ configuration.Configuration, logger *zerolog.Logger) co
115115

116116
func defaultInputDirectory() configuration.DefaultValueFunction {
117117
callback := func(_ configuration.Configuration, existingValue interface{}) (interface{}, error) {
118-
if existingValue == nil {
118+
existingString, isString := existingValue.(string)
119+
if isString {
120+
existingString = strings.TrimSpace(existingString)
121+
}
122+
123+
if len(existingString) == 0 {
119124
path, err := os.Getwd()
120125
if err != nil {
121126
return "", err
122127
}
123128
return path, nil
124129
} else {
125-
return existingValue, nil
130+
return existingString, nil
126131
}
127132
}
128133
return callback

pkg/app/app_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,148 @@ func createMockPAT(t *testing.T, payload string) string {
444444
signature := "signature"
445445
return fmt.Sprintf("snyk_uat.12345678.%s.%s", encodedPayload, signature)
446446
}
447+
448+
func TestDefaultInputDirectory(t *testing.T) {
449+
defaultFunction := defaultInputDirectory()
450+
assert.NotNil(t, defaultFunction)
451+
452+
// Create a mock configuration for testing
453+
mockConfig := configuration.New()
454+
455+
tests := []struct {
456+
name string
457+
existingValue interface{}
458+
expectedError bool
459+
expectedResult interface{}
460+
description string
461+
}{
462+
{
463+
name: "nil input",
464+
existingValue: nil,
465+
expectedError: false,
466+
expectedResult: nil, // Will fall back to current working directory
467+
description: "should handle nil input gracefully and return current working directory",
468+
},
469+
{
470+
name: "empty string",
471+
existingValue: "",
472+
expectedError: false,
473+
expectedResult: nil, // Will fall back to current working directory
474+
description: "should handle empty string and return current working directory",
475+
},
476+
{
477+
name: "whitespace only string",
478+
existingValue: " \t\n ",
479+
expectedError: false,
480+
expectedResult: nil, // Will fall back to current working directory after trimming
481+
description: "should handle whitespace-only string and return current working directory",
482+
},
483+
{
484+
name: "valid absolute path",
485+
existingValue: "/usr/local/bin",
486+
expectedError: false,
487+
expectedResult: "/usr/local/bin",
488+
description: "should return valid absolute path as-is",
489+
},
490+
{
491+
name: "valid relative path",
492+
existingValue: "./relative/path",
493+
expectedError: false,
494+
expectedResult: "./relative/path",
495+
description: "should return valid relative path as-is",
496+
},
497+
{
498+
name: "path with leading/trailing whitespace",
499+
existingValue: " /path/with/whitespace ",
500+
expectedError: false,
501+
expectedResult: "/path/with/whitespace",
502+
description: "should trim whitespace and return clean path",
503+
},
504+
{
505+
name: "non-string type - integer",
506+
existingValue: 123,
507+
expectedError: false,
508+
expectedResult: nil, // Will fall back to current working directory
509+
description: "should handle non-string types gracefully and return current working directory",
510+
},
511+
{
512+
name: "non-string type - boolean",
513+
existingValue: true,
514+
expectedError: false,
515+
expectedResult: nil, // Will fall back to current working directory
516+
description: "should handle non-string types gracefully and return current working directory",
517+
},
518+
{
519+
name: "non-string type - slice",
520+
existingValue: []string{"path1", "path2"},
521+
expectedError: false,
522+
expectedResult: nil, // Will fall back to current working directory
523+
description: "should handle non-string types gracefully and return current working directory",
524+
},
525+
{
526+
name: "non-string type - map",
527+
existingValue: map[string]string{"key": "value"},
528+
expectedError: false,
529+
expectedResult: nil, // Will fall back to current working directory
530+
description: "should handle non-string types gracefully and return current working directory",
531+
},
532+
{
533+
name: "current directory symbol",
534+
existingValue: ".",
535+
expectedError: false,
536+
expectedResult: ".",
537+
description: "should return current directory symbol as-is",
538+
},
539+
{
540+
name: "parent directory symbol",
541+
existingValue: "..",
542+
expectedError: false,
543+
expectedResult: "..",
544+
description: "should return parent directory symbol as-is",
545+
},
546+
{
547+
name: "home directory symbol",
548+
existingValue: "~",
549+
expectedError: false,
550+
expectedResult: "~",
551+
description: "should return home directory symbol as-is",
552+
},
553+
{
554+
name: "path with special characters",
555+
existingValue: "/path/with/special-chars_123",
556+
expectedError: false,
557+
expectedResult: "/path/with/special-chars_123",
558+
description: "should handle paths with special characters",
559+
},
560+
}
561+
562+
for _, tt := range tests {
563+
t.Run(tt.name, func(t *testing.T) {
564+
result, err := defaultFunction(mockConfig, tt.existingValue)
565+
566+
// Check error expectations
567+
if tt.expectedError {
568+
assert.Error(t, err)
569+
} else {
570+
assert.NoError(t, err)
571+
}
572+
573+
// Check result expectations
574+
if tt.expectedResult != nil {
575+
// For specific expected results, check exact match
576+
assert.Equal(t, tt.expectedResult, result, tt.description)
577+
} else {
578+
// For fallback cases, just ensure we get a non-empty result
579+
assert.NotNil(t, result, tt.description)
580+
if str, ok := result.(string); ok {
581+
assert.NotEmpty(t, str, tt.description)
582+
}
583+
}
584+
})
585+
}
586+
587+
// Additional test to verify the function actually returns a function
588+
t.Run("returns callable function", func(t *testing.T) {
589+
assert.IsType(t, defaultFunction, defaultFunction)
590+
})
591+
}

0 commit comments

Comments
 (0)