code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v7pushaction/handle_app_path_override_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
     9  	"code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	"code.cloudfoundry.org/cli/util/manifestparser"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("HandleAppPathOverride", func() {
    18  	var (
    19  		transformedManifest manifestparser.Manifest
    20  		executeErr          error
    21  
    22  		parsedManifest manifestparser.Manifest
    23  		flagOverrides  FlagOverrides
    24  		err            error
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		flagOverrides = FlagOverrides{}
    29  		parsedManifest = manifestparser.Manifest{}
    30  		Expect(err).NotTo(HaveOccurred())
    31  	})
    32  
    33  	JustBeforeEach(func() {
    34  		transformedManifest, executeErr = HandleAppPathOverride(
    35  			parsedManifest,
    36  			flagOverrides,
    37  		)
    38  	})
    39  
    40  	When("the path flag override is set", func() {
    41  		var relativeAppFilePath string
    42  
    43  		BeforeEach(func() {
    44  			file, err := ioutil.TempFile("", "")
    45  			Expect(err).NotTo(HaveOccurred())
    46  			relativeAppFilePath = file.Name()
    47  			defer file.Close()
    48  
    49  			flagOverrides = FlagOverrides{
    50  				ProvidedAppPath: relativeAppFilePath,
    51  			}
    52  		})
    53  
    54  		AfterEach(func() {
    55  			err := os.RemoveAll(relativeAppFilePath)
    56  			Expect(err).NotTo(HaveOccurred())
    57  		})
    58  
    59  		When("there are multiple apps in the manifest", func() {
    60  			BeforeEach(func() {
    61  				parsedManifest = manifestparser.Manifest{
    62  					Applications: []manifestparser.Application{
    63  						{},
    64  						{},
    65  					},
    66  				}
    67  			})
    68  
    69  			It("returns an error", func() {
    70  				Expect(executeErr).To(MatchError(translatableerror.CommandLineArgsWithMultipleAppsError{}))
    71  			})
    72  		})
    73  
    74  		When("there is a single app in the manifest", func() {
    75  			BeforeEach(func() {
    76  				parsedManifest = manifestparser.Manifest{
    77  					Applications: []manifestparser.Application{
    78  						{
    79  							Path: "some-path",
    80  						},
    81  					},
    82  					PathToManifest: "/path/to/manifest.yml",
    83  				}
    84  			})
    85  
    86  			It("overrides the path for the first app in the manifest", func() {
    87  				Expect(transformedManifest.Applications[0].Path).To(matchers.MatchPath(relativeAppFilePath))
    88  			})
    89  
    90  			When("the application's path is relative and passed as a flag", func() {
    91  				var cwd string
    92  				var absoluteAppFilehandle *os.File
    93  				BeforeEach(func() {
    94  					absoluteAppFilehandle, err = ioutil.TempFile("", "")
    95  					Expect(err).NotTo(HaveOccurred())
    96  					defer absoluteAppFilehandle.Close()
    97  					relativeAppFilePath = filepath.Join(".", absoluteAppFilehandle.Name())
    98  					flagOverrides.ProvidedAppPath = relativeAppFilePath
    99  
   100  					// TODO: Do NOT use Chdir! it affects ALL other threads
   101  					// Save the current working directory so you can return to it later
   102  					cwd, err = os.Getwd()
   103  					Expect(err).NotTo(HaveOccurred())
   104  					// Go to root directory before executing HandleAppPathOverride()
   105  					err = os.Chdir("/")
   106  					Expect(err).NotTo(HaveOccurred())
   107  				})
   108  				AfterEach(func() {
   109  					err = os.Chdir(cwd)
   110  					Expect(err).NotTo(HaveOccurred())
   111  				})
   112  
   113  				It("doesn't override the path for the first app in the manifest", func() {
   114  					Expect(executeErr).NotTo(HaveOccurred())
   115  					Expect(transformedManifest.Applications[0].Path).To(matchers.MatchPath(relativeAppFilePath))
   116  				})
   117  			})
   118  		})
   119  	})
   120  
   121  	When("the path flag override is not set", func() {
   122  		BeforeEach(func() {
   123  			parsedManifest = manifestparser.Manifest{
   124  				Applications: []manifestparser.Application{
   125  					{},
   126  				},
   127  			}
   128  		})
   129  
   130  		It("does not change the app path", func() {
   131  			Expect(executeErr).NotTo(HaveOccurred())
   132  			Expect(transformedManifest.Applications[0].Path).To(Equal(""))
   133  		})
   134  	})
   135  
   136  	When("the manifest contains an invalid path", func() {
   137  		BeforeEach(func() {
   138  			parsedManifest = manifestparser.Manifest{
   139  				Applications: []manifestparser.Application{
   140  					{
   141  						Path: "some-non-existent-path",
   142  					},
   143  				},
   144  			}
   145  		})
   146  
   147  		It("returns an error", func() {
   148  			Expect(executeErr).To(MatchError(manifestparser.InvalidManifestApplicationPathError{
   149  				Path: "some-non-existent-path",
   150  			}))
   151  		})
   152  	})
   153  
   154  	When("docker info is set in the manifest", func() {
   155  		BeforeEach(func() {
   156  			flagOverrides.ProvidedAppPath = "/some/path"
   157  
   158  			parsedManifest.Applications = []manifestparser.Application{
   159  				{
   160  					Name:   "some-app",
   161  					Docker: &manifestparser.Docker{},
   162  				},
   163  			}
   164  		})
   165  
   166  		It("returns an error", func() {
   167  			Expect(executeErr).To(MatchError(translatableerror.ArgumentManifestMismatchError{
   168  				Arg:              "--path, -p",
   169  				ManifestProperty: "docker",
   170  			}))
   171  		})
   172  	})
   173  })