github.com/Songmu/goxz@v0.9.1/goxz_test.go (about)

     1  package goxz
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"io/ioutil"
     7  	"path/filepath"
     8  	"reflect"
     9  	"sort"
    10  	"testing"
    11  )
    12  
    13  func TestRun_help(t *testing.T) {
    14  	err := Run(context.Background(), []string{"-h"}, ioutil.Discard, ioutil.Discard)
    15  	if err != flag.ErrHelp {
    16  		t.Errorf("somthing went wrong: %s", err)
    17  	}
    18  }
    19  
    20  func TestResolvePlatforms(t *testing.T) {
    21  	testCases := []struct {
    22  		name   string
    23  		inOS   string
    24  		inArch string
    25  		expect []platform
    26  	}{
    27  		{
    28  			name:   "simple",
    29  			inOS:   "linux",
    30  			inArch: "amd64",
    31  			expect: []platform{{"linux", "amd64"}},
    32  		},
    33  		{
    34  			name:   "comma separated 2 os and whitespece separated 2 arch",
    35  			inOS:   "linux,windows",
    36  			inArch: "amd64 386",
    37  			expect: []platform{
    38  				{"linux", "amd64"},
    39  				{"linux", "386"},
    40  				{"windows", "amd64"},
    41  				{"windows", "386"},
    42  			},
    43  		},
    44  		{
    45  			name:   "empty OS",
    46  			inOS:   "",
    47  			inArch: "amd64 386",
    48  			expect: []platform{},
    49  		},
    50  		{
    51  			name:   "empty Arch",
    52  			inOS:   "linux",
    53  			inArch: "",
    54  			expect: []platform{},
    55  		},
    56  		{
    57  			name:   "mixed separators",
    58  			inOS:   "linux ,windows darwin ",
    59  			inArch: "amd64  386,     arm",
    60  			expect: []platform{
    61  				{"linux", "amd64"},
    62  				{"linux", "386"},
    63  				{"linux", "arm"},
    64  				{"windows", "amd64"},
    65  				{"windows", "386"},
    66  				{"windows", "arm"},
    67  				{"darwin", "amd64"},
    68  				{"darwin", "386"},
    69  				{"darwin", "arm"},
    70  			},
    71  		},
    72  	}
    73  	for _, tc := range testCases {
    74  		o, err := resolvePlatforms(tc.inOS, tc.inArch)
    75  		if err != nil {
    76  			t.Errorf("error should be nil but: %s", err)
    77  		}
    78  		out := []platform{}
    79  		for _, pf := range o {
    80  			out = append(out, *pf)
    81  		}
    82  		if !reflect.DeepEqual(out, tc.expect) {
    83  			t.Errorf("wrong resolvePlatform (%s)\n  out: %v\nexpect: %v", tc.name, out, tc.expect)
    84  		}
    85  	}
    86  }
    87  
    88  func TestGatherResources(t *testing.T) {
    89  	projDir, _ := filepath.Abs("./testdata")
    90  	gx := &goxz{
    91  		projDir: projDir,
    92  		include: "sample.*",
    93  	}
    94  	files, err := gx.gatherResources()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	out := make([]string, len(files))
    99  	for i, r := range files {
   100  		out[i], _ = filepath.Rel(projDir, r)
   101  	}
   102  	expect := []string{"CREDITS", "LICENSE.txt", "README.md", "sample.conf"}
   103  	sort.Strings(expect)
   104  	sort.Strings(out)
   105  	if !reflect.DeepEqual(out, expect) {
   106  		t.Errorf("something went wrong:\n  out: %v\nexpect: %v", out, expect)
   107  	}
   108  }