github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/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) TestFindAllConceptFilesInNestedDir(c *C) {
    81  	data := []byte(`#Concept Heading
    82  * Say "hello" to gauge
    83  `)
    84  	_, err := CreateFileIn(dir, "concept1.cpt", data)
    85  	c.Assert(err, Equals, nil)
    86  	c.Assert(len(FindConceptFilesIn(dir)), Equals, 1)
    87  
    88  	dir1, err := ioutil.TempDir(dir, "gaugeTest1")
    89  	c.Assert(err, Equals, nil)
    90  
    91  	_, err = CreateFileIn(dir1, "concept2.cpt", data)
    92  	c.Assert(err, Equals, nil)
    93  	c.Assert(len(FindConceptFilesIn(dir)), Equals, 2)
    94  }
    95  
    96  func (s *MySuite) TestGetConceptFiles(c *C) {
    97  	config.ProjectRoot = "_testdata"
    98  	specsDir, _ := filepath.Abs(filepath.Join("_testdata", "specs"))
    99  
   100  	c.Assert(len(GetConceptFiles(specsDir)), Equals, 2)
   101  	c.Assert(len(GetConceptFiles(filepath.Join(specsDir, "concept1.cpt"))), Equals, 2)
   102  	c.Assert(len(GetConceptFiles(filepath.Join(specsDir, "subdir"))), Equals, 2)
   103  	c.Assert(len(GetConceptFiles(filepath.Join(specsDir, "subdir", "concept2.cpt"))), Equals, 2)
   104  }
   105  
   106  func (s *MySuite) TestFindAllNestedDirs(c *C) {
   107  	nested1 := filepath.Join(dir, "nested")
   108  	nested2 := filepath.Join(dir, "nested2")
   109  	nested3 := filepath.Join(dir, "nested2", "deep")
   110  	nested4 := filepath.Join(dir, "nested2", "deep", "deeper")
   111  	os.Mkdir(nested1, 0755)
   112  	os.Mkdir(nested2, 0755)
   113  	os.Mkdir(nested3, 0755)
   114  	os.Mkdir(nested4, 0755)
   115  
   116  	nestedDirs := FindAllNestedDirs(dir)
   117  	c.Assert(len(nestedDirs), Equals, 4)
   118  	c.Assert(stringInSlice(nested1, nestedDirs), Equals, true)
   119  	c.Assert(stringInSlice(nested2, nestedDirs), Equals, true)
   120  	c.Assert(stringInSlice(nested3, nestedDirs), Equals, true)
   121  	c.Assert(stringInSlice(nested4, nestedDirs), Equals, true)
   122  }
   123  
   124  func (s *MySuite) TestFindAllNestedDirsWhenDirDoesNotExist(c *C) {
   125  	nestedDirs := FindAllNestedDirs("unknown-dir")
   126  	c.Assert(len(nestedDirs), Equals, 0)
   127  }
   128  
   129  func (s *MySuite) TestIsDir(c *C) {
   130  	c.Assert(IsDir(dir), Equals, true)
   131  	c.Assert(IsDir(filepath.Join(dir, "foo.txt")), Equals, false)
   132  	c.Assert(IsDir("unknown path"), Equals, false)
   133  	c.Assert(IsDir("foo/goo.txt"), Equals, false)
   134  }
   135  
   136  func stringInSlice(a string, list []string) bool {
   137  	for _, b := range list {
   138  		if b == a {
   139  			return true
   140  		}
   141  	}
   142  	return false
   143  }
   144  
   145  func (s *MySuite) TestGetPathToFile(c *C) {
   146  	var path string
   147  	config.ProjectRoot = "PROJECT_ROOT"
   148  	absPath, _ := filepath.Abs("resources")
   149  	path = GetPathToFile(absPath)
   150  	c.Assert(path, Equals, absPath)
   151  
   152  	path = GetPathToFile("resources")
   153  	c.Assert(path, Equals, filepath.Join(config.ProjectRoot, "resources"))
   154  }