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