github.com/adevinta/lava@v0.7.2/internal/assettypes/assettypes_test.go (about) 1 // Copyright 2023 Adevinta 2 3 package assettypes 4 5 import ( 6 "errors" 7 "io/fs" 8 "regexp" 9 "testing" 10 11 types "github.com/adevinta/vulcan-types" 12 ) 13 14 func TestIsValid(t *testing.T) { 15 tests := []struct { 16 name string 17 at types.AssetType 18 want bool 19 }{ 20 { 21 name: "lava type", 22 at: Path, 23 want: true, 24 }, 25 { 26 name: "vulcan type", 27 at: types.Hostname, 28 want: false, 29 }, 30 { 31 name: "zero value", 32 at: types.AssetType(""), 33 want: false, 34 }, 35 } 36 37 for _, tt := range tests { 38 t.Run(tt.name, func(t *testing.T) { 39 got := IsValid(tt.at) 40 if got != tt.want { 41 t.Errorf("unexpected value: want: %v, got: %v", tt.want, got) 42 } 43 }) 44 } 45 } 46 47 func TestToVulcan(t *testing.T) { 48 tests := []struct { 49 name string 50 at types.AssetType 51 want types.AssetType 52 }{ 53 { 54 name: "lava type", 55 at: Path, 56 want: types.GitRepository, 57 }, 58 { 59 name: "vulcan type", 60 at: types.Hostname, 61 want: types.Hostname, 62 }, 63 { 64 name: "zero value", 65 at: types.AssetType(""), 66 want: types.AssetType(""), 67 }, 68 } 69 70 for _, tt := range tests { 71 t.Run(tt.name, func(t *testing.T) { 72 got := ToVulcan(tt.at) 73 if got != tt.want { 74 t.Errorf("unexpected value: want: %v, got: %v", tt.want, got) 75 } 76 }) 77 } 78 } 79 80 func TestCheckReachable(t *testing.T) { 81 tests := []struct { 82 name string 83 typ types.AssetType 84 ident string 85 wantErr error 86 wantErrRegexp *regexp.Regexp 87 }{ 88 { 89 name: "git folder", 90 typ: types.GitRepository, 91 ident: "testdata", 92 wantErr: nil, 93 }, 94 { 95 name: "git file", 96 typ: types.GitRepository, 97 ident: "testdata/foo.txt", 98 wantErrRegexp: regexp.MustCompile(`^not a directory$`), 99 }, 100 { 101 name: "git not exists", 102 typ: types.GitRepository, 103 ident: "notexists", 104 wantErr: ErrUnsupported, 105 }, 106 { 107 name: "path folder", 108 typ: Path, 109 ident: "testdata", 110 wantErr: nil, 111 }, 112 { 113 name: "path file", 114 typ: Path, 115 ident: "testdata/foo.txt", 116 wantErr: nil, 117 }, 118 { 119 name: "path not exists", 120 typ: Path, 121 ident: "notexists", 122 wantErr: fs.ErrNotExist, 123 }, 124 { 125 name: "unsupported asset type", 126 typ: types.AWSAccount, 127 ident: "012345678901", 128 wantErr: ErrUnsupported, 129 }, 130 } 131 132 for _, tt := range tests { 133 t.Run(tt.name, func(t *testing.T) { 134 err := CheckReachable(tt.typ, tt.ident) 135 switch { 136 case tt.wantErr != nil: 137 if !errors.Is(err, tt.wantErr) { 138 t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr) 139 } 140 case tt.wantErrRegexp != nil: 141 if err == nil { 142 t.Errorf("unexpected nil error: want: %v", tt.wantErrRegexp) 143 } else if !tt.wantErrRegexp.MatchString(err.Error()) { 144 t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErrRegexp) 145 } 146 default: 147 if err != nil { 148 t.Errorf("unexpected error: %v", err) 149 } 150 } 151 }) 152 } 153 }