github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/otto/testing_appfile.go (about)

     1  package otto
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	"github.com/hashicorp/otto/appfile"
     8  	"github.com/hashicorp/otto/appfile/detect"
     9  )
    10  
    11  // TestAppfile returns a compiled appfile for the given path. This uses
    12  // defaults for detectors and such so it is up to you to use a fairly
    13  // complete Appfile.
    14  func TestAppfile(t TestT, path string) *appfile.Compiled {
    15  	def, err := appfile.Default(filepath.Dir(path), &detect.Config{
    16  		Detectors: []*detect.Detector{
    17  			&detect.Detector{
    18  				Type: "test",
    19  				File: []string{"Appfile"},
    20  			},
    21  		},
    22  	})
    23  	if err != nil {
    24  		t.Fatal("err: ", err)
    25  	}
    26  
    27  	// Default type should be "test"
    28  	def.Infrastructure[0].Type = "test"
    29  	def.Infrastructure[0].Flavor = "test"
    30  	def.Infrastructure[0].Foundations = nil
    31  
    32  	// Parse the raw file
    33  	f, err := appfile.ParseFile(path)
    34  	if err != nil {
    35  		t.Fatal("err: ", err)
    36  	}
    37  
    38  	// Merge
    39  	if err := def.Merge(f); err != nil {
    40  		t.Fatal("err: ", err)
    41  	}
    42  	f = def
    43  
    44  	// Create a temporary directory for the compilation data. We don't
    45  	// delete this now in case we're using any of that data, but the
    46  	// temp dir should get cleaned up by the system at some point.
    47  	td, err := ioutil.TempDir("", "otto")
    48  	if err != nil {
    49  		t.Fatal("err: ", err)
    50  	}
    51  
    52  	// Compile it!
    53  	compiler, err := appfile.NewCompiler(&appfile.CompileOpts{
    54  		Dir: td,
    55  	})
    56  	if err != nil {
    57  		t.Fatal("err: ", err)
    58  	}
    59  	result, err := compiler.Compile(f)
    60  	if err != nil {
    61  		t.Fatal("err: ", err)
    62  	}
    63  
    64  	return result
    65  }