github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/config.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/util/configv3"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  // TurnOffColors sets CF_COLOR to 'false'.
    16  func TurnOffColors() {
    17  	Expect(os.Setenv("CF_COLOR", "false")).To(Succeed())
    18  }
    19  
    20  // TurnOnExperimental sets CF_CLI_EXPERIMENTAL to 'true'.
    21  func TurnOnExperimental() {
    22  	Expect(os.Setenv("CF_CLI_EXPERIMENTAL", "true")).To(Succeed())
    23  }
    24  
    25  // TurnOffExperimental unsets CF_CLI_EXPERIMENTAL.
    26  func TurnOffExperimental() {
    27  	Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).To(Succeed())
    28  }
    29  
    30  // TurnOnExperimentalLogin sets CF_EXPERIMENTAL_LOGIN to 'true'.
    31  func TurnOnExperimentalLogin() {
    32  	Expect(os.Setenv("CF_EXPERIMENTAL_LOGIN", "true")).To(Succeed())
    33  }
    34  
    35  // TurnOffExperimentalLogin unsets CF_EXPERIMENTAL_LOGIN.
    36  func TurnOffExperimentalLogin() {
    37  	Expect(os.Unsetenv("CF_EXPERIMENTAL_LOGIN")).To(Succeed())
    38  }
    39  
    40  // SetHomeDir sets CF_HOME and CF_PLUGIN_HOME to a temp directory and outputs
    41  // the created directory through GinkgoWriter.
    42  func SetHomeDir() string {
    43  	var err error
    44  	homeDir, err := ioutil.TempDir("", "cli-integration-test")
    45  	Expect(err).NotTo(HaveOccurred())
    46  
    47  	Expect(os.Setenv("CF_HOME", homeDir)).To(Succeed())
    48  	Expect(os.Setenv("CF_PLUGIN_HOME", homeDir)).To(Succeed())
    49  
    50  	GinkgoWriter.Write([]byte(fmt.Sprintln("\nHOME DIR>", homeDir)))
    51  	return homeDir
    52  }
    53  
    54  // SetupSynchronizedSuite runs a setup function in its own CF context, creating
    55  // and destroying a home directory around it.
    56  func SetupSynchronizedSuite(setup func()) {
    57  	homeDir := SetHomeDir()
    58  	SetAPI()
    59  	LoginCF()
    60  	setup()
    61  	DestroyHomeDir(homeDir)
    62  }
    63  
    64  // DestroyHomeDir safely removes the given directory checking for errors.
    65  func DestroyHomeDir(homeDir string) {
    66  	if homeDir != "" {
    67  		Expect(os.RemoveAll(homeDir)).To(Succeed())
    68  	}
    69  }
    70  
    71  // GetConfig loads a CF config JSON file and returns the parsed struct.
    72  func GetConfig() *configv3.Config {
    73  	c, err := configv3.LoadConfig()
    74  	Expect(err).ToNot(HaveOccurred())
    75  	return c
    76  }
    77  
    78  // SetConfig allows for a given function to modify a CF config JSON and writes
    79  // the result back down to the filesystem.
    80  func SetConfig(cb func(conf *configv3.Config)) {
    81  	config, err := configv3.LoadConfig()
    82  	Expect(err).ToNot(HaveOccurred())
    83  
    84  	cb(config)
    85  
    86  	err = configv3.WriteConfig(config)
    87  	Expect(err).ToNot(HaveOccurred())
    88  }
    89  
    90  // SetConfigContent writes given raw config into given directory as "config.json".
    91  func SetConfigContent(dir string, rawConfig string) {
    92  	err := os.MkdirAll(filepath.Join(dir), 0777)
    93  	Expect(err).ToNot(HaveOccurred())
    94  	err = ioutil.WriteFile(filepath.Join(dir, "config.json"), []byte(rawConfig), 0644)
    95  	Expect(err).ToNot(HaveOccurred())
    96  }
    97  
    98  // ExpiredAccessToken returns an example expired bearer token.
    99  func ExpiredAccessToken() string {
   100  	return "bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiJiMjZmMTFjMWNhYmI0ZmY0ODhlN2RhYTJkZTQxMTA4NiIsInN1YiI6IjBjZWMwY2E4LTA5MmYtNDkzYy1hYmExLWM4ZTZiMTRiODM3NiIsInNjb3BlIjpbIm9wZW5pZCIsInJvdXRpbmcucm91dGVyX2dyb3Vwcy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