github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/e2e/hermeticmode/hermetic_suite_test.go (about)

     1  package hermeticmodetest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/benchkram/bob/bob"
    12  	"github.com/benchkram/bob/bob/bobfile"
    13  	"github.com/benchkram/bob/pkg/file"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var (
    19  	dir     string
    20  	version string
    21  
    22  	ctx context.Context
    23  )
    24  
    25  type project struct {
    26  	name  string
    27  	gomod string
    28  	// .gox extension to avoid `main redeclared in this block`
    29  	main string
    30  }
    31  
    32  var _ = BeforeSuite(func() {
    33  	ctx = context.Background()
    34  
    35  	version = bob.Version
    36  	bob.Version = "1.0.0"
    37  
    38  	// Initialize mock bob files from local directory
    39  	bobFiles := []string{
    40  		"build",
    41  		"init",
    42  		"init_once",
    43  		"binary",
    44  	}
    45  	nameToBobfile := make(map[string]*bobfile.Bobfile)
    46  	for _, name := range bobFiles {
    47  		abs, err := filepath.Abs("./" + name)
    48  		Expect(err).NotTo(HaveOccurred())
    49  		bf, err := bobfile.BobfileRead(abs)
    50  		Expect(err).NotTo(HaveOccurred())
    51  		nameToBobfile[strings.ReplaceAll(name, "/", "_")] = bf
    52  	}
    53  
    54  	testDir, err := os.MkdirTemp("", "bob-test-hermetic-mode-*")
    55  	Expect(err).NotTo(HaveOccurred())
    56  
    57  	projects := []project{
    58  		{
    59  			name:  "server",
    60  			gomod: "./server/go.mod",
    61  			main:  "./server/main.go",
    62  		},
    63  		{
    64  			name:  "server-with-env",
    65  			gomod: "./server-with-env/go.mod",
    66  			main:  "./server-with-env/main.go",
    67  		},
    68  	}
    69  
    70  	// copy projects in the test dir
    71  	for _, p := range projects {
    72  		err = file.Copy(p.gomod, testDir+"/"+p.name+"_go.mod")
    73  		Expect(err).NotTo(HaveOccurred())
    74  
    75  		err = file.Copy(p.main, testDir+"/"+p.name+"_main.gox")
    76  		Expect(err).NotTo(HaveOccurred())
    77  	}
    78  
    79  	dir = testDir
    80  	err = os.Chdir(dir)
    81  	Expect(err).NotTo(HaveOccurred())
    82  
    83  	// Save bob files in dir to have them available in tests
    84  	for name, bf := range nameToBobfile {
    85  		err = bf.BobfileSave(dir, name+".yaml")
    86  		Expect(err).NotTo(HaveOccurred())
    87  	}
    88  })
    89  
    90  var _ = AfterSuite(func() {
    91  	err := os.RemoveAll(dir)
    92  	Expect(err).NotTo(HaveOccurred())
    93  
    94  	bob.Version = version
    95  })
    96  
    97  func TestRun(t *testing.T) {
    98  	_, err := exec.LookPath("nix")
    99  	if err != nil {
   100  		// Allow skipping tests only locally.
   101  		// CI is always set to true on GitHub actions.
   102  		// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
   103  		if os.Getenv("CI") != "true" {
   104  			t.Skip("Test skipped because nix is not installed on your system")
   105  		}
   106  	}
   107  	RegisterFailHandler(Fail)
   108  	RunSpecs(t, "hermetic mode suite")
   109  }
   110  
   111  /*// useBobfile sets the right bobfile to be used for test
   112  func useBobfile(name string) {
   113  	err := os.Rename(name+".yaml", "bob.yaml")
   114  	Expect(err).NotTo(HaveOccurred())
   115  }
   116  
   117  // releaseBobfile will revert changes done in useBobfile
   118  func releaseBobfile(name string) {
   119  	err := os.Rename("bob.yaml", name+".yaml")
   120  	Expect(err).NotTo(HaveOccurred())
   121  }
   122  
   123  // readLines reads a whole file into memory
   124  // and returns a slice of its lines.
   125  func readLines(path string) ([]string, error) {
   126  	file, err := os.Open(path)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	defer file.Close()
   131  
   132  	var lines []string
   133  	scanner := bufio.NewScanner(file)
   134  	for scanner.Scan() {
   135  		lines = append(lines, scanner.Text())
   136  	}
   137  	return lines, scanner.Err()
   138  }
   139  
   140  // useProject set up the project to be used
   141  // make sure to call releaseProject to revert changes
   142  func useProject(name string) {
   143  	err := os.Rename(name+"_go.mod", "go.mod")
   144  	Expect(err).NotTo(HaveOccurred())
   145  
   146  	err = os.Rename(name+"_main.gox", "main.go")
   147  	Expect(err).NotTo(HaveOccurred())
   148  }
   149  
   150  // releaseProject will revert the changes done in useProject
   151  func releaseProject(name string) {
   152  	err := os.Rename("go.mod", name+"_go.mod")
   153  	Expect(err).NotTo(HaveOccurred())
   154  
   155  	err = os.Rename("main.go", name+"_main.gox")
   156  	Expect(err).NotTo(HaveOccurred())
   157  }
   158  
   159  // keyHasValue checks that in env, the variable key has its value expectedValue
   160  func keyHasValue(key, expectedValue string, env []string) bool {
   161  	for _, v := range env {
   162  		pair := strings.SplitN(v, "=", 2)
   163  		if pair[0] == key {
   164  			return pair[1] == expectedValue
   165  		}
   166  	}
   167  	return false
   168  }*/