github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/file_test.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/evergreen-ci/evergreen/testutil"
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func TestWriteToTempFile(t *testing.T) {
    14  	Convey("When writing content to a temp file", t, func() {
    15  		Convey("ensure the exact contents passed are written", func() {
    16  			fileData := "data"
    17  			filePath, err := WriteToTempFile(fileData)
    18  			testutil.HandleTestingErr(err, t, "error writing to temp file %v")
    19  			fileBytes, err := ioutil.ReadFile(filePath)
    20  			testutil.HandleTestingErr(err, t, "error reading from temp file %v")
    21  			So(string(fileBytes), ShouldEqual, fileData)
    22  			testutil.HandleTestingErr(os.Remove(filePath), t,
    23  				"error removing to temp file %v")
    24  		})
    25  	})
    26  }
    27  
    28  func TestFileExists(t *testing.T) {
    29  	tmpfile, err := ioutil.TempFile("", "testFileOne")
    30  	testutil.HandleTestingErr(err, t, "error creating test file")
    31  	defer os.Remove(tmpfile.Name())
    32  
    33  	Convey("When testing that a file exists", t, func() {
    34  
    35  		Convey("an existing file should be reported as existing", func() {
    36  			exists, err := FileExists(tmpfile.Name())
    37  			So(err, ShouldBeNil)
    38  			So(exists, ShouldBeTrue)
    39  		})
    40  
    41  		Convey("a nonexistent file should be reported as missing", func() {
    42  			exists, err := FileExists("testFileThatDoesNotExist1234567")
    43  			So(err, ShouldBeNil)
    44  			So(exists, ShouldBeFalse)
    45  		})
    46  	})
    47  }
    48  
    49  func TestBuildFileList(t *testing.T) {
    50  	fnames := []string{
    51  		"testFile1",
    52  		"testFile2",
    53  		"testFile.go",
    54  		"testFile2.go",
    55  		"testFile3.yml",
    56  		"built.yml",
    57  		"built.go",
    58  		"built.cpp",
    59  	}
    60  	dirNames := []string{
    61  		"dir1",
    62  		"dir2",
    63  	}
    64  	// create all the files in the current directory
    65  	for _, fname := range fnames {
    66  		_, err := os.Create(fname)
    67  		testutil.HandleTestingErr(err, t, "error creating test file")
    68  	}
    69  
    70  	// create all the files in the sub directories
    71  	for _, dirName := range dirNames {
    72  		err := os.Mkdir(dirName, 0777)
    73  		testutil.HandleTestingErr(err, t, "error creating test directory")
    74  		for _, fname := range fnames {
    75  			path := fmt.Sprintf("%s%v%s", dirName, string(os.PathSeparator), fname)
    76  			_, err := os.Create(path)
    77  			testutil.HandleTestingErr(err, t, "error creating test file")
    78  		}
    79  	}
    80  	defer func() {
    81  		for _, dirName := range dirNames {
    82  			testutil.HandleTestingErr(os.RemoveAll(dirName), t, "error removing test directory")
    83  		}
    84  		for _, fname := range fnames {
    85  			testutil.HandleTestingErr(os.Remove(fname), t, "error removing test file")
    86  		}
    87  	}()
    88  	Convey("When files and directories exists", t, func() {
    89  		Convey("Should just match one file when expression is a name", func() {
    90  			files, err := BuildFileList(".", fnames[0])
    91  			So(err, ShouldBeNil)
    92  			So(files, ShouldContain, fnames[0])
    93  			for i := 1; i < len(fnames); i++ {
    94  				So(files, ShouldNotContain, fnames[i])
    95  			}
    96  		})
    97  		Convey("Should match all files in base directory with prefix", func() {
    98  			files, err := BuildFileList(".", "/testFile*")
    99  			So(err, ShouldBeNil)
   100  			for i, fname := range fnames {
   101  				if i <= 4 {
   102  					So(files, ShouldContain, fname)
   103  				} else {
   104  					So(files, ShouldNotContain, fname)
   105  				}
   106  			}
   107  		})
   108  		Convey("Should match all files in base directory with suffix", func() {
   109  			files, err := BuildFileList(".", "/*.go")
   110  			So(err, ShouldBeNil)
   111  			for i, fname := range fnames {
   112  				if i == 2 || i == 3 || i == 6 {
   113  					So(files, ShouldContain, fname)
   114  				} else {
   115  					So(files, ShouldNotContain, fname)
   116  				}
   117  			}
   118  		})
   119  		Convey("Should match all files in a specified sub directory with suffix", func() {
   120  			files, err := BuildFileList(".", "/dir1/*.go")
   121  			So(err, ShouldBeNil)
   122  			for i, fname := range fnames {
   123  				path := fmt.Sprintf("%s%v%s", "dir1", string(os.PathSeparator), fname)
   124  				if i == 2 || i == 3 || i == 6 {
   125  					So(files, ShouldContain, path)
   126  					So(files, ShouldNotContain, fname)
   127  				} else {
   128  					So(files, ShouldNotContain, path)
   129  					So(files, ShouldNotContain, fname)
   130  				}
   131  			}
   132  		})
   133  		Convey("Should match all files in multiple wildcard sub directory with suffix", func() {
   134  			files, err := BuildFileList(".", "/*/*.go")
   135  			So(err, ShouldBeNil)
   136  			for i, fname := range fnames {
   137  				for _, dirName := range dirNames {
   138  					path := fmt.Sprintf("%s%v%s", dirName, string(os.PathSeparator), fname)
   139  					if i == 2 || i == 3 || i == 6 {
   140  						So(files, ShouldContain, path)
   141  						So(files, ShouldNotContain, fname)
   142  					} else {
   143  						So(files, ShouldNotContain, path)
   144  						So(files, ShouldNotContain, fname)
   145  					}
   146  				}
   147  			}
   148  		})
   149  	})
   150  }