github.com/databricks/cli@v0.203.0/bundle/config/mutator/select_default_environment_test.go (about)

     1  package mutator_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/databricks/cli/bundle"
     8  	"github.com/databricks/cli/bundle/config"
     9  	"github.com/databricks/cli/bundle/config/mutator"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestSelectDefaultEnvironmentNoEnvironments(t *testing.T) {
    14  	bundle := &bundle.Bundle{
    15  		Config: config.Root{
    16  			Environments: map[string]*config.Environment{},
    17  		},
    18  	}
    19  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    20  	assert.ErrorContains(t, err, "no environments defined")
    21  }
    22  
    23  func TestSelectDefaultEnvironmentSingleEnvironments(t *testing.T) {
    24  	bundle := &bundle.Bundle{
    25  		Config: config.Root{
    26  			Environments: map[string]*config.Environment{
    27  				"foo": {},
    28  			},
    29  		},
    30  	}
    31  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    32  	assert.NoError(t, err)
    33  	assert.Equal(t, "foo", bundle.Config.Bundle.Environment)
    34  }
    35  
    36  func TestSelectDefaultEnvironmentNoDefaults(t *testing.T) {
    37  	bundle := &bundle.Bundle{
    38  		Config: config.Root{
    39  			Environments: map[string]*config.Environment{
    40  				"foo": {},
    41  				"bar": {},
    42  				"qux": {},
    43  			},
    44  		},
    45  	}
    46  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    47  	assert.ErrorContains(t, err, "please specify environment")
    48  }
    49  
    50  func TestSelectDefaultEnvironmentNoDefaultsWithNil(t *testing.T) {
    51  	bundle := &bundle.Bundle{
    52  		Config: config.Root{
    53  			Environments: map[string]*config.Environment{
    54  				"foo": nil,
    55  				"bar": nil,
    56  			},
    57  		},
    58  	}
    59  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    60  	assert.ErrorContains(t, err, "please specify environment")
    61  }
    62  
    63  func TestSelectDefaultEnvironmentMultipleDefaults(t *testing.T) {
    64  	bundle := &bundle.Bundle{
    65  		Config: config.Root{
    66  			Environments: map[string]*config.Environment{
    67  				"foo": {Default: true},
    68  				"bar": {Default: true},
    69  				"qux": {Default: true},
    70  			},
    71  		},
    72  	}
    73  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    74  	assert.ErrorContains(t, err, "multiple environments are marked as default")
    75  }
    76  
    77  func TestSelectDefaultEnvironmentSingleDefault(t *testing.T) {
    78  	bundle := &bundle.Bundle{
    79  		Config: config.Root{
    80  			Environments: map[string]*config.Environment{
    81  				"foo": {},
    82  				"bar": {Default: true},
    83  				"qux": {},
    84  			},
    85  		},
    86  	}
    87  	err := mutator.SelectDefaultEnvironment().Apply(context.Background(), bundle)
    88  	assert.NoError(t, err)
    89  	assert.Equal(t, "bar", bundle.Config.Bundle.Environment)
    90  }