github.com/motyar/up@v0.2.10/config/hooks_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/tj/assert"
     8  )
     9  
    10  func TestHook(t *testing.T) {
    11  	t.Run("missing", func(t *testing.T) {
    12  		s := []byte(`{}`)
    13  
    14  		var c struct {
    15  			Build Hook
    16  		}
    17  
    18  		err := json.Unmarshal(s, &c)
    19  		assert.NoError(t, err, "unmarshal")
    20  
    21  		assert.Equal(t, Hook(nil), c.Build)
    22  	})
    23  
    24  	t.Run("invalid type", func(t *testing.T) {
    25  		s := []byte(`
    26        {
    27          "build": 5
    28        }
    29      `)
    30  
    31  		var c struct {
    32  			Build Hook
    33  		}
    34  
    35  		err := json.Unmarshal(s, &c)
    36  		assert.EqualError(t, err, `hook must be a string or array of strings`)
    37  	})
    38  
    39  	t.Run("string", func(t *testing.T) {
    40  		s := []byte(`
    41        {
    42          "build": "go build main.go"
    43        }
    44      `)
    45  
    46  		var c struct {
    47  			Build Hook
    48  		}
    49  
    50  		err := json.Unmarshal(s, &c)
    51  		assert.NoError(t, err, "unmarshal")
    52  
    53  		assert.Equal(t, Hook{"go build main.go"}, c.Build)
    54  	})
    55  
    56  	t.Run("array", func(t *testing.T) {
    57  		s := []byte(`
    58        {
    59          "build": [
    60            "go build main.go",
    61            "browserify src/index.js > app.js"
    62          ]
    63        }
    64      `)
    65  
    66  		var c struct {
    67  			Build Hook
    68  		}
    69  
    70  		err := json.Unmarshal(s, &c)
    71  		assert.NoError(t, err, "unmarshal")
    72  
    73  		assert.Equal(t, Hook{
    74  			"go build main.go",
    75  			"browserify src/index.js > app.js",
    76  		}, c.Build)
    77  	})
    78  }