github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/testutil/integration_test_setup.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/evergreen-ci/evergreen"
    11  	"github.com/evergreen-ci/evergreen/model"
    12  	"github.com/pkg/errors"
    13  	"gopkg.in/yaml.v2"
    14  )
    15  
    16  // Creates a project ref local config that can be used for testing, with the string identifier given
    17  // and the local config from a path
    18  func CreateTestLocalConfig(testSettings *evergreen.Settings, projectName, projectPath string) error {
    19  	if projectPath == "" {
    20  		config, err := findConfig(testSettings.ConfigDir)
    21  		if err != nil {
    22  			return err
    23  		}
    24  		projectPath = filepath.Join(config, "project", fmt.Sprintf("%v.yml", projectName))
    25  	}
    26  
    27  	projectRef, err := model.FindOneProjectRef(projectName)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	if projectRef == nil {
    33  		projectRef = &model.ProjectRef{}
    34  	}
    35  
    36  	data, err := ioutil.ReadFile(projectPath)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	err = yaml.Unmarshal(data, projectRef)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	projectRef.LocalConfig = string(data)
    47  
    48  	return projectRef.Upsert()
    49  }
    50  
    51  // findConfig finds the config root in the home directory.
    52  // Returns an error if the root cannot be found or EVGHOME is unset.
    53  func findConfig(configName string) (string, error) {
    54  	home := evergreen.FindEvergreenHome()
    55  	if len(home) > 0 {
    56  		root, yes := isConfigRoot(home, configName)
    57  		if yes {
    58  			return root, nil
    59  		}
    60  		return "", errors.Errorf("Can't find evergreen config root: '%v'", root)
    61  	}
    62  
    63  	return "", errors.Errorf("%v environment variable must be set", evergreen.EvergreenHome)
    64  }
    65  
    66  func isConfigRoot(home string, configName string) (fixed string, is bool) {
    67  	fixed = filepath.Join(home, configName)
    68  	fixed = strings.Replace(fixed, "\\", "/", -1)
    69  	stat, err := os.Stat(fixed)
    70  	if err == nil && stat.IsDir() {
    71  		is = true
    72  	}
    73  	return
    74  }