github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/environment_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 env
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    28  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdocs"
    29  	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
    30  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    31  	"github.com/dolthub/dolt/go/store/hash"
    32  	"github.com/dolthub/dolt/go/store/types"
    33  )
    34  
    35  const (
    36  	testHomeDir = "/user/bheni"
    37  	workingDir  = "/user/bheni/datasets/addresses"
    38  )
    39  
    40  func testHomeDirFunc() (string, error) {
    41  	return testHomeDir, nil
    42  }
    43  
    44  func createTestEnv(isInitialized bool, hasLocalConfig bool) *DoltEnv {
    45  	initialDirs := []string{testHomeDir, workingDir}
    46  	initialFiles := map[string][]byte{}
    47  
    48  	if isInitialized {
    49  		doltDir := filepath.Join(workingDir, dbfactory.DoltDir)
    50  		doltDataDir := filepath.Join(workingDir, dbfactory.DoltDataDir)
    51  		initialDirs = append(initialDirs, doltDir)
    52  		initialDirs = append(initialDirs, doltDataDir)
    53  
    54  		hashStr := hash.Hash{}.String()
    55  		masterRef := ref.NewBranchRef("master")
    56  		repoState := &RepoState{ref.MarshalableRef{Ref: masterRef}, hashStr, hashStr, nil, nil, nil}
    57  		repoStateData, err := json.Marshal(repoState)
    58  
    59  		if err != nil {
    60  			panic("Could not setup test.  Could not marshall repostate struct")
    61  		}
    62  
    63  		initialFiles[getRepoStateFile()] = []byte(repoStateData)
    64  
    65  		if hasLocalConfig {
    66  			initialFiles[getLocalConfigPath()] = []byte(`{"user.name":"bheni"}`)
    67  		}
    68  	} else if hasLocalConfig {
    69  		panic("Bad test.  Cant have a local config in a non initialized directory.")
    70  	}
    71  
    72  	fs := filesys.NewInMemFS(initialDirs, initialFiles, workingDir)
    73  	dEnv := Load(context.Background(), testHomeDirFunc, fs, doltdb.InMemDoltDB, "test")
    74  
    75  	return dEnv
    76  }
    77  
    78  func TestNonRepoDir(t *testing.T) {
    79  	dEnv := createTestEnv(false, false)
    80  
    81  	if !isCWDEmpty(dEnv) {
    82  		t.Error("Should start with a clean wd")
    83  	}
    84  
    85  	if dEnv.HasDoltDir() || dEnv.HasLocalConfig() {
    86  		t.Fatal("These should not exist in the environment for a non repo dir.")
    87  	}
    88  
    89  	if dEnv.CfgLoadErr != nil {
    90  		t.Error("Only global config load / create error should result in an error")
    91  	}
    92  
    93  	if dEnv.RSLoadErr == nil {
    94  		t.Error("File doesn't exist.  There should be an error if the directory doesn't exist.")
    95  	}
    96  
    97  	if dEnv.DocsLoadErr != nil {
    98  		t.Error("There shouldn't be an error if the directory doesn't exist.")
    99  	}
   100  }
   101  
   102  func TestRepoDir(t *testing.T) {
   103  	dEnv := createTestEnv(true, true)
   104  
   105  	if !dEnv.HasDoltDir() || !dEnv.HasLocalConfig() {
   106  		t.Fatal("local config and .dolt dir should have been created")
   107  	}
   108  
   109  	if dEnv.CfgLoadErr != nil {
   110  		t.Error("Only global config load / create error should result in an error")
   111  	}
   112  
   113  	if dEnv.RSLoadErr != nil {
   114  		t.Error("Repostate should be valid for an initialized directory")
   115  	}
   116  
   117  	if dEnv.DocsLoadErr != nil {
   118  		t.Error("Docs should be valid for an initialized directory")
   119  	}
   120  
   121  	if un, err := dEnv.Config.GetString("user.name"); err != nil || un != "bheni" {
   122  		t.Error("Bad local config value.")
   123  	}
   124  }
   125  
   126  func TestRepoDirNoLocal(t *testing.T) {
   127  	dEnv := createTestEnv(true, false)
   128  
   129  	if !dEnv.HasDoltDir() {
   130  		t.Fatal(".dolt dir should exist.")
   131  	} else if dEnv.HasLocalConfig() {
   132  		t.Fatal("This should not be here before creation")
   133  	}
   134  
   135  	if dEnv.CfgLoadErr != nil {
   136  		t.Error("Only global config load / create error should result in an error")
   137  	}
   138  
   139  	if dEnv.RSLoadErr != nil {
   140  		t.Error("File doesn't exist.  There should be an error if the directory doesn't exist.")
   141  	}
   142  
   143  	if dEnv.DocsLoadErr != nil {
   144  		t.Error("Files don't exist.  There should be an error if the directory doesn't exist.")
   145  	}
   146  
   147  	err := dEnv.Config.CreateLocalConfig(map[string]string{"user.name": "bheni"})
   148  	require.NoError(t, err)
   149  
   150  	if !dEnv.HasLocalConfig() {
   151  		t.Error("Failed to create local config file")
   152  	}
   153  
   154  	if un, err := dEnv.Config.GetString("user.name"); err != nil || un != "bheni" {
   155  		t.Error("Bad local config value.")
   156  	}
   157  }
   158  
   159  func TestInitRepo(t *testing.T) {
   160  	dEnv := createTestEnv(false, false)
   161  	err := dEnv.InitRepo(context.Background(), types.Format_Default, "aoeu aoeu", "aoeu@aoeu.org")
   162  
   163  	if err != nil {
   164  		t.Error("Failed to init repo.", err.Error())
   165  	}
   166  
   167  	_, err = dEnv.WorkingRoot(context.Background())
   168  
   169  	if err != nil {
   170  		t.Error("Failed to get working root value.")
   171  	}
   172  
   173  	_, err = dEnv.StagedRoot(context.Background())
   174  
   175  	if err != nil {
   176  		t.Error("Failed to get staged root value.")
   177  	}
   178  
   179  	for _, doc := range doltdocs.SupportedDocs {
   180  		docPath := doltdocs.GetDocFilePath(doc.File)
   181  		if len(docPath) > 0 && !strings.Contains(doc.File, docPath) {
   182  			t.Error("Doc file path should exist: ", doc.File)
   183  		}
   184  	}
   185  }
   186  
   187  func isCWDEmpty(dEnv *DoltEnv) bool {
   188  	isEmpty := true
   189  	dEnv.FS.Iter("./", true, func(_ string, _ int64, _ bool) bool {
   190  		isEmpty = false
   191  		return true
   192  	})
   193  
   194  	return isEmpty
   195  }
   196  
   197  func TestBestEffortDelete(t *testing.T) {
   198  	dEnv := createTestEnv(true, true)
   199  
   200  	if isCWDEmpty(dEnv) {
   201  		t.Error("Dir should not be empty before delete.")
   202  	}
   203  
   204  	dEnv.bestEffortDeleteAll(workingDir)
   205  
   206  	if !isCWDEmpty(dEnv) {
   207  		t.Error("Dir should be empty after delete.")
   208  	}
   209  }