github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/config_flag_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"encoding/json"
    15  
    16  	"github.com/mattermost/mattermost-server/utils"
    17  )
    18  
    19  func TestConfigFlag(t *testing.T) {
    20  	dir, err := ioutil.TempDir("", "")
    21  	require.NoError(t, err)
    22  	defer os.RemoveAll(dir)
    23  
    24  	utils.TranslationsPreInit()
    25  	config, _, _, err := utils.LoadConfig("config.json")
    26  	require.Nil(t, err)
    27  	configPath := filepath.Join(dir, "foo.json")
    28  	require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))
    29  
    30  	timezones := utils.LoadTimezones("timezones.json")
    31  	tzConfigPath := filepath.Join(dir, "timezones.json")
    32  	timezoneData, _ := json.Marshal(timezones)
    33  	require.NoError(t, ioutil.WriteFile(tzConfigPath, timezoneData, 0600))
    34  
    35  	i18n, ok := utils.FindDir("i18n")
    36  	require.True(t, ok)
    37  	require.NoError(t, utils.CopyDir(i18n, filepath.Join(dir, "i18n")))
    38  
    39  	prevDir, err := os.Getwd()
    40  	require.NoError(t, err)
    41  	defer os.Chdir(prevDir)
    42  	os.Chdir(dir)
    43  
    44  	require.Error(t, RunCommand(t, "version"))
    45  	CheckCommand(t, "--config", "foo.json", "version")
    46  	CheckCommand(t, "--config", "./foo.json", "version")
    47  	CheckCommand(t, "--config", configPath, "version")
    48  }