github.com/Songmu/goxz@v0.9.1/cli_test.go (about) 1 package goxz 2 3 import ( 4 "io/ioutil" 5 "os" 6 "reflect" 7 "sort" 8 "strings" 9 "testing" 10 ) 11 12 func setup(t *testing.T) string { 13 tmpd, err := ioutil.TempDir("", "goxz-") 14 if err != nil { 15 t.Fatal(err) 16 } 17 return tmpd 18 } 19 20 func TestCliRun(t *testing.T) { 21 testCases := []struct { 22 name string 23 input []string 24 files []string 25 errStr string 26 }{ 27 { 28 name: "simple", 29 input: []string{"./testdata/hello"}, 30 files: []string{ 31 "goxz_darwin_amd64.zip", 32 "goxz_darwin_arm64.zip", 33 "goxz_linux_amd64.tar.gz", 34 "goxz_linux_arm64.tar.gz", 35 "goxz_windows_amd64.zip", 36 "goxz_windows_arm64.zip", 37 }, 38 }, 39 { 40 name: "zip always and specify multi arch", 41 input: []string{"-z", "-os=freebsd,linux", "-arch=386 amd64", "./testdata/hello"}, 42 files: []string{ 43 "goxz_freebsd_amd64.zip", 44 "goxz_freebsd_386.zip", 45 "goxz_linux_amd64.zip", 46 "goxz_linux_386.zip", 47 }, 48 }, 49 { 50 name: "zip always, static and specify multi os", 51 input: []string{"-z", "-static", "-os=darwin,linux,freebsd,windows", "./testdata/hello"}, 52 files: []string{ 53 "goxz_darwin_amd64.zip", 54 "goxz_darwin_arm64.zip", 55 "goxz_linux_amd64.zip", 56 "goxz_linux_arm64.zip", 57 "goxz_windows_amd64.zip", 58 "goxz_windows_arm64.zip", 59 "goxz_freebsd_amd64.zip", 60 "goxz_freebsd_arm64.zip", 61 }, 62 }, 63 { 64 name: "build multiple pakcages with app name", 65 input: []string{"-n=abc", "-os=linux", "-arch=amd64", "./testdata/hello", "./cmd/goxz"}, 66 files: []string{"abc_linux_amd64.tar.gz"}, 67 }, 68 { 69 name: "output option with version", 70 input: []string{"-o=abc", "-C=.", "-pv=0.1.1", "-os=freebsd", "./testdata/hello"}, 71 files: []string{ 72 "goxz_0.1.1_freebsd_amd64.tar.gz", 73 "goxz_0.1.1_freebsd_arm64.tar.gz", 74 }, 75 }, 76 { 77 name: "[error] no resulting object", 78 input: []string{}, // same as []string{"."} 79 errStr: `can't build artifact for non main package: "goxz"`, 80 }, 81 { 82 name: "[error] multiple packages and -o flag are not compatible", 83 input: []string{"-o=hoge", "./testdata/hello", "./cmd/goxz"}, 84 errStr: "When building multiple packages", 85 }, 86 { 87 name: "[error] package not exists", 88 input: []string{"-work", "./testdata/hello___"}, 89 errStr: "go list failed with following output", 90 }, 91 } 92 93 for _, tc := range testCases { 94 t.Run(tc.name, func(t *testing.T) { 95 cl := &cli{outStream: ioutil.Discard, errStream: ioutil.Discard} 96 tmpd := setup(t) 97 defer os.RemoveAll(tmpd) 98 args := append([]string{"-d=" + tmpd}, tc.input...) 99 err := cl.run(args) 100 if tc.errStr == "" { 101 if err != nil { 102 t.Errorf("%s: error should be nil but: %s", tc.name, err) 103 } 104 } else { 105 if err == nil { 106 t.Errorf("%s: error should be occured but nil", tc.name) 107 } else if !strings.Contains(err.Error(), tc.errStr) { 108 t.Errorf("%s: error should be contains %q, but %q", tc.name, tc.errStr, err) 109 } 110 } 111 files, err := ioutil.ReadDir(tmpd) 112 if err != nil { 113 t.Fatal(err) 114 } 115 var outs []string 116 for _, f := range files { 117 if !f.IsDir() { 118 outs = append(outs, f.Name()) 119 } 120 } 121 sort.Strings(tc.files) 122 sort.Strings(outs) 123 if !reflect.DeepEqual(tc.files, outs) { 124 t.Errorf("%s: files are not built correctly\n out: %v\nexpect: %v", 125 tc.name, outs, tc.files) 126 } 127 }) 128 } 129 } 130 131 func TestCliRun_projDir(t *testing.T) { 132 if err := os.Chdir("./testdata"); err != nil { 133 t.Fatal(err) 134 } 135 defer os.Chdir("../") 136 137 input := []string{"-o=abc", "-C=../", "-pv=0.1.1", "-os=freebsd", "./testdata/hello"} 138 builtFiles := []string{ 139 "goxz_0.1.1_freebsd_amd64.tar.gz", 140 "goxz_0.1.1_freebsd_arm64.tar.gz", 141 } 142 143 cl := &cli{outStream: ioutil.Discard, errStream: ioutil.Discard} 144 tmpd := setup(t) 145 // This deletion process is performed to check whether goxz itself creates 146 // a directory correctly 147 if err := os.RemoveAll(tmpd); err != nil { 148 t.Fatal(err) 149 } 150 defer os.RemoveAll(tmpd) 151 args := append([]string{"-d=" + tmpd}, input...) 152 err := cl.run(args) 153 154 if err != nil { 155 156 t.Errorf("error should be nil but: %s", err) 157 } 158 159 files, err := ioutil.ReadDir(tmpd) 160 if err != nil { 161 t.Fatal(err) 162 } 163 var outs []string 164 for _, f := range files { 165 if !f.IsDir() { 166 outs = append(outs, f.Name()) 167 } 168 } 169 if !reflect.DeepEqual(builtFiles, outs) { 170 t.Errorf("files are not built correctly\n out: %v\nexpect: %v", outs, builtFiles) 171 } 172 173 }