github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/isolated/unset_env_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("unset-env command", func() {
    16  	var (
    17  		orgName    string
    18  		spaceName  string
    19  		appName    string
    20  		envVarName string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		orgName = helpers.NewOrgName()
    25  		spaceName = helpers.NewSpaceName()
    26  		appName = helpers.PrefixedRandomName("app")
    27  		envVarName = "SOME_ENV_VAR"
    28  	})
    29  
    30  	Describe("help", func() {
    31  		When("--help flag is set", func() {
    32  			It("appears in cf help -a", func() {
    33  				session := helpers.CF("help", "-a")
    34  				Eventually(session).Should(Exit(0))
    35  				Expect(session).To(HaveCommandInCategoryWithDescription("unset-env", "APPS", "Remove an env variable from an app"))
    36  			})
    37  
    38  			It("displays command usage to output", func() {
    39  				session := helpers.CF("unset-env", "--help")
    40  
    41  				Eventually(session).Should(Say("NAME:"))
    42  				Eventually(session).Should(Say("unset-env - Remove an env variable from an app"))
    43  				Eventually(session).Should(Say("USAGE:"))
    44  				Eventually(session).Should(Say("cf unset-env APP_NAME ENV_VAR_NAME"))
    45  				Eventually(session).Should(Say("ALIAS:"))
    46  				Eventually(session).Should(Say("ue"))
    47  				Eventually(session).Should(Say("SEE ALSO:"))
    48  				Eventually(session).Should(Say("apps, env, restart, set-env, stage"))
    49  				Eventually(session).Should(Exit(0))
    50  			})
    51  		})
    52  	})
    53  
    54  	When("the app name is not provided", func() {
    55  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    56  			session := helpers.CF("unset-env")
    57  
    58  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `ENV_VAR_NAME` were not provided"))
    59  			Eventually(session).Should(Say("NAME:"))
    60  			Eventually(session).Should(Exit(1))
    61  		})
    62  	})
    63  
    64  	When("ENV_VAR_NAME is not provided", func() {
    65  		It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() {
    66  			session := helpers.CF("unset-env", appName)
    67  
    68  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_NAME` was not provided"))
    69  			Eventually(session).Should(Say("NAME:"))
    70  			Eventually(session).Should(Exit(1))
    71  		})
    72  	})
    73  
    74  	When("the environment is not setup correctly", func() {
    75  		It("fails with the appropriate errors", func() {
    76  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "unset-env", appName, envVarName)
    77  		})
    78  	})
    79  
    80  	When("the environment is set up correctly", func() {
    81  		var userName string
    82  
    83  		BeforeEach(func() {
    84  			helpers.SetupCF(orgName, spaceName)
    85  			userName, _ = helpers.GetCredentials()
    86  		})
    87  
    88  		When("the app does not exist", func() {
    89  			It("displays app not found and exits 1", func() {
    90  				invalidAppName := "invalid-app-name"
    91  				session := helpers.CF("unset-env", invalidAppName, envVarName)
    92  
    93  				Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName))
    94  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
    95  				Eventually(session).Should(Say("FAILED"))
    96  				Eventually(session).Should(Exit(1))
    97  			})
    98  		})
    99  
   100  		When("the app exists", func() {
   101  			BeforeEach(func() {
   102  				helpers.WithHelloWorldApp(func(appDir string) {
   103  					Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
   104  				})
   105  			})
   106  
   107  			When("the environment variable has not been previously set", func() {
   108  				It("returns a warning indicating variable was not set", func() {
   109  					session := helpers.CF("unset-env", appName, envVarName)
   110  
   111  					Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   112  					Eventually(session).Should(Say("Env variable %s was not set.", envVarName))
   113  					Eventually(session).Should(Say("OK"))
   114  					Eventually(session).Should(Exit(0))
   115  				})
   116  			})
   117  
   118  			When("the environment variable has been previously set", func() {
   119  				BeforeEach(func() {
   120  					Eventually(helpers.CF("set-env", appName, envVarName, "some-value")).Should(Exit(0))
   121  				})
   122  
   123  				It("overrides the value of the existing environment variable", func() {
   124  					session := helpers.CF("unset-env", appName, envVarName)
   125  
   126  					Eventually(session).Should(Say(`Removing env variable %s from app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   127  					Eventually(session).Should(Say("OK"))
   128  					Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName))
   129  
   130  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   131  					Eventually(session).ShouldNot(Say(`"%s"`, envVarName))
   132  					Eventually(session).Should(Exit(0))
   133  				})
   134  			})
   135  		})
   136  	})
   137  })