...

Source file src/github.com/palantir/go-githubapp/appconfig/yaml_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  	"testing"
    19  )
    20  
    21  func TestYAMLRemoteRefParser(t *testing.T) {
    22  	tests := map[string]struct {
    23  		Input  string
    24  		Output *RemoteRef
    25  		Error  bool
    26  	}{
    27  		"complete": {
    28  			Input: "{remote: test/test, path: test.yaml, ref: main}",
    29  			Output: &RemoteRef{
    30  				Remote: "test/test",
    31  				Path:   "test.yaml",
    32  				Ref:    "main",
    33  			},
    34  		},
    35  		"missingRemote": {
    36  			Input:  "{path: test.yaml, ref: main}",
    37  			Output: nil,
    38  		},
    39  		"missingPath": {
    40  			Input: "{remote: test/test, ref: main}",
    41  			Output: &RemoteRef{
    42  				Remote: "test/test",
    43  				Ref:    "main",
    44  			},
    45  		},
    46  		"missingRef": {
    47  			Input: "{remote: test/test, path: test.yaml}",
    48  			Output: &RemoteRef{
    49  				Remote: "test/test",
    50  				Path:   "test.yaml",
    51  			},
    52  		},
    53  		"emptyRemote": {
    54  			Input: "{remote: ''}",
    55  			Error: true,
    56  		},
    57  		"empty": {
    58  			Input:  "",
    59  			Output: nil,
    60  		},
    61  		"commentsOnly": {
    62  			Input:  "# the existence of this file enables the app\n",
    63  			Output: nil,
    64  		},
    65  		"extraFields": {
    66  			Input:  "{remote: test/test, path: test.yaml, ref: main, key: value}",
    67  			Output: nil,
    68  		},
    69  		"onlyUnknownFields": {
    70  			Input:  "{key: value}",
    71  			Output: nil,
    72  		},
    73  	}
    74  
    75  	for name, test := range tests {
    76  		t.Run(name, func(t *testing.T) {
    77  			ref, err := YAMLRemoteRefParser("test.yml", []byte(test.Input))
    78  			if test.Error {
    79  				if err == nil {
    80  					t.Fatal("expected error parsing ref, but got nil")
    81  				}
    82  				return
    83  			}
    84  			if err != nil {
    85  				t.Fatalf("unexpected error parsing ref: %v", err)
    86  			}
    87  
    88  			switch {
    89  			case test.Output == nil && ref != nil:
    90  				t.Errorf("expected nil ref, but got: %+v", *ref)
    91  			case test.Output != nil && ref == nil:
    92  				t.Errorf("expected %+v, but got nil", *test.Output)
    93  			case test.Output != nil && ref != nil:
    94  				if *test.Output != *ref {
    95  					t.Errorf("expected %+v, but got %+v", *test.Output, *ref)
    96  				}
    97  			}
    98  		})
    99  	}
   100  }
   101  

View as plain text