|
| 1 | +// Copyright (c) 2019 voidint <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | +// this software and associated documentation files (the "Software"), to deal in |
| 5 | +// the Software without restriction, including without limitation the rights to |
| 6 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 | +// subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + |
| 20 | +package cli |
| 21 | + |
| 22 | +import ( |
| 23 | + "errors" |
| 24 | + "io/fs" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func TestListLocalVersions(t *testing.T) { |
| 33 | + tmpDir := t.TempDir() |
| 34 | + |
| 35 | + os.Mkdir(filepath.Join(tmpDir, "1.25rc1"), 0755) |
| 36 | + os.Mkdir(filepath.Join(tmpDir, "1.24rc3"), 0755) |
| 37 | + os.Mkdir(filepath.Join(tmpDir, "1.18beta2"), 0755) |
| 38 | + os.Mkdir(filepath.Join(tmpDir, "1.19beta1"), 0755) |
| 39 | + os.Mkdir(filepath.Join(tmpDir, "1.24.4"), 0755) |
| 40 | + os.Mkdir(filepath.Join(tmpDir, "1.20.14"), 0755) |
| 41 | + os.Mkdir(filepath.Join(tmpDir, "1.20"), 0755) |
| 42 | + os.Mkdir(filepath.Join(tmpDir, "invalid_version"), 0755) |
| 43 | + os.WriteFile(filepath.Join(tmpDir, "not_a_directory"), []byte{}, 0644) |
| 44 | + |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + dirPath string |
| 48 | + vnames []string |
| 49 | + checkErr func(error) bool |
| 50 | + }{ |
| 51 | + {"Directory does not exist", "/not/exist/path", nil, func(err error) bool { |
| 52 | + var pathError *fs.PathError |
| 53 | + return errors.As(err, &pathError) |
| 54 | + }}, |
| 55 | + |
| 56 | + {"The file path is not a directory", filepath.Join(tmpDir, "not_a_directory"), nil, func(err error) bool { |
| 57 | + var pathError *fs.PathError |
| 58 | + return errors.As(err, &pathError) |
| 59 | + }}, |
| 60 | + |
| 61 | + {"正常版本目录", tmpDir, []string{"1.18beta2", "1.19beta1", "1.20", "1.20.14", "1.24rc3", "1.24.4", "1.25rc1"}, func(err error) bool { |
| 62 | + return err == nil |
| 63 | + }}, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + got, err := listLocalVersions(tt.dirPath) |
| 69 | + assert.True(t, tt.checkErr(err)) |
| 70 | + |
| 71 | + var actualNames []string |
| 72 | + for _, v := range got { |
| 73 | + actualNames = append(actualNames, v.Name()) |
| 74 | + } |
| 75 | + assert.Equal(t, tt.vnames, actualNames) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments