@@ -420,4 +420,68 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) {
420
420
})
421
421
422
422
})
423
+
424
+ repoContentDirectoryRespBody := marshal (t , []github.RepositoryContent {
425
+ {
426
+ Encoding : & enc ,
427
+ Content : & b64FileContent ,
428
+ SHA : & sha ,
429
+ URL : & apiUrl ,
430
+ },
431
+ })
432
+
433
+ t .Run ("extract only non-file data if the path is for a directory" , func (t * testing.T ) {
434
+ // test setup
435
+ repositoryFullName := fmt .Sprintf ("%s/%s" , org , repo )
436
+
437
+ expectedID := fmt .Sprintf ("%s/%s" , repo , fileName )
438
+ expectedRepo := "test-repo"
439
+
440
+ ts := githubApiMock ([]* mockResponse {
441
+ {
442
+ ExpectedUri : fmt .Sprintf ("/repos/%s/%s/contents/%s?ref=%s" , org , repo , fileName , branch ),
443
+ ResponseBody : repoContentDirectoryRespBody ,
444
+ StatusCode : http .StatusOK ,
445
+ },
446
+ })
447
+ defer ts .Close ()
448
+
449
+ httpCl := http .DefaultClient
450
+ httpCl .Transport = http .DefaultTransport
451
+
452
+ client := github .NewClient (httpCl )
453
+ u , _ := url .Parse (ts .URL + "/" )
454
+ client .BaseURL = u
455
+
456
+ meta := & Owner {
457
+ name : owner ,
458
+ v3client : client ,
459
+ }
460
+
461
+ testSchema := map [string ]* schema.Schema {
462
+ "repository" : {Type : schema .TypeString },
463
+ "file" : {Type : schema .TypeString },
464
+ "branch" : {Type : schema .TypeString },
465
+ "commit_sha" : {Type : schema .TypeString },
466
+ "content" : {Type : schema .TypeString },
467
+ "id" : {Type : schema .TypeString },
468
+ }
469
+
470
+ schema := schema .TestResourceDataRaw (t , testSchema , map [string ]interface {}{
471
+ "repository" : repositoryFullName ,
472
+ "file" : fileName ,
473
+ "branch" : branch ,
474
+ "commit_sha" : sha ,
475
+ })
476
+
477
+ // actual call
478
+ err := dataSourceGithubRepositoryFileRead (schema , meta )
479
+
480
+ // assertions
481
+ assert .Nil (t , err )
482
+ assert .Equal (t , expectedRepo , schema .Get ("repository" ))
483
+ assert .Equal (t , expectedID , schema .Get ("id" ))
484
+ assert .Equal (t , "" , schema .Get ("content" ))
485
+ assert .Equal (t , nil , schema .Get ("sha" ))
486
+ })
423
487
}
0 commit comments