summaryrefslogtreecommitdiff
path: root/config/reader_test.go
blob: e11e7af62ba3022cfadf4f5eeed247a44ff8bdfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package config_test

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"

	"phabricator.wikimedia.org/source/blubber/config"
)

func ExampleResolveIncludes() {
	cfg, _ := config.ReadConfig([]byte(`---
    variants:
      varA: { includes: [varB, varC] }
      varB: { includes: [varD, varE] }
      varC: {}
      varD: { includes: [varF] }
      varE: {}
      varF: {}`))

	includes, _ := config.ResolveIncludes(cfg, "varA")

	fmt.Printf("%v\n", includes)

	// Output: [varF varD varE varB varC varA]
}

func TestResolveIncludesPreventsInfiniteRecursion(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    variants:
      varA: { includes: [varB] }
      varB: { includes: [varA] }`))

	assert.NoError(t, err)

	_, err2 := config.ResolveIncludes(cfg, "varA")

	assert.EqualError(t, err2, "variant expansion detected loop")
}

func TestMultiLevelIncludes(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    base: nodejs-slim
    variants:
      build:
        base: nodejs-devel
        node: {env: build}
      development:
        includes: [build]
        node: {env: development}
        entrypoint: [npm, start]
      test:
        includes: [development]
        node: {dependencies: true}
        entrypoint: [npm, test]`))

	assert.NoError(t, err)

	variant, _ := config.ExpandVariant(cfg, "test")

	assert.Equal(t, "nodejs-devel", variant.Base)
	assert.Equal(t, "development", variant.Node.Env)

	devVariant, _ := config.ExpandVariant(cfg, "development")

	assert.True(t, variant.Node.Dependencies.True)
	assert.False(t, devVariant.Node.Dependencies.True)
}

func TestMultiIncludes(t *testing.T) {
	cfg, err := config.ReadConfig([]byte(`---
    variants:
      mammal:
        base: neutral
      human:
        base: moral
        includes: [mammal]
      lizard:
        base: immoral
      lizardman:
        includes: [human, lizard]`))

	if assert.NoError(t, err) {
		variant, err := config.ExpandVariant(cfg, "lizardman")

		if assert.NoError(t, err) {
			assert.Equal(t, "immoral", variant.Base)
		}
	}
}