github.com/SpiderOak/wwhrd@v0.2.2-0.20181011170608-77c9537cbd49/cli_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCliListSuccess(t *testing.T) {
    14  	var out = &bytes.Buffer{}
    15  	log.SetOutput(out)
    16  
    17  	dir, rm := mockGoPackageDir(t, "TestCliListSuccess")
    18  	defer rm()
    19  
    20  	// Change working dir to test dir
    21  	err := os.Chdir(dir)
    22  	assert.NoError(t, err)
    23  
    24  	cases := []struct {
    25  		inArgs            []string
    26  		outputWant        []string
    27  		outputWantNoColor []string
    28  	}{
    29  		{
    30  			[]string{"list"},
    31  			[]string{"\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n"},
    32  			[]string{`level=info msg="Found License" license=FreeBSD package="github.com/fake/package"`, `level=info msg="Found License" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
    33  		},
    34  		{
    35  			[]string{"ls"},
    36  			[]string{"\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[34mINFO\x1b[0m[0000] Found License                                 \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n"},
    37  			[]string{`level=info msg="Found License" license=FreeBSD package="github.com/fake/nested/inside/a/package"`, `level=info msg="Found License" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
    38  		},
    39  	}
    40  
    41  	for _, c := range cases {
    42  		_, err = newCli().ParseArgs(c.inArgs)
    43  		assert.NoError(t, err)
    44  
    45  		assert.Contains(t, c.outputWant, out.String())
    46  		out.Reset()
    47  
    48  		// no color
    49  		_, err = newCli().ParseArgs(append(c.inArgs, "--no-color"))
    50  		assert.NoError(t, err)
    51  
    52  		for _, want := range c.outputWantNoColor {
    53  			assert.Contains(t, out.String(), want)
    54  		}
    55  		out.Reset()
    56  	}
    57  }
    58  
    59  func TestCliQuiet(t *testing.T) {
    60  	initial := log.GetLevel()
    61  	defer log.SetLevel(initial)
    62  
    63  	err := setQuiet()
    64  	assert.NoError(t, err)
    65  
    66  	after := log.GetLevel()
    67  	assert.Equal(t, log.ErrorLevel, after)
    68  }
    69  
    70  func TestCliCheck(t *testing.T) {
    71  	var out = &bytes.Buffer{}
    72  	log.SetOutput(out)
    73  
    74  	dir, rm := mockGoPackageDir(t, "TestCliCheck")
    75  	defer rm()
    76  
    77  	// Change working dir to test dir
    78  	err := os.Chdir(dir)
    79  	assert.NoError(t, err)
    80  
    81  	cases := []struct {
    82  		inArgs            []string
    83  		outputWant        []string
    84  		outputWantNoColor []string
    85  		err               []error
    86  	}{
    87  		{
    88  			[]string{"check"},
    89  			[]string{"\x1b[34mINFO\x1b[0m[0000] Found Approved license                        \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[34mINFO\x1b[0m[0000] Found Approved license                        \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[34mINFO\x1b[0m[0000] Found Approved license                        \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[34mINFO\x1b[0m[0000] Found Approved license                        \x1b[34mlicense\x1b[0m=FreeBSD \x1b[34mpackage\x1b[0m=\"github.com/fake/package\"\n"},
    90  			[]string{`level=info msg="Found Approved license" license=FreeBSD package="github.com/fake/package"`, `level=info msg="Found Approved license" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
    91  			[]error{nil},
    92  		},
    93  		{
    94  			[]string{"check", "-f", ".wwhrd-ex.yml"},
    95  			[]string{"\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/package\"\n"},
    96  			[]string{`level=warning msg="Found exceptioned package" license=FreeBSD package="github.com/fake/package"`, `level=warning msg="Found exceptioned package" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
    97  			[]error{nil},
    98  		},
    99  		{
   100  			[]string{"check", "-f", ".wwhrd-exwc.yml"},
   101  			[]string{"\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[33mWARN\x1b[0m[0000] Found exceptioned package                     \x1b[33mlicense\x1b[0m=FreeBSD \x1b[33mpackage\x1b[0m=\"github.com/fake/package\"\n"},
   102  			[]string{`level=warning msg="Found exceptioned package" license=FreeBSD package="github.com/fake/package"`, `level=warning msg="Found exceptioned package" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
   103  			[]error{nil},
   104  		},
   105  		{
   106  			[]string{"check", "-f", ".wwhrd-bl.yml"},
   107  			[]string{"\x1b[31mERRO\x1b[0m[0000] Found Non-Approved license                    \x1b[31mlicense\x1b[0m=FreeBSD \x1b[31mpackage\x1b[0m=\"github.com/fake/package\"\n\x1b[31mERRO\x1b[0m[0000] Found Non-Approved license                    \x1b[31mlicense\x1b[0m=FreeBSD \x1b[31mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n", "\x1b[31mERRO\x1b[0m[0000] Found Non-Approved license                    \x1b[31mlicense\x1b[0m=FreeBSD \x1b[31mpackage\x1b[0m=\"github.com/fake/nested/inside/a/package\"\n\x1b[31mERRO\x1b[0m[0000] Found Non-Approved license                    \x1b[31mlicense\x1b[0m=FreeBSD \x1b[31mpackage\x1b[0m=\"github.com/fake/package\"\n"},
   108  			[]string{`level=error msg="Found Non-Approved license" license=FreeBSD package="github.com/fake/package"`, `level=error msg="Found Non-Approved license" license=FreeBSD package="github.com/fake/nested/inside/a/package"`},
   109  			[]error{fmt.Errorf("Non-Approved license found")},
   110  		},
   111  		{
   112  			[]string{"check", "-f", "NONEXISTENT"},
   113  			[]string{""},
   114  			[]string{""},
   115  			[]error{fmt.Errorf("Can't read config file: stat NONEXISTENT: no such file or directory"), fmt.Errorf("Can't read config file: GetFileAttributesEx NONEXISTENT: The system cannot find the file specified.")},
   116  		},
   117  		{
   118  			[]string{"check", "-f", ".wwhrd-botched.yml"},
   119  			[]string{""},
   120  			[]string{""},
   121  			[]error{fmt.Errorf("Can't read config file: Invalid timestamp: 'whitelist - THISMAKESNOSENSE' at line 1, column 0")},
   122  		},
   123  	}
   124  
   125  	for _, c := range cases {
   126  		_, err = newCli().ParseArgs(c.inArgs)
   127  		assert.Contains(t, c.err, err)
   128  
   129  		assert.Contains(t, c.outputWant, out.String())
   130  		out.Reset()
   131  
   132  		// no color
   133  		_, err = newCli().ParseArgs(append(c.inArgs, "--no-color"))
   134  		assert.Contains(t, c.err, err)
   135  
   136  		for _, want := range c.outputWantNoColor {
   137  			assert.Contains(t, out.String(), want)
   138  		}
   139  		out.Reset()
   140  	}
   141  }