...

Source file src/github.com/palantir/go-githubapp/appconfig/appconfig_test.go

Documentation: github.com/palantir/go-githubapp/appconfig

     1  // Copyright 2021 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package appconfig
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"net/http"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/google/go-github/v47/github"
    25  )
    26  
    27  const (
    28  	TestOwner = "test"
    29  	TestRef   = "develop"
    30  )
    31  
    32  func TestLoadConfig(t *testing.T) {
    33  	tests := map[string]struct {
    34  		Paths    []string
    35  		Options  []Option
    36  		Repo     string
    37  		Expected Config
    38  		Error    bool
    39  	}{
    40  		"localFile": {
    41  			Paths: []string{".github/test-app.yml"},
    42  			Repo:  "local-file",
    43  			Expected: Config{
    44  				Content: []byte("message: hello\n"),
    45  				Source:  "test/local-file@develop",
    46  				Path:    ".github/test-app.yml",
    47  			},
    48  		},
    49  		"localFileFallback": {
    50  			Paths: []string{".github/test-app.v2.yml", ".github/test-app.yml"},
    51  			Repo:  "local-file",
    52  			Expected: Config{
    53  				Content: []byte("message: hello\n"),
    54  				Source:  "test/local-file@develop",
    55  				Path:    ".github/test-app.yml",
    56  			},
    57  		},
    58  		"localFileLarge": {
    59  			Paths: []string{".github/test-app.yml"},
    60  			Repo:  "local-file-large",
    61  			Expected: Config{
    62  				Content: []byte("message: hello\n"),
    63  				Source:  "test/local-file-large@develop",
    64  				Path:    ".github/test-app.yml",
    65  			},
    66  		},
    67  		"remoteReference": {
    68  			Paths: []string{".github/test-app.yml"},
    69  			Repo:  "remote-ref",
    70  			Expected: Config{
    71  				Content:  []byte("message: hello\n"),
    72  				Source:   "remote/config@develop",
    73  				Path:     "config/test-app.yml",
    74  				IsRemote: true,
    75  			},
    76  		},
    77  		"remoteReferenceEmptyGitRef": {
    78  			Paths: []string{".github/test-app.yml"},
    79  			Repo:  "remote-ref-empty-git-ref",
    80  			Expected: Config{
    81  				Content:  []byte("message: hello\n"),
    82  				Source:   "remote/config@main",
    83  				Path:     "config/test-app.yml",
    84  				IsRemote: true,
    85  			},
    86  		},
    87  		"defaultConfig": {
    88  			Paths: []string{".github/test-app.yml"},
    89  			Repo:  "default-config",
    90  			Expected: Config{
    91  				Content: []byte("message: hello\n"),
    92  				Source:  "test/.github@develop",
    93  				Path:    "test-app.yml",
    94  			},
    95  		},
    96  		"defaultConfigRemoteReference": {
    97  			Paths: []string{".github-remote/test-app.yml"},
    98  			Options: []Option{
    99  				WithOwnerDefault(".github-remote", []string{"test-app.yml"}),
   100  			},
   101  			Repo: "default-config-remote-ref",
   102  			Expected: Config{
   103  				Content:  []byte("message: hello\n"),
   104  				Source:   "remote/config@develop",
   105  				Path:     "config/test-app.yml",
   106  				IsRemote: true,
   107  			},
   108  		},
   109  	}
   110  
   111  	ctx := context.Background()
   112  	client := makeTestClient()
   113  
   114  	for name, test := range tests {
   115  		t.Run(name, func(t *testing.T) {
   116  			ld := NewLoader(test.Paths, test.Options...)
   117  
   118  			cfg, err := ld.LoadConfig(ctx, client, TestOwner, test.Repo, TestRef)
   119  			if test.Error {
   120  				if err == nil {
   121  					t.Fatal("expected error loading config, but got nil")
   122  				}
   123  				return
   124  			}
   125  			if err != nil {
   126  				t.Fatalf("unexpected error loading config: %v", err)
   127  			}
   128  
   129  			if test.Expected.Source != cfg.Source {
   130  				t.Errorf("incorrect source: expected: %q, actual: %q", test.Expected.Source, cfg.Source)
   131  			}
   132  			if test.Expected.Path != cfg.Path {
   133  				t.Errorf("incorrect path: expected: %q, actual: %q", test.Expected.Path, cfg.Path)
   134  			}
   135  			if test.Expected.IsRemote != cfg.IsRemote {
   136  				t.Errorf("incorrect remote flag: expected: %t, actual: %t", test.Expected.IsRemote, cfg.IsRemote)
   137  			}
   138  			if !bytes.Equal(test.Expected.Content, cfg.Content) {
   139  				t.Errorf("incorrect content\nexpected: %s\n  actual: %s", test.Expected.Content, cfg.Content)
   140  			}
   141  		})
   142  	}
   143  }
   144  
   145  func makeTestClient() *github.Client {
   146  	rp := &ResponsePlayer{}
   147  	for route, f := range map[string]string{
   148  		"/repos/test/local-file/contents/.github/test-app.yml":    "local-file-contents.yml",
   149  		"/repos/test/local-file/contents/.github/test-app.v2.yml": "404.yml",
   150  
   151  		"/repos/test/local-file-large/contents/.github/test-app.yml": "local-file-large-contents.yml",
   152  		"/repos/test/local-file-large/contents/.github":              "local-file-large-dir-contents.yml",
   153  		"/test/local-file-large/develop/.github/test-app.yml":        "local-file-large-download.yml",
   154  
   155  		"/repos/test/remote-ref/contents/.github/test-app.yml":               "remote-ref-contents.yml",
   156  		"/repos/test/remote-ref-empty-git-ref/contents/.github/test-app.yml": "remote-ref-empty-git-ref-contents.yml",
   157  		"/repos/remote/config/contents/config/test-app.yml":                  "config-contents.yml",
   158  		"/repos/remote/config": "remote-config.yml",
   159  
   160  		"/repos/test/default-config/contents/.github/test-app.yml": "404.yml",
   161  		"/repos/test/.github":                       "dot-github.yml",
   162  		"/repos/test/.github/contents/test-app.yml": "dot-github-contents.yml",
   163  
   164  		"/repos/test/default-config-remote-ref/contents/.github-remote/test-app.yml": "404.yml",
   165  		"/repos/test/.github-remote":               "remote-config.yml",
   166  		"/repos/test/config/contents/test-app.yml": "remote-ref-contents.yml",
   167  	} {
   168  		rp.AddRule(ExactPathMatcher(route), filepath.Join("testdata", f))
   169  	}
   170  	return github.NewClient(&http.Client{Transport: rp})
   171  }
   172  

View as plain text