@@ -18,13 +18,17 @@ import (
1818 "context"
1919 "fmt"
2020 "math/rand"
21+ "net/http"
22+ "net/http/httptest"
23+ "net/url"
2124 "os"
2225 "testing"
2326
2427 "github.com/form3tech-oss/go-github-utils/pkg/branch"
2528 ghfileutils "github.com/form3tech-oss/go-github-utils/pkg/file"
2629 "github.com/google/go-github/v54/github"
2730 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
31+ "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2832 "github.com/hashicorp/terraform-plugin-sdk/terraform"
2933 "golang.org/x/oauth2"
3034)
@@ -207,3 +211,82 @@ func testAccCheckFileDestroy(s *terraform.State) error {
207211 }
208212 return nil
209213}
214+
215+ // newTestResourceData creates a schema.ResourceData with the file resource schema
216+ // populated from the given raw values, suitable for unit testing.
217+ func newTestResourceData (t * testing.T , values map [string ]interface {}) * schema.ResourceData {
218+ t .Helper ()
219+ return schema .TestResourceDataRaw (t , resourceFile ().Schema , values )
220+ }
221+
222+ // newMockGitHubClient creates a github.Client pointing at the given httptest.Server.
223+ func newMockGitHubClient (server * httptest.Server ) * github.Client {
224+ client := github .NewClient (server .Client ())
225+ serverURL , _ := url .Parse (server .URL + "/" )
226+ client .BaseURL = serverURL
227+ return client
228+ }
229+
230+ func TestResourceFileDelete_ArchivedRepo (t * testing.T ) {
231+ // Set up a mock HTTP server that returns an archived repository
232+ mux := http .NewServeMux ()
233+ mux .HandleFunc ("/repos/test-owner/test-repo" , func (w http.ResponseWriter , r * http.Request ) {
234+ w .Header ().Set ("Content-Type" , "application/json" )
235+ fmt .Fprint (w , `{"id":1,"name":"test-repo","full_name":"test-owner/test-repo","archived":true}` )
236+ })
237+ server := httptest .NewServer (mux )
238+ defer server .Close ()
239+
240+ config := & providerConfiguration {
241+ githubClient : newMockGitHubClient (server ),
242+ }
243+
244+ d := newTestResourceData (t , map [string ]interface {}{
245+ repositoryOwnerAttributeName : "test-owner" ,
246+ repositoryNameAttributeName : "test-repo" ,
247+ branchAttributeName : "main" ,
248+ pathAttributeName : "some/file.txt" ,
249+ contentsAttributeName : "some content" ,
250+ })
251+ d .SetId ("test-owner/test-repo:main:some/file.txt" )
252+
253+ // Delete should succeed without error — archived repo skips deletion
254+ err := resourceFileDelete (d , config )
255+ if err != nil {
256+ t .Fatalf ("expected no error for archived repo deletion, got: %v" , err )
257+ }
258+ }
259+
260+ func TestResourceFileDelete_NonArchivedRepo_FileNotFound (t * testing.T ) {
261+ // Set up a mock HTTP server that returns a non-archived repository
262+ // and a 404 for the file content (file doesn't exist)
263+ mux := http .NewServeMux ()
264+ mux .HandleFunc ("/repos/test-owner/test-repo" , func (w http.ResponseWriter , r * http.Request ) {
265+ w .Header ().Set ("Content-Type" , "application/json" )
266+ fmt .Fprint (w , `{"id":1,"name":"test-repo","full_name":"test-owner/test-repo","archived":false}` )
267+ })
268+ mux .HandleFunc ("/repos/test-owner/test-repo/contents/some/file.txt" , func (w http.ResponseWriter , r * http.Request ) {
269+ w .WriteHeader (http .StatusNotFound )
270+ })
271+ server := httptest .NewServer (mux )
272+ defer server .Close ()
273+
274+ config := & providerConfiguration {
275+ githubClient : newMockGitHubClient (server ),
276+ }
277+
278+ d := newTestResourceData (t , map [string ]interface {}{
279+ repositoryOwnerAttributeName : "test-owner" ,
280+ repositoryNameAttributeName : "test-repo" ,
281+ branchAttributeName : "main" ,
282+ pathAttributeName : "some/file.txt" ,
283+ contentsAttributeName : "some content" ,
284+ })
285+ d .SetId ("test-owner/test-repo:main:some/file.txt" )
286+
287+ // Delete should succeed — file not found is treated as already deleted
288+ err := resourceFileDelete (d , config )
289+ if err != nil {
290+ t .Fatalf ("expected no error when file not found on non-archived repo, got: %v" , err )
291+ }
292+ }
0 commit comments