github.com/dnephin/dobi@v0.15.0/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  	"gotest.tools/v3/fs"
     9  )
    10  
    11  func TestSorted(t *testing.T) {
    12  	config := NewConfig()
    13  	config.Resources = map[string]Resource{
    14  		"beta":  &ImageConfig{},
    15  		"alpha": &ImageConfig{},
    16  		"cabo":  &ImageConfig{},
    17  	}
    18  	sorted := config.Sorted()
    19  	assert.Check(t, is.DeepEqual([]string{"alpha", "beta", "cabo"}, sorted))
    20  }
    21  
    22  func TestResourceResolveDoesNotMutate(t *testing.T) {
    23  	resolver := newFakeResolver(nil)
    24  
    25  	for name, fromConfigFunc := range resourceTypeRegistry {
    26  		value := make(map[string]interface{})
    27  		resource, err := fromConfigFunc("resourcename", value)
    28  		assert.NilError(t, err)
    29  		resolved, err := resource.Resolve(resolver)
    30  		assert.NilError(t, err)
    31  		assert.Check(t, resource != resolved,
    32  			"Expected different pointers for %q: %p, %p",
    33  			name, resource, resolved)
    34  	}
    35  }
    36  
    37  type fakeResolver struct {
    38  	mapping map[string]string
    39  }
    40  
    41  func (r *fakeResolver) Resolve(tmpl string) (string, error) {
    42  	value, ok := r.mapping[tmpl]
    43  	if ok {
    44  		return value, nil
    45  	}
    46  	return tmpl, nil
    47  }
    48  
    49  func (r *fakeResolver) ResolveSlice(tmpls []string) ([]string, error) {
    50  	values := []string{}
    51  	for _, key := range tmpls {
    52  		value, _ := r.Resolve(key)
    53  		values = append(values, value)
    54  	}
    55  	return values, nil
    56  }
    57  
    58  func newFakeResolver(mapping map[string]string) *fakeResolver {
    59  	if mapping == nil {
    60  		mapping = make(map[string]string)
    61  	}
    62  	return &fakeResolver{mapping: mapping}
    63  }
    64  
    65  // FIXME: not a full config
    66  func TestLoadFullFromYaml(t *testing.T) {
    67  	dir := fs.NewDir(t, "load-full-yaml",
    68  		fs.WithFile("dobi.yaml", `
    69  meta:
    70      project: fulltest
    71      default: one
    72      exec-id: exec_id
    73  
    74  alias=one:
    75      tasks: []
    76  alias=two:
    77      tasks: []
    78  alias=three:
    79      tasks: []
    80  
    81  alias=aliasresource:
    82      tasks: [one, two, three]
    83      annotations:
    84          description: This is an alias resource
    85          tags: [lots, things]
    86  `))
    87  	defer dir.Remove()
    88  
    89  	yamlPath := dir.Join("dobi.yaml")
    90  	config, err := Load(yamlPath)
    91  	assert.NilError(t, err)
    92  	expected := &Config{
    93  		Meta: &MetaConfig{
    94  			Project: "fulltest",
    95  			Default: "one",
    96  			ExecID:  "exec_id",
    97  		},
    98  		Resources: map[string]Resource{
    99  			"aliasresource": &AliasConfig{
   100  				Tasks: []string{"one", "two", "three"},
   101  				Annotations: Annotations{
   102  					Annotations: AnnotationFields{
   103  						Description: "This is an alias resource",
   104  						Tags:        []string{"lots", "things"},
   105  					},
   106  				},
   107  			},
   108  			"one":   &AliasConfig{Tasks: []string{}},
   109  			"two":   &AliasConfig{Tasks: []string{}},
   110  			"three": &AliasConfig{Tasks: []string{}},
   111  		},
   112  		WorkingDir: dir.Path(),
   113  		FilePath:   yamlPath,
   114  	}
   115  	assert.Check(t, is.DeepEqual(expected, config, cmpConfigOpt))
   116  }