From 0ab38e0f77efe10b4adef5e319f886f376b50871 Mon Sep 17 00:00:00 2001 From: Lou Marvin Caraig Date: Thu, 18 Apr 2019 13:05:53 +0200 Subject: [PATCH 1/4] Added TestUASTGetLanguagesSuite/TestSameEnryLanguage This test ensures that the language label returned by enry is the same used by bblfsh Signed-off-by: Lou Marvin Caraig --- server/handler/detect_lang_test.go | 10 +- server/handler/uast_integration_test.go | 137 ++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/server/handler/detect_lang_test.go b/server/handler/detect_lang_test.go index 7d26b9c..83a0ade 100644 --- a/server/handler/detect_lang_test.go +++ b/server/handler/detect_lang_test.go @@ -41,7 +41,7 @@ func (suite *DetectLangSuite) TestOnlyContent() { suite.Equal(http.StatusOK, res.Code) - lang, langType := langResponse(res.Body.Bytes()) + lang, langType := UnmarshalDetectLangResponse(res.Body.Bytes()) suite.Equal("JavaScript", lang) suite.Equal(enry.Programming, langType) } @@ -55,7 +55,7 @@ func (suite *DetectLangSuite) TestOnlyFilename() { suite.Equal(http.StatusOK, res.Code) - lang, langType := langResponse(res.Body.Bytes()) + lang, langType := UnmarshalDetectLangResponse(res.Body.Bytes()) suite.Equal("JavaScript", lang) suite.Equal(enry.Programming, langType) } @@ -69,7 +69,7 @@ func (suite *DetectLangSuite) TestDetect() { suite.Equal(http.StatusOK, res.Code) - lang, langType := langResponse(res.Body.Bytes()) + lang, langType := UnmarshalDetectLangResponse(res.Body.Bytes()) suite.Equal("MATLAB", lang) suite.Equal(enry.Programming, langType) } @@ -83,12 +83,12 @@ func (suite *DetectLangSuite) TestUnknownContent() { suite.Equal(http.StatusOK, res.Code) - lang, langType := langResponse(res.Body.Bytes()) + lang, langType := UnmarshalDetectLangResponse(res.Body.Bytes()) suite.Equal("", lang) suite.Equal(enry.Unknown, langType) } -func langResponse(b []byte) (string, enry.Type) { +func UnmarshalDetectLangResponse(b []byte) (string, enry.Type) { var resBody struct { Data struct { Language string `json:"language"` diff --git a/server/handler/uast_integration_test.go b/server/handler/uast_integration_test.go index 53e2040..c3da69a 100644 --- a/server/handler/uast_integration_test.go +++ b/server/handler/uast_integration_test.go @@ -2,19 +2,156 @@ package handler_test import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "strings" "testing" + "github.com/go-chi/chi" "github.com/pressly/lg" "github.com/sirupsen/logrus" "github.com/stretchr/testify/suite" + "gopkg.in/src-d/enry.v1" "github.com/src-d/gitbase-web/server/handler" "github.com/src-d/gitbase-web/server/serializer" + "github.com/src-d/gitbase-web/server/service" ) +type UASTGetLanguagesSuite struct { + suite.Suite + handler http.Handler +} + +func TestUASTGetLanguagesSuite(t *testing.T) { + if !isIntegration() { + t.Skip("use the env var GITBASEPG_INTEGRATION_TESTS=true to run this test") + } + + q := new(UASTGetLanguagesSuite) + r := chi.NewRouter() + r.Use(lg.RequestLogger(logrus.New())) + r.Post("/detect-lang", handler.APIHandlerFunc(handler.DetectLanguage())) + r.Get("/get-languages", handler.APIHandlerFunc(handler.GetLanguages(bblfshServerURL()))) + + q.handler = r + + suite.Run(t, q) +} + +func UnmarshalGetLanguagesResponse(b []byte) []service.Language { + var resBody struct { + Data []service.Language `json:"data"` + } + json.Unmarshal(b, &resBody) + return resBody.Data +} + +func (suite *UASTGetLanguagesSuite) TestSameEnryLanguage() { + require := suite.Require() + + req, _ := http.NewRequest("GET", "/get-languages", strings.NewReader("")) + + res := httptest.NewRecorder() + suite.handler.ServeHTTP(res, req) + + require.Equal(http.StatusOK, res.Code, res.Body.String()) + + escapeForJSON := func(s string) string { + return strings.Replace(strings.Replace(s, "\"", "\\\"", -1), + "\n", "\\n", -1) + } + + for _, lang := range UnmarshalGetLanguagesResponse(res.Body.Bytes()) { + langName := lang.Name + suite.T().Run(langName, func(t *testing.T) { + content, filename := suite.getContentAndFilename(langName) + jsonRequest := fmt.Sprintf(`{ "content": "%s", "filename": "%s" }`, + escapeForJSON(content), filename) + req, _ := http.NewRequest("POST", "/detect-lang", strings.NewReader(jsonRequest)) + + res = httptest.NewRecorder() + suite.handler.ServeHTTP(res, req) + + require.Equal(http.StatusOK, res.Code, res.Body.String()) + + detectedLang, detectedLangType := handler.UnmarshalDetectLangResponse(res.Body.Bytes()) + + if langName == "Bash" { + require.NotEqual(langName, detectedLang) + t.Skip("TEST FAILURE IS A KNOWN ISSUE") + } + + require.Equal(langName, detectedLang) + require.Equal(enry.Programming, detectedLangType) + }) + } +} + +func (suite *UASTGetLanguagesSuite) getContentAndFilename(lang string) (string, string) { + suite.T().Helper() + + switch lang { + case "Bash": + return "echo 'Hello World!'", "hello.sh" + case "C#": + return ` +class HelloWorldProgram +{ + public static void Main()\n + { + System.Console.WriteLine("Hello, world!");\n + } +} +`, "hello.cs" + case "C++": + return ` +#include + +int main() +{ + std::cout << "Hello World!" << std::endl; + return 0; +} +`, "hello.cpp" + case "Go": + return ` +package main + +import "fmt" + +func main() { + fmt.Println("Hello World!") +} +`, "hello.go" + case "Java": + return ` +public class HelloWorld { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } + +} +`, "hello.java" + case "JavaScript": + return "console.log('Hello World!')", "hello.js" + case "PHP": + return ` + +`, "hello.php" + case "Python": + return "print('Hello World!')", "hello.py" + case "Ruby": + return "puts 'Hello world!'", "hello.rb" + } + + return "", "" +} + type UASTParseSuite struct { suite.Suite handler http.Handler From 30df0ebe04dda15f39826a7098d910665a53cfd2 Mon Sep 17 00:00:00 2001 From: Lou Marvin Caraig Date: Thu, 18 Apr 2019 18:58:15 +0200 Subject: [PATCH 2/4] Fixed require for inner test Signed-off-by: Lou Marvin Caraig --- server/handler/uast_integration_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/handler/uast_integration_test.go b/server/handler/uast_integration_test.go index c3da69a..0cb7e0a 100644 --- a/server/handler/uast_integration_test.go +++ b/server/handler/uast_integration_test.go @@ -11,6 +11,7 @@ import ( "github.com/go-chi/chi" "github.com/pressly/lg" "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "gopkg.in/src-d/enry.v1" @@ -49,14 +50,12 @@ func UnmarshalGetLanguagesResponse(b []byte) []service.Language { } func (suite *UASTGetLanguagesSuite) TestSameEnryLanguage() { - require := suite.Require() - req, _ := http.NewRequest("GET", "/get-languages", strings.NewReader("")) res := httptest.NewRecorder() suite.handler.ServeHTTP(res, req) - require.Equal(http.StatusOK, res.Code, res.Body.String()) + suite.Require().Equal(http.StatusOK, res.Code, res.Body.String()) escapeForJSON := func(s string) string { return strings.Replace(strings.Replace(s, "\"", "\\\"", -1), @@ -66,6 +65,8 @@ func (suite *UASTGetLanguagesSuite) TestSameEnryLanguage() { for _, lang := range UnmarshalGetLanguagesResponse(res.Body.Bytes()) { langName := lang.Name suite.T().Run(langName, func(t *testing.T) { + require := require.New(t) + content, filename := suite.getContentAndFilename(langName) jsonRequest := fmt.Sprintf(`{ "content": "%s", "filename": "%s" }`, escapeForJSON(content), filename) From 6b067bd6e022856dc83f2b6e899ace9a919588ed Mon Sep 17 00:00:00 2001 From: Lou Marvin Caraig Date: Thu, 18 Apr 2019 19:29:13 +0200 Subject: [PATCH 3/4] Removed leftovers Signed-off-by: Lou Marvin Caraig --- server/handler/uast_integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/handler/uast_integration_test.go b/server/handler/uast_integration_test.go index 0cb7e0a..4d01163 100644 --- a/server/handler/uast_integration_test.go +++ b/server/handler/uast_integration_test.go @@ -100,9 +100,9 @@ func (suite *UASTGetLanguagesSuite) getContentAndFilename(lang string) (string, return ` class HelloWorldProgram { - public static void Main()\n + public static void Main() { - System.Console.WriteLine("Hello, world!");\n + System.Console.WriteLine("Hello, world!"); } } `, "hello.cs" From b1a304cf5b4548b891612f47031ad8748f825ad9 Mon Sep 17 00:00:00 2001 From: Lou Marvin Caraig Date: Mon, 22 Apr 2019 18:28:04 +0200 Subject: [PATCH 4/4] Simplified test by exposing DetectLangRequest Signed-off-by: Lou Marvin Caraig --- server/handler/detect_lang.go | 4 ++-- server/handler/uast_integration_test.go | 15 ++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/server/handler/detect_lang.go b/server/handler/detect_lang.go index 1a5726c..08593d3 100644 --- a/server/handler/detect_lang.go +++ b/server/handler/detect_lang.go @@ -10,7 +10,7 @@ import ( "github.com/src-d/gitbase-web/server/serializer" ) -type detectLangRequest struct { +type DetectLangRequest struct { Content string `json:"content"` Filename string `json:"filename"` } @@ -18,7 +18,7 @@ type detectLangRequest struct { // DetectLanguage returns a function that detects language by filename and content func DetectLanguage() RequestProcessFunc { return func(r *http.Request) (res *serializer.Response, err error) { - var req detectLangRequest + var req DetectLangRequest body, err := ioutil.ReadAll(r.Body) if err != nil { return nil, err diff --git a/server/handler/uast_integration_test.go b/server/handler/uast_integration_test.go index 4d01163..1754f20 100644 --- a/server/handler/uast_integration_test.go +++ b/server/handler/uast_integration_test.go @@ -1,8 +1,8 @@ package handler_test import ( + "bytes" "encoding/json" - "fmt" "net/http" "net/http/httptest" "strings" @@ -57,20 +57,17 @@ func (suite *UASTGetLanguagesSuite) TestSameEnryLanguage() { suite.Require().Equal(http.StatusOK, res.Code, res.Body.String()) - escapeForJSON := func(s string) string { - return strings.Replace(strings.Replace(s, "\"", "\\\"", -1), - "\n", "\\n", -1) - } - for _, lang := range UnmarshalGetLanguagesResponse(res.Body.Bytes()) { langName := lang.Name suite.T().Run(langName, func(t *testing.T) { require := require.New(t) content, filename := suite.getContentAndFilename(langName) - jsonRequest := fmt.Sprintf(`{ "content": "%s", "filename": "%s" }`, - escapeForJSON(content), filename) - req, _ := http.NewRequest("POST", "/detect-lang", strings.NewReader(jsonRequest)) + b, _ := json.Marshal(handler.DetectLangRequest{ + Filename: filename, + Content: content, + }) + req, _ := http.NewRequest("POST", "/detect-lang", bytes.NewReader(b)) res = httptest.NewRecorder() suite.handler.ServeHTTP(res, req)