github.com/opentofu/opentofu@v1.7.1/internal/command/jsonconfig/config_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package jsonconfig 7 8 import ( 9 "testing" 10 ) 11 12 func TestFindSourceProviderConfig(t *testing.T) { 13 tests := []struct { 14 StartKey string 15 FullName string 16 ProviderMap map[string]providerConfig 17 Want string 18 }{ 19 { 20 StartKey: "null", 21 FullName: "hashicorp/null", 22 ProviderMap: map[string]providerConfig{}, 23 Want: "", 24 }, 25 { 26 StartKey: "null", 27 FullName: "hashicorp/null", 28 ProviderMap: map[string]providerConfig{ 29 "null": { 30 Name: "null", 31 FullName: "hashicorp/null", 32 ModuleAddress: "", 33 }, 34 }, 35 Want: "null", 36 }, 37 { 38 StartKey: "null2", 39 FullName: "hashicorp/null", 40 ProviderMap: map[string]providerConfig{ 41 "null": { 42 Name: "null", 43 FullName: "hashicorp/null", 44 ModuleAddress: "", 45 }, 46 }, 47 Want: "", 48 }, 49 { 50 StartKey: "null", 51 FullName: "hashicorp2/null", 52 ProviderMap: map[string]providerConfig{ 53 "null": { 54 Name: "null", 55 FullName: "hashicorp/null", 56 ModuleAddress: "", 57 }, 58 }, 59 Want: "", 60 }, 61 { 62 StartKey: "module.a:null", 63 FullName: "hashicorp/null", 64 ProviderMap: map[string]providerConfig{ 65 "null": { 66 Name: "null", 67 FullName: "hashicorp/null", 68 ModuleAddress: "", 69 }, 70 "module.a:null": { 71 Name: "module.a:null", 72 FullName: "hashicorp/null", 73 ModuleAddress: "module.a", 74 parentKey: "null", 75 }, 76 }, 77 Want: "null", 78 }, 79 { 80 StartKey: "module.a:null", 81 FullName: "hashicorp2/null", 82 ProviderMap: map[string]providerConfig{ 83 "null": { 84 Name: "null", 85 FullName: "hashicorp/null", 86 ModuleAddress: "", 87 }, 88 "module.a:null": { 89 Name: "module.a:null", 90 FullName: "hashicorp2/null", 91 ModuleAddress: "module.a", 92 parentKey: "null", 93 }, 94 }, 95 Want: "module.a:null", 96 }, 97 } 98 99 for _, test := range tests { 100 got := findSourceProviderKey(test.StartKey, test.FullName, test.ProviderMap) 101 if got != test.Want { 102 t.Errorf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want) 103 } 104 } 105 }