github.com/YousefHaggyHeroku/pack@v1.5.5/internal/builder/lifecycle_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"archive/tar"
     5  	"io"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/heroku/color"
    12  	"github.com/sclevine/spec"
    13  	"github.com/sclevine/spec/report"
    14  
    15  	"github.com/YousefHaggyHeroku/pack/internal/blob"
    16  	"github.com/YousefHaggyHeroku/pack/internal/builder"
    17  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    18  )
    19  
    20  func TestLifecycle(t *testing.T) {
    21  	color.Disable(true)
    22  	defer color.Disable(false)
    23  	spec.Run(t, "testLifecycle", testLifecycle, spec.Parallel(), spec.Report(report.Terminal{}))
    24  }
    25  
    26  func testLifecycle(t *testing.T, when spec.G, it spec.S) {
    27  	when("#NewLifecycle", func() {
    28  		when("platform api 0.3", func() {
    29  			it("makes a lifecycle from a blob", func() {
    30  				_, err := builder.NewLifecycle(blob.NewBlob(filepath.Join("testdata", "lifecycle", "platform-0.3")))
    31  				h.AssertNil(t, err)
    32  			})
    33  		})
    34  
    35  		when("platform api 0.4", func() {
    36  			it("makes a lifecycle from a blob", func() {
    37  				_, err := builder.NewLifecycle(blob.NewBlob(filepath.Join("testdata", "lifecycle", "platform-0.4")))
    38  				h.AssertNil(t, err)
    39  			})
    40  		})
    41  
    42  		when("the blob can't open", func() {
    43  			it("throws an error", func() {
    44  				_, err := builder.NewLifecycle(blob.NewBlob(filepath.Join("testdata", "doesn't exist")))
    45  				h.AssertError(t, err, "open lifecycle blob")
    46  			})
    47  		})
    48  
    49  		when("there is no descriptor file", func() {
    50  			it("throws an error", func() {
    51  				_, err := builder.NewLifecycle(&fakeEmptyBlob{})
    52  				h.AssertError(t, err, "could not find entry path 'lifecycle.toml': not exist")
    53  			})
    54  		})
    55  
    56  		when("the descriptor file isn't valid", func() {
    57  			var tmpDir string
    58  
    59  			it.Before(func() {
    60  				var err error
    61  				tmpDir, err = ioutil.TempDir("", "lifecycle")
    62  				h.AssertNil(t, err)
    63  
    64  				h.AssertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "lifecycle.toml"), []byte(`
    65  [api]
    66    platform "0.1"
    67  `), 0711))
    68  			})
    69  
    70  			it.After(func() {
    71  				h.AssertNil(t, os.RemoveAll(tmpDir))
    72  			})
    73  
    74  			it("returns an error", func() {
    75  				_, err := builder.NewLifecycle(blob.NewBlob(tmpDir))
    76  				h.AssertError(t, err, "decoding descriptor")
    77  			})
    78  		})
    79  
    80  		when("the lifecycle has incomplete list of binaries", func() {
    81  			var tmpDir string
    82  
    83  			it.Before(func() {
    84  				var err error
    85  				tmpDir, err = ioutil.TempDir("", "")
    86  				h.AssertNil(t, err)
    87  
    88  				h.AssertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "lifecycle.toml"), []byte(`
    89  [api]
    90    platform = "0.2"
    91    buildpack = "0.3"
    92  
    93  [lifecycle]
    94    version = "1.2.3"
    95  `), os.ModePerm))
    96  
    97  				h.AssertNil(t, os.Mkdir(filepath.Join(tmpDir, "lifecycle"), os.ModePerm))
    98  				h.AssertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "lifecycle", "analyzer"), []byte("content"), os.ModePerm))
    99  				h.AssertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "lifecycle", "detector"), []byte("content"), os.ModePerm))
   100  				h.AssertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "lifecycle", "builder"), []byte("content"), os.ModePerm))
   101  			})
   102  
   103  			it.After(func() {
   104  				h.AssertNil(t, os.RemoveAll(tmpDir))
   105  			})
   106  
   107  			it("returns an error", func() {
   108  				_, err := builder.NewLifecycle(blob.NewBlob(tmpDir))
   109  				h.AssertError(t, err, "validating binaries")
   110  			})
   111  		})
   112  	})
   113  }
   114  
   115  type fakeEmptyBlob struct {
   116  }
   117  
   118  func (f *fakeEmptyBlob) Open() (io.ReadCloser, error) {
   119  	pr, pw := io.Pipe()
   120  	go func() {
   121  		defer pw.Close()
   122  		tw := tar.NewWriter(pw)
   123  		defer tw.Close()
   124  	}()
   125  	return pr, nil
   126  }