Explorar o código

Add compiler package

Andrea Fazzi %!s(int64=7) %!d(string=hai) anos
pai
achega
98a587e9af
Modificáronse 2 ficheiros con 182 adicións e 0 borrados
  1. 61 0
      compiler/compiler.go
  2. 121 0
      compiler/compiler_test.go

+ 61 - 0
compiler/compiler.go

@@ -0,0 +1,61 @@
+package compiler
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"time"
+)
+
+type Compiler struct {
+	Url string
+}
+
+type Program struct {
+	Main string `json:"body"`
+}
+
+type Event struct {
+	Message string
+	Kind    string        // "stdout" or "stderr"
+	Delay   time.Duration // time to wait before printing Message
+}
+
+type Result struct {
+	Errors string
+	Events []Event
+}
+
+func New(url string) *Compiler {
+	return &Compiler{url}
+}
+
+func (c *Compiler) Run(program *Program) (*Result, error) {
+	jsonRequest, err := json.Marshal(program)
+	if err != nil {
+		return nil, err
+	}
+	req, err := http.NewRequest("POST", c.Url, bytes.NewBuffer(jsonRequest))
+	if err != nil {
+		return nil, err
+	}
+	req.Header.Set("Content-Type", "application/json")
+
+	client := &http.Client{}
+	resp, err := client.Do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+	body, _ := ioutil.ReadAll(resp.Body)
+
+	result := &Result{}
+	err = json.Unmarshal(body, result)
+	if err != nil {
+		return nil, fmt.Errorf("Error during JSON unmarshaling. The raw response body was %s. The error is %s", body, err)
+	}
+
+	return result, nil
+}

+ 121 - 0
compiler/compiler_test.go

@@ -0,0 +1,121 @@
+package compiler
+
+import (
+	"testing"
+
+	"github.com/remogatto/prettytest"
+	"gogs.carducci-dante.gov.it/andrea.fazzi/karmen/config"
+	"gogs.carducci-dante.gov.it/andrea.fazzi/karmen/webdav"
+)
+
+type testSuite struct {
+	prettytest.Suite
+}
+
+var (
+	compiler   *Compiler
+	client     *webdav.Client
+	simpleMain string = `
+package main
+
+func main() {
+  println("Hello World!")
+}
+
+`
+	webDAVTemplateProgram string = `
+package main
+
+import (
+	"bytes"
+	"fmt"
+	"text/template"
+
+	"gogs.carducci-dante.gov.it/andrea.fazzi/karmen/webdav"
+)
+
+func main() {
+	var (
+		WebDAVUrl      = "http://localhost:8082/remote.php/webdav/"
+		WebDavUsername = "admin"
+		WebDAVPassword = "password"
+		data           struct{ Data string }
+		output         bytes.Buffer
+	)
+	client, err := webdav.NewClient(WebDAVUrl, WebDavUsername, WebDAVPassword)
+	if err != nil {
+		fmt.Print(err)
+	}
+	content, err := client.Download("Templates/template.tpl")
+	if err != nil {
+		fmt.Print(err)
+	}
+	tpl, err := template.New("template.tpl").Parse(string(content))
+	if err != nil {
+		fmt.Print(err)
+	}
+	data.Data = "Foo"
+	err = tpl.Execute(&output, data)
+	if err != nil {
+		fmt.Print(err)
+	}
+
+	err = client.Mkdir("Test")
+	if err != nil {
+		fmt.Print(err)
+	}
+
+	err = client.Upload(output.Bytes(), "Test/foo.txt")
+	if err != nil {
+		fmt.Print(err)
+	}
+        fmt.Print("ok")
+}
+`
+)
+
+func TestRunner(t *testing.T) {
+	prettytest.Run(
+		t,
+		new(testSuite),
+	)
+}
+
+func (t *testSuite) BeforeAll() {
+	var err error
+	compiler = New("http://localhost:8083/compile")
+	client, err = webdav.NewClient(
+		config.WebDAVUrl,
+		config.WebDavUsername,
+		config.WebDAVPassword,
+	)
+	if err != nil {
+		panic(err)
+	}
+
+}
+
+func (t *testSuite) TestRunSimpleMain() {
+	program := &Program{simpleMain}
+	result, err := compiler.Run(program)
+	t.Nil(err)
+	t.Equal("Hello World!\n", result.Events[0].Message)
+}
+
+func (t *testSuite) TestWebDAVTemplate() {
+	program := &Program{webDAVTemplateProgram}
+	result, err := compiler.Run(program)
+	t.Nil(err)
+	t.Equal("", result.Errors)
+	if result.Errors == "" {
+		t.Equal("ok", result.Events[0].Message)
+		data, err := client.Download("Test/foo.txt")
+		t.Nil(err)
+		t.Equal("Foo", string(data))
+	}
+
+}
+
+func (t *testSuite) After() {
+	client.Delete("Test")
+}