github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/dtestutils/environment.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 dtestutils 16 17 import ( 18 "context" 19 "os" 20 "path/filepath" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 23 "github.com/dolthub/dolt/go/libraries/doltcore/env" 24 "github.com/dolthub/dolt/go/libraries/utils/config" 25 "github.com/dolthub/dolt/go/libraries/utils/filesys" 26 "github.com/dolthub/dolt/go/store/types" 27 ) 28 29 const ( 30 TestHomeDirPrefix = "/user/dolt/" 31 WorkingDirPrefix = "/user/dolt/datasets/" 32 ) 33 34 // CreateTestEnv creates a new DoltEnv suitable for testing. The CreateTestEnvWithName 35 // function should generally be preferred over this method, especially when working 36 // with tests using multiple databases within a MultiRepoEnv. 37 func CreateTestEnv() *env.DoltEnv { 38 return CreateTestEnvWithName("test") 39 } 40 41 // CreateTestEnvForLocalFilesystem creates a new DoltEnv for testing, using a local FS, instead of an in-memory 42 // filesystem, for persisting files. This is useful for tests that require a disk-based filesystem and will not 43 // work correctly with an in-memory filesystem and in-memory blob store (e.g. dolt_undrop() tests). 44 func CreateTestEnvForLocalFilesystem() *env.DoltEnv { 45 tempDir, err := os.MkdirTemp(os.TempDir(), "dolt-*") 46 if err != nil { 47 panic(err) 48 } 49 50 fs, err := filesys.LocalFilesysWithWorkingDir(tempDir) 51 if err != nil { 52 panic(err) 53 } 54 55 err = fs.MkDirs("test") 56 if err != nil { 57 panic(err) 58 } 59 60 fs, err = fs.WithWorkingDir("test") 61 if err != nil { 62 panic(err) 63 } 64 65 homeDir := filepath.Join(tempDir, "home") 66 err = fs.MkDirs("home") 67 if err != nil { 68 panic(err) 69 } 70 homeDirFunc := func() (string, error) { return homeDir, nil } 71 72 return createTestEnvWithNameAndFilesystem("test", fs, homeDirFunc) 73 } 74 75 // CreateTestEnvWithName creates a new DoltEnv suitable for testing and uses 76 // the specified name to distinguish it from other test envs. This function 77 // should generally be preferred over CreateTestEnv, especially when working with 78 // tests using multiple databases within a MultiRepoEnv. 79 func CreateTestEnvWithName(envName string) *env.DoltEnv { 80 initialDirs := []string{TestHomeDirPrefix + envName, WorkingDirPrefix + envName} 81 fs := filesys.NewInMemFS(initialDirs, nil, WorkingDirPrefix+envName) 82 homeDirFunc := func() (string, error) { return TestHomeDirPrefix + envName, nil } 83 84 return createTestEnvWithNameAndFilesystem(envName, fs, homeDirFunc) 85 } 86 87 // createTestEnvWithNameAndFilesystem creates a Dolt environment for testing, using the |envName| for the name, the 88 // specified |fs| for persisting files, and |homeDirFunc| for finding the location to load global Dolt configuration. 89 func createTestEnvWithNameAndFilesystem(envName string, fs filesys.Filesys, homeDirFunc func() (string, error)) *env.DoltEnv { 90 const name = "billy bob" 91 const email = "bigbillieb@fake.horse" 92 93 var urlStr string 94 _, isInMemFs := fs.(*filesys.InMemFS) 95 if isInMemFs { 96 urlStr = doltdb.InMemDoltDB + envName 97 } else { 98 urlStr = doltdb.LocalDirDoltDB 99 } 100 101 dEnv := env.Load(context.Background(), homeDirFunc, fs, urlStr, "test") 102 cfg, _ := dEnv.Config.GetConfig(env.GlobalConfig) 103 cfg.SetStrings(map[string]string{ 104 config.UserNameKey: name, 105 config.UserEmailKey: email, 106 }) 107 108 err := dEnv.InitRepo(context.Background(), types.Format_Default, name, email, env.DefaultInitBranch) 109 if err != nil { 110 panic("Failed to initialize environment:" + err.Error()) 111 } 112 113 return dEnv 114 }