Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds TestUASTGetLanguagesSuite/TestSameEnryLanguage #412

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/handler/detect_lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/src-d/gitbase-web/server/serializer"
)

type detectLangRequest struct {
type DetectLangRequest struct {
Content string `json:"content"`
Filename string `json:"filename"`
}

// 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
Expand Down
10 changes: 5 additions & 5 deletions server/handler/detect_lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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"`
Expand Down
135 changes: 135 additions & 0 deletions server/handler/uast_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,155 @@
package handler_test

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"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"

"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() {
req, _ := http.NewRequest("GET", "/get-languages", strings.NewReader(""))

res := httptest.NewRecorder()
suite.handler.ServeHTTP(res, req)

suite.Require().Equal(http.StatusOK, res.Code, res.Body.String())

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)
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)

require.Equal(http.StatusOK, res.Code, res.Body.String())
se7entyse7en marked this conversation as resolved.
Show resolved Hide resolved

detectedLang, detectedLangType := handler.UnmarshalDetectLangResponse(res.Body.Bytes())

if langName == "Bash" {
require.NotEqual(langName, detectedLang)
t.Skip("TEST FAILURE IS A KNOWN ISSUE")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Isn't this due to the aliasing problem that we also have on engine? But shouldn't it be true also for C#?

}

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()
{
System.Console.WriteLine("Hello, world!");
}
}
`, "hello.cs"
case "C++":
return `
#include <iostream>

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 `
<?php
echo "Hello World!";
?>
`, "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
Expand Down