1package bindata 2 3import ( 4 "os" 5 "path/filepath" 6 "testing" 7) 8 9func TestValidateInput(t *testing.T) { 10 tests := []struct { 11 desc string 12 cfg *Config 13 expErr string 14 }{{ 15 desc: `With empty list`, 16 cfg: &Config{}, 17 expErr: ErrNoInput.Error(), 18 }, { 19 desc: `With empty path`, 20 cfg: &Config{ 21 Input: []InputConfig{{ 22 Path: "", 23 }}, 24 }, 25 expErr: `Failed to stat input path '': lstat : no such file or directory`, 26 }, { 27 desc: `With directory not exist`, 28 cfg: &Config{ 29 Input: []InputConfig{{ 30 Path: "./notexist", 31 }}, 32 }, 33 expErr: `Failed to stat input path './notexist': lstat ./notexist: no such file or directory`, 34 }, { 35 desc: `With file as input`, 36 cfg: &Config{ 37 Input: []InputConfig{{ 38 Path: "./README.md", 39 }}, 40 }, 41 }} 42 43 for _, test := range tests { 44 t.Log(test.desc) 45 46 err := test.cfg.validateInput() 47 if err != nil { 48 assert(t, test.expErr, err.Error(), true) 49 continue 50 } 51 } 52} 53 54func TestValidateOutput(t *testing.T) { 55 cwd, err := os.Getwd() 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 tests := []struct { 61 desc string 62 cfg *Config 63 expErr string 64 expOutput string 65 }{{ 66 desc: `With empty`, 67 cfg: &Config{ 68 cwd: cwd, 69 }, 70 expOutput: filepath.Join(cwd, DefOutputName), 71 }, { 72 desc: `With unwriteable directory`, 73 cfg: &Config{ 74 Output: "/root/.ssh/template.go", 75 }, 76 expErr: `Create output directory: mkdir /root/.ssh/: permission denied`, 77 }, { 78 desc: `With unwriteable file`, 79 cfg: &Config{ 80 Output: "/template.go", 81 }, 82 expErr: `open /template.go: permission denied`, 83 }, { 84 desc: `With output as directory`, 85 cfg: &Config{ 86 Output: "/tmp/", 87 }, 88 expOutput: filepath.Join("/tmp", DefOutputName), 89 }} 90 91 for _, test := range tests { 92 t.Log(test.desc) 93 94 err := test.cfg.validateOutput() 95 if err != nil { 96 assert(t, test.expErr, err.Error(), true) 97 continue 98 } 99 100 assert(t, test.expOutput, test.cfg.Output, true) 101 } 102} 103