github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/env/multi_repo_env_test.go (about)

     1  // Copyright 2020 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 env
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/dconfig"
    28  	"github.com/dolthub/dolt/go/libraries/utils/config"
    29  	"github.com/dolthub/dolt/go/libraries/utils/earl"
    30  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    31  	"github.com/dolthub/dolt/go/libraries/utils/test"
    32  	"github.com/dolthub/dolt/go/store/types"
    33  )
    34  
    35  func TestDirToDBName(t *testing.T) {
    36  	replaceHyphenTests := map[string]string{
    37  		"corona-virus":       "corona_virus",
    38  		"  real - name     ": "real_name",
    39  	}
    40  
    41  	err := os.Setenv(dconfig.EnvDbNameReplace, "true")
    42  	require.NoError(t, err)
    43  
    44  	for dirName, expected := range replaceHyphenTests {
    45  		actual := dbfactory.DirToDBName(dirName)
    46  		assert.Equal(t, expected, actual)
    47  	}
    48  
    49  	allowHyphenTests := map[string]string{
    50  		"irs":                "irs",
    51  		"corona-virus":       "corona-virus",
    52  		"  fake - name     ": "  fake - name     ",
    53  	}
    54  
    55  	err = os.Setenv(dconfig.EnvDbNameReplace, "")
    56  	require.NoError(t, err)
    57  
    58  	for dirName, expected := range allowHyphenTests {
    59  		actual := dbfactory.DirToDBName(dirName)
    60  		assert.Equal(t, expected, actual)
    61  	}
    62  }
    63  
    64  func TestGetRepoRootDir(t *testing.T) {
    65  	tests := []struct {
    66  		path     string
    67  		sep      string
    68  		expected string
    69  	}{
    70  		{``, `/`, ``},
    71  		{``, `\`, ``},
    72  		{`/`, `/`, ``},
    73  		{`C:\`, `\`, ``},
    74  		{`.dolt/noms`, `/`, ``},
    75  		{`.dolt\noms`, `\`, ``},
    76  		{`name/.dolt/noms`, `/`, `name`},
    77  		{`name\.dolt\noms`, `\`, `name`},
    78  		{`name/.dolt/noms/`, `/`, `name`},
    79  		{`name\.dolt\noms\`, `\`, `name`},
    80  		{`/var/folders/w6/mhtq880n6y55xxm3_2kn0bs80000gn/T/dolt-repo-76581`, `/`, `dolt-repo-76581`},
    81  		{`/var/folders/w6/mhtq880n6y55xxm3_2kn0bs80000gn/T/dolt-repo-76581/.dolt/noms`, `/`, `dolt-repo-76581`},
    82  		{`/Users/u/name/.dolt/noms`, `/`, `name`},
    83  		{`C:\files\name\.dolt\noms`, `\`, `name`},
    84  		{`/Users/u/name/.dolt/noms/`, `/`, `name`},
    85  		{`C:\files\name\.dolt\noms\`, `\`, `name`},
    86  		{`//Users////u//name//.dolt/noms/////`, `/`, `name`},
    87  		{`C:\\files\\\\name\\.dolt\noms\\\\\\`, `\`, `name`},
    88  	}
    89  
    90  	for _, test := range tests {
    91  		actual := getRepoRootDir(test.path, test.sep)
    92  		assert.Equal(t, test.expected, actual, "For '%s' expected '%s' got '%s'", test.path, test.expected, actual)
    93  	}
    94  }
    95  
    96  func initRepoWithRelativePath(t *testing.T, envPath string, hdp HomeDirProvider) *DoltEnv {
    97  	err := filesys.LocalFS.MkDirs(envPath)
    98  	require.NoError(t, err)
    99  
   100  	fs, err := filesys.LocalFilesysWithWorkingDir(envPath)
   101  	require.NoError(t, err)
   102  
   103  	urlStr := earl.FileUrlFromPath(filepath.Join(envPath, ".dolt", "noms"), os.PathSeparator)
   104  	dEnv := Load(context.Background(), hdp, fs, urlStr, "test")
   105  	cfg, _ := dEnv.Config.GetConfig(GlobalConfig)
   106  	cfg.SetStrings(map[string]string{
   107  		config.UserNameKey:  name,
   108  		config.UserEmailKey: email,
   109  	})
   110  
   111  	err = dEnv.InitRepo(context.Background(), types.Format_Default, name, email, DefaultInitBranch)
   112  	require.NoError(t, err)
   113  
   114  	return Load(context.Background(), hdp, fs, urlStr, "test")
   115  }
   116  
   117  func TestMultiEnvForDirectory(t *testing.T) {
   118  	rootPath, err := test.ChangeToTestDir("TestDoltEnvAsMultiEnv")
   119  	require.NoError(t, err)
   120  
   121  	hdp := func() (string, error) { return rootPath, nil }
   122  	envPath := filepath.Join(rootPath, " test---name _ 123")
   123  	dEnv := initRepoWithRelativePath(t, envPath, hdp)
   124  
   125  	mrEnv, err := MultiEnvForDirectory(context.Background(), dEnv.Config.WriteableConfig(), dEnv.FS, dEnv.Version, dEnv)
   126  	require.NoError(t, err)
   127  	assert.Len(t, mrEnv.envs, 1)
   128  
   129  	type envCmp struct {
   130  		name    string
   131  		doltDir string
   132  	}
   133  
   134  	expected := []envCmp{
   135  		{
   136  			name:    " test---name _ 123",
   137  			doltDir: dEnv.GetDoltDir(),
   138  		},
   139  	}
   140  
   141  	var actual []envCmp
   142  	for _, env := range mrEnv.envs {
   143  		actual = append(actual, envCmp{
   144  			name:    env.name,
   145  			doltDir: env.env.GetDoltDir(),
   146  		})
   147  	}
   148  
   149  	assert.Equal(t, expected, actual)
   150  }
   151  
   152  func TestMultiEnvForDirectoryWithMultipleRepos(t *testing.T) {
   153  	rootPath, err := test.ChangeToTestDir("TestDoltEnvAsMultiEnvWithMultipleRepos")
   154  	require.NoError(t, err)
   155  
   156  	hdp := func() (string, error) { return rootPath, nil }
   157  	envPath := filepath.Join(rootPath, " test---name _ 123")
   158  	dEnv := initRepoWithRelativePath(t, envPath, hdp)
   159  	subEnv1 := initRepoWithRelativePath(t, filepath.Join(envPath, "abc"), hdp)
   160  	subEnv2 := initRepoWithRelativePath(t, filepath.Join(envPath, "def"), hdp)
   161  
   162  	mrEnv, err := MultiEnvForDirectory(context.Background(), dEnv.Config.WriteableConfig(), dEnv.FS, dEnv.Version, dEnv)
   163  	require.NoError(t, err)
   164  	assert.Len(t, mrEnv.envs, 3)
   165  
   166  	expected := make(map[string]string)
   167  	expected[" test---name _ 123"] = dEnv.GetDoltDir()
   168  	expected["abc"] = subEnv1.GetDoltDir()
   169  	expected["def"] = subEnv2.GetDoltDir()
   170  
   171  	actual := make(map[string]string)
   172  	for _, env := range mrEnv.envs {
   173  		actual[env.name] = env.env.GetDoltDir()
   174  	}
   175  
   176  	assert.Equal(t, expected, actual)
   177  }
   178  
   179  func initMultiEnv(t *testing.T, testName string, names []string) (string, HomeDirProvider, map[string]*DoltEnv) {
   180  	rootPath, err := test.ChangeToTestDir(testName)
   181  	require.NoError(t, err)
   182  
   183  	hdp := func() (string, error) { return rootPath, nil }
   184  
   185  	envs := make(map[string]*DoltEnv)
   186  	for _, name := range names {
   187  		envPath := filepath.Join(rootPath, name)
   188  		envs[name] = initRepoWithRelativePath(t, envPath, hdp)
   189  	}
   190  
   191  	return rootPath, hdp, envs
   192  }