github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/e2e/task-decoration/suite_test.go (about)

     1  package taskdecorationtest
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/benchkram/bob/bob"
    11  	"github.com/benchkram/bob/bob/bobfile"
    12  	"github.com/benchkram/bob/pkg/buildinfostore"
    13  	"github.com/benchkram/bob/pkg/store"
    14  	"github.com/benchkram/bob/test/setup"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var (
    21  	// dir is the basic test directory
    22  	// in which the test is executed.
    23  	dir string
    24  
    25  	// artifactStore temporary store to
    26  	// avoid interfering with the users cache.
    27  	artifactStore store.Store
    28  	// buildInfoStore temporary store
    29  	// to avoid interfering with the users cache.
    30  	buildInfoStore buildinfostore.Store
    31  
    32  	// cleanup is called at the end to remove all test files from the system.
    33  	cleanup func() error
    34  
    35  	// tmpFiles tracks temporarily created files
    36  	// to be cleaned up at the end.
    37  	tmpFiles []string
    38  
    39  	secondLevelDir = "second"
    40  	thirdLevelDir  = "third"
    41  )
    42  
    43  var _ = BeforeSuite(func() {
    44  
    45  	// Initialize mock bob files from local directory
    46  	bobFiles := []string{
    47  		"with_decoration",
    48  		filepath.Join("with_decoration", secondLevelDir),
    49  		"with_thirdlevel_decoration",
    50  		filepath.Join("with_thirdlevel_decoration", secondLevelDir),
    51  		filepath.Join("with_thirdlevel_decoration", secondLevelDir, thirdLevelDir),
    52  		"with_missed_decoration",
    53  		"with_invalid_decoration",
    54  		filepath.Join("with_invalid_decoration", secondLevelDir),
    55  	}
    56  
    57  	nameToBobfile := make(map[string]*bobfile.Bobfile)
    58  	for _, name := range bobFiles {
    59  		abs, err := filepath.Abs("./" + name)
    60  		Expect(err).NotTo(HaveOccurred())
    61  		bf, err := bobfile.BobfileRead(abs)
    62  		Expect(err).NotTo(HaveOccurred())
    63  		nameToBobfile[strings.ReplaceAll(name, "/", "_")] = bf
    64  	}
    65  
    66  	var err error
    67  	var storageDir string
    68  	dir, storageDir, cleanup, err = setup.TestDirs("task-decoration")
    69  	Expect(err).NotTo(HaveOccurred())
    70  
    71  	artifactStore, err = bob.Filestore(storageDir)
    72  	Expect(err).NotTo(HaveOccurred())
    73  	buildInfoStore, err = bob.BuildinfoStore(storageDir)
    74  	Expect(err).NotTo(HaveOccurred())
    75  
    76  	err = os.Mkdir(filepath.Join(dir, secondLevelDir), 0700)
    77  	Expect(err).NotTo(HaveOccurred())
    78  
    79  	err = os.Mkdir(filepath.Join(dir, secondLevelDir, thirdLevelDir), 0700)
    80  	Expect(err).NotTo(HaveOccurred())
    81  
    82  	err = os.Chdir(dir)
    83  	Expect(err).NotTo(HaveOccurred())
    84  
    85  	// Save bob files in dir to have them available in tests
    86  	for name, bf := range nameToBobfile {
    87  		err = bf.BobfileSave(dir, name+".yaml")
    88  		Expect(err).NotTo(HaveOccurred())
    89  	}
    90  })
    91  
    92  var _ = AfterSuite(func() {
    93  	err := os.RemoveAll(dir)
    94  	Expect(err).NotTo(HaveOccurred())
    95  
    96  	for _, file := range tmpFiles {
    97  		err = os.Remove(file)
    98  		Expect(err).NotTo(HaveOccurred())
    99  	}
   100  
   101  	err = cleanup()
   102  	Expect(err).NotTo(HaveOccurred())
   103  })
   104  
   105  func TestOverwrite(t *testing.T) {
   106  	_, err := exec.LookPath("nix")
   107  	if err != nil {
   108  		// Allow to skip tests only locally.
   109  		// CI is always set to true on GitHub actions.
   110  		// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
   111  		if os.Getenv("CI") != "true" {
   112  			t.Skip("Test skipped because nix is not installed on your system")
   113  		}
   114  	}
   115  	RegisterFailHandler(Fail)
   116  	RunSpecs(t, "target cleanup suite")
   117  }