github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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/utils/earl" 27 "github.com/dolthub/dolt/go/libraries/utils/filesys" 28 "github.com/dolthub/dolt/go/libraries/utils/test" 29 "github.com/dolthub/dolt/go/store/types" 30 ) 31 32 func TestDirToDBName(t *testing.T) { 33 tests := map[string]string{ 34 "irs": "irs", 35 "corona-virus": "corona_virus", 36 " fake - name ": "fake_name", 37 } 38 39 for dirName, expected := range tests { 40 actual := dirToDBName(dirName) 41 assert.Equal(t, expected, actual) 42 } 43 } 44 45 func TestGetRepoRootDir(t *testing.T) { 46 tests := []struct { 47 path string 48 sep string 49 expected string 50 }{ 51 {``, `/`, ``}, 52 {``, `\`, ``}, 53 {`/`, `/`, ``}, 54 {`C:\`, `\`, ``}, 55 {`.dolt/noms`, `/`, ``}, 56 {`.dolt\noms`, `\`, ``}, 57 {`name/.dolt/noms`, `/`, `name`}, 58 {`name\.dolt\noms`, `\`, `name`}, 59 {`name/.dolt/noms/`, `/`, `name`}, 60 {`name\.dolt\noms\`, `\`, `name`}, 61 {`/var/folders/w6/mhtq880n6y55xxm3_2kn0bs80000gn/T/dolt-repo-76581`, `/`, `dolt-repo-76581`}, 62 {`/var/folders/w6/mhtq880n6y55xxm3_2kn0bs80000gn/T/dolt-repo-76581/.dolt/noms`, `/`, `dolt-repo-76581`}, 63 {`/Users/u/name/.dolt/noms`, `/`, `name`}, 64 {`C:\files\name\.dolt\noms`, `\`, `name`}, 65 {`/Users/u/name/.dolt/noms/`, `/`, `name`}, 66 {`C:\files\name\.dolt\noms\`, `\`, `name`}, 67 {`//Users////u//name//.dolt/noms/////`, `/`, `name`}, 68 {`C:\\files\\\\name\\.dolt\noms\\\\\\`, `\`, `name`}, 69 } 70 71 for _, test := range tests { 72 actual := getRepoRootDir(test.path, test.sep) 73 assert.Equal(t, test.expected, actual, "For '%s' expected '%s' got '%s'", test.path, test.expected, actual) 74 } 75 } 76 77 func initRepoWithRelativePath(t *testing.T, envPath string, hdp HomeDirProvider) *DoltEnv { 78 err := filesys.LocalFS.MkDirs(envPath) 79 require.NoError(t, err) 80 81 fs, err := filesys.LocalFilesysWithWorkingDir(envPath) 82 require.NoError(t, err) 83 84 urlStr := earl.FileUrlFromPath(filepath.Join(envPath, ".dolt", "noms"), os.PathSeparator) 85 dEnv := Load(context.Background(), hdp, fs, urlStr, "test") 86 cfg, _ := dEnv.Config.GetConfig(GlobalConfig) 87 cfg.SetStrings(map[string]string{ 88 UserNameKey: name, 89 UserEmailKey: email, 90 }) 91 92 err = dEnv.InitRepo(context.Background(), types.Format_Default, name, email) 93 require.NoError(t, err) 94 95 return Load(context.Background(), hdp, fs, urlStr, "test") 96 } 97 98 func TestDoltEnvAsMultiEnv(t *testing.T) { 99 rootPath, err := test.ChangeToTestDir("TestDoltEnvAsMultiEnv") 100 require.NoError(t, err) 101 102 hdp := func() (string, error) { return rootPath, nil } 103 envPath := filepath.Join(rootPath, " test---name _ 123") 104 dEnv := initRepoWithRelativePath(t, envPath, hdp) 105 106 mrEnv, err := DoltEnvAsMultiEnv(dEnv) 107 require.NoError(t, err) 108 assert.Len(t, mrEnv, 1) 109 110 for k, v := range mrEnv { 111 assert.Equal(t, "test_name_123", k) 112 assert.Equal(t, dEnv, v) 113 } 114 } 115 116 func initMultiEnv(t *testing.T, testName string, names []string) (string, HomeDirProvider, map[string]*DoltEnv) { 117 rootPath, err := test.ChangeToTestDir(testName) 118 require.NoError(t, err) 119 120 hdp := func() (string, error) { return rootPath, nil } 121 122 envs := make(map[string]*DoltEnv) 123 for _, name := range names { 124 envPath := filepath.Join(rootPath, name) 125 envs[name] = initRepoWithRelativePath(t, envPath, hdp) 126 } 127 128 return rootPath, hdp, envs 129 } 130 131 func TestLoadMultiEnv(t *testing.T) { 132 names := []string{"env 1", " env 2", "env-3"} 133 rootPath, hdp, _ := initMultiEnv(t, "TestLoadMultiEnv", names) 134 135 envNamesAndPaths := make([]EnvNameAndPath, len(names)) 136 for i, name := range names { 137 envNamesAndPaths[i] = EnvNameAndPath{name, filepath.Join(rootPath, name)} 138 } 139 140 mrEnv, err := LoadMultiEnv(context.Background(), hdp, filesys.LocalFS, "test", envNamesAndPaths...) 141 require.NoError(t, err) 142 143 for _, name := range names { 144 _, ok := mrEnv[name] 145 assert.True(t, ok) 146 } 147 } 148 149 func TestLoadMultiEnvFromDir(t *testing.T) { 150 dirNameToDBName := map[string]string{ 151 "env 1": "env_1", 152 " env 2": "env_2", 153 "env-3": "env_3", 154 } 155 156 names := make([]string, 0, len(dirNameToDBName)) 157 for k := range dirNameToDBName { 158 names = append(names, k) 159 } 160 161 rootPath, hdp, envs := initMultiEnv(t, "TestLoadMultiEnvFromDir", names) 162 mrEnv, err := LoadMultiEnvFromDir(context.Background(), hdp, filesys.LocalFS, rootPath, "test") 163 require.NoError(t, err) 164 165 assert.Len(t, mrEnv, len(names)) 166 for _, dirName := range names { 167 dbName := dirNameToDBName[dirName] 168 _, ok := envs[dirName] 169 require.True(t, ok) 170 _, ok = mrEnv[dbName] 171 require.True(t, ok) 172 } 173 }