github.com/mattevans/edward@v1.9.2/edward/copy_test.go (about)

     1  package edward_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"path"
    10  	"time"
    11  
    12  	"github.com/mattevans/edward/common"
    13  	"github.com/mattevans/edward/home"
    14  
    15  	"github.com/mattevans/edward/edward"
    16  )
    17  
    18  func createClient(configFile, testName, testPath string) (*edward.Client, string, func(), error) {
    19  	// Copy test content into a temp dir on the GOPATH & defer deletion
    20  	wd, cleanup, err := createWorkingDir(testName, testPath)
    21  	if err != nil {
    22  		return nil, "", func() {}, err
    23  	}
    24  
    25  	dirConfig := &home.EdwardConfiguration{}
    26  	err = dirConfig.InitializeWithDir(path.Join(wd, "edwardHome"))
    27  	if err != nil {
    28  		return nil, "", func() {}, err
    29  	}
    30  
    31  	client, err := edward.NewClientWithConfig(
    32  		path.Join(wd, configFile),
    33  		common.EdwardVersion,
    34  	)
    35  	if err != nil {
    36  		return nil, "", func() {}, err
    37  	}
    38  
    39  	client.DirConfig = dirConfig
    40  	client.EdwardExecutable = edwardExecutable
    41  	client.DisableConcurrentPhases = true
    42  	client.WorkingDir = wd
    43  	client.Tags = []string{fmt.Sprintf("test.%d", time.Now().UnixNano())}
    44  
    45  	return client, wd, cleanup, nil
    46  }
    47  
    48  // createWorkingDir creates a directory to work in and changes into that directory.
    49  // Returns a cleanup function.
    50  func createWorkingDir(testName, testPath string) (string, func(), error) {
    51  	wd, err := os.Getwd()
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  	workingPath := path.Join(wd, "testdata", ".working")
    56  	if _, err := os.Stat(workingPath); os.IsNotExist(err) {
    57  		os.Mkdir(workingPath, os.ModePerm)
    58  	}
    59  	workingDir, err := ioutil.TempDir(workingPath, testName)
    60  	if err != nil {
    61  		return "", func() {}, err
    62  	}
    63  	copy_folder(testPath, workingDir)
    64  	return workingDir, func() {
    65  		os.RemoveAll(workingDir)
    66  	}, nil
    67  }
    68  
    69  func copy_folder(source string, dest string) (err error) {
    70  
    71  	sourceinfo, err := os.Stat(source)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	err = os.MkdirAll(dest, sourceinfo.Mode())
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	directory, _ := os.Open(source)
    82  
    83  	objects, err := directory.Readdir(-1)
    84  
    85  	for _, obj := range objects {
    86  
    87  		sourcefilepointer := source + "/" + obj.Name()
    88  
    89  		destinationfilepointer := dest + "/" + obj.Name()
    90  
    91  		if obj.IsDir() {
    92  			err = copy_folder(sourcefilepointer, destinationfilepointer)
    93  			if err != nil {
    94  				fmt.Println("Copying folder:", err)
    95  			}
    96  		} else {
    97  			err = copy_file(sourcefilepointer, destinationfilepointer)
    98  			if err != nil {
    99  				fmt.Println("Copying file:", err)
   100  			}
   101  		}
   102  
   103  	}
   104  	return
   105  }
   106  
   107  func copy_file(source string, dest string) (err error) {
   108  	sourcefile, err := os.Open(source)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	defer sourcefile.Close()
   114  
   115  	destfile, err := os.Create(dest)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	defer destfile.Close()
   121  
   122  	_, err = io.Copy(destfile, sourcefile)
   123  
   124  	// Copy permissions
   125  	si, err := os.Stat(source)
   126  	if err != nil {
   127  		return
   128  	}
   129  	err = os.Chmod(dest, si.Mode())
   130  	if err != nil {
   131  		return
   132  	}
   133  
   134  	return
   135  }