gopkg.in/frapposelli/wwhrd.v0@v0.2.1/wwhrd_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/jessevdk/go-flags"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func mockGoPackageDir(t *testing.T, prefix string) (dir string, rm func()) {
    15  
    16  	dir, err := ioutil.TempDir("", prefix)
    17  	if err != nil {
    18  		log.Fatal(err)
    19  	}
    20  
    21  	if err := os.MkdirAll(filepath.Join(dir, "vendor/github.com/fake/package"), 0755); err != nil {
    22  		log.Fatal(err)
    23  	}
    24  
    25  	if err := os.MkdirAll(filepath.Join(dir, "vendor/github.com/fake/nested/inside/a/package"), 0755); err != nil {
    26  		log.Fatal(err)
    27  	}
    28  
    29  	files := []struct {
    30  		name    string
    31  		content []byte
    32  	}{
    33  		{"mockpkg.go", []byte(mockGo)},
    34  		{".wwhrd.yml", []byte(mockConf)},
    35  		{".wwhrd-bl.yml", []byte(mockConfBL)},
    36  		{".wwhrd-ex.yml", []byte(mockConfEX)},
    37  		{".wwhrd-exwc.yml", []byte(mockConfEXWC)},
    38  		{".wwhrd-botched.yml", []byte(mockConfBotched)},
    39  		{filepath.Join("vendor/github.com/fake/package", "mockpkg.go"), []byte(mockVendor)},
    40  		{filepath.Join("vendor/github.com/fake/package", "LICENSE"), []byte(mockLicense)},
    41  		{filepath.Join("vendor/github.com/fake/nested", "LICENSE"), []byte(mockLicense)},
    42  		{filepath.Join("vendor/github.com/fake/nested/inside/a/package", "mockpkg.go"), []byte(mockVendor)},
    43  	}
    44  
    45  	for _, c := range files {
    46  		tmpfn := filepath.Join(dir, c.name)
    47  		if err := ioutil.WriteFile(tmpfn, c.content, 0666); err != nil {
    48  			log.Fatal(err)
    49  		}
    50  	}
    51  
    52  	return dir, func() {
    53  		defer os.RemoveAll(dir)
    54  	}
    55  
    56  }
    57  
    58  //TestKillCmdParseErrors test for cli arguments and flags
    59  func TestCliCommandsErrors(t *testing.T) {
    60  	parser := newCli()
    61  
    62  	cases := []struct {
    63  		inArgs  []string
    64  		errWant error
    65  	}{
    66  		{
    67  			[]string{"check", "-f"}, &flags.Error{Type: flags.ErrExpectedArgument},
    68  		},
    69  	}
    70  	for _, c := range cases {
    71  
    72  		// Parse the flags
    73  		_, errGot := parser.ParseArgs(c.inArgs)
    74  		assert.IsType(t, c.errWant, errGot, "type mismatch")
    75  		if _, ok := errGot.(*flags.Error); ok {
    76  			typ := errGot.(*flags.Error).Type
    77  			assert.Equal(t, typ, c.errWant.(*flags.Error).Type)
    78  		}
    79  	}
    80  }
    81  
    82  var mockConf = `---
    83  whitelist:
    84    - FreeBSD
    85  `
    86  
    87  var mockConfBL = `---
    88  blacklist:
    89    - FreeBSD
    90  `
    91  
    92  var mockConfEX = `---
    93  exceptions:
    94    - github.com/fake/package
    95    - github.com/fake/nested/inside/a/package
    96  `
    97  
    98  var mockConfEXWC = `---
    99  exceptions:
   100    - github.com/fake/...
   101  `
   102  
   103  var mockConfBotched = `---
   104  whitelist
   105  - THISMAKESNOSENSE
   106  `
   107  
   108  var mockGo = `package main
   109  import (
   110  	"github.com/fake/package"
   111  	"github.com/fake/nested/inside/a/package"
   112  )
   113  func main() {}
   114  `
   115  
   116  var mockVendor = `package main
   117  func main() {}
   118  `
   119  
   120  var mockLicense = `Copyright (c) 2016, Fabio Rapposelli
   121  All rights reserved.
   122  
   123  Redistribution and use in source and binary forms, with or without
   124  modification, are permitted provided that the following conditions are
   125  met:
   126  
   127  		Redistributions of source code must retain the above copyright
   128  notice, this list of conditions and the following disclaimer.
   129  		Redistributions in binary form must reproduce the above copyright
   130  notice, this list of conditions and the following disclaimer in the
   131  documentation and/or other materials provided with the distribution.
   132  		The names of its contributors may not be used to endorse or promote
   133  products derived from this software without specific prior written
   134  permission.
   135  
   136  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   137  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   138  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   139  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   140  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   141  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   142  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   143  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   144  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   145  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   146  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   147  `