github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/e2e/target-cleanup/target_cleanup_test.go (about)

     1  package targetcleanuptest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/benchkram/bob/bob"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Testing correct removal of directory targets", func() {
    13  	ctx := context.Background()
    14  
    15  	When("caching is enabled", func() {
    16  
    17  		var b *bob.B
    18  		It("should setup test environment", func() {
    19  			err := useBobfile("with_dir_target")
    20  			Expect(err).NotTo(HaveOccurred())
    21  
    22  			bob, err := BobSetup()
    23  			Expect(err).NotTo(HaveOccurred())
    24  			b = bob
    25  		})
    26  
    27  		It("should build the task", func() {
    28  			err := b.Build(ctx, "build")
    29  			Expect(err).NotTo(HaveOccurred())
    30  
    31  			dirContents, err := readDir("sub-dir")
    32  			Expect(err).NotTo(HaveOccurred())
    33  			Expect(dirContents).To(HaveLen(1))
    34  			Expect(dirContents).To(ContainElement("non-empty-file"))
    35  		})
    36  
    37  		It("should invalidate the target by adding an empty file", func() {
    38  			emptyFile, err := os.Create("./sub-dir/empty-file")
    39  			Expect(err).NotTo(HaveOccurred())
    40  			err = emptyFile.Close()
    41  			Expect(err).NotTo(HaveOccurred())
    42  
    43  			dirContents, err := readDir("sub-dir")
    44  			Expect(err).NotTo(HaveOccurred())
    45  			Expect(dirContents).To(HaveLen(2))
    46  			Expect(dirContents).To(ContainElement("non-empty-file"))
    47  			Expect(dirContents).To(ContainElement("empty-file"))
    48  		})
    49  
    50  		It("should remove the target directory and expect the targets being loaded from the cache", func() {
    51  			err := b.Build(ctx, "build")
    52  			Expect(err).NotTo(HaveOccurred())
    53  
    54  			// the empty file should not be there anymore
    55  			dirContents, err := readDir("sub-dir")
    56  			Expect(err).NotTo(HaveOccurred())
    57  			Expect(dirContents).To(HaveLen(1))
    58  			Expect(dirContents).To(ContainElement("non-empty-file"))
    59  			Expect(dirContents).NotTo(ContainElement("empty-file"))
    60  		})
    61  
    62  		It("should cleanup test environment", func() {
    63  			err := releaseBobfile("with_dir_target")
    64  			Expect(err).NotTo(HaveOccurred())
    65  		})
    66  
    67  	})
    68  
    69  	When("cache is disabled", func() {
    70  
    71  		var b *bob.B
    72  		It("should setup test environment", func() {
    73  			err := useBobfile("with_dir_target")
    74  			Expect(err).NotTo(HaveOccurred())
    75  
    76  			bob, err := BobSetupNoCache()
    77  			Expect(err).NotTo(HaveOccurred())
    78  			b = bob
    79  		})
    80  
    81  		It("should build the task", func() {
    82  			err := b.Build(ctx, "build")
    83  			Expect(err).NotTo(HaveOccurred())
    84  
    85  			dirContents, err := readDir("sub-dir")
    86  			Expect(err).NotTo(HaveOccurred())
    87  			Expect(dirContents).To(HaveLen(1))
    88  			Expect(dirContents).To(ContainElement("non-empty-file"))
    89  		})
    90  
    91  		It("should invalidate the target by adding an empty file", func() {
    92  			emptyFile, err := os.Create("./sub-dir/empty-file")
    93  			Expect(err).NotTo(HaveOccurred())
    94  			err = emptyFile.Close()
    95  			Expect(err).NotTo(HaveOccurred())
    96  
    97  			dirContents, err := readDir("sub-dir")
    98  			Expect(err).NotTo(HaveOccurred())
    99  			Expect(dirContents).To(HaveLen(2))
   100  			Expect(dirContents).To(ContainElement("non-empty-file"))
   101  			Expect(dirContents).To(ContainElement("empty-file"))
   102  
   103  		})
   104  
   105  		It("should remove the target directory and expect the targets being recreating doing a full rebuild", func() {
   106  			err := b.Build(ctx, "build")
   107  			Expect(err).NotTo(HaveOccurred())
   108  
   109  			// the empty file should not be there anymore
   110  			dirContents, err := readDir("sub-dir")
   111  			Expect(err).NotTo(HaveOccurred())
   112  			Expect(dirContents).To(HaveLen(1))
   113  			Expect(dirContents).To(ContainElement("non-empty-file"))
   114  			Expect(dirContents).NotTo(ContainElement("empty-file"))
   115  		})
   116  
   117  		It("should cleanup test environment", func() {
   118  			err := releaseBobfile("with_dir_target")
   119  			Expect(err).NotTo(HaveOccurred())
   120  		})
   121  	})
   122  })