github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/getproviders/hash_test.go (about) 1 package getproviders 2 3 import ( 4 "testing" 5 ) 6 7 func TestParseHash(t *testing.T) { 8 tests := []struct { 9 Input string 10 Want Hash 11 WantErr string 12 }{ 13 { 14 Input: "h1:foo", 15 Want: HashScheme1.New("foo"), 16 }, 17 { 18 Input: "zh:bar", 19 Want: HashSchemeZip.New("bar"), 20 }, 21 { 22 // A scheme we don't know is considered valid syntax, it just won't match anything. 23 Input: "unknown:baz", 24 Want: HashScheme("unknown:").New("baz"), 25 }, 26 { 27 // A scheme with an empty value is weird, but allowed. 28 Input: "unknown:", 29 Want: HashScheme("unknown:").New(""), 30 }, 31 { 32 Input: "", 33 WantErr: "hash string must start with a scheme keyword followed by a colon", 34 }, 35 { 36 // A naked SHA256 hash in hex format is not sufficient 37 Input: "1e5f7a5f3ade7b8b1d1d59c5cea2e1a2f8d2f8c3f41962dbbe8647e222be8239", 38 WantErr: "hash string must start with a scheme keyword followed by a colon", 39 }, 40 { 41 // An empty scheme is not allowed 42 Input: ":blah", 43 WantErr: "hash string must start with a scheme keyword followed by a colon", 44 }, 45 } 46 47 for _, test := range tests { 48 t.Run(test.Input, func(t *testing.T) { 49 got, err := ParseHash(test.Input) 50 51 if test.WantErr != "" { 52 if err == nil { 53 t.Fatalf("want error: %s", test.WantErr) 54 } 55 if got, want := err.Error(), test.WantErr; got != want { 56 t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want) 57 } 58 return 59 } 60 61 if err != nil { 62 t.Fatalf("unexpected error: %s", err.Error()) 63 } 64 65 if got != test.Want { 66 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 67 } 68 }) 69 } 70 }