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