(t *testing.T)
| 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 | } |
nothing calls this directly
no test coverage detected