github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/config/resolver_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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package config
    23  
    24  import (
    25  	"fmt"
    26  	"os"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/dolthub/dolt/go/libraries/utils/osutil"
    33  	"github.com/dolthub/dolt/go/store/spec"
    34  )
    35  
    36  const (
    37  	localSpec = nbsSpec
    38  	testDs    = "testds"
    39  )
    40  
    41  type testData struct {
    42  	input    string
    43  	expected string
    44  }
    45  
    46  var (
    47  	rtestRoot = os.TempDir()
    48  
    49  	rtestConfig = &Config{
    50  		"",
    51  		map[string]DbConfig{
    52  			DefaultDbAlias: {Url: localSpec},
    53  		},
    54  		AWSConfig{},
    55  	}
    56  
    57  	dbTestsNoAliases = []testData{
    58  		{localSpec, localSpec},
    59  	}
    60  
    61  	dbTestsWithAliases = []testData{
    62  		{"", localSpec},
    63  	}
    64  )
    65  
    66  func withConfig(t *testing.T) *Resolver {
    67  	return withGivenConfig(rtestConfig, t)
    68  }
    69  
    70  func withGivenConfig(config *Config, t *testing.T) *Resolver {
    71  	assert := assert.New(t)
    72  	dir := filepath.Join(rtestRoot, "with-config")
    73  	_, err := config.WriteTo(dir)
    74  	assert.NoError(err, dir)
    75  	assert.NoError(os.Chdir(dir))
    76  	r := NewResolver() // resolver must be created after changing directory
    77  	return r
    78  }
    79  
    80  func withoutConfig(t *testing.T) *Resolver {
    81  	assert := assert.New(t)
    82  	dir := filepath.Join(rtestRoot, "without-config")
    83  	assert.NoError(os.MkdirAll(dir, os.ModePerm), dir)
    84  	assert.NoError(os.Chdir(dir))
    85  	r := NewResolver() // resolver must be created after changing directory
    86  	return r
    87  }
    88  
    89  func assertPathSpecsEquiv(assert *assert.Assertions, expected string, actual string) {
    90  	e, err := spec.ForPath(expected)
    91  	assert.NoError(err)
    92  	a, err := spec.ForPath(actual)
    93  	assert.NoError(err)
    94  
    95  	databaseSpec := func(sp spec.Spec) string {
    96  		return fmt.Sprintf("%s:%s", sp.Protocol, sp.DatabaseName)
    97  	}
    98  
    99  	assertDbSpecsEquiv(assert, databaseSpec(e), databaseSpec(a))
   100  	assert.Equal(e.Path.String(), a.Path.String())
   101  }
   102  
   103  func TestResolveDatabaseWithConfig(t *testing.T) {
   104  	if osutil.IsWindows {
   105  		t.Skip("Skipping test as it is specific to Unix")
   106  	}
   107  	r := withConfig(t)
   108  	assert := assert.New(t)
   109  	for _, d := range append(dbTestsNoAliases, dbTestsWithAliases...) {
   110  		db := r.ResolveDbSpec(d.input)
   111  		assertDbSpecsEquiv(assert, d.expected, db)
   112  	}
   113  }
   114  
   115  func TestResolveDatabaseWithoutConfig(t *testing.T) {
   116  	r := withoutConfig(t)
   117  	assert := assert.New(t)
   118  	for _, d := range dbTestsNoAliases {
   119  		db := r.ResolveDbSpec(d.input)
   120  		assert.Equal(d.expected, db, d.input)
   121  	}
   122  }
   123  
   124  func TestPathResolutionWhenSeparatorMissing(t *testing.T) {
   125  	rConfig := withGivenConfig(rtestConfig, t)
   126  	rNoConfig := withoutConfig(t)
   127  	assert := assert.New(t)
   128  
   129  	assert.Equal(rConfig.ResolvePathSpec("db"), rNoConfig.ResolvePathSpec("db"))
   130  	assert.Equal(spec.Separator+"branch", rNoConfig.ResolvePathSpec(spec.Separator+"branch"))
   131  	assertPathSpecsEquiv(assert, localSpec+spec.Separator+"branch", rConfig.ResolvePathSpec(spec.Separator+"branch"))
   132  }
   133  
   134  func TestResolveDestPathWithDot(t *testing.T) {
   135  	if osutil.IsWindows {
   136  		t.Skip("Skipping test as it is specific to Unix")
   137  	}
   138  	r := withConfig(t)
   139  	assert := assert.New(t)
   140  
   141  	data := []struct {
   142  		src     string
   143  		dest    string
   144  		expSrc  string
   145  		expDest string
   146  	}{
   147  		{"::" + testDs, nbsSpec + "::.", localSpec + "::" + testDs, nbsSpec + "::" + testDs},
   148  		{nbsSpec + "::" + testDs, "::.", nbsSpec + "::" + testDs, localSpec + "::" + testDs},
   149  	}
   150  	for _, d := range data {
   151  		src := r.ResolvePathSpec(d.src)
   152  		dest := r.ResolvePathSpec(d.dest)
   153  		assertPathSpecsEquiv(assert, d.expSrc, src)
   154  		assertPathSpecsEquiv(assert, d.expDest, dest)
   155  	}
   156  }