github.com/gopinath-langote/1build@v1.7.0/testing/cli_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	utils2 "github.com/gopinath-langote/1build/cmd/utils"
    10  
    11  	"github.com/gopinath-langote/1build/testing/fixtures"
    12  	"github.com/gopinath-langote/1build/testing/utils"
    13  )
    14  
    15  var binaryName = "1build"
    16  var binaryPath string
    17  var testDirectory string
    18  
    19  func TestAll(t *testing.T) {
    20  	Tests := fixtures.GetFixtures()
    21  	for _, tt := range Tests {
    22  		testResourceDirectory := utils.RecreateTestResourceDirectory(testDirectory)
    23  
    24  		t.Run(".:."+tt.Feature+".:."+tt.Name, func(t *testing.T) {
    25  			if tt.Setup != nil {
    26  				_ = tt.Setup(testResourceDirectory)
    27  			}
    28  			var args []string
    29  			if tt.CmdArgs != nil {
    30  				args = tt.CmdArgs(testResourceDirectory)
    31  			}
    32  			cmd := exec.Command(binaryPath, args...)
    33  			cmd.Dir = testResourceDirectory
    34  			out, _ := cmd.Output()
    35  			_ = tt.Assertion(testResourceDirectory, string(out), t)
    36  			if tt.Teardown != nil {
    37  				_ = tt.Teardown(testResourceDirectory)
    38  			}
    39  		})
    40  
    41  	}
    42  
    43  }
    44  
    45  func TestMain(m *testing.M) {
    46  	testDir, _ := utils.CreateTempDir()
    47  	testDirectory = testDir
    48  
    49  	binaryPath = testDir + "/" + binaryName
    50  	buildBinary(binaryPath)
    51  
    52  	fmt.Println(utils2.Dash() + "\nBinary Path:- '" + binaryPath + "'\n" + utils2.Dash())
    53  
    54  	exitCode := m.Run()
    55  
    56  	utils.RemoveAllFilesFromDir(testDir)
    57  	os.Exit(exitCode)
    58  }
    59  
    60  func buildBinary(path string) {
    61  	err := os.Chdir("..")
    62  	if err != nil {
    63  		fmt.Printf("could not make binary for %s: %v", binaryName, err.Error())
    64  		os.Exit(1)
    65  	}
    66  
    67  	getDep := exec.Command("go", "build", "-o", path)
    68  	if err := getDep.Run(); err != nil {
    69  		fmt.Printf("could not make binary for %s: %v", binaryName, err.Error())
    70  		os.Exit(1)
    71  	}
    72  }