github.com/wawandco/oxpecker-plugins@v0.1.1/tools/standard/build_test.go (about)

     1  package standard
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestBinaryOutput(t *testing.T) {
    11  	c := &Plugin{}
    12  	output := c.binaryOutput("aaa")
    13  
    14  	if output != "bin/aaa" {
    15  		t.Errorf("binaryOutput should be %v not %v", "bin/aaa", output)
    16  	}
    17  }
    18  
    19  func TestComposeBuildArgs(t *testing.T) {
    20  	td := t.TempDir()
    21  	err := os.Chdir(td)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	file := `module wawandco/app`
    27  	err = ioutil.WriteFile("go.mod", []byte(file), 0444)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	c := &Plugin{}
    33  	c.static = true
    34  	args, err := c.composeBuildArgs()
    35  	if err != nil {
    36  		t.Fatal("shoud not get error")
    37  	}
    38  
    39  	expectedArgs := []string{
    40  		"build",
    41  		"--ldflags",
    42  		"-linkmode external",
    43  		"--ldflags",
    44  		`-extldflags "-static"`,
    45  		"-o",
    46  		"bin/app",
    47  		"./cmd/app",
    48  	}
    49  
    50  	if strings.Join(args, " ") != strings.Join(expectedArgs, " ") {
    51  		t.Fatalf("args do not match should be %v,got %v", expectedArgs, args)
    52  	}
    53  
    54  	c.output = "other/bin"
    55  	c.static = true
    56  	args, err = c.composeBuildArgs()
    57  	if err != nil {
    58  		t.Fatal("shoud not get error")
    59  	}
    60  
    61  	expectedArgs = []string{
    62  		"build",
    63  		"--ldflags",
    64  		"-linkmode external",
    65  		"--ldflags",
    66  		`-extldflags "-static"`,
    67  		"-o",
    68  		"other/bin",
    69  		"./cmd/app",
    70  	}
    71  
    72  	if strings.Join(args, " ") != strings.Join(expectedArgs, " ") {
    73  		t.Fatalf("args do not match should be %v", expectedArgs)
    74  	}
    75  
    76  	c.buildTags = []string{"netdns=go"}
    77  
    78  	args, err = c.composeBuildArgs()
    79  	if err != nil {
    80  		t.Fatal("shoud not get error")
    81  	}
    82  
    83  	expectedArgs = []string{
    84  		"build",
    85  		"--ldflags",
    86  		"-linkmode external",
    87  		"--ldflags",
    88  		`-extldflags "-static"`,
    89  		"-o",
    90  		"other/bin",
    91  		"-tags",
    92  		"netdns=go",
    93  		"./cmd/app",
    94  	}
    95  
    96  	if strings.Join(args, " ") != strings.Join(expectedArgs, " ") {
    97  		t.Fatalf("args do not match should be %v, got %v", expectedArgs, args)
    98  	}
    99  
   100  	c.static = false
   101  	args, err = c.composeBuildArgs()
   102  	if err != nil {
   103  		t.Fatal("shoud not get error")
   104  	}
   105  	expectedArgs = []string{
   106  		"build",
   107  		"-o",
   108  		"other/bin",
   109  		"-tags",
   110  		"netdns=go",
   111  		"./cmd/app",
   112  	}
   113  
   114  	if strings.Join(args, " ") != strings.Join(expectedArgs, " ") {
   115  		t.Fatalf("args do not match should be %v,got %v", expectedArgs, args)
   116  	}
   117  
   118  }