|
1 | 1 | package command |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
4 | 7 | "testing" |
5 | 8 |
|
| 9 | + "github.com/pkg/errors" |
6 | 10 | "gotest.tools/assert" |
7 | 11 | ) |
8 | 12 |
|
@@ -31,3 +35,39 @@ func TestStringSliceReplaceAt(t *testing.T) { |
31 | 35 | assert.Assert(t, !ok) |
32 | 36 | assert.DeepEqual(t, []string{"foo"}, out) |
33 | 37 | } |
| 38 | + |
| 39 | +func TestValidateOutputPath(t *testing.T) { |
| 40 | + basedir, err := ioutil.TempDir("", "TestValidateOutputPath") |
| 41 | + assert.NilError(t, err) |
| 42 | + defer os.RemoveAll(basedir) |
| 43 | + dir := filepath.Join(basedir, "dir") |
| 44 | + notexist := filepath.Join(basedir, "notexist") |
| 45 | + err = os.MkdirAll(dir, 0755) |
| 46 | + assert.NilError(t, err) |
| 47 | + file := filepath.Join(dir, "file") |
| 48 | + err = ioutil.WriteFile(file, []byte("hi"), 0644) |
| 49 | + assert.NilError(t, err) |
| 50 | + var testcases = []struct { |
| 51 | + path string |
| 52 | + err error |
| 53 | + }{ |
| 54 | + {basedir, nil}, |
| 55 | + {file, nil}, |
| 56 | + {dir, nil}, |
| 57 | + {dir + string(os.PathSeparator), nil}, |
| 58 | + {notexist, nil}, |
| 59 | + {notexist + string(os.PathSeparator), nil}, |
| 60 | + {filepath.Join(notexist, "file"), errors.New("does not exist")}, |
| 61 | + } |
| 62 | + |
| 63 | + for _, testcase := range testcases { |
| 64 | + t.Run(testcase.path, func(t *testing.T) { |
| 65 | + err := ValidateOutputPath(testcase.path) |
| 66 | + if testcase.err == nil { |
| 67 | + assert.NilError(t, err) |
| 68 | + } else { |
| 69 | + assert.ErrorContains(t, err, testcase.err.Error()) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments