github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/util/fileUtils_test.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package util 19 20 import ( 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/getgauge/gauge/config" 27 . "gopkg.in/check.v1" 28 ) 29 30 func Test(t *testing.T) { TestingT(t) } 31 32 type MySuite struct{} 33 34 var _ = Suite(&MySuite{}) 35 var dir string 36 37 func (s *MySuite) SetUpTest(c *C) { 38 var err error 39 dir, err = ioutil.TempDir("", "gaugeTest") 40 c.Assert(err, Equals, nil) 41 } 42 43 func (s *MySuite) TearDownTest(c *C) { 44 err := os.RemoveAll(dir) 45 c.Assert(err, Equals, nil) 46 } 47 48 func (s *MySuite) TestFindAllSpecFiles(c *C) { 49 data := []byte(`Specification Heading 50 ===================== 51 Scenario 1 52 ---------- 53 * say hello 54 `) 55 spec1, err := createFileIn(dir, "gaugeSpec1.spec", data) 56 c.Assert(err, Equals, nil) 57 58 dataRead, err := ioutil.ReadFile(spec1) 59 c.Assert(err, Equals, nil) 60 c.Assert(string(dataRead), Equals, string(data)) 61 c.Assert(len(FindSpecFilesIn(dir)), Equals, 1) 62 63 _, err = createFileIn(dir, "gaugeSpec2.spec", data) 64 c.Assert(err, Equals, nil) 65 66 c.Assert(len(FindSpecFilesIn(dir)), Equals, 2) 67 } 68 69 func (s *MySuite) TestFindAllConceptFiles(c *C) { 70 data := []byte(`#Concept Heading`) 71 _, err := createFileIn(dir, "concept1.cpt", data) 72 c.Assert(err, Equals, nil) 73 c.Assert(len(FindConceptFilesIn(dir)), Equals, 1) 74 75 _, err = createFileIn(dir, "concept2.cpt", data) 76 c.Assert(err, Equals, nil) 77 c.Assert(len(FindConceptFilesIn(dir)), Equals, 2) 78 } 79 80 func (s *MySuite) TestFindAllConceptFilesShouldFilterDirectoriesThatAreSkipped(c *C) { 81 config.ProjectRoot = dir 82 data := []byte(`#Concept Heading`) 83 git, _ := createDirIn(dir, ".git") 84 bin, _ := createDirIn(dir, "gauge_bin") 85 reports, _ := createDirIn(dir, "reports") 86 env, _ := createDirIn(dir, "env") 87 88 createFileIn(git, "concept1.cpt", data) 89 createFileIn(bin, "concept2.cpt", data) 90 createFileIn(reports, "concept3.cpt", data) 91 createFileIn(env, "concept4.cpt", data) 92 93 c.Assert(len(FindConceptFilesIn(dir)), Equals, 0) 94 95 _, err := createFileIn(dir, "concept2.cpt", data) 96 c.Assert(err, Equals, nil) 97 c.Assert(len(FindConceptFilesIn(dir)), Equals, 1) 98 } 99 100 func (s *MySuite) TestFindAllConceptFilesInNestedDir(c *C) { 101 data := []byte(`#Concept Heading 102 * Say "hello" to gauge 103 `) 104 _, err := createFileIn(dir, "concept1.cpt", data) 105 c.Assert(err, Equals, nil) 106 c.Assert(len(FindConceptFilesIn(dir)), Equals, 1) 107 108 dir1, err := ioutil.TempDir(dir, "gaugeTest1") 109 c.Assert(err, Equals, nil) 110 111 _, err = createFileIn(dir1, "concept2.cpt", data) 112 c.Assert(err, Equals, nil) 113 c.Assert(len(FindConceptFilesIn(dir)), Equals, 2) 114 } 115 116 func (s *MySuite) TestGetConceptFiles(c *C) { 117 config.ProjectRoot = "_testdata" 118 specsDir, _ := filepath.Abs(filepath.Join("_testdata", "specs")) 119 120 config.ProjectRoot = specsDir 121 c.Assert(len(GetConceptFiles()), Equals, 2) 122 123 config.ProjectRoot = filepath.Join(specsDir, "concept1.cpt") 124 c.Assert(len(GetConceptFiles()), Equals, 1) 125 126 config.ProjectRoot = filepath.Join(specsDir, "subdir") 127 c.Assert(len(GetConceptFiles()), Equals, 1) 128 129 config.ProjectRoot = filepath.Join(specsDir, "subdir", "concept2.cpt") 130 c.Assert(len(GetConceptFiles()), Equals, 1) 131 } 132 133 func (s *MySuite) TestFindAllNestedDirs(c *C) { 134 nested1 := filepath.Join(dir, "nested") 135 nested2 := filepath.Join(dir, "nested2") 136 nested3 := filepath.Join(dir, "nested2", "deep") 137 nested4 := filepath.Join(dir, "nested2", "deep", "deeper") 138 os.Mkdir(nested1, 0755) 139 os.Mkdir(nested2, 0755) 140 os.Mkdir(nested3, 0755) 141 os.Mkdir(nested4, 0755) 142 143 nestedDirs := FindAllNestedDirs(dir) 144 c.Assert(len(nestedDirs), Equals, 4) 145 c.Assert(stringInSlice(nested1, nestedDirs), Equals, true) 146 c.Assert(stringInSlice(nested2, nestedDirs), Equals, true) 147 c.Assert(stringInSlice(nested3, nestedDirs), Equals, true) 148 c.Assert(stringInSlice(nested4, nestedDirs), Equals, true) 149 } 150 151 func (s *MySuite) TestFindAllNestedDirsWhenDirDoesNotExist(c *C) { 152 nestedDirs := FindAllNestedDirs("unknown-dir") 153 c.Assert(len(nestedDirs), Equals, 0) 154 } 155 156 func (s *MySuite) TestIsDir(c *C) { 157 c.Assert(IsDir(dir), Equals, true) 158 c.Assert(IsDir(filepath.Join(dir, "foo.txt")), Equals, false) 159 c.Assert(IsDir("unknown path"), Equals, false) 160 c.Assert(IsDir("foo/goo.txt"), Equals, false) 161 } 162 163 func stringInSlice(a string, list []string) bool { 164 for _, b := range list { 165 if b == a { 166 return true 167 } 168 } 169 return false 170 } 171 172 func (s *MySuite) TestGetPathToFile(c *C) { 173 var path string 174 config.ProjectRoot = "PROJECT_ROOT" 175 absPath, _ := filepath.Abs("resources") 176 path = GetPathToFile(absPath) 177 c.Assert(path, Equals, absPath) 178 179 path = GetPathToFile("resources") 180 c.Assert(path, Equals, filepath.Join(config.ProjectRoot, "resources")) 181 } 182 183 func createFileIn(dir string, fileName string, data []byte) (string, error) { 184 os.MkdirAll(dir, 0755) 185 err := ioutil.WriteFile(filepath.Join(dir, fileName), data, 0644) 186 return filepath.Join(dir, fileName), err 187 } 188 189 func createDirIn(dir string, dirName string) (string, error) { 190 tempDir, err := ioutil.TempDir(dir, dirName) 191 fullDirName := filepath.Join(dir, dirName) 192 err = os.Rename(tempDir, fullDirName) 193 return fullDirName, err 194 }