diff --git a/pkg/cmd.go b/pkg/cmd.go index 5dd6f0e..874488e 100644 --- a/pkg/cmd.go +++ b/pkg/cmd.go @@ -50,8 +50,10 @@ func ParseFlags(progname string, args []string) (*Config, string, error) { } // LoadConfig loads configuration from a YAML file +var readFileFunc = os.ReadFile + func LoadConfig(configPath string) (*Config, error) { - data, err := os.ReadFile(configPath) + data, err := readFileFunc(configPath) if err != nil { if os.IsNotExist(err) { // Return an empty config if the file does not exist diff --git a/pkg/cmd_test.go b/pkg/cmd_test.go index cfa2b8b..6b7e8c2 100644 --- a/pkg/cmd_test.go +++ b/pkg/cmd_test.go @@ -245,6 +245,20 @@ input: } } +func TestLoadConfig_PermissionDenied(t *testing.T) { + restrictedDir := "/restricted" + configFilePath := restrictedDir + "/restricted.yaml" + + readFileFunc = func(filename string) ([]byte, error) { + return nil, os.ErrPermission + } + defer func() { readFileFunc = os.ReadFile }() + + conf, err := LoadConfig(configFilePath) + assert.ErrorIs(t, err, os.ErrPermission, "Expected permission denied error") + assert.Nil(t, conf, "Expected config to be nil for permission denied error") +} + func TestMergeConfig(t *testing.T) { tests := []struct { name string