55 "regexp"
66 "testing"
77
8- "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
98 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
109 "github.com/hashicorp/terraform-plugin-testing/knownvalue"
1110 "github.com/hashicorp/terraform-plugin-testing/plancheck"
@@ -19,39 +18,30 @@ func TestAccGithubRepositoryCustomProperty(t *testing.T) {
1918 t .Run ("single_select" , func (t * testing.T ) {
2019 t .Parallel ()
2120
22- randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
23- repoName := fmt .Sprintf ("%s%s" , testResourcePrefix , randomID )
24- propertyName := fmt .Sprintf ("%sproperty-%s" , testResourcePrefix , randomID )
25- option1 := "option1"
26- option2 := "option2"
21+ prop := mustCreateTestOrganizationRepositoryCustomProperty (t , "single_select" , []string {"option1" , "option2" })
22+ repo := mustCreateTestRepository (t )
23+ allowed := prop .GetAllowedValues ()
2724
2825 config := fmt .Sprintf (`
29- resource "github_organization_custom_properties" "test" {
30- allowed_values = ["%s", "%s"]
31- description = "Test Description"
32- property_name = "%s"
33- value_type = "single_select"
34- }
35-
36- resource "github_repository" "test" {
37- name = "%s"
38- auto_init = true
39- }
40-
4126resource "github_repository_custom_property" "test" {
42- repository = github_repository.test.name
43- property_name = github_organization_custom_properties.test.property_name
44- property_type = github_organization_custom_properties.test.value_type
27+ repository = "%s"
28+ property_name = "%s"
29+ property_type = "%s"
4530 property_value = %%s
4631}
47- ` , option1 , option2 , propertyName , repoName )
32+ ` , repo . GetName (), prop . GetPropertyName (), prop . GetValueType () )
4833
4934 resource .Test (t , resource.TestCase {
5035 PreCheck : func () { skipUnlessHasOrgs (t ) },
5136 ProviderFactories : providerFactories ,
5237 Steps : []resource.TestStep {
5338 {
54- Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , option1 )),
39+ Config : fmt .Sprintf (config , `[]` ),
40+ ExpectError : regexp .MustCompile (`Not enough list items` ),
41+ PlanOnly : true ,
42+ },
43+ {
44+ Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , allowed [0 ])),
5545 ConfigStateChecks : []statecheck.StateCheck {
5646 statecheck .ExpectKnownValue ("github_repository_custom_property.test" , tfjsonpath .New ("repository_id" ), knownvalue .NotNull ()),
5747 },
@@ -61,7 +51,7 @@ resource "github_repository_custom_property" "test" {
6151 ExpectError : regexp .MustCompile (`is not allowed for property` ),
6252 },
6353 {
64- Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , option2 )),
54+ Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , allowed [ 1 ] )),
6555 ConfigPlanChecks : resource.ConfigPlanChecks {
6656 PreApply : []plancheck.PlanCheck {
6757 plancheck .ExpectResourceAction ("github_repository_custom_property.test" , plancheck .ResourceActionUpdate ),
@@ -80,40 +70,30 @@ resource "github_repository_custom_property" "test" {
8070 t .Run ("multi_select" , func (t * testing.T ) {
8171 t .Parallel ()
8272
83- randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
84- repoName := fmt .Sprintf ("%s%s" , testResourcePrefix , randomID )
85- propertyName := fmt .Sprintf ("%sproperty-%s" , testResourcePrefix , randomID )
86- option1 := "option1"
87- option2 := "option2"
88- option3 := "option3"
73+ prop := mustCreateTestOrganizationRepositoryCustomProperty (t , "multi_select" , []string {"option1" , "option2" , "option3" })
74+ repo := mustCreateTestRepository (t )
75+ allowed := prop .GetAllowedValues ()
8976
9077 config := fmt .Sprintf (`
91- resource "github_organization_custom_properties" "test" {
92- allowed_values = ["%s", "%s", "%s"]
93- description = "Test Description"
94- property_name = "%s"
95- value_type = "multi_select"
96- }
97-
98- resource "github_repository" "test" {
99- name = "%s"
100- auto_init = true
101- }
102-
10378resource "github_repository_custom_property" "test" {
104- repository = github_repository.test.name
105- property_name = github_organization_custom_properties.test.property_name
106- property_type = github_organization_custom_properties.test.value_type
79+ repository = "%s"
80+ property_name = "%s"
81+ property_type = "%s"
10782 property_value = %%s
10883}
109- ` , option1 , option2 , option3 , propertyName , repoName )
84+ ` , repo . GetName (), prop . GetPropertyName (), prop . GetValueType () )
11085
11186 resource .Test (t , resource.TestCase {
11287 PreCheck : func () { skipUnlessHasOrgs (t ) },
11388 ProviderFactories : providerFactories ,
11489 Steps : []resource.TestStep {
11590 {
116- Config : fmt .Sprintf (config , fmt .Sprintf (`["%s", "%s"]` , option1 , option2 )),
91+ Config : fmt .Sprintf (config , `[]` ),
92+ ExpectError : regexp .MustCompile (`Not enough list items` ),
93+ PlanOnly : true ,
94+ },
95+ {
96+ Config : fmt .Sprintf (config , fmt .Sprintf (`["%s", "%s"]` , allowed [0 ], allowed [1 ])),
11797 ConfigStateChecks : []statecheck.StateCheck {
11898 statecheck .ExpectKnownValue ("github_repository_custom_property.test" , tfjsonpath .New ("repository_id" ), knownvalue .NotNull ()),
11999 },
@@ -123,7 +103,7 @@ resource "github_repository_custom_property" "test" {
123103 ExpectError : regexp .MustCompile (`is not allowed for property` ),
124104 },
125105 {
126- Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , option3 )),
106+ Config : fmt .Sprintf (config , fmt .Sprintf (`["%s"]` , allowed [ 2 ] )),
127107 ConfigPlanChecks : resource.ConfigPlanChecks {
128108 PreApply : []plancheck.PlanCheck {
129109 plancheck .ExpectResourceAction ("github_repository_custom_property.test" , plancheck .ResourceActionUpdate ),
@@ -142,34 +122,27 @@ resource "github_repository_custom_property" "test" {
142122 t .Run ("true_false" , func (t * testing.T ) {
143123 t .Parallel ()
144124
145- randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
146- repoName := fmt .Sprintf ("%s%s" , testResourcePrefix , randomID )
147- propertyName := fmt .Sprintf ("%sproperty-%s" , testResourcePrefix , randomID )
125+ prop := mustCreateTestOrganizationRepositoryCustomProperty (t , "true_false" , nil )
126+ repo := mustCreateTestRepository (t )
148127
149128 config := fmt .Sprintf (`
150- resource "github_organization_custom_properties" "test" {
151- description = "Test Description"
152- property_name = "%s"
153- value_type = "true_false"
154- }
155-
156- resource "github_repository" "test" {
157- name = "%s"
158- auto_init = true
159- }
160-
161129resource "github_repository_custom_property" "test" {
162- repository = github_repository.test.name
163- property_name = github_organization_custom_properties.test.property_name
164- property_type = github_organization_custom_properties.test.value_type
130+ repository = "%s"
131+ property_name = "%s"
132+ property_type = "%s"
165133 property_value = %%s
166134}
167- ` , propertyName , repoName )
135+ ` , repo . GetName (), prop . GetPropertyName (), prop . GetValueType () )
168136
169137 resource .Test (t , resource.TestCase {
170138 PreCheck : func () { skipUnlessHasOrgs (t ) },
171139 ProviderFactories : providerFactories ,
172140 Steps : []resource.TestStep {
141+ {
142+ Config : fmt .Sprintf (config , `[]` ),
143+ ExpectError : regexp .MustCompile (`Not enough list items` ),
144+ PlanOnly : true ,
145+ },
173146 {
174147 Config : fmt .Sprintf (config , `["true"]` ),
175148 ConfigStateChecks : []statecheck.StateCheck {
@@ -200,34 +173,27 @@ resource "github_repository_custom_property" "test" {
200173 t .Run ("url" , func (t * testing.T ) {
201174 t .Parallel ()
202175
203- randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
204- repoName := fmt .Sprintf ("%s%s" , testResourcePrefix , randomID )
205- propertyName := fmt .Sprintf ("%sproperty-%s" , testResourcePrefix , randomID )
176+ prop := mustCreateTestOrganizationRepositoryCustomProperty (t , "url" , nil )
177+ repo := mustCreateTestRepository (t )
206178
207179 config := fmt .Sprintf (`
208- resource "github_organization_custom_properties" "test" {
209- description = "Test Description"
210- property_name = "%s"
211- value_type = "url"
212- }
213-
214- resource "github_repository" "test" {
215- name = "%s"
216- auto_init = true
217- }
218-
219180resource "github_repository_custom_property" "test" {
220- repository = github_repository.test.name
221- property_name = github_organization_custom_properties.test.property_name
222- property_type = github_organization_custom_properties.test.value_type
181+ repository = "%s"
182+ property_name = "%s"
183+ property_type = "%s"
223184 property_value = %%s
224185}
225- ` , propertyName , repoName )
186+ ` , repo . GetName (), prop . GetPropertyName (), prop . GetValueType () )
226187
227188 resource .Test (t , resource.TestCase {
228189 PreCheck : func () { skipUnlessHasOrgs (t ) },
229190 ProviderFactories : providerFactories ,
230191 Steps : []resource.TestStep {
192+ {
193+ Config : fmt .Sprintf (config , `[]` ),
194+ ExpectError : regexp .MustCompile (`Not enough list items` ),
195+ PlanOnly : true ,
196+ },
231197 {
232198 Config : fmt .Sprintf (config , `["https://example.com"]` ),
233199 ConfigStateChecks : []statecheck.StateCheck {
@@ -258,29 +224,17 @@ resource "github_repository_custom_property" "test" {
258224 t .Run ("string" , func (t * testing.T ) {
259225 t .Parallel ()
260226
261- randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
262- repoName := fmt .Sprintf ("%s%s" , testResourcePrefix , randomID )
263- propertyName := fmt .Sprintf ("%sproperty-%s" , testResourcePrefix , randomID )
227+ prop := mustCreateTestOrganizationRepositoryCustomProperty (t , "string" , nil )
228+ repo := mustCreateTestRepository (t )
264229
265230 config := fmt .Sprintf (`
266- resource "github_organization_custom_properties" "test" {
267- description = "Test Description"
268- property_name = "%s"
269- value_type = "string"
270- }
271-
272- resource "github_repository" "test" {
273- name = "%s"
274- auto_init = true
275- }
276-
277231resource "github_repository_custom_property" "test" {
278- repository = github_repository.test.name
279- property_name = github_organization_custom_properties.test.property_name
280- property_type = github_organization_custom_properties.test.value_type
232+ repository = "%s"
233+ property_name = "%s"
234+ property_type = "%s"
281235 property_value = %%s
282236}
283- ` , propertyName , repoName )
237+ ` , repo . GetName (), prop . GetPropertyName (), prop . GetValueType () )
284238
285239 resource .Test (t , resource.TestCase {
286240 PreCheck : func () { skipUnlessHasOrgs (t ) },
0 commit comments