github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/v7/isolated/delete_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	. "github.com/onsi/gomega/gbytes"
     8  	. "github.com/onsi/gomega/gexec"
     9  )
    10  
    11  var _ = Describe("delete command", func() {
    12  	var (
    13  		orgName   string
    14  		spaceName string
    15  		appName   string
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		orgName = helpers.NewOrgName()
    20  		spaceName = helpers.NewSpaceName()
    21  		appName = helpers.PrefixedRandomName("app")
    22  	})
    23  
    24  	When("--help flag is set", func() {
    25  		It("Displays command usage to output", func() {
    26  			session := helpers.CF("delete", "--help")
    27  			Eventually(session).Should(Say("NAME:"))
    28  			Eventually(session).Should(Say("delete - Delete an app"))
    29  			Eventually(session).Should(Say("USAGE:"))
    30  			Eventually(session).Should(Say(`cf delete APP_NAME \[-r\] \[-f\]`))
    31  			Eventually(session).Should(Say("OPTIONS:"))
    32  			Eventually(session).Should(Say(`\s+-f\s+Force deletion without confirmation`))
    33  			Eventually(session).Should(Say(`\s+-r\s+Also delete any mapped routes \[Not currently functional\]`))
    34  			Eventually(session).Should(Exit(0))
    35  		})
    36  	})
    37  
    38  	When("the app name is not provided", func() {
    39  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    40  			session := helpers.CF("delete")
    41  
    42  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    43  			Eventually(session).Should(Say("NAME:"))
    44  			Eventually(session).Should(Exit(1))
    45  		})
    46  	})
    47  
    48  	When("the environment is not setup correctly", func() {
    49  		It("fails with the appropriate errors", func() {
    50  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "delete", "-f", appName)
    51  		})
    52  	})
    53  
    54  	When("the environment is setup correctly", func() {
    55  		BeforeEach(func() {
    56  			helpers.SetupCF(orgName, spaceName)
    57  		})
    58  
    59  		AfterEach(func() {
    60  			helpers.QuickDeleteOrg(orgName)
    61  		})
    62  
    63  		When("the app does not exist", func() {
    64  			When("the -f flag is provided", func() {
    65  				It("it displays the app does not exist", func() {
    66  					username, _ := helpers.GetCredentials()
    67  					session := helpers.CF("delete", appName, "-f")
    68  					Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
    69  					Eventually(session).Should(Say("App %s does not exist", appName))
    70  					Eventually(session).Should(Say("OK"))
    71  					Eventually(session).Should(Exit(0))
    72  				})
    73  			})
    74  
    75  			When("the -f flag not is provided", func() {
    76  				var buffer *Buffer
    77  
    78  				BeforeEach(func() {
    79  					buffer = NewBuffer()
    80  				})
    81  
    82  				When("the user enters 'y'", func() {
    83  					BeforeEach(func() {
    84  						_, err := buffer.Write([]byte("y\n"))
    85  						Expect(err).ToNot(HaveOccurred())
    86  					})
    87  
    88  					It("it displays the app does not exist", func() {
    89  						username, _ := helpers.GetCredentials()
    90  						session := helpers.CFWithStdin(buffer, "delete", appName)
    91  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
    92  						Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
    93  						Eventually(session).Should(Say("App %s does not exist", appName))
    94  						Eventually(session).Should(Say("OK"))
    95  						Eventually(session).Should(Exit(0))
    96  					})
    97  				})
    98  
    99  				When("the user enters 'n'", func() {
   100  					BeforeEach(func() {
   101  						_, err := buffer.Write([]byte("n\n"))
   102  						Expect(err).ToNot(HaveOccurred())
   103  					})
   104  
   105  					It("does not delete the app", func() {
   106  						session := helpers.CFWithStdin(buffer, "delete", appName)
   107  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   108  						Eventually(session).Should(Say("Delete cancelled"))
   109  						Eventually(session).Should(Exit(0))
   110  					})
   111  				})
   112  
   113  				When("the user enters the default input (hits return)", func() {
   114  					BeforeEach(func() {
   115  						_, err := buffer.Write([]byte("\n"))
   116  						Expect(err).ToNot(HaveOccurred())
   117  					})
   118  
   119  					It("does not delete the app", func() {
   120  						session := helpers.CFWithStdin(buffer, "delete", appName)
   121  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   122  						Eventually(session).Should(Say("Delete cancelled"))
   123  						Eventually(session).Should(Exit(0))
   124  					})
   125  				})
   126  
   127  				When("the user enters an invalid answer", func() {
   128  					BeforeEach(func() {
   129  						// The second '\n' is intentional. Otherwise the buffer will be
   130  						// closed while the interaction is still waiting for input; it gets
   131  						// an EOF and causes an error.
   132  						_, err := buffer.Write([]byte("wat\n\n"))
   133  						Expect(err).ToNot(HaveOccurred())
   134  					})
   135  
   136  					It("asks again", func() {
   137  						session := helpers.CFWithStdin(buffer, "delete", appName)
   138  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   139  						Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   140  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   141  						Eventually(session).Should(Exit(0))
   142  					})
   143  				})
   144  			})
   145  		})
   146  
   147  		When("the app exists", func() {
   148  			BeforeEach(func() {
   149  				helpers.WithHelloWorldApp(func(appDir string) {
   150  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   151  				})
   152  			})
   153  
   154  			It("deletes the app", func() {
   155  				session := helpers.CF("delete", appName, "-f")
   156  				username, _ := helpers.GetCredentials()
   157  				Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   158  				Eventually(session).Should(Say("OK"))
   159  				Eventually(session).Should(Exit(0))
   160  
   161  				Eventually(helpers.CF("app", appName)).Should(Exit(1))
   162  			})
   163  		})
   164  	})
   165  })