github.com/rohankumardubey/draft-classic@v0.16.0/pkg/linguist/linguist_test.go (about) 1 package linguist 2 3 import ( 4 "path/filepath" 5 "testing" 6 ) 7 8 var ( 9 appPythonPath = filepath.Join("testdata", "app-python") 10 appEmptydirPath = filepath.Join("testdata", "app-emptydir") 11 ) 12 13 func TestProcessDir(t *testing.T) { 14 output, err := ProcessDir(appPythonPath) 15 if err != nil { 16 t.Error("expected detect to pass") 17 } 18 if output[0].Language != "Python" { 19 t.Errorf("expected output == 'Python', got '%s'", output[0].Language) 20 } 21 22 // test with a bad dir 23 if _, err := ProcessDir(filepath.Join("/dir", "does", "not", "exist")); err == nil { 24 t.Error("expected err when running detect with a dir that does not exist") 25 } 26 27 // test an application that should fail detection 28 output, _ = ProcessDir(appEmptydirPath) 29 if len(output) != 0 { 30 t.Errorf("expected no languages detected, got '%d'", len(output)) 31 } 32 } 33 34 func TestGitAttributes(t *testing.T) { 35 testCases := []struct { 36 path string 37 expectedLang string 38 }{ 39 {filepath.Join("testdata", "app-duck"), "Duck"}, 40 {filepath.Join("testdata", "app-vendored"), "Python"}, 41 {filepath.Join("testdata", "app-not-vendored"), "HTML"}, 42 {filepath.Join("testdata", "app-documentation"), "Python"}, 43 {filepath.Join("testdata", "app-generated"), "Python"}, 44 } 45 46 for _, tc := range testCases { 47 t.Run(tc.path, func(t *testing.T) { 48 output, err := ProcessDir(tc.path) 49 if err != nil { 50 t.Errorf("expected ProcessDir() to pass, got %s", err) 51 } 52 if output[0].Language != tc.expectedLang { 53 t.Errorf("expected output == '%s', got '%s'", tc.expectedLang, output[0].Language) 54 } 55 }) 56 } 57 } 58 59 //TestDirectoryIsIgnored checks to see if directory paths such as 'docs/' are ignored from being classified by linguist when added to the "ignore" list. 60 func TestDirectoryIsIgnored(t *testing.T) { 61 path := filepath.Join("testdata", "app-documentation") 62 // populate isIgnored 63 ProcessDir(path) 64 ignorePath := filepath.Join(path, "docs") 65 if !isIgnored(ignorePath) { 66 t.Errorf("expected dir '%s' to be ignored", ignorePath) 67 } 68 } 69 70 func TestGetAlias(t *testing.T) { 71 testcases := map[string]string{ 72 "maven pom": "Java", 73 "mAvEn POM": "Java", 74 "c#": "csharp", 75 "Python": "Python", 76 } 77 78 for packName, expectedAlias := range testcases { 79 alias := Alias(&Language{Language: packName}) 80 if alias.Language != expectedAlias { 81 t.Errorf("Expected alias to be '%s', got '%s'", expectedAlias, alias.Language) 82 } 83 } 84 }