github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/create_app_manifest_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  	"path/filepath"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/v2v3action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/manifest"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("create-app-manifest Command", func() {
    22  	var (
    23  		cmd             CreateAppManifestCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v6fakes.FakeCreateAppManifestActor
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeCreateAppManifestActor)
    37  
    38  		cmd = CreateAppManifestCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		cmd.RequiredArgs.AppName = "some-app"
    46  		cmd.FilePath = flag.Path("some-file-path")
    47  
    48  		binaryName = "faceman"
    49  		fakeConfig.BinaryNameReturns(binaryName)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("checking target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    59  		})
    60  
    61  		It("returns an error if the check fails", func() {
    62  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeTrue())
    67  			Expect(checkTargetedSpace).To(BeTrue())
    68  		})
    69  	})
    70  
    71  	When("the user is logged in, and org and space are targeted", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.HasTargetedOrganizationReturns(true)
    74  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    75  			fakeConfig.HasTargetedSpaceReturns(true)
    76  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    77  				GUID: "some-space-guid",
    78  				Name: "some-space"})
    79  			fakeConfig.CurrentUserReturns(
    80  				configv3.User{Name: "some-user"},
    81  				nil)
    82  		})
    83  
    84  		When("creating the manifest errors", func() {
    85  			BeforeEach(func() {
    86  				fakeActor.CreateApplicationManifestByNameAndSpaceReturns(manifest.Application{}, v2v3action.Warnings{"some-warning"}, errors.New("some-error"))
    87  			})
    88  
    89  			It("returns the error, prints warnings", func() {
    90  				Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
    91  				Expect(testUI.Err).To(Say("some-warning"))
    92  				Expect(executeErr).To(MatchError("some-error"))
    93  			})
    94  		})
    95  
    96  		When("creating the manifest succeeds", func() {
    97  			BeforeEach(func() {
    98  				fakeActor.CreateApplicationManifestByNameAndSpaceReturns(manifest.Application{}, v2v3action.Warnings{"some-warning"}, nil)
    99  			})
   100  
   101  			It("displays the file it created and returns no errors", func() {
   102  				Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
   103  				Expect(testUI.Err).To(Say("some-warning"))
   104  				Expect(testUI.Out).To(Say("OK"))
   105  				Expect(testUI.Out).To(Say("Manifest file created successfully at some-file-path"))
   106  				Expect(executeErr).ToNot(HaveOccurred())
   107  
   108  				Expect(fakeActor.CreateApplicationManifestByNameAndSpaceCallCount()).To(Equal(1))
   109  				appArg, spaceArg := fakeActor.CreateApplicationManifestByNameAndSpaceArgsForCall(0)
   110  				Expect(appArg).To(Equal("some-app"))
   111  				Expect(spaceArg).To(Equal("some-space-guid"))
   112  
   113  				Expect(fakeActor.WriteApplicationManifestCallCount()).To(Equal(1))
   114  				manifestArg, pathArg := fakeActor.WriteApplicationManifestArgsForCall(0)
   115  				Expect(pathArg).To(Equal("some-file-path"))
   116  				Expect(manifestArg).To(Equal(manifest.Application{}))
   117  			})
   118  
   119  			When("no filepath is provided", func() {
   120  				BeforeEach(func() {
   121  					cmd.FilePath = ""
   122  				})
   123  
   124  				It("creates application manifest in current directry as <app-name>-manifest.yml", func() {
   125  					Expect(testUI.Out).To(Say("Creating an app manifest from current settings of app some-app in org some-org / space some-space as some-user..."))
   126  					Expect(testUI.Err).To(Say("some-warning"))
   127  					Expect(testUI.Out).To(Say("OK"))
   128  					Expect(testUI.Out).To(Say(`Manifest file created successfully at .+some-app_manifest\.yml`))
   129  					Expect(executeErr).ToNot(HaveOccurred())
   130  
   131  					Expect(fakeActor.CreateApplicationManifestByNameAndSpaceCallCount()).To(Equal(1))
   132  					appArg, spaceArg := fakeActor.CreateApplicationManifestByNameAndSpaceArgsForCall(0)
   133  					Expect(appArg).To(Equal("some-app"))
   134  					Expect(spaceArg).To(Equal("some-space-guid"))
   135  
   136  					Expect(fakeActor.WriteApplicationManifestCallCount()).To(Equal(1))
   137  					manifestArg, pathArg := fakeActor.WriteApplicationManifestArgsForCall(0)
   138  					Expect(pathArg).To(Equal(filepath.FromSlash("./some-app_manifest.yml")))
   139  					Expect(manifestArg).To(Equal(manifest.Application{}))
   140  				})
   141  			})
   142  		})
   143  	})
   144  })