github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/validator/validator_test.go (about)

     1  package validator
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/app/internal"
     7  	"gotest.tools/assert"
     8  	"gotest.tools/fs"
     9  )
    10  
    11  type mockRule struct {
    12  	acceptCalled   bool
    13  	validateCalled bool
    14  }
    15  
    16  func (m *mockRule) Collect(path string, key string, value interface{}) {
    17  
    18  }
    19  
    20  func (m *mockRule) Accept(path string, key string) bool {
    21  	m.acceptCalled = true
    22  	return true
    23  }
    24  
    25  func (m *mockRule) Validate(value interface{}) []error {
    26  	m.validateCalled = true
    27  	return nil
    28  }
    29  
    30  func TestValidate(t *testing.T) {
    31  	composeData := `
    32  version: '3.7'
    33  services:
    34    nginx:
    35      image: nginx
    36      volumes:
    37        - ./foo:/data
    38  `
    39  	inputDir := fs.NewDir(t, "app_input_",
    40  		fs.WithFile(internal.ComposeFileName, composeData),
    41  	)
    42  	defer inputDir.Remove()
    43  
    44  	appName := "my.dockerapp"
    45  	dir := fs.NewDir(t, "app_",
    46  		fs.WithDir(appName),
    47  	)
    48  	defer dir.Remove()
    49  
    50  	r := &mockRule{}
    51  	v := NewValidator(func(v *Validator) {
    52  		v.Rules = append(v.Rules, r)
    53  	})
    54  
    55  	err := v.Validate(inputDir.Join(internal.ComposeFileName))
    56  	assert.NilError(t, err)
    57  	assert.Equal(t, r.acceptCalled, true)
    58  	assert.Equal(t, r.validateCalled, true)
    59  }