github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/config/file_config_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 config
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    21  )
    22  
    23  const (
    24  	cfgPath = "/home/bheni/.ld/config.json"
    25  )
    26  
    27  func TestGetAndSet(t *testing.T) {
    28  	fs := filesys.NewInMemFS([]string{}, map[string][]byte{}, "/")
    29  	cfg, err := NewFileConfig(cfgPath, fs, map[string]string{})
    30  
    31  	if err != nil {
    32  		t.Fatal("Failed to create empty config")
    33  	}
    34  
    35  	params := map[string]string{
    36  		"string": "this is a string",
    37  		"int":    "-15",
    38  		"uint":   "1234567",
    39  		"float":  "3.1415",
    40  	}
    41  
    42  	err = cfg.SetStrings(params)
    43  
    44  	if err != nil {
    45  		t.Fatal("Failed to set values")
    46  	}
    47  
    48  	if exists, isDir := fs.Exists(cfgPath); !exists || isDir {
    49  		t.Fatal("File not written after SetStrings was called")
    50  	}
    51  
    52  	cfg, err = FromFile(cfgPath, fs)
    53  
    54  	if err != nil {
    55  		t.Fatal("Error reading config")
    56  	}
    57  
    58  	if str, err := cfg.GetString("string"); err != nil || str != "this is a string" {
    59  		t.Error("Failed to read back string after setting it")
    60  	}
    61  
    62  	testIteration(t, params, cfg)
    63  
    64  	err = cfg.Unset([]string{"int", "float"})
    65  
    66  	if err != nil {
    67  		t.Error("Failed to unset properties")
    68  	}
    69  
    70  	testIteration(t, map[string]string{"string": "this is a string", "uint": "1234567"}, cfg)
    71  }