github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/configv3/config_unix_test.go (about) 1 // +build !windows 2 3 package configv3_test 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 . "code.cloudfoundry.org/cli/util/configv3" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/ginkgo/extensions/table" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Verbose", func() { 19 var homeDir string 20 21 BeforeEach(func() { 22 homeDir = setup() 23 }) 24 25 AfterEach(func() { 26 teardown(homeDir) 27 }) 28 29 DescribeTable("absolute paths", 30 func(env string, configTrace string, flag bool, expected bool, location []string) { 31 rawConfig := fmt.Sprintf(`{ "Trace":"%s", "ConfigVersion": %d }`, configTrace, CurrentConfigVersion) 32 setConfig(homeDir, rawConfig) 33 34 defer os.Unsetenv("CF_TRACE") 35 if env == "" { 36 Expect(os.Unsetenv("CF_TRACE")).ToNot(HaveOccurred()) 37 } else { 38 Expect(os.Setenv("CF_TRACE", env)).ToNot(HaveOccurred()) 39 } 40 41 config, err := LoadConfig(FlagOverride{ 42 Verbose: flag, 43 }) 44 Expect(err).ToNot(HaveOccurred()) 45 Expect(config).ToNot(BeNil()) 46 47 verbose, parsedLocation := config.Verbose() 48 Expect(verbose).To(Equal(expected)) 49 Expect(parsedLocation).To(Equal(location)) 50 }, 51 52 Entry("CF_TRACE true: enables verbose", "true", "", false, true, nil), 53 Entry("CF_TRACE true, config trace false: enables verbose", "true", "false", false, true, nil), 54 Entry("CF_TRACE true, config trace file path: enables verbose AND logging to file", "true", "/foo/bar", false, true, []string{"/foo/bar"}), 55 56 Entry("CF_TRACE false: disables verbose", "false", "", false, false, nil), 57 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true, true, nil), 58 Entry("CF_TRACE false, config trace true: disables verbose", "false", "true", false, false, nil), 59 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo/bar", false, false, []string{"/foo/bar"}), 60 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo/bar", true, true, []string{"/foo/bar"}), 61 62 Entry("CF_TRACE empty: disables verbose", "", "", false, false, nil), 63 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true, true, nil), 64 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false, true, nil), 65 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo/bar", false, false, []string{"/foo/bar"}), 66 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo/bar", true, true, []string{"/foo/bar"}), 67 68 Entry("CF_TRACE filepath: enables logging to file", "/foo/bar", "", false, false, []string{"/foo/bar"}), 69 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo/bar", "", true, true, []string{"/foo/bar"}), 70 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo/bar", "true", false, true, []string{"/foo/bar"}), 71 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo/bar", "/baz", false, false, []string{"/foo/bar", "/baz"}), 72 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo/bar", "/baz", true, true, []string{"/foo/bar", "/baz"}), 73 ) 74 75 Context("paths that cannot be tested in DescribeTable", func() { 76 Describe("relative paths", func() { 77 It("resolves relative paths into absolute paths", func() { 78 configTrace := "foo/bar" 79 80 rawConfig := fmt.Sprintf(`{ "Trace":"%s", "ConfigVersion": %d }`, configTrace, CurrentConfigVersion) 81 setConfig(homeDir, rawConfig) 82 83 cfTrace := "foo2/bar2" 84 Expect(os.Setenv("CF_TRACE", cfTrace)).ToNot(HaveOccurred()) 85 defer os.Unsetenv("CF_TRACE") 86 87 config, err := LoadConfig(FlagOverride{}) 88 Expect(err).ToNot(HaveOccurred()) 89 Expect(config).ToNot(BeNil()) 90 91 cwd, err := os.Getwd() 92 Expect(err).ToNot(HaveOccurred()) 93 _, parsedLocation := config.Verbose() 94 Expect(parsedLocation).To(Equal([]string{filepath.Join(cwd, cfTrace), filepath.Join(cwd, configTrace)})) 95 }) 96 }) 97 98 Describe("symlinks", func() { 99 var tempDir, ogLogPath, symlinkLogPath string 100 101 BeforeEach(func() { 102 var err error 103 tempDir, err = ioutil.TempDir("", "config-trace") 104 Expect(err).ToNot(HaveOccurred()) 105 106 tempDirAbs, err := filepath.EvalSymlinks(tempDir) 107 Expect(err).ToNot(HaveOccurred()) 108 ogLogPath = filepath.Join(tempDirAbs, "og.log") 109 err = ioutil.WriteFile(ogLogPath, nil, 0666) 110 Expect(err).ToNot(HaveOccurred()) 111 112 symlinkLogPath = filepath.Join(tempDirAbs, "sym.log") 113 err = os.Symlink(ogLogPath, symlinkLogPath) 114 Expect(err).ToNot(HaveOccurred()) 115 }) 116 117 AfterEach(func() { 118 Expect(os.RemoveAll(tempDir)).ToNot(HaveOccurred()) 119 }) 120 121 It("resolves symlinks into absolute paths", func() { 122 rawConfig := fmt.Sprintf(`{ "Trace":"%s", "ConfigVersion": %d }`, symlinkLogPath, CurrentConfigVersion) 123 setConfig(homeDir, rawConfig) 124 125 config, err := LoadConfig(FlagOverride{}) 126 Expect(err).ToNot(HaveOccurred()) 127 Expect(config).ToNot(BeNil()) 128 129 _, parsedLocation := config.Verbose() 130 Expect(parsedLocation).To(ConsistOf(ogLogPath)) 131 }) 132 }) 133 }) 134 })