github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/config/config_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  const version = "0.0.0"
    26  const remoteURL = "http://localhost:50051/test-org/test-repo"
    27  const revision = "nl5v5qu36e2dfmhnjqiu4crefam52iif"
    28  
    29  var testConfig = fmt.Sprintf(`version %s
    30  remote %s
    31  revision %s
    32  `, version, remoteURL, revision)
    33  
    34  var noVersionConfig = fmt.Sprintf(`remote %s
    35  revision %s
    36  `, remoteURL, revision)
    37  
    38  var noRemoteConfig = fmt.Sprintf(`version %s
    39  revision %s
    40  `, version, revision)
    41  
    42  var noRevisionConfig = fmt.Sprintf(`version %s
    43  remote %s
    44  `, version, remoteURL)
    45  
    46  var wanted = GitDoltConfig{
    47  	Version:  version,
    48  	Remote:   remoteURL,
    49  	Revision: revision,
    50  }
    51  
    52  func TestParse(t *testing.T) {
    53  	type args struct {
    54  		c string
    55  	}
    56  	happyTests := []struct {
    57  		name string
    58  		args args
    59  		want GitDoltConfig
    60  	}{
    61  		{"parses config", args{testConfig}, wanted},
    62  		{"defaults version to current git-dolt version if missing", args{noVersionConfig}, wanted},
    63  	}
    64  	for _, tt := range happyTests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			got, err := Parse(tt.args.c)
    67  			assert.Nil(t, err)
    68  			assert.Equal(t, tt.want, got)
    69  		})
    70  	}
    71  
    72  	errorTests := []struct {
    73  		name string
    74  		args args
    75  		want error
    76  	}{
    77  		{"returns an error if missing remote", args{noRemoteConfig}, errors.New("no remote specified")},
    78  		{"returns an error if missing revision", args{noRevisionConfig}, errors.New("no revision specified")},
    79  	}
    80  	for _, tt := range errorTests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			_, err := Parse(tt.args.c)
    83  			assert.Equal(t, tt.want, err)
    84  		})
    85  	}
    86  }