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