github.com/doytsujin/wwhrd@v0.2.1/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 parser := newCli() 15 var out = &bytes.Buffer{} 16 log.SetOutput(out) 17 18 dir, rm := mockGoPackageDir(t, "TestCliListSuccess") 19 defer rm() 20 21 cases := []struct { 22 inArgs []string 23 outputWant []string 24 }{ 25 { 26 []string{"list"}, 27 []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"}, 28 }, 29 { 30 []string{"ls"}, 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 }, 33 } 34 for _, c := range cases { 35 36 // Change working dir to test dir 37 err := os.Chdir(dir) 38 assert.NoError(t, err) 39 40 _, err = parser.ParseArgs(c.inArgs) 41 assert.NoError(t, err) 42 43 assert.Contains(t, c.outputWant, out.String()) 44 out.Reset() 45 46 } 47 } 48 49 func TestCliQuiet(t *testing.T) { 50 initial := log.GetLevel() 51 defer log.SetLevel(initial) 52 53 err := setQuiet() 54 assert.NoError(t, err) 55 56 after := log.GetLevel() 57 assert.Equal(t, log.ErrorLevel, after) 58 } 59 60 func TestCliCheck(t *testing.T) { 61 parser := newCli() 62 var out = &bytes.Buffer{} 63 log.SetOutput(out) 64 65 dir, rm := mockGoPackageDir(t, "TestCliCheck") 66 defer rm() 67 68 cases := []struct { 69 inArgs []string 70 outputWant []string 71 err []error 72 }{ 73 { 74 []string{"check"}, 75 []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"}, 76 []error{nil}, 77 }, 78 { 79 []string{"check", "-f", ".wwhrd-ex.yml"}, 80 []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"}, 81 []error{nil}, 82 }, 83 { 84 []string{"check", "-f", ".wwhrd-exwc.yml"}, 85 []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"}, 86 []error{nil}, 87 }, 88 { 89 []string{"check", "-f", ".wwhrd-bl.yml"}, 90 []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"}, 91 []error{fmt.Errorf("Non-Approved license found")}, 92 }, 93 { 94 []string{"check", "-f", "NONEXISTENT"}, 95 []string{""}, 96 []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.")}, 97 }, 98 { 99 []string{"check", "-f", ".wwhrd-botched.yml"}, 100 []string{""}, 101 []error{fmt.Errorf("Can't read config file: Invalid timestamp: 'whitelist - THISMAKESNOSENSE' at line 1, column 0")}, 102 }, 103 } 104 for _, c := range cases { 105 106 // Change working dir to test dir 107 err := os.Chdir(dir) 108 assert.NoError(t, err) 109 110 _, err = parser.ParseArgs(c.inArgs) 111 assert.Contains(t, c.err, err) 112 113 assert.Contains(t, c.outputWant, out.String()) 114 out.Reset() 115 116 } 117 }